Skip to content

Commit

Permalink
ft(upgrade): list users
Browse files Browse the repository at this point in the history
- add retrive profiles view

- add profiles url

- add support for profiles

- add profiles serializer
[DELIVERS #160617680]

change profiles serializer
  • Loading branch information
kcharles52 committed Oct 15, 2018
1 parent d3f8f22 commit 904e7e5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 13 deletions.
24 changes: 16 additions & 8 deletions authors/apps/profiles/renderers.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
import json
from rest_framework.renderers import JSONRenderer
from rest_framework.utils.serializer_helpers import ReturnDict, ReturnList


class ProfileJSONRenderer(JSONRenderer):

charset = 'utf-8'

def render(self, data, media_type=None, renderer_context=None):
errors = data.get('errors', None)
if errors is not None:

return super(ProfileJSONRenderer, self).render(data)
return json.dumps({
'profile': data
})
def render(self, data, accepted_media_type=None, renderer_context=None):
if type(data) != ReturnList:
errors = data.get('errors', None)
if errors is not None:
return super(ProfileJSONRenderer, self).render(data)

if type(data) == ReturnDict:
return json.dumps({
'profile': data
})
else:
return json.dumps({
'profiles': data
})
11 changes: 10 additions & 1 deletion authors/apps/profiles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,13 @@ class ProfileSerializer(serializers.ModelSerializer):
class Meta:
model = Profile
fields = ('username', 'first_name','last_name','bio','image','following','number_of_articles','created_at','updated_at')
read_only_fields = ('username',)
read_only_fields = ('username',)

class ProfilesSerializers(serializers.ModelSerializer):
username = serializers.CharField(source='user.username')
first_name = serializers.CharField(allow_blank=True, required=False)
last_name = serializers.CharField(allow_blank=True, required=False)
bio = serializers.CharField(allow_blank=True, required=False)
class Meta:
model = Profile
fields = ('username', 'first_name','last_name','bio')
3 changes: 2 additions & 1 deletion authors/apps/profiles/urls.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
from django.urls import path

from .views import ProfileRetrieveUpdateView
from .views import ProfileRetrieveUpdateView, RetriveProfilesView

app_name ='profiles'

urlpatterns = [
path('profiles/',RetriveProfilesView.as_view()),
path('profiles/<str:username>/', ProfileRetrieveUpdateView.as_view()),
]

25 changes: 22 additions & 3 deletions authors/apps/profiles/views.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from rest_framework import status
from rest_framework.generics import RetrieveUpdateAPIView
from rest_framework.generics import RetrieveUpdateAPIView, RetrieveAPIView
from rest_framework.permissions import IsAuthenticated, AllowAny
from rest_framework.response import Response
from django.shortcuts import get_object_or_404
from .models import Profile
from .renderers import ProfileJSONRenderer
from .serializers import ProfileSerializer
from .serializers import ProfileSerializer,ProfilesSerializers
from .exceptions import ProfileDoesNotExist
from rest_framework.exceptions import PermissionDenied,ValidationError
import jwt
Expand Down Expand Up @@ -73,4 +73,23 @@ def update(self, request, *args, **kwargs):

return Response(serializer.data, status=status.HTTP_200_OK)

raise PermissionDenied("You don't have rights to modify this profile")
raise PermissionDenied("You don't have rights to modify this profile")

class RetriveProfilesView(RetrieveAPIView):
permission_classes = (IsAuthenticated,)
renderer_classes = (ProfileJSONRenderer,)
serializer_class = ProfilesSerializers

def retrieve(self, request, *args, **kwargs):

try:

profiles = Profile.objects.all()

except Profile.DoesNotExist:
raise ProfileDoesNotExist



serializer = self.serializer_class(profiles, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)

0 comments on commit 904e7e5

Please sign in to comment.