Convert url slug to integer TeamMember id

pull/1/head
Luca 2 years ago
parent d186f048fb
commit f793d73b28

@ -1,4 +1,6 @@
from base64 import urlsafe_b64encode
from django.contrib import admin
from django.shortcuts import reverse
from .models import *
@ -26,11 +28,21 @@ class FallbackAssignmentInline(admin.TabularInline):
@admin.register(TeamMember)
class TeamMemberAdmin(admin.ModelAdmin):
fields = ("id", "name")
readonly_fields = ("id",)
fields = ("id", "name", "url")
readonly_fields = ("id", "url")
list_display = ("name", "shift_count")
inlines = (FallbackAssignmentInline,)
actions = (assign_random_shifts, clear_shifts, reshuffle_shifts)
def shift_count(self, object):
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")
},
)

@ -10,7 +10,7 @@ night_shift_query = Q(start_at__hour__gte=21) | Q(start_at__hour__lte=10)
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):

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

@ -1,11 +1,18 @@
from base64 import urlsafe_b64decode
from django.shortcuts import render, get_object_or_404
from shiftregister.fallback.models import TeamMember
# Create your views here.
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)
context = {
"team_member": team_member,

Loading…
Cancel
Save