5
5
from django .contrib .auth import get_user_model
6
6
from django .contrib .auth .models import Group
7
7
from django .db import transaction
8
+ from django .db .models import Q
8
9
from django .shortcuts import redirect
9
10
from django .urls import reverse
10
11
from django .views .generic import TemplateView
@@ -25,8 +26,9 @@ class CheckinList(AnyApplicationPermissionRequiredMixin, SingleTableMixin, Filte
25
26
filterset_class = CheckinTableFilter
26
27
27
28
def get_queryset (self ):
28
- return get_user_model ().objects .filter (application__status = Application .STATUS_CONFIRMED ,
29
- application__edition = Edition .get_default_edition ()).distinct ()
29
+ return get_user_model ().objects .filter (Q (application__status = Application .STATUS_CONFIRMED ,
30
+ application__edition = Edition .get_default_edition ()) |
31
+ Q (groups__name = 'Organizer' , qr_code = '' )).distinct ()
30
32
31
33
32
34
class CheckinUser (TemplateView ):
@@ -41,14 +43,23 @@ def has_permission(self, types):
41
43
return False
42
44
return True
43
45
46
+ def get_accepted_status_to_checkin (self ):
47
+ accepted_status = [Application .STATUS_CONFIRMED ]
48
+ if self .request .user .is_staff :
49
+ accepted_status .append (Application .STATUS_ATTENDED )
50
+ return accepted_status
51
+
44
52
def get_context_data (self , ** kwargs ):
45
53
context = super ().get_context_data (** kwargs )
46
54
User = get_user_model ()
47
55
try :
48
56
uid = User .decode_encoded_pk (kwargs .get ('uid' ))
49
57
user = User .objects .get (pk = uid )
50
- application_types = user .application_set .actual ().filter (status = Application .STATUS_CONFIRMED )\
51
- .values_list ('type__name' , flat = True )
58
+ accepted_status = self .get_accepted_status_to_checkin ()
59
+ application_types = list (user .application_set .actual ().filter (status__in = accepted_status )
60
+ .values_list ('type__name' , flat = True ))
61
+ if user .is_organizer ():
62
+ application_types .append ('Organizer' )
52
63
context .update ({'app_user' : user , 'types' : application_types ,
53
64
'has_permission' : self .has_permission (types = application_types )})
54
65
except (User .DoesNotExist , ValueError ):
@@ -61,26 +72,60 @@ def get_code(self):
61
72
return '' .join ([choice (string .ascii_letters + string .digits + string .punctuation ) for i in range (12 )])
62
73
return qr_code
63
74
75
+ def manage_application_confirm (self , application ):
76
+ application .set_status (Application .STATUS_ATTENDED )
77
+ application .save ()
78
+ application_log = ApplicationLog (application = application , user = self .request .user , comment = '' ,
79
+ name = 'Checked-in' )
80
+ application_log .changes = {'status' : {'new' : Application .STATUS_ATTENDED ,
81
+ 'old' : Application .STATUS_CONFIRMED }}
82
+ application_log .save ()
83
+
84
+ def manage_application_attended (self , application ):
85
+ application_log = ApplicationLog (application = application , user = self .request .user , comment = '' ,
86
+ name = 'Changed QR code' )
87
+ application_log .save ()
88
+
89
+ def redirect_successful (self ):
90
+ next_ = self .request .GET .get ('next' , reverse ('checkin_list' ))
91
+ if next_ [0 ] != '/' :
92
+ next_ = reverse ('checkin_list' )
93
+ return redirect (next_ )
94
+
64
95
def post (self , request , * args , ** kwargs ):
65
96
context = self .get_context_data (** kwargs )
66
97
qr_code = self .get_code ()
67
98
if context ['has_permission' ] and len (context ['types' ]) > 0 and qr_code is not None :
68
99
user = context ['app_user' ]
69
100
user .qr_code = qr_code
70
- applications = user .application_set .actual ().filter (status = Application .STATUS_CONFIRMED )\
101
+ accepted_status = self .get_accepted_status_to_checkin ()
102
+ applications = user .application_set .actual ().filter (status__in = accepted_status )\
71
103
.select_related ('type' )
72
104
groups = Group .objects .filter (name__in = context ['types' ])
73
105
with transaction .atomic ():
74
106
user .save ()
75
107
user .groups .add (* groups )
76
108
for application in applications :
77
- application .set_status (Application .STATUS_ATTENDED )
78
- application .save ()
79
- application_log = ApplicationLog (application = application , user = request .user , comment = '' ,
80
- name = 'Checked-in' )
81
- application_log .changes = {'status' : {'new' : Application .STATUS_ATTENDED ,
82
- 'old' : Application .STATUS_CONFIRMED }}
83
- application_log .save ()
84
- return redirect (reverse ('checkin_list' ))
109
+ if application .status == Application .STATUS_CONFIRMED :
110
+ self .manage_application_confirm (application )
111
+ else :
112
+ self .manage_application_attended (application )
113
+ messages .success (request , _ ('User checked in!' ))
114
+ return self .redirect_successful ()
85
115
messages .error (request , _ ('Permission denied' ))
86
116
return self .render_to_response (context )
117
+
118
+
119
+ class CheckinAdminList (CheckinList ):
120
+ def get_queryset (self ):
121
+ if self .request .user .is_staff :
122
+ return get_user_model ().objects .filter (Q (application__status__in = [Application .STATUS_CONFIRMED ,
123
+ Application .STATUS_ATTENDED ],
124
+ application__edition = Edition .get_default_edition ()) |
125
+ Q (groups__name = 'Organizer' )).distinct ()
126
+ return get_user_model ().objects .none ()
127
+
128
+ def get_context_data (self , ** kwargs ):
129
+ context = super ().get_context_data (** kwargs )
130
+ context .update ({'admin' : True })
131
+ return context
0 commit comments