add team room view with secret token

pull/1/head
parent 7c19caab6b
commit 5a826c04a6

@ -1,5 +1,5 @@
from django.contrib import admin from django.contrib import admin
from .models import IncomingMessage from .models import IncomingMessage, RoomViewToken
# Register your models here. # Register your models here.
@ -8,3 +8,9 @@ from .models import IncomingMessage
class IncomingMessageAdmin(admin.ModelAdmin): class IncomingMessageAdmin(admin.ModelAdmin):
list_display = ("sender", "created_at") list_display = ("sender", "created_at")
readonly_fields = ("id", "sender", "content", "created_at", "read") readonly_fields = ("id", "sender", "content", "created_at", "read")
@admin.register(RoomViewToken)
class RoomViewTokenAdmin(admin.ModelAdmin):
list_display = ("room", "id")
readonly_fields = ("id", "url")

@ -0,0 +1,37 @@
# Generated by Django 4.0.4 on 2023-05-18 15:15
from django.db import migrations, models
import django.db.models.deletion
import shiftregister.team.models
class Migration(migrations.Migration):
dependencies = [
("app", "0011_room_description"),
("team", "0003_incomingmessage_read_and_more"),
]
operations = [
migrations.CreateModel(
name="RoomViewToken",
fields=[
(
"id",
models.CharField(
default=shiftregister.team.models.gen_token,
editable=False,
max_length=20,
primary_key=True,
serialize=False,
),
),
(
"room",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE, to="app.room"
),
),
],
),
]

@ -1,6 +1,7 @@
from django.db import models from django.db import models
from phonenumber_field.modelfields import PhoneNumberField from phonenumber_field.modelfields import PhoneNumberField
from shiftregister.app.models import * from shiftregister.app.models import *
import secrets
# Create your models here. # Create your models here.
@ -25,3 +26,23 @@ class IncomingMessage(models.Model):
def display_sender(self): def display_sender(self):
helper = self.from_helper() helper = self.from_helper()
return helper.name if helper else self.sender return helper.name if helper else self.sender
def gen_token():
return secrets.token_urlsafe(15)
class RoomViewToken(models.Model):
id = models.CharField(
max_length=20, primary_key=True, default=gen_token, editable=False
)
room = models.ForeignKey(Room, on_delete=models.CASCADE)
def __str__(self):
return f"{self.room.name} {self.id}"
def url(self):
return "https://helfen.kntkt.de" + reverse(
"team:room_view_token",
kwargs={"token": self.id},
)

@ -0,0 +1,25 @@
{% extends "base.html" %}
{% block everything %}
<section class="section">
<div class="container">
<h3 class="title" style="background:var(--background);margin:0 -1rem;padding:1rem;position:sticky;top:0;">Helfer:innen für {{ room.name }}</h3>
<table class="table">
<thead style="background:inherit;position:sticky;top:4rem;">
<tr>
<th>Startzeit</th>
<th>Helfer:innen</th>
</tr>
</thead>
<tbody>
{% for shift in room.shift_set.all|dictsort:"start_at" %}
<tr>
<td>{{ shift.start_at }}</td>
<td>{% for reg in shift.shiftregistration_set.all %}{{ reg.helper.name }}{% if not forloop.last %}, {% endif %}{% endfor %}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</section>
{% endblock %}

@ -19,4 +19,5 @@ urlpatterns = [
path("incoming/", views.incoming_messages, name="incoming_messages"), path("incoming/", views.incoming_messages, name="incoming_messages"),
path("incoming/<int:pk>", views.incoming_message, name="incoming_message"), path("incoming/<int:pk>", views.incoming_message, name="incoming_message"),
path("incoming/mark_as_read/<int:pk>", views.mark_as_read, name="mark_as_read"), path("incoming/mark_as_read/<int:pk>", views.mark_as_read, name="mark_as_read"),
path("list/<slug:token>", views.room_view_token, name="room_view_token"),
] ]

@ -3,7 +3,15 @@ from django.contrib.auth.decorators import login_required
from django.utils import timezone from django.utils import timezone
from django.db.models.fields import DateTimeField from django.db.models.fields import DateTimeField
from django.db.models import F, Count, Q, ExpressionWrapper, Case, When from django.db.models import F, Count, Q, ExpressionWrapper, Case, When
from .models import ShiftRegistration, Room, Shift, Helper, Message, IncomingMessage from .models import (
ShiftRegistration,
Room,
Shift,
Helper,
Message,
IncomingMessage,
RoomViewToken,
)
from django.views.generic import DetailView, ListView from django.views.generic import DetailView, ListView
from django.views.generic.edit import FormMixin from django.views.generic.edit import FormMixin
from django.contrib.auth.mixins import LoginRequiredMixin from django.contrib.auth.mixins import LoginRequiredMixin
@ -355,3 +363,8 @@ def mark_as_read(request, pk):
IncomingMessage.objects.filter(sender=helper.phone).update(read=True) IncomingMessage.objects.filter(sender=helper.phone).update(read=True)
return redirect("team:helper", pk=pk) return redirect("team:helper", pk=pk)
def room_view_token(request, token):
tk = get_object_or_404(RoomViewToken, pk=token)
return render(request, "room_registrations.html", {"room": tk.room})

Loading…
Cancel
Save