7
7
from django .views .generic .edit import CreateView , UpdateView , DeleteView
8
8
from django .views .generic .list import ListView
9
9
from django .contrib .auth .mixins import LoginRequiredMixin
10
+ from django .shortcuts import get_object_or_404
10
11
from django .urls import reverse_lazy
11
12
12
13
@@ -176,7 +177,7 @@ def get_object(self, queryset = None):
176
177
return room
177
178
if not room .campaign .users .filter (id = user .id ).exists ():
178
179
raise PermissionDenied ('You are not allowed to update this Campaign' )
179
- return room
180
+ return room
180
181
181
182
def get_context_data (self , ** kwargs ):
182
183
# Get the default context data
@@ -284,22 +285,22 @@ def get_current_mean(dimension):
284
285
285
286
class ParticipantDetailView (LoginRequiredMixin , DetailView ):
286
287
model = CustomUser
287
- template_name = 'campaigns/participant /detail.html'
288
+ template_name = 'campaigns/participants /detail.html'
288
289
context_object_name = 'participant'
289
290
pk_url_kwarg = 'user'
290
291
291
- def test_func (self ):
292
- # Define permission logic. For example, only superusers or campaign organizers can view.
293
- user = self .request .user
294
- return user .is_authenticated and user .is_superuser # Adjust as needed
295
292
296
- def get_queryset (self ):
297
- """
298
- Optionally, restrict the queryset to users associated with the campaign.
299
- This ensures that users not part of the campaign cannot access details.
300
- """
301
- campaign_pk = self .kwargs .get ('pk' ) # Campaign's pk from URL
302
- return CustomUser .objects .filter (campaigns__pk = campaign_pk ) # Adjust the relationship as per your models
293
+ def dispatch (self , request , * args , ** kwargs ):
294
+ self .campaign = get_object_or_404 (Campaign , pk = kwargs ['pk' ])
295
+
296
+ if self .request .user .is_superuser :
297
+ return super ().dispatch (request , * args , ** kwargs )
298
+ # only users in campaign
299
+ if not self .campaign .users .filter (id = self .request .user .id ).exists ():
300
+ raise PermissionDenied ("You are not allowed to create a Room" )
301
+
302
+ return super ().dispatch (request , * args , ** kwargs )
303
+
303
304
304
305
def get_context_data (self , ** kwargs ):
305
306
context = super ().get_context_data (** kwargs )
@@ -336,8 +337,8 @@ def get_current_mean(dimension):
336
337
temperature_color = Dimension .get_color (Dimension .TEMPERATURE , current_temperature ) if current_temperature else None
337
338
338
339
# VOC Index
339
- current_uvi = get_current_mean (Dimension .UVI )
340
- uvi_color = Dimension .get_color (Dimension .UVI , current_uvi ) if current_uvi else None
340
+ current_uvi = get_current_mean (Dimension .UVS )
341
+ uvi_color = Dimension .get_color (Dimension .UVS , current_uvi ) if current_uvi else None
341
342
342
343
# data 24h
343
344
now = datetime .utcnow ()
@@ -357,7 +358,7 @@ def get_current_mean(dimension):
357
358
for m in measurements
358
359
for val in m .values .all ()
359
360
if val .dimension == target_dim
360
- ] for target_dim in (Dimension .TEMPERATURE , Dimension .PM2_5 , Dimension . CO2 , Dimension . TVOC )
361
+ ] for target_dim in (Dimension .TEMPERATURE , Dimension .UVS )
361
362
]
362
363
for i , x in enumerate (data ):
363
364
data_24h [i + 1 ].append (statistics .mean (x ) if x else 0 )
@@ -372,6 +373,8 @@ def get_current_mean(dimension):
372
373
context ['uvi_color' ] = uvi_color
373
374
context ['data_24h' ] = data_24h
374
375
376
+ context ['campaign' ] = self .campaign
377
+
375
378
return context
376
379
377
380
@@ -450,7 +453,7 @@ class ParticipantsAddDevicesView(LoginRequiredMixin, UpdateView):
450
453
template_name = 'campaigns/participants/add_device.html'
451
454
452
455
def get_success_url (self ):
453
- return reverse_lazy ('campaigns -detail' , kwargs = {'pk' : self .campaign .pk })
456
+ return reverse_lazy ('participants -detail' , kwargs = {'pk' : self .campaign . pk , 'user' : self . object .pk })
454
457
455
458
def dispatch (self , request , * args , ** kwargs ):
456
459
self .campaign = Campaign .objects .get (pk = kwargs ['campaign_pk' ])
0 commit comments