Skip to content

Commit 1c7e2ef

Browse files
committed
(add) database view template
1 parent 55cd9fb commit 1c7e2ef

File tree

5 files changed

+39
-17
lines changed

5 files changed

+39
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
# Generated by Django 3.2.25 on 2024-07-09 08:56
1+
# Generated by Django 3.2.25 on 2024-07-09 17:41
22

3+
import datetime
34
from django.conf import settings
45
from django.db import migrations, models
56
import django.db.models.deletion
@@ -14,17 +15,17 @@ class Migration(migrations.Migration):
1415

1516
operations = [
1617
migrations.CreateModel(
17-
name='ShiftObserve',
18+
name='ShiftWatch',
1819
fields=[
1920
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20-
('notification_timedelta', models.DurationField()),
21+
('notification_timedelta', models.DurationField(default=datetime.timedelta(days=2))),
2122
('created_at', models.DateTimeField(auto_now_add=True)),
22-
('shift_to_be_notified_about', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shifts.shift')),
23-
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='shift_notification_entry', to=settings.AUTH_USER_MODEL)),
23+
('shift_watched', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='shifts.shift')),
24+
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='user_watching_shift', to=settings.AUTH_USER_MODEL)),
2425
],
2526
),
2627
migrations.AddConstraint(
27-
model_name='shiftobserve',
28-
constraint=models.UniqueConstraint(fields=('user', 'shift_to_be_notified_about'), name='user_shift_constraint'),
28+
model_name='shiftwatch',
29+
constraint=models.UniqueConstraint(fields=('user', 'shift_watched'), name='shift_watch_constraint'),
2930
),
3031
]

tapir/shifts/models.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1117,18 +1117,18 @@ class SolidarityShift(models.Model):
11171117
date_used = models.DateField(null=True)
11181118

11191119

1120-
class ShiftObserve(models.Model):
1120+
class ShiftWatch(models.Model):
11211121
class Meta:
11221122
constraints = [
11231123
models.UniqueConstraint(
1124-
fields=["user", "shift_to_be_notified_about"],
1125-
name="user_shift_constraint",
1124+
fields=["user", "shift_watched"],
1125+
name="shift_watch_constraint",
11261126
)
11271127
]
11281128

11291129
user = models.ForeignKey(
1130-
TapirUser, related_name="shift_notification_entry", on_delete=models.CASCADE
1130+
TapirUser, related_name="user_watching_shift", on_delete=models.CASCADE
11311131
)
1132-
shift_to_be_notified_about = models.ForeignKey(Shift, on_delete=models.CASCADE)
1133-
notification_timedelta = models.DurationField()
1132+
shift_watched = models.ForeignKey(Shift, on_delete=models.CASCADE)
1133+
notification_timedelta = models.DurationField(default=datetime.timedelta(days=2))
11341134
created_at = models.DateTimeField(auto_now_add=True)

tapir/shifts/templates/shifts/shift_detail.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ <h5 class="d-flex justify-content-between align-items-center">
2929
{% if perms.shifts.manage %}
3030
<span>
3131

32-
{% if True %}
32+
{% if request.user.user_watching_shift|user_watching_shift:shift.id %}
3333
<a class="{% tapir_button_link %}"
34-
href="{% url 'shifts:shift_detail_printable' shift.pk %}">
34+
href="{% url 'shifts:unwatch_shift' shift.pk %}">
3535
<span class="material-icons">visibility_off</span>{% translate 'Unwatch' %}
3636
</a>
3737
{% else %}
38-
<a class="{% tapir_button_link %}"
39-
href="{% url 'shifts:shift_detail_printable' shift.pk %}">
38+
<a class="{% tapir_button_link %}"
39+
href="{% url 'shifts:watch_shift' shift.pk %}">
4040
<span class="material-icons">visibility</span>{% translate 'Watch' %}
4141
</a>
4242
{% endif %}

tapir/shifts/urls.py

+2
Original file line numberDiff line numberDiff line change
@@ -252,4 +252,6 @@
252252
views.ShiftStatusEvolutionJsonView.as_view(),
253253
name="shift_status_evolution_json",
254254
),
255+
path("watch_shift/<int:shift_id>/", views.watch_shift, name="watch_shift"),
256+
path("unwatch_shift/<int:shift_id>/", views.unwatch_shift, name="unwatch_shift"),
255257
]

tapir/shifts/views/views.py

+19
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
SHIFT_ATTENDANCE_STATES,
4848
ShiftTemplate,
4949
SolidarityShift,
50+
ShiftWatch,
5051
)
5152
from tapir.shifts.models import (
5253
ShiftSlot,
@@ -495,3 +496,21 @@ def get_context_data(self, **kwargs):
495496
},
496497
}
497498
return context_data
499+
500+
501+
def watch_shift(request, shift_id):
502+
shift_to_watch = get_object_or_404(Shift, id=shift_id)
503+
if not request.user.user_watching_shift.filter(shift_watched_id=shift_id).exists():
504+
ShiftWatch.objects.create(user=request.user, shift_watched=shift_to_watch)
505+
return redirect("shifts:shift_detail", pk=shift_id)
506+
507+
508+
def unwatch_shift(request, shift_id):
509+
shift_to_unwatch = get_object_or_404(Shift, id=shift_id)
510+
request.user.user_watching_shift.filter(shift_watched_id=shift_id).delete()
511+
return redirect("shifts:shift_detail", pk=shift_id)
512+
513+
514+
@register.filter
515+
def user_watching_shift(user, shift_id) -> bool:
516+
return user.filter(shift_watched_id=shift_id).exists()

0 commit comments

Comments
 (0)