forked from DGNum/gestioCOF
Fewer requests on participant admin create and updateviews.
- Fewer requests on choicesreventes and tabular inlines (of attributions) - User and tirage cannot be updated if updating a participant instance. - Tabular inlines are fixed: - the one which is used for spectacles with listing only propose choices in spectacle with listing - same with the other (spectacles without listing) - Still does too much request (because of tabularinlines)
This commit is contained in:
parent
6451f971bd
commit
0750551d7c
1 changed files with 65 additions and 21 deletions
86
bda/admin.py
86
bda/admin.py
|
@ -13,32 +13,85 @@ from bda.models import Spectacle, Salle, Participant, ChoixSpectacle,\
|
||||||
Attribution, Tirage, Quote, CategorieSpectacle, SpectacleRevente
|
Attribution, Tirage, Quote, CategorieSpectacle, SpectacleRevente
|
||||||
|
|
||||||
|
|
||||||
|
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 ChoixSpectacleInline(admin.TabularInline):
|
class ChoixSpectacleInline(admin.TabularInline):
|
||||||
model = ChoixSpectacle
|
model = ChoixSpectacle
|
||||||
sortable_field_name = "priority"
|
sortable_field_name = "priority"
|
||||||
|
|
||||||
|
|
||||||
class AttributionInline(admin.TabularInline):
|
class AttributionAdminForm(forms.ModelForm):
|
||||||
model = Attribution
|
|
||||||
extra = 0
|
|
||||||
|
|
||||||
def get_queryset(self, request):
|
def __init__(self, *args, **kwargs):
|
||||||
qs = super(AttributionInline, self).get_queryset(request)
|
super().__init__(*args, **kwargs)
|
||||||
return qs.filter(spectacle__listing=False)
|
self.fields['spectacle'].queryset = (
|
||||||
|
Spectacle.objects
|
||||||
|
.select_related('location')
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AttributionNoListingAdminForm(AttributionAdminForm):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields['spectacle'].queryset = (
|
||||||
|
self.fields['spectacle'].queryset
|
||||||
|
.filter(listing=False)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class AttributionListingAdminForm(AttributionAdminForm):
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
super().__init__(*args, **kwargs)
|
||||||
|
self.fields['spectacle'].queryset = (
|
||||||
|
self.fields['spectacle'].queryset
|
||||||
|
.filter(listing=True)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class AttributionInlineListing(admin.TabularInline):
|
class AttributionInlineListing(admin.TabularInline):
|
||||||
model = Attribution
|
model = Attribution
|
||||||
exclude = ('given', )
|
|
||||||
extra = 0
|
extra = 0
|
||||||
|
form = AttributionListingAdminForm
|
||||||
|
|
||||||
def get_queryset(self, request):
|
def get_queryset(self, request):
|
||||||
qs = super(AttributionInlineListing, self).get_queryset(request)
|
qs = super().get_queryset(request)
|
||||||
return qs.filter(spectacle__listing=True)
|
return qs.filter(spectacle__listing=True)
|
||||||
|
|
||||||
|
|
||||||
class ParticipantAdmin(admin.ModelAdmin):
|
class AttributionInlineNoListing(admin.TabularInline):
|
||||||
inlines = [AttributionInline, AttributionInlineListing]
|
model = Attribution
|
||||||
|
exclude = ('given', )
|
||||||
|
extra = 0
|
||||||
|
form = AttributionNoListingAdminForm
|
||||||
|
|
||||||
|
def get_queryset(self, request):
|
||||||
|
qs = super().get_queryset(request)
|
||||||
|
return qs.filter(spectacle__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):
|
||||||
|
inlines = [AttributionInlineListing, AttributionInlineNoListing]
|
||||||
|
|
||||||
def get_queryset(self, request):
|
def get_queryset(self, request):
|
||||||
return Participant.objects.annotate(nb_places=Count('attributions'),
|
return Participant.objects.annotate(nb_places=Count('attributions'),
|
||||||
|
@ -65,6 +118,8 @@ class ParticipantAdmin(admin.ModelAdmin):
|
||||||
actions_on_bottom = True
|
actions_on_bottom = True
|
||||||
list_per_page = 400
|
list_per_page = 400
|
||||||
readonly_fields = ("total",)
|
readonly_fields = ("total",)
|
||||||
|
readonly_fields_update = ('user', 'tirage')
|
||||||
|
form = ParticipantAdminForm
|
||||||
|
|
||||||
def send_attribs(self, request, queryset):
|
def send_attribs(self, request, queryset):
|
||||||
datatuple = []
|
datatuple = []
|
||||||
|
@ -120,17 +175,6 @@ class AttributionAdminForm(forms.ModelForm):
|
||||||
return cleaned_data
|
return cleaned_data
|
||||||
|
|
||||||
|
|
||||||
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 AttributionAdmin(ReadOnlyMixin, admin.ModelAdmin):
|
class AttributionAdmin(ReadOnlyMixin, admin.ModelAdmin):
|
||||||
def paid(self, obj):
|
def paid(self, obj):
|
||||||
return obj.participant.paid
|
return obj.participant.paid
|
||||||
|
|
Loading…
Add table
Reference in a new issue