From aca1d2def65c28df797fe877d82c05d4e81d8904 Mon Sep 17 00:00:00 2001 From: Alexandre Iooss Date: Sat, 10 Aug 2019 08:04:46 +0200 Subject: [PATCH] Merge index views --- README.md | 2 +- med/admin.py | 29 ++++++ med/urls.py | 6 +- media/admin.py | 10 +- media/templates/media/index.html | 81 ---------------- media/views.py | 13 ++- theme/templates/admin/base_site.html | 16 ++-- theme/templates/admin/index.html | 133 +++++++++++++++++++++++++++ users/admin.py | 7 +- 9 files changed, 189 insertions(+), 108 deletions(-) create mode 100644 med/admin.py delete mode 100644 media/templates/media/index.html create mode 100644 theme/templates/admin/index.html diff --git a/README.md b/README.md index c73b180..20d9ff6 100644 --- a/README.md +++ b/README.md @@ -9,7 +9,7 @@ Elle permet de gérer les medias, bd, jeux, emprunts, ainsi que les adhérents d ## Licence -Ce projet est sous la licence GNU public license v2.0. +Ce projet est sous la licence GNU public license v3.0. ## Développement diff --git a/med/admin.py b/med/admin.py new file mode 100644 index 0000000..e97205b --- /dev/null +++ b/med/admin.py @@ -0,0 +1,29 @@ +# -*- mode: python; coding: utf-8 -*- +# Copyright (C) 2017-2019 by BDE ENS Paris-Saclay +# SPDX-License-Identifier: GPL-3.0-or-later + +from django.contrib.admin import AdminSite +from django.utils.translation import gettext_lazy as _ +from django.views.decorators.cache import never_cache + +from media.models import Emprunt + + +class DatabaseAdmin(AdminSite): + index_title = _('Welcome to the Mediatek database') + + @never_cache + def index(self, request, extra_context=None): + """ + Add borrowed item to admin index + """ + response = super().index(request, extra_context) + + # User is always authenticated + user_borrowed = Emprunt.objects.filter(user=request.user) + response.context_data["borrowed_items"] = user_borrowed + + return response + + +admin_site = DatabaseAdmin() diff --git a/med/urls.py b/med/urls.py index ff8ce81..4b957cb 100644 --- a/med/urls.py +++ b/med/urls.py @@ -3,11 +3,11 @@ # SPDX-License-Identifier: GPL-3.0-or-later from django.conf.urls import include, url -from django.contrib import admin from django.contrib.auth.views import password_reset from django.views.generic import RedirectView from media.views import index +from .admin import admin_site urlpatterns = [ url(r'^$', index, name='index'), @@ -24,6 +24,6 @@ urlpatterns = [ url(r'^accounts/', include('django.contrib.auth.urls')), url(r'^accounts/profile/', RedirectView.as_view(pattern_name='index')), - url(r'^admin/doc/', include('django.contrib.admindocs.urls')), - url(r'^admin/', admin.site.urls), + url(r'^database/doc/', include('django.contrib.admindocs.urls')), + url(r'^database/', admin_site.urls), ] diff --git a/media/admin.py b/media/admin.py index 61d0547..e65454a 100644 --- a/media/admin.py +++ b/media/admin.py @@ -2,9 +2,9 @@ # Copyright (C) 2017-2019 by BDE ENS Paris-Saclay # SPDX-License-Identifier: GPL-3.0-or-later -from django.contrib import admin from reversion.admin import VersionAdmin +from med.admin import admin_site from .models import Auteur, Emprunt, Jeu, Media @@ -29,7 +29,7 @@ class JeuAdmin(VersionAdmin): 'nombre_joueurs_max', 'comment') -admin.site.register(Auteur, AuteurAdmin) -admin.site.register(Media, MediaAdmin) -admin.site.register(Emprunt, EmpruntAdmin) -admin.site.register(Jeu, JeuAdmin) +admin_site.register(Auteur, AuteurAdmin) +admin_site.register(Media, MediaAdmin) +admin_site.register(Emprunt, EmpruntAdmin) +admin_site.register(Jeu, JeuAdmin) diff --git a/media/templates/media/index.html b/media/templates/media/index.html deleted file mode 100644 index 41d12c4..0000000 --- a/media/templates/media/index.html +++ /dev/null @@ -1,81 +0,0 @@ -{% extends "admin/base_site.html" %} -{% comment %} -SPDX-License-Identifier: GPL-3.0-or-later -{% endcomment %} - -{% load i18n static %} - -{% block title %}Base de donnée de la Mediatek{% endblock %} - -{% block extrastyle %} - {{ block.super }} - -{% endblock %} - -{% block coltype %}colMS{% endblock %} - -{% block bodyclass %}{{ block.super }} dashboard{% endblock %} - -{% block breadcrumbs %}{% endblock %} - -{% block content %} -
-

Bienvenue sur la base de données de la Mediatek.

-

Welcome to procrastination heaven !

-

- Le site va subir progressivement des mises à jour pendant ces vacances. - Si vous rencontrez des instabilités, - veuillez nous faire remonter les problèmes au webmaster. -

- Poulpy -
-{% endblock %} - -{% block sidebar %} - -{% endblock %} diff --git a/media/views.py b/media/views.py index e23bfdd..f20c942 100644 --- a/media/views.py +++ b/media/views.py @@ -8,6 +8,7 @@ from django.db import transaction from django.shortcuts import redirect, render from django.template.context_processors import csrf from django.utils import timezone +from django.utils.translation import gettext_lazy as _ from reversion import revisions as reversion from users.models import User @@ -70,13 +71,11 @@ def retour_emprunt(request, empruntid): def index(request): """ - Home page with user's borrowed items + Home page which redirect to admin when logged in """ if request.user.is_authenticated: - borrowed_items = Emprunt.objects.filter(user=request.user) + return redirect('admin:index') else: - borrowed_items = [] - - return render(request, 'media/index.html', { - 'borrowed_items': borrowed_items, - }) + return render(request, 'admin/index.html', { + 'title': _('Welcome to the Mediatek database'), + }) diff --git a/theme/templates/admin/base_site.html b/theme/templates/admin/base_site.html index 02fe48a..d0c1aaf 100644 --- a/theme/templates/admin/base_site.html +++ b/theme/templates/admin/base_site.html @@ -25,10 +25,9 @@ SPDX-License-Identifier: GPL-3.0-or-later {% endif %} {% endblock %} {% block userlinks %} - {% if user.is_authenticated %} - {% if available_apps %} - {# When in admin site, list all admin pages and documentation #} - + {% if available_apps %} + {# When in admin site, list all admin pages and documentation #} + {% trans 'Explore database' %} {% for app in available_apps %} @@ -47,10 +46,11 @@ SPDX-License-Identifier: GPL-3.0-or-later {% endif %} / - {% elif user.is_staff %} - {# When not in admin site, but user is staff then add a link #} - {% trans 'Explore database' %} / - {% endif %} + {% else %} + {# When not in admin site, but user is staff then add a link #} + {% trans 'Explore database' %} / + {% endif %} + {% if user.is_authenticated %} {% trans 'Log out' %} {% else %} {% trans 'Log in' %} diff --git a/theme/templates/admin/index.html b/theme/templates/admin/index.html new file mode 100644 index 0000000..d8bf3a8 --- /dev/null +++ b/theme/templates/admin/index.html @@ -0,0 +1,133 @@ +{% extends "admin/index.html" %} +{% comment %} +SPDX-License-Identifier: GPL-3.0-or-later +{% endcomment %} + +{% load i18n static %} + +{% block content %} +
+

Welcome to procrastination haven !

+

+ Le site va subir progressivement des mises à jour pendant ces vacances. + Si vous rencontrez des instabilités, + veuillez nous faire remonter les problèmes au webmaster. +

+ + {% if app_list %} + {% for app in app_list %} +
+ + + {% for model in app.models %} + + {% if model.admin_url %} + + {% else %} + + {% endif %} + + {% if model.add_url %} + + {% else %} + + {% endif %} + + {% if model.admin_url %} + + {% else %} + + {% endif %} + + {% endfor %} +
+ {{ app.name }} +
{{ model.name }}{{ model.name }}{% trans 'Add' %} {% trans 'Change' %} 
+
+ {% endfor %} + {% endif %} + + Poulpy +
+{% endblock %} + + +{% block sidebar %} + +{% endblock %} + + + diff --git a/users/admin.py b/users/admin.py index bcb4ecc..01d5095 100644 --- a/users/admin.py +++ b/users/admin.py @@ -13,6 +13,7 @@ from reversion.admin import VersionAdmin from .forms import UserCreationAdminForm from .models import Adhesion, Clef, User +from med.admin import admin_site class ClefAdmin(VersionAdmin): @@ -122,6 +123,6 @@ class UserAdmin(VersionAdmin, BaseUserAdmin): actions_btn.allow_tags = True -admin.site.register(User, UserAdmin) -admin.site.register(Adhesion, AdhesionAdmin) -admin.site.register(Clef, ClefAdmin) +admin_site.register(User, UserAdmin) +admin_site.register(Adhesion, AdhesionAdmin) +admin_site.register(Clef, ClefAdmin)