diff --git a/backend/admin.py b/backend/admin.py new file mode 100644 index 0000000..b85f1f6 --- /dev/null +++ b/backend/admin.py @@ -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" diff --git a/category/admin.py b/category/admin.py new file mode 100644 index 0000000..608075c --- /dev/null +++ b/category/admin.py @@ -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('{}', 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" diff --git a/category/templates/admin/category/migrate.html b/category/templates/admin/category/migrate.html new file mode 100644 index 0000000..0cdde38 --- /dev/null +++ b/category/templates/admin/category/migrate.html @@ -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 %} diff --git a/chords/urls.py b/chords/urls.py index 2060a9c..dada1ee 100644 --- a/chords/urls.py +++ b/chords/urls.py @@ -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) diff --git a/frontend/templates/admin/base/migrate.html b/frontend/templates/admin/base/migrate.html new file mode 100644 index 0000000..efb5345 --- /dev/null +++ b/frontend/templates/admin/base/migrate.html @@ -0,0 +1,34 @@ +{% extends "admin/base.html" %} +{% load i18n %} +{% load bootstrap4 %} + +{% block title %}{% endblock %} +{% block content_title %}{% endblock %} +{% block content %} +
+ {{ form.media }} +{% endblock %} \ No newline at end of file diff --git a/pdf/admin.py b/pdf/admin.py new file mode 100644 index 0000000..318dc1f --- /dev/null +++ b/pdf/admin.py @@ -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('{}', 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) diff --git a/pdf/templates/admin/pdf/migrate.html b/pdf/templates/admin/pdf/migrate.html new file mode 100644 index 0000000..3d276b9 --- /dev/null +++ b/pdf/templates/admin/pdf/migrate.html @@ -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 %} diff --git a/tenants/admin.py b/tenants/admin.py new file mode 100644 index 0000000..07728d5 --- /dev/null +++ b/tenants/admin.py @@ -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('{}', 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"