gestioCOF/cof/admin.py

104 lines
3.5 KiB
Python
Raw Normal View History

2016-07-15 00:02:56 +02:00
# -*- coding: utf-8 -*-
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
import django.utils.six as six
2012-06-27 23:28:35 +02:00
from .petits_cours_models import PetitCoursDemande, \
PetitCoursSubject, PetitCoursAbility, PetitCoursAttribution, \
PetitCoursAttributionCounter
from .models import (
SurveyQuestionAnswer, SurveyQuestion, CofProfile, Survey
)
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()
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 ""
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
cls.readonly_fields =\
list(getattr(cls, 'readonly_fields', [])) + ['link']
2012-06-27 23:28:35 +02:00
return cls
return add_link
2012-06-27 23:28:35 +02:00
class SurveyQuestionAnswerInline(admin.TabularInline):
model = SurveyQuestionAnswer
2016-06-17 13:26:55 +02:00
@add_link_field(desc_text=lambda x: "Réponses",
link_text=lambda x: "Éditer les réponses")
2012-06-27 23:28:35 +02:00
class SurveyQuestionInline(admin.TabularInline):
model = SurveyQuestion
2012-06-27 23:28:35 +02:00
class SurveyQuestionAdmin(admin.ModelAdmin):
search_fields = ('survey__title', 'answer')
2012-06-27 23:28:35 +02:00
inlines = [
SurveyQuestionAnswerInline,
]
2012-06-27 23:28:35 +02:00
2012-06-27 23:28:35 +02:00
class SurveyAdmin(admin.ModelAdmin):
search_fields = ('title', 'details')
2012-06-27 23:28:35 +02:00
inlines = [
SurveyQuestionInline,
]
2012-06-27 23:28:35 +02:00
2013-10-01 15:27:19 +02:00
class PetitCoursAbilityAdmin(admin.ModelAdmin):
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')
list_filter = ('matiere', 'niveau', 'agrege')
2013-10-01 15:27:19 +02:00
2013-10-01 15:27:19 +02:00
class PetitCoursAttributionAdmin(admin.ModelAdmin):
list_display = ('user', 'demande', 'matiere', 'rank', )
search_fields = ('user__username', 'matiere__name')
2013-10-01 15:27:19 +02:00
2013-10-01 15:27:19 +02:00
class PetitCoursAttributionCounterAdmin(admin.ModelAdmin):
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')
actions = ['reset', ]
actions_on_bottom = True
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
2013-10-01 15:27:19 +02:00
class PetitCoursDemandeAdmin(admin.ModelAdmin):
list_display = ('name', 'email', 'agrege_requis', 'niveau', 'created',
'traitee', 'processed')
list_filter = ('traitee', 'niveau')
search_fields = ('name', 'email', 'phone', 'lieu', 'remarques')
2012-06-27 23:28:35 +02:00
admin.site.register(Survey, SurveyAdmin)
admin.site.register(SurveyQuestion, SurveyQuestionAdmin)
2012-07-11 17:39:20 +02:00
admin.site.register(CofProfile)
admin.site.register(PetitCoursSubject)
2013-10-01 15:27:19 +02:00
admin.site.register(PetitCoursAbility, PetitCoursAbilityAdmin)
admin.site.register(PetitCoursAttribution, PetitCoursAttributionAdmin)
admin.site.register(PetitCoursAttributionCounter,
PetitCoursAttributionCounterAdmin)
2013-10-01 15:27:19 +02:00
admin.site.register(PetitCoursDemande, PetitCoursDemandeAdmin)