gestioCOF/gestioncof/admin.py
2012-07-11 17:39:20 +02:00

100 lines
3.4 KiB
Python

# coding: utf-8
from django.contrib import admin
from gestioncof.models import Survey, SurveyQuestion, SurveyQuestionAnswer
from gestioncof.models import Event, EventOption, EventOptionChoice
from gestioncof.models import CofProfile
from django.contrib.auth.models import User
from django.contrib.auth.admin import UserAdmin
from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe
def add_link_field(target_model = '', field = '', link_text = unicode, desc_text = unicode):
def add_link(cls):
reverse_name = target_model or cls.model.__name__.lower()
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,))
return mark_safe("<a href='%s'>%s</a>" % (url, link_text(link_obj)))
link.allow_tags = True
link.short_description = desc_text(reverse_name + ' link')
cls.link = link
cls.readonly_fields = list(getattr(cls, 'readonly_fields', [])) + ['link']
return cls
return add_link
class SurveyQuestionAnswerInline(admin.TabularInline):
model = SurveyQuestionAnswer
@add_link_field(desc_text = lambda x: "Réponses", link_text = lambda x: "Éditer les réponses")
class SurveyQuestionInline(admin.TabularInline):
model = SurveyQuestion
class SurveyQuestionAdmin(admin.ModelAdmin):
inlines = [
SurveyQuestionAnswerInline,
]
class SurveyAdmin(admin.ModelAdmin):
inlines = [
SurveyQuestionInline,
]
class EventOptionChoiceInline(admin.TabularInline):
model = EventOptionChoice
@add_link_field(desc_text = lambda x: "Choix", link_text = lambda x: "Éditer les choix")
class EventOptionInline(admin.TabularInline):
model = EventOption
class EventOptionAdmin(admin.ModelAdmin):
inlines = [
EventOptionChoiceInline,
]
class EventAdmin(admin.ModelAdmin):
inlines = [
EventOptionInline,
]
class CofProfileInline(admin.StackedInline):
model = CofProfile
inline_classes = ("collapse open",)
class UserProfileAdmin(UserAdmin):
def login_clipper(self, obj):
try:
return obj.get_profile().login_clipper
except UserProfile.DoesNotExist:
return ""
def is_buro(self, obj):
try:
return obj.get_profile().is_buro
except UserProfile.DoesNotExist:
return False
is_buro.short_description = 'Membre du Buro'
is_buro.boolean = True
def is_cof(self, obj):
try:
return obj.get_profile().is_cof
except UserProfile.DoesNotExist:
return False
is_cof.short_description = 'Membre du COF'
is_cof.boolean = True
list_display = UserAdmin.list_display + ('login_clipper','is_cof','is_buro',)
list_filter = UserAdmin.list_filter + ('profile__is_cof', 'profile__is_buro')
inlines = [
CofProfileInline,
]
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)