2012-06-27 23:28:35 +02:00
|
|
|
# coding: utf-8
|
|
|
|
|
|
|
|
from django.contrib import admin
|
2013-09-05 22:20:52 +02:00
|
|
|
from gestioncof.models import *
|
|
|
|
from gestioncof.petits_cours_models import *
|
2012-07-11 17:39:20 +02:00
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.contrib.auth.admin import UserAdmin
|
2012-06-27 23:28:35 +02:00
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.utils.safestring import mark_safe
|
2013-09-05 22:20:52 +02:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2012-06-27 23:28:35 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
import autocomplete_light
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
def add_link_field(target_model='', field='', link_text=unicode,
|
|
|
|
desc_text=unicode):
|
2012-06-27 23:28:35 +02:00
|
|
|
def add_link(cls):
|
|
|
|
reverse_name = target_model or cls.model.__name__.lower()
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
def link(self, instance):
|
|
|
|
app_name = instance._meta.app_label
|
|
|
|
reverse_path = "admin:%s_%s_change" % (app_name, reverse_name)
|
|
|
|
link_obj = getattr(instance, field, None) or instance
|
|
|
|
if not link_obj.id:
|
|
|
|
return ""
|
2016-07-09 21:19:37 +02:00
|
|
|
url = reverse(reverse_path, args=(link_obj.id,))
|
2016-07-09 22:31:56 +02:00
|
|
|
return mark_safe("<a href='%s'>%s</a>" %
|
|
|
|
(url, link_text(link_obj)))
|
2012-06-27 23:28:35 +02:00
|
|
|
link.allow_tags = True
|
|
|
|
link.short_description = desc_text(reverse_name + ' link')
|
|
|
|
cls.link = link
|
2016-07-09 22:31:56 +02:00
|
|
|
cls.readonly_fields =\
|
|
|
|
list(getattr(cls, 'readonly_fields', [])) + ['link']
|
2012-06-27 23:28:35 +02:00
|
|
|
return cls
|
|
|
|
return add_link
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
class SurveyQuestionAnswerInline(admin.TabularInline):
|
|
|
|
model = SurveyQuestionAnswer
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-17 13:26:55 +02:00
|
|
|
@add_link_field(desc_text=lambda x: "Réponses",
|
2016-07-09 21:19:37 +02:00
|
|
|
link_text=lambda x: "Éditer les réponses")
|
2012-06-27 23:28:35 +02:00
|
|
|
class SurveyQuestionInline(admin.TabularInline):
|
|
|
|
model = SurveyQuestion
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
class SurveyQuestionAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
2016-07-09 22:31:56 +02:00
|
|
|
SurveyQuestionAnswerInline,
|
|
|
|
]
|
2012-06-27 23:28:35 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
class SurveyAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
2016-07-09 22:31:56 +02:00
|
|
|
SurveyQuestionInline,
|
|
|
|
]
|
2012-06-27 23:28:35 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-07-11 17:39:20 +02:00
|
|
|
class EventOptionChoiceInline(admin.TabularInline):
|
2012-06-27 23:28:35 +02:00
|
|
|
model = EventOptionChoice
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-17 13:26:55 +02:00
|
|
|
@add_link_field(desc_text=lambda x: "Choix",
|
2016-07-09 21:19:37 +02:00
|
|
|
link_text=lambda x: "Éditer les choix")
|
2012-07-11 17:39:20 +02:00
|
|
|
class EventOptionInline(admin.TabularInline):
|
2012-06-27 23:28:35 +02:00
|
|
|
model = EventOption
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2014-08-19 12:54:22 +02:00
|
|
|
class EventCommentFieldInline(admin.TabularInline):
|
|
|
|
model = EventCommentField
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
class EventOptionAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
2016-07-09 22:31:56 +02:00
|
|
|
EventOptionChoiceInline,
|
|
|
|
]
|
2012-06-27 23:28:35 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
class EventAdmin(admin.ModelAdmin):
|
|
|
|
inlines = [
|
2016-07-09 22:31:56 +02:00
|
|
|
EventOptionInline,
|
|
|
|
EventCommentFieldInline,
|
|
|
|
]
|
2012-06-27 23:28:35 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2012-07-11 17:39:20 +02:00
|
|
|
class CofProfileInline(admin.StackedInline):
|
|
|
|
model = CofProfile
|
|
|
|
inline_classes = ("collapse open",)
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
class FkeyLookup(object):
|
2016-07-09 22:31:56 +02:00
|
|
|
def __init__(self, fkeydecl, short_description=None,
|
|
|
|
admin_order_field=None):
|
2013-09-05 22:20:52 +02:00
|
|
|
self.fk, fkattrs = fkeydecl.split('__', 1)
|
|
|
|
self.fkattrs = fkattrs.split('__')
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
self.short_description = short_description or self.fkattrs[-1]
|
|
|
|
self.admin_order_field = admin_order_field or fkeydecl
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
def __get__(self, obj, klass):
|
|
|
|
if obj is None:
|
2016-07-09 22:31:56 +02:00
|
|
|
"""
|
|
|
|
hack required to make Django validate (if obj is
|
|
|
|
None, then we're a class, and classes are callable
|
|
|
|
<wink>)
|
|
|
|
"""
|
|
|
|
return self
|
2013-09-05 22:20:52 +02:00
|
|
|
item = getattr(obj, self.fk)
|
|
|
|
for attr in self.fkattrs:
|
|
|
|
item = getattr(item, attr)
|
|
|
|
return item
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-17 13:26:55 +02:00
|
|
|
def ProfileInfo(field, short_description, boolean=False):
|
2013-09-05 22:20:52 +02:00
|
|
|
def getter(self):
|
2012-07-11 17:39:20 +02:00
|
|
|
try:
|
2016-05-24 00:02:25 +02:00
|
|
|
return getattr(self.profile, field)
|
2013-09-05 22:20:52 +02:00
|
|
|
except CofProfile.DoesNotExist:
|
2012-07-11 17:39:20 +02:00
|
|
|
return ""
|
2013-09-05 22:20:52 +02:00
|
|
|
getter.short_description = short_description
|
|
|
|
getter.boolean = boolean
|
|
|
|
return getter
|
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
User.profile_login_clipper = FkeyLookup("profile__login_clipper",
|
|
|
|
"Login clipper")
|
2013-09-05 22:20:52 +02:00
|
|
|
User.profile_num = FkeyLookup("profile__num", "Numéro")
|
|
|
|
User.profile_phone = ProfileInfo("phone", "Téléphone")
|
|
|
|
User.profile_occupation = ProfileInfo("occupation", "Occupation")
|
|
|
|
User.profile_departement = ProfileInfo("departement", "Departement")
|
|
|
|
User.profile_mailing_cof = ProfileInfo("mailing_cof", "ML COF", True)
|
|
|
|
User.profile_mailing_bda = ProfileInfo("mailing_bda", "ML BDA", True)
|
2016-07-09 22:31:56 +02:00
|
|
|
User.profile_mailing_bda_revente = ProfileInfo("mailing_bda_revente",
|
|
|
|
"ML BDA-R", True)
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
class UserProfileAdmin(UserAdmin):
|
2012-07-11 17:39:20 +02:00
|
|
|
def is_buro(self, obj):
|
|
|
|
try:
|
2016-05-24 00:02:25 +02:00
|
|
|
return obj.profile.is_buro
|
2013-09-05 22:20:52 +02:00
|
|
|
except CofProfile.DoesNotExist:
|
2012-07-11 17:39:20 +02:00
|
|
|
return False
|
|
|
|
is_buro.short_description = 'Membre du Buro'
|
|
|
|
is_buro.boolean = True
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2012-07-11 17:39:20 +02:00
|
|
|
def is_cof(self, obj):
|
|
|
|
try:
|
2016-05-24 00:02:25 +02:00
|
|
|
return obj.profile.is_cof
|
2013-09-05 22:20:52 +02:00
|
|
|
except CofProfile.DoesNotExist:
|
2012-07-11 17:39:20 +02:00
|
|
|
return False
|
|
|
|
is_cof.short_description = 'Membre du COF'
|
|
|
|
is_cof.boolean = True
|
2016-06-17 13:26:55 +02:00
|
|
|
list_display = ('profile_num',) + UserAdmin.list_display \
|
2016-07-09 22:31:56 +02:00
|
|
|
+ ('profile_login_clipper', 'profile_phone', 'profile_occupation',
|
|
|
|
'profile_mailing_cof', 'profile_mailing_bda',
|
|
|
|
'profile_mailing_bda_revente', 'is_cof', 'is_buro', )
|
|
|
|
list_display_links = ('username', 'email', 'first_name', 'last_name')
|
2016-06-17 13:26:55 +02:00
|
|
|
list_filter = UserAdmin.list_filter \
|
2016-07-09 22:31:56 +02:00
|
|
|
+ ('profile__is_cof', 'profile__is_buro', 'profile__mailing_cof',
|
|
|
|
'profile__mailing_bda')
|
2016-05-21 23:57:36 +02:00
|
|
|
search_fields = UserAdmin.search_fields + ('profile__phone',)
|
2012-07-11 17:39:20 +02:00
|
|
|
inlines = [
|
2016-07-09 22:31:56 +02:00
|
|
|
CofProfileInline,
|
|
|
|
]
|
2012-07-11 17:39:20 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
def user_unicode(self):
|
|
|
|
if self.first_name and self.last_name:
|
|
|
|
return u"%s %s (%s)" % (self.first_name, self.last_name, self.username)
|
|
|
|
else:
|
|
|
|
return self.username
|
|
|
|
User.__unicode__ = user_unicode
|
2016-07-09 21:19:37 +02:00
|
|
|
|
|
|
|
|
2013-09-05 22:20:52 +02:00
|
|
|
class EventRegistrationAdmin(admin.ModelAdmin):
|
2016-05-24 00:25:06 +02:00
|
|
|
form = autocomplete_light.modelform_factory(EventRegistration, exclude=[])
|
2016-07-09 22:31:56 +02:00
|
|
|
list_display = ('__unicode__', 'event', 'user', 'paid')
|
2013-09-05 22:20:52 +02:00
|
|
|
list_filter = ('paid',)
|
2016-06-17 13:26:55 +02:00
|
|
|
search_fields = ('user__username', 'user__first_name', 'user__last_name',
|
|
|
|
'user__email', 'event__title')
|
2013-09-05 22:20:52 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-01 15:27:19 +02:00
|
|
|
class PetitCoursAbilityAdmin(admin.ModelAdmin):
|
2016-07-09 22:31:56 +02:00
|
|
|
list_display = ('user', 'matiere', 'niveau', 'agrege')
|
2016-06-17 13:26:55 +02:00
|
|
|
search_fields = ('user__username', 'user__first_name', 'user__last_name',
|
|
|
|
'user__email', 'matiere__name', 'niveau')
|
2016-07-09 22:31:56 +02:00
|
|
|
list_filter = ('matiere', 'niveau', 'agrege')
|
2013-10-01 15:27:19 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-01 15:27:19 +02:00
|
|
|
class PetitCoursAttributionAdmin(admin.ModelAdmin):
|
2016-07-09 22:31:56 +02:00
|
|
|
list_display = ('user', 'demande', 'matiere', 'rank', )
|
2013-10-01 15:27:19 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-01 15:27:19 +02:00
|
|
|
class PetitCoursAttributionCounterAdmin(admin.ModelAdmin):
|
2016-07-09 22:31:56 +02:00
|
|
|
list_display = ('user', 'matiere', 'count', )
|
2013-10-01 15:27:19 +02:00
|
|
|
list_filter = ('matiere',)
|
2016-06-17 13:26:55 +02:00
|
|
|
search_fields = ('user__username', 'user__first_name', 'user__last_name',
|
|
|
|
'user__email', 'matiere__name')
|
2016-07-09 22:31:56 +02:00
|
|
|
actions = ['reset', ]
|
2016-06-17 18:31:10 +02:00
|
|
|
actions_on_bottom = True
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-06-17 18:31:10 +02:00
|
|
|
def reset(self, request, queryset):
|
|
|
|
queryset.update(count=0)
|
|
|
|
reset.short_description = u"Remise à zéro du compteur"
|
2013-10-01 15:27:19 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2013-10-01 15:27:19 +02:00
|
|
|
class PetitCoursDemandeAdmin(admin.ModelAdmin):
|
2016-07-09 22:31:56 +02:00
|
|
|
list_display = ('name', 'email', 'agrege_requis', 'niveau', 'created',
|
|
|
|
'traitee', 'processed')
|
|
|
|
list_filter = ('traitee', 'niveau')
|
2013-10-01 15:27:19 +02:00
|
|
|
|
2012-06-27 23:28:35 +02:00
|
|
|
admin.site.register(Survey, SurveyAdmin)
|
|
|
|
admin.site.register(SurveyQuestion, SurveyQuestionAdmin)
|
|
|
|
admin.site.register(Event, EventAdmin)
|
|
|
|
admin.site.register(EventOption, EventOptionAdmin)
|
2012-07-11 17:39:20 +02:00
|
|
|
admin.site.unregister(User)
|
|
|
|
admin.site.register(User, UserProfileAdmin)
|
|
|
|
admin.site.register(CofProfile)
|
2014-08-19 12:54:22 +02:00
|
|
|
admin.site.register(Club)
|
|
|
|
admin.site.register(CustomMail)
|
2013-09-05 22:20:52 +02:00
|
|
|
admin.site.register(PetitCoursSubject)
|
2013-10-01 15:27:19 +02:00
|
|
|
admin.site.register(PetitCoursAbility, PetitCoursAbilityAdmin)
|
|
|
|
admin.site.register(PetitCoursAttribution, PetitCoursAttributionAdmin)
|
2016-07-09 22:31:56 +02:00
|
|
|
admin.site.register(PetitCoursAttributionCounter,
|
|
|
|
PetitCoursAttributionCounterAdmin)
|
2013-10-01 15:27:19 +02:00
|
|
|
admin.site.register(PetitCoursDemande, PetitCoursDemandeAdmin)
|
2013-09-05 22:20:52 +02:00
|
|
|
admin.site.register(EventRegistration, EventRegistrationAdmin)
|