gestioCOF/bda/admin.py

318 lines
10 KiB
Python
Raw Normal View History

from datetime import timedelta
from custommail.shortcuts import send_mass_custom_mail
2012-07-11 17:39:20 +02:00
from django.contrib import admin
from django.db.models import Sum, Count
from django.template.defaultfilters import pluralize
from django.utils import timezone
from django import forms
from dal.autocomplete import ModelSelect2
from bda.models import Spectacle, Salle, Participant, ChoixSpectacle,\
Attribution, Tirage, Quote, CategorieSpectacle, SpectacleRevente
2012-07-11 17:39:20 +02:00
class ReadOnlyMixin(object):
readonly_fields_update = ()
def get_readonly_fields(self, request, obj=None):
readonly_fields = super().get_readonly_fields(request, obj)
if obj is None:
return readonly_fields
else:
return readonly_fields + self.readonly_fields_update
class ChoixSpectacleAdminForm(forms.ModelForm):
class Meta:
widgets = {
'participant': ModelSelect2(url='bda-participant-autocomplete'),
'spectacle': ModelSelect2(url='bda-spectacle-autocomplete'),
}
2012-07-11 17:39:20 +02:00
class ChoixSpectacleInline(admin.TabularInline):
model = ChoixSpectacle
form = ChoixSpectacleAdminForm
2012-07-11 17:39:20 +02:00
sortable_field_name = "priority"
2017-05-10 12:39:56 +02:00
class AttributionTabularAdminForm(forms.ModelForm):
listing = None
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
2017-05-10 12:39:56 +02:00
spectacles = Spectacle.objects.select_related('location')
if self.listing is not None:
spectacles = spectacles.filter(listing=self.listing)
self.fields['spectacle'].queryset = spectacles
2017-05-10 12:39:56 +02:00
class WithoutListingAttributionTabularAdminForm(AttributionTabularAdminForm):
listing = False
2017-05-10 12:39:56 +02:00
class WithListingAttributionTabularAdminForm(AttributionTabularAdminForm):
listing = True
2017-05-10 12:39:56 +02:00
class AttributionInline(admin.TabularInline):
model = Attribution
extra = 0
2017-05-10 12:39:56 +02:00
listing = None
def get_queryset(self, request):
qs = super().get_queryset(request)
2017-05-10 12:39:56 +02:00
if self.listing is not None:
qs = qs.filter(spectacle__listing=self.listing)
2017-05-10 12:39:56 +02:00
return qs
2017-05-10 12:39:56 +02:00
class WithListingAttributionInline(AttributionInline):
exclude = ('given', )
2017-05-10 12:39:56 +02:00
form = WithListingAttributionTabularAdminForm
listing = True
2017-05-10 12:39:56 +02:00
class WithoutListingAttributionInline(AttributionInline):
form = WithoutListingAttributionTabularAdminForm
listing = False
class ParticipantAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['choicesrevente'].queryset = (
Spectacle.objects
.select_related('location')
)
class ParticipantAdmin(ReadOnlyMixin, admin.ModelAdmin):
2017-05-10 12:39:56 +02:00
inlines = [WithListingAttributionInline, WithoutListingAttributionInline]
def get_queryset(self, request):
return Participant.objects.annotate(nb_places=Count('attributions'),
total=Sum('attributions__price'))
def nb_places(self, obj):
return obj.nb_places
nb_places.admin_order_field = "nb_places"
nb_places.short_description = "Nombre de places"
def total(self, obj):
tot = obj.total
if tot:
return "%.02f" % tot
else:
return "0 €"
total.admin_order_field = "total"
total.short_description = "Total à payer"
list_display = ("user", "nb_places", "total", "paid", "paymenttype",
"tirage")
list_filter = ("paid", "tirage")
search_fields = ('user__username', 'user__first_name', 'user__last_name')
actions = ['send_attribs', ]
actions_on_bottom = True
list_per_page = 400
readonly_fields = ("total",)
readonly_fields_update = ('user', 'tirage')
form = ParticipantAdminForm
def send_attribs(self, request, queryset):
datatuple = []
for member in queryset.all():
attribs = member.attributions.all()
context = {'member': member.user}
shortname = ""
if len(attribs) == 0:
shortname = "bda-attributions-decus"
else:
shortname = "bda-attributions"
context['places'] = attribs
print(context)
datatuple.append((shortname, context, "bda@ens.fr",
[member.user.email]))
send_mass_custom_mail(datatuple)
count = len(queryset.all())
if count == 1:
message_bit = "1 membre a"
plural = ""
else:
message_bit = "%d membres ont" % count
plural = "s"
self.message_user(request, "%s été informé%s avec succès."
2016-07-10 00:26:02 +02:00
% (message_bit, plural))
send_attribs.short_description = "Envoyer les résultats par mail"
2016-06-25 23:17:31 +02:00
class AttributionAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if 'spectacle' in self.fields:
self.fields['spectacle'].queryset = (
Spectacle.objects
.select_related('location')
)
if 'participant' in self.fields:
self.fields['participant'].queryset = (
Participant.objects
.select_related('user', 'tirage')
)
2016-06-25 23:17:31 +02:00
def clean(self):
2018-01-16 16:22:52 +01:00
cleaned_data = super().clean()
2016-06-25 23:17:31 +02:00
participant = cleaned_data.get("participant")
spectacle = cleaned_data.get("spectacle")
if participant and spectacle:
if participant.tirage != spectacle.tirage:
raise forms.ValidationError(
2016-07-15 00:02:56 +02:00
"Erreur : le participant et le spectacle n'appartiennent"
"pas au même tirage")
return cleaned_data
2016-06-25 23:17:31 +02:00
class AttributionAdmin(ReadOnlyMixin, admin.ModelAdmin):
def paid(self, obj):
return obj.participant.paid
paid.short_description = 'A payé'
paid.boolean = True
list_display = ("id", "spectacle", "participant", "given", "paid")
search_fields = ('spectacle__title', 'participant__user__username',
'participant__user__first_name',
'participant__user__last_name')
2016-06-25 23:17:31 +02:00
form = AttributionAdminForm
readonly_fields_update = ('spectacle', 'participant')
class ChoixSpectacleAdmin(admin.ModelAdmin):
form = ChoixSpectacleAdminForm
def tirage(self, obj):
return obj.participant.tirage
list_display = ("participant", "tirage", "spectacle", "priority",
"double_choice")
list_filter = ("double_choice", "participant__tirage")
search_fields = ('participant__user__username',
'participant__user__first_name',
'participant__user__last_name',
'spectacle__title')
2012-07-11 17:39:20 +02:00
class QuoteInline(admin.TabularInline):
model = Quote
class SpectacleAdmin(admin.ModelAdmin):
inlines = [QuoteInline]
model = Spectacle
list_display = ("title", "date", "tirage", "location", "slots", "price",
"listing")
list_filter = ("location", "tirage",)
search_fields = ("title", "location__name")
readonly_fields = ("rappel_sent", )
class TirageAdmin(admin.ModelAdmin):
model = Tirage
list_display = ("title", "ouverture", "fermeture", "active",
"enable_do_tirage")
readonly_fields = ("tokens", )
list_filter = ("active", )
search_fields = ("title", )
class SalleAdmin(admin.ModelAdmin):
model = Salle
search_fields = ('name', 'address')
class SpectacleReventeAdminForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['confirmed_entry'].queryset = (
Participant.objects
.select_related('user', 'tirage')
)
self.fields['seller'].queryset = (
Participant.objects
.select_related('user', 'tirage')
)
self.fields['soldTo'].queryset = (
Participant.objects
.select_related('user', 'tirage')
)
2016-07-22 22:48:09 +02:00
class SpectacleReventeAdmin(admin.ModelAdmin):
"""
Administration des reventes de spectacles
"""
2016-07-22 22:48:09 +02:00
model = SpectacleRevente
def spectacle(self, obj):
"""
Raccourci vers le spectacle associé à la revente.
"""
2016-07-22 22:48:09 +02:00
return obj.attribution.spectacle
2016-09-05 03:10:06 +02:00
list_display = ("spectacle", "seller", "date", "soldTo")
2016-07-23 22:21:30 +02:00
raw_id_fields = ("attribution",)
2016-11-14 15:52:02 +01:00
readonly_fields = ("date_tirage",)
search_fields = ['attribution__spectacle__title',
'seller__user__username',
'seller__user__first_name',
'seller__user__last_name']
2016-07-22 22:48:09 +02:00
actions = ['transfer', 'reinit']
2016-10-28 03:46:57 +02:00
actions_on_bottom = True
form = SpectacleReventeAdminForm
2016-10-28 03:46:57 +02:00
def transfer(self, request, queryset):
"""
Effectue le transfert des reventes pour lesquels on connaît l'acheteur.
"""
reventes = queryset.exclude(soldTo__isnull=True).all()
count = reventes.count()
for revente in reventes:
2016-10-28 18:15:37 +02:00
attrib = revente.attribution
attrib.participant = revente.soldTo
attrib.save()
self.message_user(
request,
"%d attribution%s %s été transférée%s avec succès." % (
count, pluralize(count),
pluralize(count, "a,ont"), pluralize(count))
)
2016-10-28 03:46:57 +02:00
transfer.short_description = "Transférer les reventes sélectionnées"
def reinit(self, request, queryset):
"""
Réinitialise les reventes.
"""
count = queryset.count()
2016-11-08 13:41:12 +01:00
for revente in queryset.filter(
attribution__spectacle__date__gte=timezone.now()):
2017-12-19 12:50:20 +01:00
revente.reset(new_date=timezone.now() - timedelta(hours=1))
self.message_user(
request,
"%d attribution%s %s été réinitialisée%s avec succès." % (
count, pluralize(count),
pluralize(count, "a,ont"), pluralize(count))
)
2016-10-28 03:46:57 +02:00
reinit.short_description = "Réinitialiser les reventes sélectionnées"
2016-07-22 22:48:09 +02:00
admin.site.register(CategorieSpectacle)
admin.site.register(Spectacle, SpectacleAdmin)
admin.site.register(Salle, SalleAdmin)
2012-07-11 17:39:20 +02:00
admin.site.register(Participant, ParticipantAdmin)
admin.site.register(Attribution, AttributionAdmin)
admin.site.register(ChoixSpectacle, ChoixSpectacleAdmin)
admin.site.register(Tirage, TirageAdmin)
2016-07-22 22:48:09 +02:00
admin.site.register(SpectacleRevente, SpectacleReventeAdmin)