def update_profile(request): if request.method == "PUT": profile = get_object_or_404(Profile, user=request.user) serializer = ProfileSerializer(profile, data=request.data) if serializer.is_valid(): serializer.save() data = serializer.data data['username'] = request.user.username return Response(data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def profile_view(request, username): try: account = Account.objects.get(username=username) except Account.DoesNotExist: return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': serializer = ProfileSerializer(account) return Response(serializer.data)
def get_profile(request): if request.method == "GET": profile = get_object_or_404(Profile, user=request.user) pr_serializer = ProfileSerializer(profile) data = pr_serializer.data data['username'] = profile.user.username orgs = profile.organizations.all().order_by("name") data['orgs'] = [{"org_id": org.id, "org_name": org.name} for org in orgs] return Response(data)
class StatusSerializer(serializers.ModelSerializer): profile = ProfileSerializer() class Meta: model = Status fields = [ 'pk', 'profile', 'text', 'timestamp', ]
class DesignSerializer(serializers.ModelSerializer): profile = ProfileSerializer() class Meta: model = Design fields = [ 'pk', 'profile', 'title', 'image', 'description', 'timestamp', ]
def api_profile_details(request): try: prof = Profile.objects.get(pk=request.user.profile.id) except Profile.DoesNotExist: content = "Profile Does Not Exist !!" return Response(content) return Response(status=status.HTTP_404_NOT_FOUND) if request.method == 'GET': if request.user.profile.id: serializer = ProfileSerializer(prof) return Response(serializer.data) else: content = "Error Token" return Response(content) elif request.method == 'PUT': serializer = ProfileSerializer(prof, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def profile_create(request): serializer = ProfileSerializer(data=request.data) if serializer.is_valid(): serializer.save() return JsonResponse({'success': True, "data": serializer.data}) return JsonResponse({'success': False, 'message': "user doesn't exist"})