Code cleanup

This commit is contained in:
Martin Pépin 2016-06-17 13:26:55 +02:00
parent f00e081b0b
commit 28487bc483
2 changed files with 73 additions and 45 deletions

View file

@ -9,7 +9,7 @@ from django.core.urlresolvers import reverse
from django.utils.safestring import mark_safe from django.utils.safestring import mark_safe
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
def add_link_field(target_model = '', field = '', link_text = unicode, desc_text = unicode): def add_link_field(target_model='', field='', link_text=unicode, desc_text=unicode):
def add_link(cls): def add_link(cls):
reverse_name = target_model or cls.model.__name__.lower() reverse_name = target_model or cls.model.__name__.lower()
def link(self, instance): def link(self, instance):
@ -30,7 +30,8 @@ def add_link_field(target_model = '', field = '', link_text = unicode, desc_text
class SurveyQuestionAnswerInline(admin.TabularInline): class SurveyQuestionAnswerInline(admin.TabularInline):
model = SurveyQuestionAnswer model = SurveyQuestionAnswer
@add_link_field(desc_text = lambda x: "Réponses", link_text = lambda x: "Éditer les réponses") @add_link_field(desc_text=lambda x: "Réponses",
link_text=lambda x: "Éditer les réponses")
class SurveyQuestionInline(admin.TabularInline): class SurveyQuestionInline(admin.TabularInline):
model = SurveyQuestion model = SurveyQuestion
@ -47,7 +48,8 @@ class SurveyAdmin(admin.ModelAdmin):
class EventOptionChoiceInline(admin.TabularInline): class EventOptionChoiceInline(admin.TabularInline):
model = EventOptionChoice model = EventOptionChoice
@add_link_field(desc_text = lambda x: "Choix", link_text = lambda x: "Éditer les choix") @add_link_field(desc_text=lambda x: "Choix",
link_text=lambda x: "Éditer les choix")
class EventOptionInline(admin.TabularInline): class EventOptionInline(admin.TabularInline):
model = EventOption model = EventOption
@ -67,7 +69,6 @@ class EventAdmin(admin.ModelAdmin):
class CofProfileInline(admin.StackedInline): class CofProfileInline(admin.StackedInline):
model = CofProfile model = CofProfile
#form = BaseDynamicEntityForm
inline_classes = ("collapse open",) inline_classes = ("collapse open",)
class FkeyLookup(object): class FkeyLookup(object):
@ -80,14 +81,15 @@ class FkeyLookup(object):
def __get__(self, obj, klass): def __get__(self, obj, klass):
if obj is None: if obj is None:
return self # hack required to make Django validate (if obj is None, then we're a class, and classes are callable <wink>) return self # hack required to make Django validate (if obj is
# None, then we're a class, and classes are callable
# <wink>)
item = getattr(obj, self.fk) item = getattr(obj, self.fk)
for attr in self.fkattrs: for attr in self.fkattrs:
item = getattr(item, attr) item = getattr(item, attr)
return item return item
def ProfileInfo(field, short_description, boolean = False): def ProfileInfo(field, short_description, boolean=False):
def getter(self): def getter(self):
try: try:
return getattr(self.profile, field) return getattr(self.profile, field)
@ -121,9 +123,14 @@ class UserProfileAdmin(UserAdmin):
return False return False
is_cof.short_description = 'Membre du COF' is_cof.short_description = 'Membre du COF'
is_cof.boolean = True is_cof.boolean = True
list_display = ('profile_num',) + UserAdmin.list_display + ('profile_login_clipper','profile_phone','profile_occupation','profile_mailing_cof','profile_mailing_bda','profile_mailing_bda_revente','is_cof','is_buro',) list_display = ('profile_num',) + UserAdmin.list_display \
+ ( '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') list_display_links = ('username','email','first_name','last_name')
list_filter = UserAdmin.list_filter + ('profile__is_cof', 'profile__is_buro', 'profile__mailing_cof', 'profile__mailing_bda') list_filter = UserAdmin.list_filter \
+ ( 'profile__is_cof', 'profile__is_buro', 'profile__mailing_cof',
'profile__mailing_bda')
search_fields = UserAdmin.search_fields + ('profile__phone',) search_fields = UserAdmin.search_fields + ('profile__phone',)
inlines = [ inlines = [
CofProfileInline, CofProfileInline,
@ -140,11 +147,13 @@ class EventRegistrationAdmin(admin.ModelAdmin):
form = autocomplete_light.modelform_factory(EventRegistration, exclude=[]) form = autocomplete_light.modelform_factory(EventRegistration, exclude=[])
list_display = ('__unicode__','event','user','paid') list_display = ('__unicode__','event','user','paid')
list_filter = ('paid',) list_filter = ('paid',)
search_fields = ('user__username', 'user__first_name', 'user__last_name', 'user__email', 'event__title') search_fields = ('user__username', 'user__first_name', 'user__last_name',
'user__email', 'event__title')
class PetitCoursAbilityAdmin(admin.ModelAdmin): class PetitCoursAbilityAdmin(admin.ModelAdmin):
list_display = ('user','matiere','niveau','agrege') list_display = ('user','matiere','niveau','agrege')
search_fields = ('user__username', 'user__first_name', 'user__last_name', 'user__email', 'matiere__name', 'niveau') search_fields = ('user__username', 'user__first_name', 'user__last_name',
'user__email', 'matiere__name', 'niveau')
list_filter = ('matiere','niveau','agrege') list_filter = ('matiere','niveau','agrege')
class PetitCoursAttributionAdmin(admin.ModelAdmin): class PetitCoursAttributionAdmin(admin.ModelAdmin):
@ -153,10 +162,12 @@ class PetitCoursAttributionAdmin(admin.ModelAdmin):
class PetitCoursAttributionCounterAdmin(admin.ModelAdmin): class PetitCoursAttributionCounterAdmin(admin.ModelAdmin):
list_display = ('user','matiere','count',) list_display = ('user','matiere','count',)
list_filter = ('matiere',) list_filter = ('matiere',)
search_fields = ('user__username', 'user__first_name', 'user__last_name', 'user__email', 'matiere__name') search_fields = ('user__username', 'user__first_name', 'user__last_name',
'user__email', 'matiere__name')
class PetitCoursDemandeAdmin(admin.ModelAdmin): class PetitCoursDemandeAdmin(admin.ModelAdmin):
list_display = ('name','email','agrege_requis','niveau','created','traitee','processed') list_display = ('name','email','agrege_requis','niveau','created',
'traitee','processed')
list_filter = ('traitee','niveau') list_filter = ('traitee','niveau')
admin.site.register(Survey, SurveyAdmin) admin.site.register(Survey, SurveyAdmin)

View file

@ -17,9 +17,9 @@ LEVELS_CHOICES = (
) )
class PetitCoursSubject(models.Model): class PetitCoursSubject(models.Model):
name = models.CharField(_(u"Matière"), max_length = 30) name = models.CharField(_(u"Matière"), max_length=30)
users = models.ManyToManyField(User, related_name = "petits_cours_matieres", users = models.ManyToManyField(User, related_name="petits_cours_matieres",
through = "PetitCoursAbility") through="PetitCoursAbility")
class Meta: class Meta:
verbose_name = "Matière de petits cours" verbose_name = "Matière de petits cours"
@ -30,11 +30,11 @@ class PetitCoursSubject(models.Model):
class PetitCoursAbility(models.Model): class PetitCoursAbility(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
matiere = models.ForeignKey(PetitCoursSubject, verbose_name = _(u"Matière")) matiere = models.ForeignKey(PetitCoursSubject, verbose_name=_(u"Matière"))
niveau = models.CharField (_(u"Niveau"), niveau = models.CharField (_(u"Niveau"),
choices = LEVELS_CHOICES, choices=LEVELS_CHOICES,
max_length = choices_length (LEVELS_CHOICES)) max_length=choices_length (LEVELS_CHOICES))
agrege = models.BooleanField(_(u"Agrégé"), default = False) agrege = models.BooleanField(_(u"Agrégé"), default=False)
class Meta: class Meta:
verbose_name = "Compétence petits cours" verbose_name = "Compétence petits cours"
@ -44,27 +44,40 @@ class PetitCoursAbility(models.Model):
return u"%s - %s - %s" % (self.user.username, self.matiere, self.niveau) return u"%s - %s - %s" % (self.user.username, self.matiere, self.niveau)
class PetitCoursDemande(models.Model): class PetitCoursDemande(models.Model):
name = models.CharField(_(u"Nom/prénom"), max_length = 200) name = models.CharField(_(u"Nom/prénom"), max_length=200)
email = models.CharField(_(u"Adresse email"), max_length = 300) email = models.CharField(_(u"Adresse email"), max_length=300)
phone = models.CharField(_(u"Téléphone (facultatif)"), max_length = 20, blank = True) phone = models.CharField(_(u"Téléphone (facultatif)"),
quand = models.CharField(_(u"Quand ?"), help_text = _(u"Indiquez ici la période désirée pour les petits cours (vacances scolaires, semaine, week-end)."), max_length = 300, blank = True) max_length=20, blank=True)
freq = models.CharField(_(u"Fréquence"), help_text = _(u"Indiquez ici la fréquence envisagée (hebdomadaire, 2 fois par semaine, ...)"), max_length = 300, blank = True) quand = models.CharField(
lieu = models.CharField(_(u"Lieu (si préférence)"), help_text = _(u"Si vous avez avez une préférence sur le lieu."), max_length = 300, blank = True) _(u"Quand ?"),
help_text=_(u"Indiquez ici la période désirée pour les petits" \
+ "cours (vacances scolaires, semaine, week-end)."),
max_length=300, blank=True)
freq = models.CharField(
_(u"Fréquence"),
help_text=_(u"Indiquez ici la fréquence envisagée " \
+ "(hebdomadaire, 2 fois par semaine, ...)"),
max_length=300, blank=True)
lieu = models.CharField(
_(u"Lieu (si préférence)"),
help_text=_(u"Si vous avez avez une préférence sur le lieu."),
max_length=300, blank=True)
matieres = models.ManyToManyField(PetitCoursSubject, verbose_name = _(u"Matières"), matieres = models.ManyToManyField(
related_name = "demandes") PetitCoursSubject, verbose_name=_(u"Matières"),
agrege_requis = models.BooleanField(_(u"Agrégé requis"), default = False) related_name="demandes")
agrege_requis = models.BooleanField(_(u"Agrégé requis"), default=False)
niveau = models.CharField (_(u"Niveau"), niveau = models.CharField (_(u"Niveau"),
default = "", default="",
choices = LEVELS_CHOICES, choices=LEVELS_CHOICES,
max_length = choices_length (LEVELS_CHOICES)) max_length=choices_length (LEVELS_CHOICES))
remarques = models.TextField(_(u"Remarques et précisions"), blank = True) remarques = models.TextField(_(u"Remarques et précisions"), blank=True)
traitee = models.BooleanField(_(u"Traitée"), default = False) traitee = models.BooleanField(_(u"Traitée"), default=False)
traitee_par = models.ForeignKey(User, blank = True, null = True) traitee_par = models.ForeignKey(User, blank=True, null=True)
processed = models.DateTimeField(_(u"Date de traitement"), blank = True) processed = models.DateTimeField(_(u"Date de traitement"), blank=True)
created = models.DateTimeField(_(u"Date de création"), auto_now_add = True) created = models.DateTimeField(_(u"Date de création"), auto_now_add=True)
class Meta: class Meta:
verbose_name = "Demande de petits cours" verbose_name = "Demande de petits cours"
@ -75,27 +88,31 @@ class PetitCoursDemande(models.Model):
class PetitCoursAttribution(models.Model): class PetitCoursAttribution(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
demande = models.ForeignKey(PetitCoursDemande, verbose_name = _("Demande")) demande = models.ForeignKey(PetitCoursDemande, verbose_name=_("Demande"))
matiere = models.ForeignKey(PetitCoursSubject, verbose_name = _(u"Matière")) matiere = models.ForeignKey(PetitCoursSubject, verbose_name=_(u"Matière"))
date = models.DateTimeField(_(u"Date d'attribution"), auto_now_add = True) date = models.DateTimeField(_(u"Date d'attribution"), auto_now_add=True)
rank = models.IntegerField("Rang dans l'email") rank = models.IntegerField("Rang dans l'email")
selected = models.BooleanField(_(u"Sélectionné par le demandeur"), default = False) selected = models.BooleanField(_(u"Sélectionné par le demandeur"),
default=False)
class Meta: class Meta:
verbose_name = "Attribution de petits cours" verbose_name = "Attribution de petits cours"
verbose_name_plural = "Attributions de petits cours" verbose_name_plural = "Attributions de petits cours"
def __unicode__(self): def __unicode__(self):
return u"Attribution de la demande %d à %s pour %s" % (self.demande.id, self.user.username, self.matiere) return u"Attribution de la demande %d à %s pour %s" \
% (self.demande.id, self.user.username, self.matiere)
class PetitCoursAttributionCounter(models.Model): class PetitCoursAttributionCounter(models.Model):
user = models.ForeignKey(User) user = models.ForeignKey(User)
matiere = models.ForeignKey(PetitCoursSubject, verbose_name = _("Matiere")) matiere = models.ForeignKey(PetitCoursSubject, verbose_name=_("Matiere"))
count = models.IntegerField("Nombre d'envois", default = 0) count = models.IntegerField("Nombre d'envois", default=0)
class Meta: class Meta:
verbose_name = "Compteur d'attribution de petits cours" verbose_name = "Compteur d'attribution de petits cours"
verbose_name_plural = "Compteurs d'attributions de petits cours" verbose_name_plural = "Compteurs d'attributions de petits cours"
def __unicode__(self): def __unicode__(self):
return u"%d demandes envoyées à %s pour %s" % (self.count, self.user.username, self.matiere) return u"%d demandes envoyées à %s pour %s" \
% (self.count, self.user.username, self.matiere)