Skip to content

Commit d495fe1

Browse files
authored
Merge pull request #123 from luftdaten-at/102-add-possibility-to-add-devices-to-campaign-participants
102 add possibility to add devices to campaign participants
2 parents 1f2bc0c + 193bf7d commit d495fe1

File tree

5 files changed

+42
-26
lines changed

5 files changed

+42
-26
lines changed

app/accounts/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ def DashboardView(request):
3838
'user': request.user,
3939
'member_campaigns': Campaign.objects.filter(users=request.user),
4040
'owner_campaigns': Campaign.objects.filter(owner=request.user),
41-
'campaigns': Campaign.objects.all() if request.user.is_superuser else context['member_campaigns'],
4241
'member_organizations': Organization.objects.filter(users=request.user),
4342
'owner_organizations': Organization.objects.filter(owner=request.user),
44-
'organizations': context['member_organizations'] if not request.user.is_superuser else Organization.objects.all()
4543
}
44+
45+
context['campaigns'] = Campaign.objects.all() if request.user.is_superuser else context['member_campaigns']
46+
context['organizations'] = context['member_organizations'] if not request.user.is_superuser else Organization.objects.all()
47+
4648
return render(request, "account/dashboard.html", context)
4749
else:
4850
# Process the login form for unauthenticated users

app/campaign/views.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from django.views.generic.edit import CreateView, UpdateView, DeleteView
88
from django.views.generic.list import ListView
99
from django.contrib.auth.mixins import LoginRequiredMixin
10+
from django.shortcuts import get_object_or_404
1011
from django.urls import reverse_lazy
1112

1213

@@ -176,7 +177,7 @@ def get_object(self, queryset = None):
176177
return room
177178
if not room.campaign.users.filter(id=user.id).exists():
178179
raise PermissionDenied('You are not allowed to update this Campaign')
179-
return room
180+
return room
180181

181182
def get_context_data(self, **kwargs):
182183
# Get the default context data
@@ -284,22 +285,22 @@ def get_current_mean(dimension):
284285

285286
class ParticipantDetailView(LoginRequiredMixin, DetailView):
286287
model = CustomUser
287-
template_name = 'campaigns/participant/detail.html'
288+
template_name = 'campaigns/participants/detail.html'
288289
context_object_name = 'participant'
289290
pk_url_kwarg = 'user'
290291

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
295292

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+
303304

304305
def get_context_data(self, **kwargs):
305306
context = super().get_context_data(**kwargs)
@@ -336,8 +337,8 @@ def get_current_mean(dimension):
336337
temperature_color = Dimension.get_color(Dimension.TEMPERATURE, current_temperature) if current_temperature else None
337338

338339
# 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
341342

342343
# data 24h
343344
now = datetime.utcnow()
@@ -357,7 +358,7 @@ def get_current_mean(dimension):
357358
for m in measurements
358359
for val in m.values.all()
359360
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)
361362
]
362363
for i, x in enumerate(data):
363364
data_24h[i + 1].append(statistics.mean(x) if x else 0)
@@ -372,6 +373,8 @@ def get_current_mean(dimension):
372373
context['uvi_color'] = uvi_color
373374
context['data_24h'] = data_24h
374375

376+
context['campaign'] = self.campaign
377+
375378
return context
376379

377380

@@ -450,7 +453,7 @@ class ParticipantsAddDevicesView(LoginRequiredMixin, UpdateView):
450453
template_name = 'campaigns/participants/add_device.html'
451454

452455
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})
454457

455458
def dispatch(self, request, *args, **kwargs):
456459
self.campaign = Campaign.objects.get(pk=kwargs['campaign_pk'])

app/main/enums.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ class Dimension():
123123
SGP40_RAW_GAS = 17
124124
SGP40_ADJUSTED_GAS = 18
125125
ADJUSTED_TEMP_CUBE = 19
126+
UVS = 20
127+
LIGHT = 21
128+
ACCELERATION = 22
129+
GYRO = 23
130+
ALTITUDE = 24
131+
126132

127133
thresholds = {
128134
TEMPERATURE: ([18, 24], [Color.BLUE, Color.GREEN, Color.RED]),
@@ -153,6 +159,8 @@ class Dimension():
153159
SGP40_RAW_GAS: "Ω",
154160
SGP40_ADJUSTED_GAS: "Ω",
155161
ADJUSTED_TEMP_CUBE: "°C",
162+
ACCELERATION: "m/s²",
163+
GYRO: "radians/s"
156164
}
157165

158166
_names = {
@@ -175,6 +183,10 @@ class Dimension():
175183
SGP40_RAW_GAS: "SGP40 Raw Gas",
176184
SGP40_ADJUSTED_GAS: "SGP40 Adjusted Gas",
177185
ADJUSTED_TEMP_CUBE: "Adjusted Temperature Air Cube",
186+
UVS: "UVS",
187+
LIGHT: "Light",
188+
ACCELERATION: "acceleration",
189+
GYRO: "gyro"
178190
}
179191

180192
_required_sensors = {
@@ -222,15 +234,15 @@ def get_name(cls, dimension_id: int) -> str:
222234
def get_sensor_community_name(cls, dimension_id: int) -> str:
223235
"""Returns the sensor-community-specific name for the dimension ID or 'Unknown' if none."""
224236
return cls._sensor_community_names.get(dimension_id, "Unknown")
225-
237+
226238
@classmethod
227239
def get_color(cls, dimension_id: int, val: float):
228240
th, colors = cls.thresholds.get(dimension_id)
229241
th = [-float('inf')] + th + [float('inf')]
230242
for i in range(len(th)):
231243
if th[i] <= val < th[i + 1]:
232244
return colors[i]
233-
245+
234246

235247
class LdProduct():
236248
AIR_AROUND = 1

app/templates/campaigns/detail.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,10 @@ <h3 class="mb-0">{% trans "Participants" %}</h3>
9191
<tbody>
9292
{% for user in campaign.users.all %}
9393
<tr>
94-
<td><a href="">{{ user.username }}</a></td>
94+
<td><a href="{% url 'participants-detail' campaign.pk user.pk %}">{{ user.username }}</a></td>
9595
<td>{{ user.email }}</td>
9696
<td>
9797
<div class="text-end d-flex flex-column flex-md-row gap-2 justify-content-end">
98-
<a href="{% url 'user-add-device' campaign.pk user.pk %}" class="btn btn-outline-primary btn-sm">{% trans "Add Device" %}</a>
9998
<a href="{% url 'participants-detail' campaign.pk user.pk %}" class="btn btn-primary btn-sm">
10099
<i class="bi bi-eye"></i>
101100
<span class="d-none d-sm-inline">{% trans "Details" %}</span>

app/templates/campaigns/participants/detail.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ <h3 class="mb-0">{% trans "24-hour overview" %}</h3>
4646
<div class="card mb-4">
4747
<div class="card-header d-flex justify-content-between align-items-center">
4848
<h3 class="mb-0">{% trans "Devices" %}</h3>
49-
<a href="{% url 'participant-add-device' participant.pk %}" class="btn btn-outline-primary btn-sm">{% trans "Add Device" %}</a>
49+
<a href="{% url 'user-add-device' campaign.pk user.pk %}" class="btn btn-outline-primary btn-sm">{% trans "Add Device" %}</a>
5050
</div>
5151
<div class="card-body">
5252
<!-- Responsive Table-Wrapper; nur nötig, wenn es sehr viele Spalten gibt -->
@@ -81,12 +81,12 @@ <h3 class="mb-0">{% trans "Devices" %}</h3>
8181
</div>
8282
</div>
8383

84-
<a href="{% url 'campaigns-detail' participant.campaign.pk %}" class="btn btn-outline-secondary">{% trans "Back to Campaign" %}</a>
84+
<a href="{% url 'campaigns-detail' campaign.pk %}" class="btn btn-outline-secondary">{% trans "Back to Campaign" %}</a>
8585
</div>
8686

8787
<script>
8888
var dataTemperature = {{ data_24h.1 }};
89-
var dataUVI = {{ data_24h.3 }};
89+
var dataUVI = {{ data_24h.2 }};
9090
var labels = {{ data_24h.0|safe }};
9191

9292
// Erstelle das Chart

0 commit comments

Comments
 (0)