From cf39d458d576194d2383ec015d5f9b1d8a6d1585 Mon Sep 17 00:00:00 2001 From: Maxim Liksakov <67663774+maxim-lixakov@users.noreply.github.com> Date: Sat, 2 Dec 2023 14:45:42 +0300 Subject: [PATCH] statistics in admin panel (#1) Co-authored-by: Ilyas --- app/distributed_db/settings.py | 6 +++ app/distributed_db/urls.py | 3 +- app/main/admin.py | 81 +++++++++++++++++++++++++++++++--- app/requirements.txt | 4 +- 4 files changed, 85 insertions(+), 9 deletions(-) diff --git a/app/distributed_db/settings.py b/app/distributed_db/settings.py index 491c19d..069977b 100644 --- a/app/distributed_db/settings.py +++ b/app/distributed_db/settings.py @@ -12,6 +12,10 @@ import os from pathlib import Path +from dotenv import load_dotenv + +load_dotenv() + # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -32,6 +36,8 @@ INSTALLED_APPS = [ 'djangocms_admin_style', + 'admin_tools_stats', + 'django_nvd3', 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', diff --git a/app/distributed_db/urls.py b/app/distributed_db/urls.py index 0aefb88..e3cbddd 100644 --- a/app/distributed_db/urls.py +++ b/app/distributed_db/urls.py @@ -14,8 +14,9 @@ 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin -from django.urls import path +from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), + path('admin_tools_stats/', include('admin_tools_stats.urls')), ] diff --git a/app/main/admin.py b/app/main/admin.py index 2edc140..8f237b4 100644 --- a/app/main/admin.py +++ b/app/main/admin.py @@ -10,15 +10,82 @@ def __init__(self, model, admin_site): super(ModelAdmin, self).__init__(model, admin_site) -admin.site.register(Cinema, ModelAdmin) +class CinemaAdmin(admin.ModelAdmin): + list_display = ['name', 'address', 'operating_hours_start', 'operating_hours_end', 'contact_number', 'total_showtimes'] + + def total_showtimes(self, obj): + return Showtime.objects.filter(hall__cinema=obj).count() + + total_showtimes.short_description = 'Total Showtimes' + + +class FilmAdmin(admin.ModelAdmin): + list_display = ['title', 'duration', 'base_price', 'total_showtimes'] + + def total_showtimes(self, obj): + return Showtime.objects.filter(film=obj).count() + + total_showtimes.short_description = 'Total Showtimes' + + +class HallAdmin(admin.ModelAdmin): + list_display = ['seating_capacity', 'total_showtimes', 'total_tickets_sold'] + + def total_showtimes(self, obj): + return Showtime.objects.filter(hall=obj).count() + + def total_tickets_sold(self, obj): + return Ticket.objects.filter(showtime__hall=obj).count() + + total_showtimes.short_description = 'Total showtimes' + total_tickets_sold.short_description = 'Tickets Sold' + + +class GenreAdmin(admin.ModelAdmin): + list_display = ['name', 'total_films'] + + def total_films(self, obj): + return FilmGenre.objects.filter(genre=obj).count() + + total_films.short_description = 'Total films' + + +class SellerAdmin(admin.ModelAdmin): + list_display = ['first_name', 'last_name', 'total_tickets_sold'] + + def total_tickets_sold(self, obj): + return Ticket.objects.filter(seller=obj).count() + + total_tickets_sold.short_description = 'Tickets sold' + + +class CustomerAdmin(admin.ModelAdmin): + list_display = ['first_name', 'last_name', 'total_tickets_purchased'] + + def total_tickets_purchased(self, obj): + return Ticket.objects.filter(customer=obj).count() + + total_tickets_purchased.short_description = 'Tickets purchased' + + +class ProductionCountryAdmin(admin.ModelAdmin): + list_display = ['country', 'total_films'] + + def total_films(self, obj): + return Film.objects.filter(productioncountry=obj).count() + + total_films.short_description = 'Films Total' + + admin.site.register(Country, ModelAdmin) -admin.site.register(Customer, ModelAdmin) -admin.site.register(Film, ModelAdmin) +admin.site.register(Customer, CustomerAdmin) +admin.site.register(Film, FilmAdmin) admin.site.register(FilmGenre, ModelAdmin) -admin.site.register(Genre, ModelAdmin) -admin.site.register(Hall, ModelAdmin) -admin.site.register(ProductionCountry, ModelAdmin) +admin.site.register(Genre, GenreAdmin) +admin.site.register(Hall, HallAdmin) +admin.site.register(ProductionCountry, ProductionCountryAdmin) admin.site.register(Seat, ModelAdmin) -admin.site.register(Seller, ModelAdmin) +admin.site.register(Seller, SellerAdmin) admin.site.register(Showtime, ModelAdmin) admin.site.register(Ticket, ModelAdmin) +admin.site.register(Cinema, CinemaAdmin) diff --git a/app/requirements.txt b/app/requirements.txt index 1186155..e74e508 100644 --- a/app/requirements.txt +++ b/app/requirements.txt @@ -2,4 +2,6 @@ asgiref==3.7.2 Django==4.2.7 sqlparse==0.4.4 djangocms-admin-style==3.2.6 -mysqlclient \ No newline at end of file +mysqlclient +django-admin-charts +python-dotenv \ No newline at end of file