From 7967983b5c864d4acfe9a324ac562cd23f2e9392 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20P=C3=A9pin?= Date: Sun, 25 Jun 2017 17:57:23 +0100 Subject: [PATCH] More sophisticated admin site for the gestion app MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Nested inlines - Limiting access to the eventsĀ : you can only edit/create events linked to associations you for which you have the `.buro` permission. --- gestioCOF/settings_dev.py | 1 + gestioCOF/urls.py | 1 + gestion/admin.py | 60 ++++++++++++++++++++++++++++----------- requirements.txt | 1 + 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/gestioCOF/settings_dev.py b/gestioCOF/settings_dev.py index 43e7f5b0..91fb5b7e 100644 --- a/gestioCOF/settings_dev.py +++ b/gestioCOF/settings_dev.py @@ -43,6 +43,7 @@ INSTALLED_APPS = ( 'channels', 'widget_tweaks', 'custommail', + 'nested_admin', 'bda.apps.BdAConfig', 'bds.apps.BDSConfig', 'cof.apps.COFConfig', diff --git a/gestioCOF/urls.py b/gestioCOF/urls.py index 4918c1ea..363d9ecd 100644 --- a/gestioCOF/urls.py +++ b/gestioCOF/urls.py @@ -81,6 +81,7 @@ urlpatterns = [ cof_views.liste_bdarevente, name="liste_bdarevente"), url(r'^k-fet/', include(kfet.urls)), + url(r"^_nested_admin/", include("nested_admin.urls")), ] if 'debug_toolbar' in settings.INSTALLED_APPS: diff --git a/gestion/admin.py b/gestion/admin.py index 82d28233..af044aeb 100644 --- a/gestion/admin.py +++ b/gestion/admin.py @@ -1,9 +1,12 @@ +import nested_admin + from django.contrib import admin from django.contrib.auth.admin import UserAdmin from django.contrib.auth.models import User +from django.forms.models import modelform_factory from .models import ( - Profile, Club, Event, + Association, Profile, Club, Event, Location, EventOption, EventCommentField, EventOptionChoice ) @@ -30,40 +33,63 @@ admin.site.register(User, UserProfileAdmin) # Clubs # --- -@admin.register(Club) -class ClubAdmin(admin.ModelAdmin): - pass +admin.site.register(Club) # --- # Events # --- -class EventOptionChoiceInline(admin.TabularInline): +class EventOptionChoiceInline(nested_admin.NestedTabularInline): model = EventOptionChoice + extra = 1 -class EventOptionInline(admin.TabularInline): +class EventOptionInline(nested_admin.NestedTabularInline): model = EventOption - show_change_link = True + extra = 1 + inlines = [EventOptionChoiceInline] -class EventCommentFieldInline(admin.TabularInline): +class EventCommentFieldInline(nested_admin.NestedTabularInline): model = EventCommentField - - -@admin.register(EventOption) -class EventOptionAdmin(admin.ModelAdmin): - search_fields = ('event__title', 'name') - inlines = [ - EventOptionChoiceInline, - ] + extra = 1 @admin.register(Event) -class EventAdmin(admin.ModelAdmin): +class EventAdmin(nested_admin.NestedModelAdmin): search_fields = ['title', 'location', 'description'] inlines = [ EventOptionInline, EventCommentFieldInline, ] + + def get_queryset(self, request): + """Restrict the event you can edit""" + qs = super().get_queryset(request) + if request.user.is_superuser: + return qs + else: + assocs = Association.objects.filter(staff_group__user=request.user) + return qs.filter(associations__in=assocs) + + def get_form(self, request, obj=None, **kwargs): + """Restrict the applications you can attach to an event""" + if request.user.is_superuser: + return super().get_form(request, obj, **kwargs) + else: + assocs = Association.objects.filter(staff_group__user=request.user) + + def formfield_callback(f, **kwargs): + if f.name == "associations": + kwargs["queryset"] = assocs + return f.formfield(**kwargs) + + return modelform_factory( + Event, + exclude=[], + formfield_callback=formfield_callback + ) + + +admin.site.register(Location) diff --git a/requirements.txt b/requirements.txt index 8da5f3ea..20ee710b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -19,3 +19,4 @@ django-widget-tweaks==1.4.1 git+https://git.eleves.ens.fr/cof-geek/django_custommail.git#egg=django_custommail ldap3 git+https://github.com/Aureplop/channels.git#egg=channels +django-nested-admin=3.0.17