2016-07-15 00:02:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2012-06-27 23:28:35 +02:00
|
|
|
|
2016-08-23 22:15:31 +02:00
|
|
|
from django import forms
|
2012-06-27 23:28:35 +02:00
|
|
|
from django.contrib import admin
|
|
|
|
from django.core.urlresolvers import reverse
|
|
|
|
from django.utils.safestring import mark_safe
|
2016-05-26 22:44:10 +02:00
|
|
|
import django.utils.six as six
|
2012-06-27 23:28:35 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
import autocomplete_light
|
|
|
|
|
2017-02-12 04:24:33 +01:00
|
|
|
from .petits_cours_models import PetitCoursDemande, \
|
|
|
|
PetitCoursSubject, PetitCoursAbility, PetitCoursAttribution, \
|
|
|
|
PetitCoursAttributionCounter
|
|
|
|
from .models import (
|
|
|
|
SurveyQuestionAnswer, SurveyQuestion, CofProfile, EventOption,
|
|
|
|
EventOptionChoice, Event, Club, EventCommentField, EventRegistration,
|
|
|
|
Survey
|
|
|
|
)
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-07-15 00:02:56 +02:00
|
|
|
def add_link_field(target_model='', field='', link_text=six.text_type,
|
|
|
|
desc_text=six.text_type):
|
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-10 00:26:02 +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):
|
2016-07-13 01:01:07 +02:00
|
|
|
search_fields = ('survey__title', 'answer')
|
2012-06-27 23:28:35 +02:00
|
|
|
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):
|
2016-07-13 01:01:07 +02:00
|
|
|
search_fields = ('title', 'details')
|
2012-06-27 23:28:35 +02:00
|
|
|
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):
|
2016-07-13 01:01:07 +02:00
|
|
|
search_fields = ('event__title', 'name')
|
2012-06-27 23:28:35 +02:00
|
|
|
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):
|
2016-07-13 01:01:07 +02:00
|
|
|
search_fields = ('title', 'location', 'description')
|
2012-06-27 23:28:35 +02:00
|
|
|
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
|
|
|
|
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-16 20:54:52 +02:00
|
|
|
list_display = ('__unicode__' if six.PY2 else '__str__', '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', )
|
2016-07-13 01:01:07 +02:00
|
|
|
search_fields = ('user__username', 'matiere__name')
|
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)
|
2016-07-08 23:04:34 +02:00
|
|
|
reset.short_description = "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')
|
2016-07-13 01:01:07 +02:00
|
|
|
search_fields = ('name', 'email', 'phone', 'lieu', 'remarques')
|
|
|
|
|
|
|
|
|
2016-08-23 22:15:31 +02:00
|
|
|
class ClubAdminForm(forms.ModelForm):
|
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(ClubAdminForm, self).clean()
|
|
|
|
respos = cleaned_data.get('respos')
|
|
|
|
members = cleaned_data.get('membres')
|
|
|
|
for respo in respos.all():
|
|
|
|
if respo not in members:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
"Erreur : le respo %s n'est pas membre du club."
|
|
|
|
% respo.get_full_name())
|
|
|
|
return cleaned_data
|
|
|
|
|
|
|
|
|
|
|
|
class ClubAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['name']
|
|
|
|
form = ClubAdminForm
|
|
|
|
|
|
|
|
|
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.register(CofProfile)
|
2016-08-23 22:15:31 +02:00
|
|
|
admin.site.register(Club, ClubAdmin)
|
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)
|