Skip to content

Commit

Permalink
Merge pull request #24 from nens/logged-in-endpoint
Browse files Browse the repository at this point in the history
Logged in endpoint
  • Loading branch information
JJFlorian authored Apr 5, 2024
2 parents a77bd37 + 57a0677 commit 1f97bae
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 62 deletions.
31 changes: 4 additions & 27 deletions api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,14 @@
from django.contrib.auth.models import User
from rest_framework import serializers

from . import models as api_models
from .mixins import UrlFieldMixin


class UserProfileSerializer(UrlFieldMixin, serializers.ModelSerializer):
organisation_name = serializers.SerializerMethodField()
organisation_kvk = serializers.SerializerMethodField()

class UserSerializer(serializers.ModelSerializer):
class Meta:
model = api_models.UserProfile
exclude = ["user"]

# NOTE:
# Removed this after removing auth-details from user profile.
# Auth-details are now linked to organisation and will need an endpoint.
# This snippet can be used in the organisation endpoint

# # Exclude token and password in the get requests
# def to_representation(self, instance):
# if self.context["request"].method == "GET":
# exclude_fields = ["bro_user_token", "bro_user_password"]
# for field in exclude_fields:
# self.fields.pop(field, None)
# return super().to_representation(instance)

def get_organisation_name(self, obj):
organisation = obj.organisation
return organisation.name if organisation else None

def get_organisation_kvk(self, obj):
organisation = obj.organisation
return organisation.kvk_number if organisation else None
model = User
fields = ["username", "first_name", "last_name", "email"]


class ImportTaskSerializer(UrlFieldMixin, serializers.ModelSerializer):
Expand Down
16 changes: 6 additions & 10 deletions api/urls.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,17 @@
from django.urls import include, path
from rest_framework import routers

from . import views

app_name = "api"

router = routers.DefaultRouter()
router.register(r"users", views.UserViewSet, basename="user")

urlpatterns = [
path("", views.APIOverview.as_view(), name="overview"),
path("gmn/", include(("gmn.urls", "gmn"), namespace="gmn")),
path("gmw/", include(("gmw.urls", "gmw"), namespace="gmw")),
path(
"userprofile/",
views.UserProfileListView.as_view(),
name="userprofile-list",
),
path(
"userprofile/<uuid:uuid>/",
views.UserProfileDetailView.as_view(),
name="userprofile-detail",
),
path("importtasks/", views.ImportTaskListView.as_view(), name="importtask-list"),
path(
"importtasks/<uuid:uuid>/",
Expand All @@ -31,3 +25,5 @@
name="uploadtask-detail",
),
]

urlpatterns += router.urls
93 changes: 68 additions & 25 deletions api/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from django.contrib.auth import logout
from django.contrib.auth.models import User
from django.shortcuts import redirect
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import generics, permissions, status, views
from rest_framework import generics, permissions, status, views, viewsets
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.reverse import reverse as drf_reverse
from rest_framework.reverse import reverse

from . import filters, mixins, models, serializers, tasks

Expand All @@ -28,43 +30,84 @@ class APIOverview(views.APIView):

def get(self, request, format=None):
data = {
"userprofile": drf_reverse(
"api:userprofile-list", request=request, format=format
),
"importtasks": drf_reverse(
"users": reverse("api:user-list", request=request, format=format),
"importtasks": reverse(
"api:importtask-list", request=request, format=format
),
"uploadtasks": drf_reverse(
"uploadtasks": reverse(
"api:uploadtask-list", request=request, format=format
),
"gmns": drf_reverse("api:gmn:gmn-list", request=request, format=format),
"measuringpoints": drf_reverse(
"gmns": reverse("api:gmn:gmn-list", request=request, format=format),
"measuringpoints": reverse(
"api:gmn:measuringpoint-list", request=request, format=format
),
"gmws": drf_reverse("api:gmw:gmw-list", request=request, format=format),
"monitoringtubes": drf_reverse(
"gmws": reverse("api:gmw:gmw-list", request=request, format=format),
"monitoringtubes": reverse(
"api:gmw:monitoringtube-list", request=request, format=format
),
}
return Response(data)


class UserProfileListView(mixins.UserOrganizationMixin, generics.ListAPIView):
serializer_class = serializers.UserProfileSerializer
permission_classes = [permissions.IsAuthenticated]
class UserViewSet(viewsets.ModelViewSet):
model = User
serializer_class = serializers.UserSerializer
lookup_field = "pk"

def get_queryset(self):
return models.UserProfile.objects.filter(user=self.request.user)

def get(self, request, *args, **kwargs):
return self.list(request, *args, **kwargs)


class UserProfileDetailView(generics.RetrieveUpdateAPIView):
queryset = models.UserProfile.objects.all()
serializer_class = serializers.UserProfileSerializer
lookup_field = "uuid"
permission_classes = [permissions.IsAuthenticated]
user = self.request.user
queryset = User.objects.filter(pk=user.pk)

return queryset

@action(
detail=False,
url_path="logged-in",
)
def logged_in(self, request):
"""Endpoint to check whether the use is logged in or not."""
user = self.request.user
if user.is_anonymous:
return Response(
{
"logged_in": False,
"login_url": reverse(
"drf-login-override",
kwargs=None,
request=request,
format=None,
),
"logout_url": None,
"user_id": None,
"username": None,
"first_name": None,
"last_name": None,
"email": None,
"organisation": None,
"kvk": None,
}
)
else:
user_profile = models.UserProfile.objects.get(user=user)
return Response(
{
"logged_in": True,
"login_url": None,
"logout_url": reverse(
"drf-logout-override",
kwargs=None,
request=request,
format=None,
),
"user_id": user.pk,
"username": user.username,
"first_name": user.first_name,
"last_name": user.last_name,
"email": user.email,
"organisation": user_profile.organisation.name,
"kvk": user_profile.organisation.kvk_number,
}
)


class ImportTaskListView(mixins.UserOrganizationMixin, generics.ListAPIView):
Expand Down

0 comments on commit 1f97bae

Please sign in to comment.