mirror of
https://github.com/i701/sarlink-portal-api.git
synced 2025-07-13 14:43:10 +00:00
feat(user): add user update endpoint with authorization checks and serializer support ✨
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m9s
All checks were successful
Build and Push Docker Images / Build and Push Docker Images (push) Successful in 4m9s
This commit is contained in:
@ -33,6 +33,21 @@ class UserProfileUpdateSerializer(serializers.ModelSerializer):
|
|||||||
) # Only allow these fields
|
) # Only allow these fields
|
||||||
|
|
||||||
|
|
||||||
|
class UserUpdateSerializer(serializers.ModelSerializer):
|
||||||
|
class Meta: # type: ignore
|
||||||
|
model = User
|
||||||
|
fields = (
|
||||||
|
"id_card",
|
||||||
|
"mobile",
|
||||||
|
"first_name",
|
||||||
|
"last_name",
|
||||||
|
"address",
|
||||||
|
"dob",
|
||||||
|
"atoll",
|
||||||
|
"island",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class CustomUserSerializer(serializers.ModelSerializer):
|
class CustomUserSerializer(serializers.ModelSerializer):
|
||||||
"""serializer for the user object"""
|
"""serializer for the user object"""
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ from .views import (
|
|||||||
UpdateUserWalletView,
|
UpdateUserWalletView,
|
||||||
VerifyOTPView,
|
VerifyOTPView,
|
||||||
UserVerifyAPIView,
|
UserVerifyAPIView,
|
||||||
|
UserUpdateAPIView,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -39,6 +40,7 @@ urlpatterns = [
|
|||||||
),
|
),
|
||||||
path("users/<int:pk>/", UserDetailAPIView.as_view(), name="user-detail"),
|
path("users/<int:pk>/", UserDetailAPIView.as_view(), name="user-detail"),
|
||||||
path("users/<int:pk>/verify/", UserVerifyAPIView.as_view(), name="user-verify"),
|
path("users/<int:pk>/verify/", UserVerifyAPIView.as_view(), name="user-verify"),
|
||||||
|
path("users/<int:pk>/update/", UserUpdateAPIView.as_view(), name="user-update"),
|
||||||
path("users/filter/", filter_user, name="filter-users"),
|
path("users/filter/", filter_user, name="filter-users"),
|
||||||
path("users/temp/filter/", filter_temporary_user, name="filter-temporary-users"),
|
path("users/temp/filter/", filter_temporary_user, name="filter-temporary-users"),
|
||||||
path("healthcheck/", healthcheck, name="healthcheck"),
|
path("healthcheck/", healthcheck, name="healthcheck"),
|
||||||
|
32
api/views.py
32
api/views.py
@ -17,6 +17,7 @@ from api.serializers import (
|
|||||||
CustomUserByWalletBalanceSerializer,
|
CustomUserByWalletBalanceSerializer,
|
||||||
OTPVerificationSerializer,
|
OTPVerificationSerializer,
|
||||||
TemporaryUserSerializer,
|
TemporaryUserSerializer,
|
||||||
|
UserUpdateSerializer,
|
||||||
)
|
)
|
||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
@ -325,6 +326,37 @@ class UserprofileAPIView(generics.RetrieveUpdateAPIView):
|
|||||||
return self.request.user
|
return self.request.user
|
||||||
|
|
||||||
|
|
||||||
|
class UserUpdateAPIView(StaffEditorPermissionMixin, generics.UpdateAPIView):
|
||||||
|
serializer_class = UserUpdateSerializer
|
||||||
|
queryset = User.objects.all()
|
||||||
|
lookup_field = "pk"
|
||||||
|
|
||||||
|
def update(self, request, *args, **kwargs):
|
||||||
|
user_id = kwargs.get("pk")
|
||||||
|
user = get_object_or_404(User, pk=user_id)
|
||||||
|
if user.is_superuser:
|
||||||
|
return Response(
|
||||||
|
{"message": "You cannot update a superuser."},
|
||||||
|
status=status.HTTP_403_FORBIDDEN,
|
||||||
|
)
|
||||||
|
if request.user != user and (
|
||||||
|
not request.user.is_authenticated
|
||||||
|
or not getattr(request.user, "is_admin", False)
|
||||||
|
):
|
||||||
|
return Response(
|
||||||
|
{"message": "You are not authorized to update this user."},
|
||||||
|
status=status.HTTP_403_FORBIDDEN,
|
||||||
|
)
|
||||||
|
serializer = self.get_serializer(
|
||||||
|
user,
|
||||||
|
data=request.data,
|
||||||
|
partial=True,
|
||||||
|
)
|
||||||
|
serializer.is_valid(raise_exception=True)
|
||||||
|
user.save()
|
||||||
|
return super().update(request, *args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
class KnoxTokenListApiView(
|
class KnoxTokenListApiView(
|
||||||
StaffEditorPermissionMixin,
|
StaffEditorPermissionMixin,
|
||||||
generics.ListAPIView,
|
generics.ListAPIView,
|
||||||
|
Reference in New Issue
Block a user