Simpler admin interface

This commit is contained in:
Martin Pépin 2017-02-18 19:06:43 +01:00
parent 52bdd9824a
commit 859f191894
3 changed files with 18 additions and 81 deletions

View file

@ -2,12 +2,8 @@
from django import forms
from django.contrib import admin
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User, Group, Permission
from django.contrib.auth.admin import UserAdmin
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
from django.db.models import Q
import django.utils.six as six
import autocomplete_light
@ -21,8 +17,6 @@ from .models import (
Survey
)
from gestion.models import Profile
def add_link_field(target_model='', field='', link_text=six.text_type,
desc_text=six.text_type):
@ -100,77 +94,6 @@ class EventAdmin(admin.ModelAdmin):
]
class ProfileInline(admin.StackedInline):
model = Profile
inline_classes = ("collapse open",)
class UserProfileAdmin(UserAdmin):
def login_clipper(self, obj):
return obj.profile.login_clipper
def phone(self, obj):
return obj.profile.phone
def occupation(self, obj):
return obj.profile.occupation
list_display = (
UserAdmin.list_display + ('login_clipper', 'phone', 'occupation')
)
list_display_links = ('username', 'email', 'first_name', 'last_name')
search_fields = UserAdmin.search_fields + ('profile__phone', )
inlines = [
ProfileInline,
]
staff_fieldsets = [
(None, {'fields': ['username', 'password']}),
(_('Personal info'), {'fields': ['first_name', 'last_name', 'email']}),
]
def get_fieldsets(self, request, user=None):
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):
cof_group, created = Group.objects.get_or_create(name='COF')
if created:
# Si le groupe COF n'était pas déjà dans la bdd
# On lui assigne les bonnes permissions
perms = Permission.objects.filter(
Q(content_type__app_label='cof')
| Q(content_type__app_label='bda')
| (Q(content_type__app_label='auth')
& Q(content_type__model='user')))
cof_group.permissions = perms
# On y associe les membres du Burô
cof_group.user_set = User.objects.filter(profile__is_buro=True)
# Sauvegarde
cof_group.save()
# le Burô est staff et appartient au groupe COF
if user.profile.is_buro:
user.is_staff = True
user.groups.add(cof_group)
else:
user.is_staff = False
user.groups.remove(cof_group)
user.save()
# FIXME: This is absolutely horrible.
def user_unicode(self):
if self.first_name and self.last_name:
return "%s %s (%s)" % (self.first_name, self.last_name, self.username)
else:
return self.username
if six.PY2:
User.__unicode__ = user_unicode
else:
User.__str__ = user_unicode
class EventRegistrationAdmin(admin.ModelAdmin):
form = autocomplete_light.modelform_factory(EventRegistration, exclude=[])
list_display = ('__unicode__' if six.PY2 else '__str__', 'event', 'user',
@ -234,8 +157,6 @@ admin.site.register(Survey, SurveyAdmin)
admin.site.register(SurveyQuestion, SurveyQuestionAdmin)
admin.site.register(Event, EventAdmin)
admin.site.register(EventOption, EventOptionAdmin)
admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)
admin.site.register(CofProfile)
admin.site.register(Club, ClubAdmin)
admin.site.register(PetitCoursSubject)

View file

@ -56,7 +56,6 @@ class CofProfile(models.Model):
@property
def is_cof(self):
print("Coucou")
return self.profile.user.has_perm('cof.member')
@is_cof.setter

View file

@ -1,3 +1,20 @@
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
from django.contrib.auth.models import User
# Register your models here.
from .models import Profile
class ProfileInline(admin.StackedInline):
model = Profile
inline_classes = ["collapse open"]
class UserProfileAdmin(UserAdmin):
inlines = [
ProfileInline,
]
admin.site.unregister(User)
admin.site.register(User, UserProfileAdmin)