Spaces:
Running
Running
Download api/views_profiles.py from Dayozoe/harvestconnect-api: direct link, hf CLI and curl.
- Browser
- Download file 2.38 kB
-
https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/views_profiles.py
- Command line
-
hf download hf://spaces/Dayozoe/harvestconnect-api/api/views_profiles.py
-
curl -L -o views_profiles.py https://huggingface.co/spaces/Dayozoe/harvestconnect-api/resolve/main/api/views_profiles.py
2.38 kB
| from django.db import models | |
| from rest_framework import viewsets, permissions, status | |
| from rest_framework.decorators import action | |
| from rest_framework.response import Response | |
| from .models import UserProfile, ChatRoom, SavedItem | |
| from .serializers import UserProfileSerializer, ChatRoomSerializer, UserSerializer, SavedItemSerializer | |
| from .services.dashboard_stats import build_dashboard_stats | |
| class UserProfileViewSet(viewsets.ViewSet): | |
| """API endpoint for user profiles""" | |
| permission_classes = [permissions.IsAuthenticated] | |
| def me(self, request): | |
| """Get or update current user's profile""" | |
| profile, _ = UserProfile.objects.get_or_create(user=request.user) | |
| if request.method in ['PUT', 'PATCH']: | |
| serializer = UserProfileSerializer(profile, data=request.data, partial=True) | |
| if serializer.is_valid(): | |
| serializer.save() | |
| # If first_name or last_name in request data, update User model | |
| if 'first_name' in request.data: | |
| request.user.first_name = request.data['first_name'] | |
| if 'last_name' in request.data: | |
| request.user.last_name = request.data['last_name'] | |
| request.user.save() | |
| return Response(serializer.data) | |
| return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | |
| # For GET, include user's relevant chat discovery | |
| discovery = ChatRoom.objects.filter( | |
| models.Q(church=profile.home_church) | | |
| models.Q(location=profile.location) | | |
| models.Q(room_type='channel') | |
| ).exclude(participants=request.user)[:5] | |
| discovery_data = ChatRoomSerializer(discovery, many=True).data | |
| user_data = UserSerializer(request.user).data | |
| user_data['discovery'] = discovery_data | |
| return Response(user_data) | |
| def stats(self, request): | |
| return Response(build_dashboard_stats(request.user)) | |
| class SavedItemViewSet(viewsets.ModelViewSet): | |
| """API endpoint for member's saved items""" | |
| serializer_class = SavedItemSerializer | |
| permission_classes = [permissions.IsAuthenticated] | |
| def get_queryset(self): | |
| return SavedItem.objects.filter(user=self.request.user) | |