Browse Source

Enhance UserProfileSerializer to handle location history retrieval

- Added checks in get_saved_location method to ensure user is authenticated and has location_history attribute before attempting to access it.
- Improved robustness of location retrieval logic to prevent errors for unauthenticated users.
master
mortezaei 3 months ago
parent
commit
e52ff6ca92
  1. 4
      apps/account/serializers/user.py
  2. 16
      apps/course/views/course.py

4
apps/account/serializers/user.py

@ -26,6 +26,10 @@ class UserProfileSerializer(serializers.ModelSerializer):
read_only_fields = ['email', 'info', 'skill', 'device_id', 'slug', 'saved_location']
def get_saved_location(self, obj):
# Check if user is authenticated and has location_history attribute
if not obj.is_authenticated or not hasattr(obj, 'location_history'):
return None
last_location = obj.location_history.order_by('-at_time').first()
if last_location:
return {

16
apps/course/views/course.py

@ -454,6 +454,22 @@ class CourseOnlineClassTokenValidateAPIView(GenericAPIView):
print(f"[Online Validate GET] user={request.user}")
print(f"[Online Validate GET] user.is_authenticated={request.user.is_authenticated}")
print(f"[Online Validate GET] user.id={request.user.id if request.user.is_authenticated else 'N/A'}")
print(f"[Online Validate GET] Authorization Header={request.META.get('HTTP_AUTHORIZATION', 'NOT FOUND')}")
print(f"[Online Validate GET] All Headers={dict((k, v) for k, v in request.META.items() if k.startswith('HTTP_'))}")
# Debug: Check if token exists in database
auth_header = request.META.get('HTTP_AUTHORIZATION', '')
if auth_header.startswith('Token '):
token_key = auth_header.split(' ')[1]
try:
from rest_framework.authtoken.models import Token
token_obj = Token.objects.get(key=token_key)
print(f"[Online Validate GET] Token found in DB - user={token_obj.user} user_id={token_obj.user.id}")
except Token.DoesNotExist:
print(f"[Online Validate GET] Token NOT found in DB - token={token_key[:10]}...")
except Exception as e:
print(f"[Online Validate GET] Token check error - {str(e)}")
print("=" * 80)
logger.info(f"[Online Validate GET] Request received - slug={slug} user_id={request.user.id if request.user.is_authenticated else 'anonymous'}")

Loading…
Cancel
Save