2
0
Fork 0

Convert url slug to integer TeamMember id

This commit is contained in:
Luca 2023-05-13 19:52:28 +02:00
parent d186f048fb
commit f793d73b28
4 changed files with 24 additions and 5 deletions

View File

@ -1,4 +1,6 @@
from base64 import urlsafe_b64encode
from django.contrib import admin from django.contrib import admin
from django.shortcuts import reverse
from .models import * from .models import *
@ -26,11 +28,21 @@ class FallbackAssignmentInline(admin.TabularInline):
@admin.register(TeamMember) @admin.register(TeamMember)
class TeamMemberAdmin(admin.ModelAdmin): class TeamMemberAdmin(admin.ModelAdmin):
fields = ("id", "name") fields = ("id", "name", "url")
readonly_fields = ("id",) readonly_fields = ("id", "url")
list_display = ("name", "shift_count") list_display = ("name", "shift_count")
inlines = (FallbackAssignmentInline,) inlines = (FallbackAssignmentInline,)
actions = (assign_random_shifts, clear_shifts, reshuffle_shifts) actions = (assign_random_shifts, clear_shifts, reshuffle_shifts)
def shift_count(self, object): def shift_count(self, object):
return object.fallback_shifts.count() return object.fallback_shifts.count()
def url(self, object):
return "https://helfen.kntkt.de" + reverse(
"my_fallback_shifts",
kwargs={
"team_member_id": urlsafe_b64encode(
object.id.to_bytes(3, byteorder="big")
).decode("utf-8")
},
)

View File

@ -10,7 +10,7 @@ night_shift_query = Q(start_at__hour__gte=21) | Q(start_at__hour__lte=10)
def generate_id(): def generate_id():
return int.from_bytes(secrets.token_bytes(3)) return int.from_bytes(secrets.token_bytes(3), byteorder="big")
class TeamMember(models.Model): class TeamMember(models.Model):

View File

@ -4,7 +4,7 @@ from . import views
urlpatterns = [ urlpatterns = [
path( path(
"fallback_shifts/<int:team_member_id>", "fallback_shifts/<slug:team_member_id>",
views.my_fallback_shifts, views.my_fallback_shifts,
name="my_fallback_shifts", name="my_fallback_shifts",
), ),

View File

@ -1,11 +1,18 @@
from base64 import urlsafe_b64decode
from django.shortcuts import render, get_object_or_404 from django.shortcuts import render, get_object_or_404
from shiftregister.fallback.models import TeamMember from shiftregister.fallback.models import TeamMember
# Create your views here. # Create your views here.
def my_fallback_shifts(request, team_member_id): def my_fallback_shifts(request, team_member_id):
try:
team_member_id = int.from_bytes(
urlsafe_b64decode(team_member_id), byteorder="big"
)
except Exception:
team_member_id = None
team_member = get_object_or_404(TeamMember, pk=team_member_id) team_member = get_object_or_404(TeamMember, pk=team_member_id)
context = { context = {
"team_member": team_member, "team_member": team_member,