From 733c6c7f8b64830f2d4eff61155bb65d80f27da7 Mon Sep 17 00:00:00 2001 From: mortezaei Date: Wed, 5 Nov 2025 17:24:32 +0330 Subject: [PATCH] feat(user): add saved_location field to UserProfileSerializer - Introduced a new SerializerMethodField `saved_location` to retrieve the user's last known location from their location history. - Updated the fields and read_only_fields in UserProfileSerializer to include `saved_location` for enhanced user profile data. - Adjusted permission classes in CalendarList and AdjustmentConfigView to AllowAny for broader access. --- apps/account/serializers/user.py | 19 +++++++++++++++++-- apps/dobodbi_calendar/views.py | 6 ++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/apps/account/serializers/user.py b/apps/account/serializers/user.py index 1c8876a..6a8a410 100644 --- a/apps/account/serializers/user.py +++ b/apps/account/serializers/user.py @@ -20,11 +20,26 @@ class UserProfileSerializer(serializers.ModelSerializer): help_text="Select the user's gender." ) fcm = serializers.CharField(required=False, help_text="Firebase Cloud Messaging token.") + saved_location = serializers.SerializerMethodField() class Meta: model = User - fields = ['id', 'device_id', 'fcm', 'fullname', 'slug', 'avatar', 'email', 'phone_number', 'password', 'info', 'skill', 'city', 'country', 'birthdate', 'gender'] - read_only_fields = ['email', 'info', 'skill', 'device_id', 'slug'] + fields = ['id', 'device_id', 'fcm', 'fullname', 'slug', 'avatar', 'email', 'phone_number', 'password', 'info', 'skill', 'city', 'country', 'birthdate', 'gender', 'saved_location'] + read_only_fields = ['email', 'info', 'skill', 'device_id', 'slug', 'saved_location'] + + def get_saved_location(self, obj): + last_location = obj.location_history.order_by('-at_time').first() + if last_location: + return { + 'lat': last_location.lat, + 'lon': last_location.lon, + 'city': last_location.city, + 'country': last_location.country, + 'timezone': last_location.timezone, + 'selected_manually': last_location.selected_manually, + 'at_time': last_location.at_time, + } + return None # def validate_email(self, value): # if User.objects.filter(email=value).exists(): diff --git a/apps/dobodbi_calendar/views.py b/apps/dobodbi_calendar/views.py index 3dca2a9..ccbf662 100644 --- a/apps/dobodbi_calendar/views.py +++ b/apps/dobodbi_calendar/views.py @@ -8,7 +8,7 @@ from django.db.models import Q from django.utils.decorators import method_decorator from django.views.decorators.cache import cache_page from rest_framework.generics import ListAPIView -from rest_framework.permissions import IsAuthenticated +from rest_framework.permissions import IsAuthenticated, AllowAny from rest_framework.response import Response from rest_framework.views import APIView, status @@ -22,7 +22,7 @@ class CalendarList(ListAPIView): serializer_class = CalendarSerializer pagination_class = None - permission_classes = (IsAuthenticated,) + permission_classes = (AllowAny,) # @method_decorator(cache_page(60 * 15)) # Cache for 1 Hour # def dispatch(self, *args, **kwargs): @@ -59,6 +59,8 @@ class CalendarList(ListAPIView): class AdjustmentConfigView(APIView): + permission_classes = (AllowAny,) + def get(self, request): adjustment_config = get_config('calendar__Adjustment') return Response(json.loads(adjustment_config))