1
1
# Register your models here.
2
+ from django .db .models import F , Count
3
+ from django .contrib import messages
2
4
from django import forms
3
5
from . import models
4
6
from core .base_admin import SummernoteModelAdmin , SummernoteInlineMixin
@@ -23,6 +25,49 @@ class EventImagesInline(SummernoteInlineMixin, NestedStackedInline):
23
25
extra = 1
24
26
25
27
28
+ class ParticipantsInline (SummernoteInlineMixin , NestedStackedInline ):
29
+ model = models .Participant
30
+ extra = 0
31
+
32
+ def get_extra (self , request , obj = None , ** kwargs ):
33
+
34
+ if obj : # Ensure we're dealing with an existing Event object
35
+ if obj .participants .count () < obj .max_capacity :
36
+ return 1 # Show one extra form
37
+ return 0 # Don't show any extra forms
38
+
39
+ def has_add_permission (self , request , obj = None ):
40
+ """
41
+ Prevent adding new participants if the event is full.
42
+ """
43
+ if obj : # Ensure we're dealing with an existing Event object
44
+ if obj .participants .count () >= obj .max_capacity :
45
+ messages .warning (
46
+ request ,
47
+ "The event is full. You cannot add more participants." ,
48
+ )
49
+ return False # Hide "Add another" button
50
+ return super ().has_add_permission (request , obj )
51
+
52
+
53
+ class ParticipantAdminForm (forms .ModelForm ):
54
+ class Meta :
55
+ model = models .Participant
56
+ fields = "__all__"
57
+
58
+ """
59
+ Overriding the __init__ method to filter the events that are checked
60
+ """
61
+
62
+ def __init__ (self , * args , ** kwargs ):
63
+ super ().__init__ (* args , ** kwargs )
64
+ self .fields ["events" ].queryset = models .Event .objects .annotate (
65
+ participant_count = Count ("participants" )
66
+ ).filter (
67
+ requires_registration = True , participant_count__lt = F ("max_capacity" )
68
+ ) # Ensures max_capacity > p_count
69
+
70
+
26
71
class EventAdminForms (forms .ModelForm ):
27
72
class Meta :
28
73
model = models .Event
@@ -33,6 +78,7 @@ class Meta:
33
78
"start_date" : "This is the date the event will start" ,
34
79
"end_date" : "This is the date the event will end" ,
35
80
"description" : "This is the description of the event" ,
81
+ "requires_registration" : "Check if the event needs registration" ,
36
82
"rsvp_url" : "This is the link to the RSVP page" ,
37
83
"add_to_calender_url" : "Add the event to your calendar" ,
38
84
"is_draft" : "This is the status of the event" ,
@@ -51,12 +97,21 @@ class EventModelAdmin(NestedModelAdmin, SummernoteModelAdmin):
51
97
list_display = ["title" , "is_draft" , "start_date" , "end_date" ]
52
98
search_fields = ["slug" , "title" ]
53
99
readonly_fields = ["created_at" , "updated_at" , "slug" ]
54
- inlines = [
55
- EventScheduleInline ,
56
- EventImagesInline ,
57
- ]
100
+ inlines = [EventScheduleInline , EventImagesInline ]
58
101
autocomplete_fields = ["location" , "event_type" , "hot_topics" ]
59
102
103
+ """Overriding this method to conditionally
104
+ add the ParticipantsInline to the inlines list
105
+ """
106
+
107
+ def get_inline_instances (self , request , obj = None ):
108
+ inline_instances = super ().get_inline_instances (request , obj )
109
+ if obj and obj .requires_registration :
110
+ inline_instances .append (
111
+ ParticipantsInline (self .model , self .admin_site )
112
+ )
113
+ return inline_instances
114
+
60
115
61
116
@admin .register (models .Speaker )
62
117
class SpeakersAdmin (SummernoteInlineMixin , admin .ModelAdmin ):
@@ -90,6 +145,25 @@ class HotTopicAdmin(admin.ModelAdmin):
90
145
search_fields = ["name" ]
91
146
92
147
148
+ @admin .register (models .Participant )
149
+ class ParticipantAdmin (admin .ModelAdmin ):
150
+ form = ParticipantAdminForm
151
+ list_display = [
152
+ "first_name" ,
153
+ "last_name" ,
154
+ "email" ,
155
+ "academy" ,
156
+ "phone_number" ,
157
+ ]
158
+ search_fields = [
159
+ "first_name" ,
160
+ "last_name" ,
161
+ "email" ,
162
+ "academy" ,
163
+ "phone_number" ,
164
+ ]
165
+
166
+
93
167
admin .site .register (
94
168
[
95
169
models .EventImage ,
0 commit comments