Skip to content

Commit

Permalink
Add admin pages
Browse files Browse the repository at this point in the history
  • Loading branch information
pehala committed Sep 20, 2023
1 parent 1d3d262 commit a260f29
Show file tree
Hide file tree
Showing 8 changed files with 172 additions and 0 deletions.
20 changes: 20 additions & 0 deletions backend/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
"""Backend Admin pages"""
from django.contrib import admin

from backend.forms import AdminSongForm
from backend.models import Song


@admin.register(Song)
class SongAdmin(admin.ModelAdmin):
"""Song Admin config"""

form = AdminSongForm
list_display = ["name", "get_categories"]

@admin.display(description="Categories")
def get_categories(self, obj):
"""Displays all categorties in a string"""
return "\n".join([p.name for p in obj.categories.all()])

get_categories.admin_order_field = "categories__name"
33 changes: 33 additions & 0 deletions category/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
"""Category admin classes"""
from django.contrib import admin
from django.http import HttpResponseRedirect
from django.urls import reverse, reverse_lazy
from django.utils.html import format_html
from django.utils.http import urlencode
from django.utils.translation import gettext_lazy as _

from category.models import Category


@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
"""Category Admin Model"""

list_display = ["name", "tenant_name"]
# list_display_links = ["tenant_name"]
actions = ["move_tenant"]

@admin.display(description=_("Tenant"))
def tenant_name(self, obj):
"""Shows Tenant name"""
link = reverse("admin:tenants_tenant_change", args=[obj.tenant.id])
return format_html('<a href="{}">{}</a>', link, obj.tenant.name)

@admin.action(description=_("Move Category to a different Tenant"))
def move_tenant(self, request, queryset):
"""Moves Categories to a different Tenant"""
ids = queryset.values_list("id", flat=True)
url = f"{reverse_lazy('category:move')}?{urlencode({'pk': list(ids)}, True)}"
return HttpResponseRedirect(url)

tenant_name.admin_order_field = "tenant__id"
8 changes: 8 additions & 0 deletions category/templates/admin/category/migrate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "admin/base/migrate.html" %}
{% load i18n %}
{% load bootstrap4 %}

{% block title %}{% trans "Migrating categories to Tenant" %}{% endblock %}
{% block content_title %}{% trans "Migrating categories to Tenant" %}{% endblock %}
{% block object_name %}{% trans "Categories" %}{% endblock %}
{% block target %}{% trans "To Tenant" %}{% endblock %}
1 change: 1 addition & 0 deletions chords/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@
path("analytics/", include(("analytics.urls", "analytics"), namespace="analytics")),
path("tenants/", include(("tenants.urls", "tenants"), namespace="tenants")),
re_path(r"^", include(("backend.urls", "backend"), namespace="chords")),
path("admin/", admin.site.urls),
path("__debug__/", include(debug_toolbar.urls)),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
34 changes: 34 additions & 0 deletions frontend/templates/admin/base/migrate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{% extends "admin/base.html" %}
{% load i18n %}
{% load bootstrap4 %}

{% block title %}{% endblock %}
{% block content_title %}{% endblock %}
{% block content %}
<form method="post">
{% csrf_token %}
{{ formset.management_form }}
{{ formset.non_form_errors.as_ul }}
<h4>{% block object_name %}{% endblock %}</h4>
<div id="sortable" class="mb-3">
{% for form in formset.forms %}
<div>
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% for field in form.visible_fields %}
{% bootstrap_field field layout="horizontal" show_label=False size="small"%}
{% endfor %}
<hr>
</div>
{% endfor %}
</div>
<hr>
<h4>{% block target %}{% endblock %}</h4>
{% bootstrap_form form %}
{% buttons %}
<button type="submit">{% trans "Submit" %}</button>
{% endbuttons %}
</form>
{{ form.media }}
{% endblock %}
31 changes: 31 additions & 0 deletions pdf/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""PDF Admin classes"""
from urllib.parse import urlencode

from django.contrib import admin
from django.http import HttpResponseRedirect
from django.urls import reverse_lazy, reverse
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

from pdf.models.request import PDFRequest


@admin.register(PDFRequest)
class RequestAdmin(admin.ModelAdmin):
"""Request Model Admin"""

list_display = ["id", "title", "update_date", "type", "file", "status", "tenant_name"]
actions = ["move_tenant"]

@admin.display(description=_("Tenant"))
def tenant_name(self, obj):
"""Shows Tenant name"""
link = reverse("admin:tenants_tenant_change", args=[obj.tenant.id])
return format_html('<a href="{}">{}</a>', link, obj.tenant.name)

@admin.action(description=_("Move Request to a different Tenant"))
def move_tenant(self, request, queryset):
"""Move Request to a different Tenant"""
ids = queryset.values_list("id", flat=True)
url = f"{reverse_lazy('pdf:move')}?{urlencode({'pk': list(ids)}, True)}"
return HttpResponseRedirect(url)
8 changes: 8 additions & 0 deletions pdf/templates/admin/pdf/migrate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% extends "admin/base/migrate.html" %}
{% load i18n %}
{% load bootstrap4 %}

{% block title %}{% trans "Migrating PDFRequests to Tenant" %}{% endblock %}
{% block content_title %}{% trans "Migrating PDFRequests to Tenant" %}{% endblock %}
{% block object_name %}{% trans "PDFRequest" %}{% endblock %}
{% block target %}{% trans "To Tenant" %}{% endblock %}
37 changes: 37 additions & 0 deletions tenants/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
"""Admin models for Tenants"""
from django.conf import settings
from django.contrib import admin
from django.urls import reverse_lazy, reverse
from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _

from category.models import Category
from tenants.forms import TenantForm
from tenants.models import Tenant


@admin.register(Tenant)
class TenantAdmin(admin.ModelAdmin):
"""Tenant Admin view"""

list_display = ["name", "hostname_link", "display_name"]
form = TenantForm

@admin.display(description=_("Tenant"))
def hostname_link(self, obj):
"""Shows link to the tenants homepage"""
link = reverse("chords:index")
return format_html('<a href="{}{}">{}</a>', obj.hostname, link, obj.hostname)

def save_model(self, request, obj, form, change):
"""Overrides saving method to create default category and redirect index page to it"""
if obj.index_redirect == "/":
obj.index_redirect = reverse_lazy("category:index", kwargs={"slug": "default"})
super().save_model(request, obj, form, change)
if not Category.objects.filter(tenant=obj).exists():
Category.objects.create(
tenant=obj, name="Default Category", slug="default", locale=settings.LANGUAGES[0][0], generate_pdf=False
)


admin.site.login_template = "registration/login.html"

0 comments on commit a260f29

Please sign in to comment.