2017-01-09 00:57:38 +01:00
|
|
|
"""
|
|
|
|
Administration minimaliste du site de l'Ernestophone.
|
2020-09-10 15:06:53 +02:00
|
|
|
Les chefs peuvent modifier les utilisateurs et les evenements.
|
|
|
|
Les super-utilisateurs peuvent en plus ajouter ou supprimer ces objets et gerer
|
2017-01-09 00:57:38 +01:00
|
|
|
les permissions.
|
|
|
|
"""
|
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.contrib.auth.admin import UserAdmin
|
2021-04-29 00:27:33 +02:00
|
|
|
from django.contrib.auth.models import Group, Permission, User
|
2017-01-09 00:57:38 +01:00
|
|
|
|
2020-09-10 15:06:53 +02:00
|
|
|
from actu.models import Actu
|
2021-04-29 00:27:33 +02:00
|
|
|
from gestion.models import ErnestoUser, Photo, VideoGallery
|
2015-03-17 19:03:51 +01:00
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
class UserProfileInline(admin.StackedInline):
|
2017-01-09 00:57:38 +01:00
|
|
|
"""Affichage du profil ernestophone dans la vue UserAdmin"""
|
2019-03-16 18:32:49 +01:00
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
model = ErnestoUser
|
2016-07-14 01:58:52 +02:00
|
|
|
|
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
def ProfileInfo(field, short_description, boolean=False):
|
|
|
|
def getter(self):
|
|
|
|
try:
|
|
|
|
return getattr(self.profile, field)
|
|
|
|
except ErnestoUser.DoesNotExist:
|
|
|
|
return ""
|
2019-03-16 18:32:49 +01:00
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
getter.short_description = short_description
|
|
|
|
getter.boolean = boolean
|
|
|
|
return getter
|
|
|
|
|
2019-03-16 18:32:49 +01:00
|
|
|
|
2019-03-16 18:37:45 +01:00
|
|
|
User.profile_doodlename = ProfileInfo("doodlename", "Nom pour le doodle")
|
2020-09-10 15:06:53 +02:00
|
|
|
User.profile_phone = ProfileInfo("phone", "Téléphone")
|
2015-03-17 19:03:51 +01:00
|
|
|
User.profile_instru = ProfileInfo("instru", "Instrument joué")
|
|
|
|
User.profile_is_ern = ProfileInfo("is_ernesto", "Ernestophoniste")
|
|
|
|
User.profile_is_chef = ProfileInfo("is_chef", "Chef Fanfare")
|
2021-10-06 13:46:45 +02:00
|
|
|
User.profile_is_chef_event = ProfileInfo("is_chef_event", "Respo événement")
|
2016-06-21 00:33:32 +02:00
|
|
|
User.profile_get_mails = ProfileInfo("mails", "Recevoir les mails")
|
2015-03-17 19:03:51 +01:00
|
|
|
|
2016-07-14 01:58:52 +02:00
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
class UserProfileAdmin(UserAdmin):
|
2017-01-09 00:57:38 +01:00
|
|
|
"""
|
|
|
|
Management des utilisateurs de l'ernestosite.
|
2020-09-10 15:06:53 +02:00
|
|
|
Lors de la sauvegarde, les chefs se voient octroyees les permissions
|
|
|
|
d'edition des comptes et evenements mais il ne peuvent pas en creer via
|
2017-01-09 00:57:38 +01:00
|
|
|
l'interface admin.
|
|
|
|
"""
|
2019-03-16 18:32:49 +01:00
|
|
|
|
|
|
|
list_display = [
|
|
|
|
"username",
|
|
|
|
"first_name",
|
|
|
|
"last_name",
|
|
|
|
"email",
|
2019-03-16 18:37:45 +01:00
|
|
|
"profile_doodlename",
|
2019-03-16 18:32:49 +01:00
|
|
|
"profile_phone",
|
|
|
|
"profile_instru",
|
|
|
|
"profile_is_ern",
|
|
|
|
"profile_is_chef",
|
2021-10-06 13:46:45 +02:00
|
|
|
"profile_is_chef_event",
|
2019-03-16 18:32:49 +01:00
|
|
|
]
|
|
|
|
list_display_links = ["username", "email", "first_name", "last_name"]
|
|
|
|
list_filter = ["profile__instru"]
|
|
|
|
ordering = ["username"]
|
|
|
|
search_fields = [
|
|
|
|
"username",
|
|
|
|
"first_name",
|
|
|
|
"last_name",
|
|
|
|
"profile__phone",
|
|
|
|
"profile__instru",
|
|
|
|
]
|
2017-01-09 00:57:38 +01:00
|
|
|
inlines = [UserProfileInline]
|
|
|
|
|
|
|
|
staff_fieldsets = [
|
2020-09-10 15:06:53 +02:00
|
|
|
("General", {"fields": ["username", "password", "first_name", "last_name"]}),
|
2019-03-16 18:32:49 +01:00
|
|
|
("Permissions", {"fields": ["is_active"]}),
|
2017-01-09 00:57:38 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_fieldsets(self, request, user=None):
|
|
|
|
"""
|
|
|
|
Les super utilisateurs ont toutes les infos.
|
|
|
|
Les autres (e.g. les chefs) ne peuvent modifier qu'un nombre restreints
|
|
|
|
de choses.
|
|
|
|
"""
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
return self.staff_fieldsets
|
|
|
|
return super(UserProfileAdmin, self).get_fieldsets(request, user)
|
|
|
|
|
|
|
|
def save_model(self, request, user, form, change):
|
|
|
|
"""
|
|
|
|
Ajoute le chef au group Chef.
|
2020-09-10 15:06:53 +02:00
|
|
|
Le groupe est cree si necessaire.
|
2017-01-09 00:57:38 +01:00
|
|
|
"""
|
|
|
|
chef_group, created = Group.objects.get_or_create(name="Chef")
|
|
|
|
if created:
|
2020-09-10 15:06:53 +02:00
|
|
|
# Si le groupe vient d'etre crée
|
2017-01-09 00:57:38 +01:00
|
|
|
# On associe les bonnes permissions au groupe Chef
|
2019-03-16 18:32:49 +01:00
|
|
|
perms = [
|
|
|
|
("change_user", "auth", "user"),
|
|
|
|
("change_event", "calendrier", "event"),
|
|
|
|
("change_ernestouser", "gestion", "ernestouser"),
|
|
|
|
]
|
2017-01-09 00:57:38 +01:00
|
|
|
for nat_key in perms:
|
|
|
|
perm = Permission.objects.get_by_natural_key(*nat_key)
|
|
|
|
chef_group.permissions.add(perm)
|
|
|
|
# On met tous les chef dans le groupe
|
2018-01-05 09:54:23 +01:00
|
|
|
chef_group.user_set.set(User.objects.filter(profile__is_chef=True))
|
2017-01-09 00:57:38 +01:00
|
|
|
# Les chefs sont dans le groupe Chef
|
|
|
|
if user.profile.is_chef:
|
|
|
|
print("J'aime la choucroute")
|
|
|
|
user.is_staff = True
|
|
|
|
user.groups.add(chef_group)
|
|
|
|
user.save()
|
2015-03-28 18:54:48 +01:00
|
|
|
|
2021-04-29 00:27:33 +02:00
|
|
|
|
2015-03-17 19:03:51 +01:00
|
|
|
admin.site.unregister(User)
|
|
|
|
admin.site.register(User, UserProfileAdmin)
|
2020-09-10 15:06:53 +02:00
|
|
|
admin.site.register(VideoGallery)
|
2021-03-31 16:05:26 +02:00
|
|
|
admin.site.register(Photo)
|
2020-09-10 15:06:53 +02:00
|
|
|
admin.site.register(Actu)
|