Skip to content

Commit

Permalink
Port personalized calendar to frontend (#3009)
Browse files Browse the repository at this point in the history
* Expose signed username in api to generate calenar link on frontend
  • Loading branch information
henrikskog authored Aug 31, 2023
1 parent b08be2e commit 4cb6334
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions apps/authentication/api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.contrib.auth.models import Group, Permission
from django.core.signing import Signer
from rest_framework import mixins, status, viewsets
from rest_framework.decorators import action
from rest_framework.permissions import AllowAny, IsAuthenticated
Expand Down Expand Up @@ -48,6 +49,9 @@ class UserViewSet(
Viewset for User serializer. Supports filtering on 'first_name', 'last_name', 'email'
"""

def get_serializer_class(self):
return self.serializer_classes.get(self.action, UserReadOnlySerializer)

permission_classes = (IsSelfOrSuperUser,)
filterset_class = UserFilter
queryset = User.objects.all()
Expand All @@ -70,6 +74,17 @@ def change_password(self, request, pk=None):

return Response(data=None, status=status.HTTP_204_NO_CONTENT)

# See templates/events/index.html. This is exposing this logic in the api.
@action(detail=True, methods=["get"], url_path="personalized_calendar_link")
def personalized_calendar_link(self, request, pk=None):
user: User = self.get_object()
username = user.username
signer = Signer()
signed_value = signer.sign(username)

link = f"http://old.online.ntnu.no/events/user/{signed_value}.ics"
return Response(data={"link": link}, status=status.HTTP_200_OK)

@action(detail=True, methods=["put"])
def anonymize_user(self, request, pk=None):
user: User = self.get_object()
Expand All @@ -83,6 +98,7 @@ def anonymize_user(self, request, pk=None):
def dump_data(self, request, pk: int):
user: User = self.get_object()
serializer = self.get_serializer(user)

return Response(data=serializer.data, status=status.HTTP_200_OK)

@action(detail=True, methods=["get"], url_path="permissions")
Expand Down

0 comments on commit 4cb6334

Please sign in to comment.