More sophisticated admin site for the gestion app

- Nested inlines
- Limiting access to the events : you can only edit/create events linked to
  associations you for which you have the `<assoc>.buro` permission.
This commit is contained in:
Martin Pépin 2017-06-25 17:57:23 +01:00
parent a9e6ef6c5c
commit 7967983b5c
4 changed files with 46 additions and 17 deletions

View file

@ -43,6 +43,7 @@ INSTALLED_APPS = (
'channels', 'channels',
'widget_tweaks', 'widget_tweaks',
'custommail', 'custommail',
'nested_admin',
'bda.apps.BdAConfig', 'bda.apps.BdAConfig',
'bds.apps.BDSConfig', 'bds.apps.BDSConfig',
'cof.apps.COFConfig', 'cof.apps.COFConfig',

View file

@ -81,6 +81,7 @@ urlpatterns = [
cof_views.liste_bdarevente, cof_views.liste_bdarevente,
name="liste_bdarevente"), name="liste_bdarevente"),
url(r'^k-fet/', include(kfet.urls)), url(r'^k-fet/', include(kfet.urls)),
url(r"^_nested_admin/", include("nested_admin.urls")),
] ]
if 'debug_toolbar' in settings.INSTALLED_APPS: if 'debug_toolbar' in settings.INSTALLED_APPS:

View file

@ -1,9 +1,12 @@
import nested_admin
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.admin import UserAdmin from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.forms.models import modelform_factory
from .models import ( from .models import (
Profile, Club, Event, Association, Profile, Club, Event, Location,
EventOption, EventCommentField, EventOptionChoice EventOption, EventCommentField, EventOptionChoice
) )
@ -30,40 +33,63 @@ admin.site.register(User, UserProfileAdmin)
# Clubs # Clubs
# --- # ---
@admin.register(Club) admin.site.register(Club)
class ClubAdmin(admin.ModelAdmin):
pass
# --- # ---
# Events # Events
# --- # ---
class EventOptionChoiceInline(admin.TabularInline): class EventOptionChoiceInline(nested_admin.NestedTabularInline):
model = EventOptionChoice model = EventOptionChoice
extra = 1
class EventOptionInline(admin.TabularInline): class EventOptionInline(nested_admin.NestedTabularInline):
model = EventOption model = EventOption
show_change_link = True extra = 1
inlines = [EventOptionChoiceInline]
class EventCommentFieldInline(admin.TabularInline): class EventCommentFieldInline(nested_admin.NestedTabularInline):
model = EventCommentField model = EventCommentField
extra = 1
@admin.register(EventOption)
class EventOptionAdmin(admin.ModelAdmin):
search_fields = ('event__title', 'name')
inlines = [
EventOptionChoiceInline,
]
@admin.register(Event) @admin.register(Event)
class EventAdmin(admin.ModelAdmin): class EventAdmin(nested_admin.NestedModelAdmin):
search_fields = ['title', 'location', 'description'] search_fields = ['title', 'location', 'description']
inlines = [ inlines = [
EventOptionInline, EventOptionInline,
EventCommentFieldInline, 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)

View file

@ -19,3 +19,4 @@ django-widget-tweaks==1.4.1
git+https://git.eleves.ens.fr/cof-geek/django_custommail.git#egg=django_custommail git+https://git.eleves.ens.fr/cof-geek/django_custommail.git#egg=django_custommail
ldap3 ldap3
git+https://github.com/Aureplop/channels.git#egg=channels git+https://github.com/Aureplop/channels.git#egg=channels
django-nested-admin=3.0.17