diff --git a/profiles/models.py b/profiles/models.py index f6e977a8b..2b137cea0 100644 --- a/profiles/models.py +++ b/profiles/models.py @@ -118,7 +118,9 @@ class SavedCompany(models.Model): class ViewedCompany(models.Model): date = models.DateTimeField(auto_now_add=True) - user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, blank=True, null=True) + user = models.ForeignKey( + CustomUser, on_delete=models.CASCADE, blank=True, null=True + ) company = models.ForeignKey(Profile, on_delete=models.CASCADE) @property diff --git a/profiles/permissions.py b/profiles/permissions.py index 7bf6456c7..bf5ab1751 100644 --- a/profiles/permissions.py +++ b/profiles/permissions.py @@ -11,11 +11,11 @@ def has_object_permission(self, request, view, obj): return True return obj.person == request.user - + class RequestIsCreate(BasePermission): def has_permission(self, request, view): - return request.method == 'POST' + return request.method == "POST" class IsOwner(BasePermission): diff --git a/profiles/serializers.py b/profiles/serializers.py index 04fb61b48..836401e95 100644 --- a/profiles/serializers.py +++ b/profiles/serializers.py @@ -240,9 +240,17 @@ class Meta: class ViewedCompanySerializer(serializers.ModelSerializer): user_profile_name = serializers.SerializerMethodField() company_name = serializers.SerializerMethodField() + class Meta: model = ViewedCompany - fields = ("id", "user", "company", "date", "user_profile_name", "company_name",) + fields = ( + "id", + "user", + "company", + "date", + "user_profile_name", + "company_name", + ) def get_user_profile_name(self, obj): if obj.user: @@ -251,7 +259,7 @@ def get_user_profile_name(self, obj): def get_company_name(self, obj): return obj.company_name - + def create(self, validated_data): user = validated_data.get("user") profile = Profile.objects.get(id=validated_data.get("company")) diff --git a/profiles/tests/test_viewed_company.py b/profiles/tests/test_viewed_company.py index 5b21cfa0b..9045efc9b 100644 --- a/profiles/tests/test_viewed_company.py +++ b/profiles/tests/test_viewed_company.py @@ -30,7 +30,9 @@ def test_create_viewed_company_authorized(self): ) self.assertEqual(status.HTTP_201_CREATED, response.status_code) self.assertEqual(self.user.id, response.data["user"]) - self.assertEqual(self.user_profile.name, response.data["user_profile_name"]) + self.assertEqual( + self.user_profile.name, response.data["user_profile_name"] + ) self.assertEqual(profile.id, response.data["company"]) self.assertEqual(profile.name, response.data["company_name"]) diff --git a/profiles/urls.py b/profiles/urls.py index b910ef0ae..9ffd2f156 100644 --- a/profiles/urls.py +++ b/profiles/urls.py @@ -28,7 +28,9 @@ name="saved_companies_destroy", ), path( - "viewed-list/", ViewedCompanyCreate.as_view(), name="viewed_company_create" + "viewed-list/", + ViewedCompanyCreate.as_view(), + name="viewed_company_create", ), path("regions/", RegionListView.as_view(), name="region-list"), path("categories/", CategoryList.as_view(), name="category_list"), diff --git a/profiles/views.py b/profiles/views.py index 73693768b..b7e9763d9 100644 --- a/profiles/views.py +++ b/profiles/views.py @@ -24,7 +24,13 @@ Activity, Region, ) -from .permissions import UserIsProfileOwnerOrReadOnly, ReadOnly, IsOwnCompany, IsOwner, RequestIsCreate +from .permissions import ( + UserIsProfileOwnerOrReadOnly, + ReadOnly, + IsOwnCompany, + IsOwner, + RequestIsCreate, +) from .serializers import ( SavedCompanySerializer, ProfileListSerializer, @@ -187,19 +193,15 @@ def perform_destroy(self, instance): class ViewedCompanyCreate(CreateAPIView): serializer_class = ViewedCompanySerializer queryset = ViewedCompany.objects.all() - permission_classes = [(RequestIsCreate & (~IsOwner | ~IsAuthenticated)), ] - - # @cached_property - # def _get_profile(self): - # self._track_contacts_request() - # return self.get_object() - + permission_classes = [ + (RequestIsCreate & (~IsOwner | ~IsAuthenticated)), + ] + def perform_create(self, serializer): serializer.save( - user=self.request.user, - company=self.request.data.get("company") - ) - + user=self.request.user, company=self.request.data.get("company") + ) + class CategoryList(ListCreateAPIView): serializer_class = CategorySerializer