Skip to content

Commit

Permalink
made some changes to backend saving component
Browse files Browse the repository at this point in the history
  • Loading branch information
nawalragih committed Nov 19, 2024
1 parent bb5d65c commit dbabfca
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 7 deletions.
5 changes: 3 additions & 2 deletions Backend/accounts/saving/urls.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import UserProfileViewSet, ArtistPortfolioViewSet
from .views import UserProfileViewSet, ArtistPortfolioViewSet, ProfileUploadView

router = DefaultRouter()
router.register(r'user-profile', UserProfileViewSet, basename='user-profile')
router.register(r'artist-portfolio', ArtistPortfolioViewSet, basename='artist-portfolio')

urlpatterns = [
path('api/', include(router.urls)),
path('', include(router.urls)),
path('api/profile/', ProfileUploadView.as_view(), name='profile-upload'),
]
43 changes: 39 additions & 4 deletions Backend/accounts/saving/views.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import logging
from rest_framework import status, viewsets
from rest_framework.response import Response
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.parsers import MultiPartParser, FormParser
from .models import Customer, ArtistPortfolio
from .serializers import UserProfileSerializer, ArtistPortfolioSerializer

# Set up logging
logger = logging.getLogger(__name__)

class UserProfileViewSet(viewsets.ModelViewSet):
queryset = Customer.objects.all()
serializer_class = UserProfileSerializer
Expand All @@ -19,14 +25,43 @@ class ArtistPortfolioViewSet(viewsets.ModelViewSet):
serializer_class = ArtistPortfolioSerializer
permission_classes = [IsAuthenticated]

def perform_create(self, serializer):
# Attempt to get the existing user profile
user_profile = Customer.objects.filter(firebase_user_id=self.request.user.uid).first()

def create(self, request, *args, **kwargs):
user_profile = Customer.objects.filter(firebase_user_id=request.user.uid).first()

if not user_profile:
return Response(
{"detail": "User profile not found. Please create a profile first."},
status=status.HTTP_400_BAD_REQUEST
)

serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save(user_profile=user_profile)
headers = self.get_success_headers(serializer.data)
return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)

class ProfileUploadView(APIView):
parser_classes = (MultiPartParser, FormParser)

def post(self, request, *args, **kwargs):
try:
business_name = request.data.get('business_name')
bio = request.data.get('bio')
profile_picture = request.FILES.get('profile_picture')
photos = request.FILES.getlist('photos')

# Log data (replace this with database saving logic if needed)
logger.info(f'Business Name: {business_name}')
logger.info(f'Bio: {bio}')
logger.info(f'Profile Picture: {profile_picture}')
logger.info(f'Photos: {photos}')

# Save logic here if applicable

return Response({'message': 'Portfolio saved successfully!'}, status=status.HTTP_200_OK)
except Exception as e:
logger.error('Error saving portfolio:', exc_info=True)
return Response(
{'message': 'Failed to save portfolio. Please try again.'},
status=status.HTTP_500_INTERNAL_SERVER_ERROR
)
2 changes: 1 addition & 1 deletion next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @type {import('next').NextConfig} */
const nextConfig = {};

export default nextConfig;
export default nextConfig;

0 comments on commit dbabfca

Please sign in to comment.