diff --git a/authors/apps/profiles/serializers.py b/authors/apps/profiles/serializers.py index 6a7ca74..e1c98e6 100644 --- a/authors/apps/profiles/serializers.py +++ b/authors/apps/profiles/serializers.py @@ -11,12 +11,15 @@ class UserProfileSerializer(serializers.ModelSerializer): username = serializers.ReadOnlyField(source='fetch_username') img_url = serializers.ReadOnlyField(source='fetch_image') created_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S") + updated_at = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S") + class Meta: model = Profile fields = ( - 'username', 'first_name', 'last_name', 'bio', 'img_url', 'created_at' + 'username', 'first_name', 'last_name', 'bio', 'img_url', 'created_at', 'updated_at' ) + class UpdateUserProfileSerializer(serializers.ModelSerializer): """ Serializer class for updating user profile @@ -36,6 +39,8 @@ class FollowingSerializer(serializers.ModelSerializer): class Meta: model = Followers fields = ('profile', 'followed') + + class UserListSerializer(serializers.ModelSerializer): """ Serializer class for getting user profile diff --git a/authors/apps/profiles/tests/basetests.py b/authors/apps/profiles/tests/basetests.py index 86f3323..34ab5f2 100644 --- a/authors/apps/profiles/tests/basetests.py +++ b/authors/apps/profiles/tests/basetests.py @@ -27,7 +27,7 @@ def setUp(self): password="@Us3r.com" ) - self.user.is_verified=True + self.user.is_verified = True self.user.save() self.user1 = User.objects.create_user( @@ -36,7 +36,7 @@ def setUp(self): password="@Us3r.com" ) - self.user1.is_verified=True + self.user1.is_verified = True self.user1.save() def login_user(self, email="", password=""): @@ -131,6 +131,7 @@ def update_user_profile_notexist(self, first_name="", last_name="", bio=""): ), content_type="application/json" ) + def list_profiles(self): """ Method to get all profiles diff --git a/authors/apps/profiles/tests/test_profiles.py b/authors/apps/profiles/tests/test_profiles.py index 7b4695b..1a65d2a 100644 --- a/authors/apps/profiles/tests/test_profiles.py +++ b/authors/apps/profiles/tests/test_profiles.py @@ -31,9 +31,11 @@ def test_update_profile_successfuly(self): Test method for successfully updtaing a user """ self.is_authenticated("adam@gmail.com", "@Us3r.com") - response = self.update_user_profile("Abraham", "Kamau", "I love history") + response = self.update_user_profile( + "Abraham", "Kamau", "I love history") self.assertEqual(response.status_code, status.HTTP_200_OK) - self.assertEqual(response.data.get("profile").get("last_name"), "Kamau") + self.assertEqual(response.data.get( + "profile").get("last_name"), "Kamau") def test_update_profile_while_loggedout(self): """ @@ -42,7 +44,8 @@ def test_update_profile_while_loggedout(self): response = self.update_user_profile( "Abraham", "Kamau", "I love history") self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - self.assertEqual(response.data.get("detail"), "Authentication credentials were not provided.") + self.assertEqual(response.data.get("detail"), + "Authentication credentials were not provided.") def test_update_profile_while_not_owner(self): """ @@ -52,8 +55,9 @@ def test_update_profile_while_not_owner(self): response = self.update_another_user_profile( "Abraham", "Kamau", "I love history") self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) - self.assertEqual(response.data.get("error"), "You do not own this profile") - + self.assertEqual(response.data.get("error"), + "You do not own this profile") + def test_update_profile_while_not_exist(self): """ Test method for updating while user is not owner @@ -80,4 +84,4 @@ def test_listing_profiles_with_unauthorised_user(self): response = self.list_profiles() self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) self.assertEqual(response.data.get("detail"), - "Authentication credentials were not provided.") \ No newline at end of file + "Authentication credentials were not provided.") diff --git a/authors/apps/profiles/urls.py b/authors/apps/profiles/urls.py index d3c009d..e44859f 100644 --- a/authors/apps/profiles/urls.py +++ b/authors/apps/profiles/urls.py @@ -3,9 +3,11 @@ from django.conf.urls import url from .views import FollowAPI, FollowersAPI -from authors.apps.profiles.views import (UserProfileView, UpdateUserProfileView, UserListView) +from authors.apps.profiles.views import ( + UserProfileView, UpdateUserProfileView, UserListView) -from authors.apps.profiles.views import (UserProfileView, UpdateUserProfileView) +from authors.apps.profiles.views import ( + UserProfileView, UpdateUserProfileView) app_name = 'profiles' @@ -15,6 +17,7 @@ UpdateUserProfileView.as_view(), name='update_profile'), path('profiles//followers/', FollowersAPI.as_view()), path('profiles//follow/', FollowAPI.as_view()), - path('profiles//', UpdateUserProfileView.as_view(), name='update_profile'), + path('profiles//', + UpdateUserProfileView.as_view(), name='update_profile'), path('profiles/', UserListView.as_view(), name='list_users'), ] diff --git a/authors/apps/profiles/views.py b/authors/apps/profiles/views.py index 5a38672..5d58d01 100644 --- a/authors/apps/profiles/views.py +++ b/authors/apps/profiles/views.py @@ -26,9 +26,9 @@ def get(self, request, username): try: profile = Profile.objects.get(user__username=username) except Exception: - return Response({ - "error": "User does not exist" - }, status = status.HTTP_404_NOT_FOUND) + return Response({ + "error": "User does not exist" + }, status=status.HTTP_404_NOT_FOUND) serializer = UserProfileSerializer( profile, context={'request': request} @@ -38,6 +38,7 @@ def get(self, request, username): 'profile': serializer.data }, status=status.HTTP_200_OK) + class UpdateUserProfileView(GenericAPIView): """ A class for updating user profile @@ -52,9 +53,9 @@ def patch(self, request, username): try: profile = Profile.objects.get(user__username=username) except Exception: - return Response({ - "error": "User does not exist" - }, status = status.HTTP_404_NOT_FOUND) + return Response({ + "error": "User does not exist" + }, status=status.HTTP_404_NOT_FOUND) user_name = request.user.username if user_name != username: return Response({ @@ -65,7 +66,7 @@ def patch(self, request, username): serializer = UpdateUserProfileSerializer( instance=request.user.profile, - data=data, + data=data, partial=True ) serializer.is_valid() @@ -189,6 +190,8 @@ def get(self, request, username): response = { 'count': len(profiles), 'current_followers': profiles} return Response(response, status=status.HTTP_200_OK) + + class UserListView(GenericAPIView): """ A class for getting all user profiles @@ -200,4 +203,4 @@ def get(self, request): serializer = UserListSerializer(queryset, many=True) return Response({ 'profiles': serializer.data - }, status=status.HTTP_200_OK) + }, status=status.HTTP_200_OK)