diff --git a/history/models.py b/history/models.py index e102cb0..5296478 100644 --- a/history/models.py +++ b/history/models.py @@ -9,9 +9,9 @@ class HistorySync(models.Model): company = models.ForeignKey('companies.Company', on_delete=models.DO_NOTHING, null=True) rss_url = models.URLField('URL del feed', null=True, blank=True) - sync_date = models.DateTimeField('Fecha de lanzamiento', null=True) + sync_date = models.DateTimeField('Fecha de lanzamiento', null=True, blank=True) result = models.TextField('Resultado', null=True, blank=True) - quantity = models.PositiveIntegerField('Productos importados', null=True) + quantity = models.PositiveIntegerField('Productos importados', null=True, blank=True) # internal created = models.DateTimeField('date of creation', auto_now_add=True) diff --git a/products/models.py b/products/models.py index 84c7060..30631bb 100644 --- a/products/models.py +++ b/products/models.py @@ -33,9 +33,9 @@ class Product(models.Model): sourcing_date = models.DateTimeField('Fecha de importación original de producto', null=True, blank=True) update_date = models.DateTimeField('Fecha de actualización de producto', null=True, blank=True) discount = models.DecimalField('Descuento', max_digits=5, decimal_places=2, null=True, blank=True) - stock = models.PositiveIntegerField('Stock', null=True) + stock = models.PositiveIntegerField('Stock', null=True, blank=True) tags = TagField(to=TreeTag) - category = SingleTagField(blank=True, null=True) # main tag category + category = SingleTagField(null=True, blank=True) # main tag category attributes = TagField(to=TreeTag, related_name='product_attributes') identifiers = models.TextField('Identificador único de producto', null=True, blank=True) diff --git a/products/tests.py b/products/tests.py index d32e1f6..b3a33ce 100644 --- a/products/tests.py +++ b/products/tests.py @@ -163,6 +163,58 @@ class ProductViewSetTest(APITestCase): # Assert number of instnaces in response self.assertEquals(len(expected_instance), len(payload)) + def test_anon_user_can_filter_company(self): + # create instances + company = CompanyFactory() + expected_instance = [ + self.factory(category='ropa', tags="zapatos, rojos", company=company), + self.factory(category='ropa', tags="rojos", company=company), + self.factory(category='ropa', tags="colores/rojos", company=company) + ] + unexpected_instance = [ + self.factory(category='roperos', tags="zapatos, azules"), + self.factory(category='enropados', tags="xxl") + ] + + # prepare url + url = f"{self.endpoint}?company={company.id}" + + # Request list + response = self.client.get(url) + payload = response.json() + + # Assert access is granted + self.assertEqual(response.status_code, status.HTTP_200_OK) + # Assert number of instnaces in response + self.assertEquals(len(expected_instance), len(payload)) + + def test_anon_user_can_order_products(self): + # create instances + company = CompanyFactory() + unexpected_instance = [ + self.factory(category='roperos', tags="zapatos, azules"), + self.factory(category='enropados', tags="xxl") + ] + expected_instance = [ + self.factory(category='ropa', tags="zapatos, rojos", company=company), + self.factory(category='ropa', tags="rojos", company=company), + self.factory(category='ropa', tags="colores/rojos", company=company) + ] + # prepare url + url = f"{self.endpoint}?ordering=created" + + # Request list + response = self.client.get(url) + payload = response.json() + + # Assert access is granted + self.assertEqual(response.status_code, status.HTTP_200_OK) + # TODO: assert correct order + previous_date = datetime.datetime.now() + for instance in payload: + self.assertTrue(datetime.datetime.fromisoformat(instance['created'][:-1]) < previous_date) + previous_date = datetime.datetime.fromisoformat(instance['created'][:-1]) + # authenticated user def test_auth_user_can_list_instances(self): """Regular logged-in user can list instance diff --git a/products/views.py b/products/views.py index fd69c1b..69d7069 100644 --- a/products/views.py +++ b/products/views.py @@ -15,6 +15,7 @@ from rest_framework import viewsets from rest_framework.response import Response from rest_framework.permissions import IsAuthenticatedOrReadOnly, IsAdminUser, IsAuthenticated from rest_framework.decorators import api_view, permission_classes, action +from rest_framework.filters import OrderingFilter import requests @@ -41,6 +42,8 @@ class ProductViewSet(viewsets.ModelViewSet): queryset = Product.objects.all() serializer_class = ProductSerializer permission_classes = [IsAuthenticatedOrReadOnly, IsCreator] + # filter_backends = [ProductTagFilter, OrderingFilter] + # ordering_fields = ['created'] filterset_class = ProductTagFilter filterset_fields = ['name', 'tags', 'category', 'attributes', 'company', 'created'] diff --git a/stats/models.py b/stats/models.py index 1de6942..0b86ec8 100644 --- a/stats/models.py +++ b/stats/models.py @@ -17,11 +17,11 @@ class StatsLog(models.Model): action_object_object_id = models.CharField(max_length=255, blank=True, null=True) action_object = GenericForeignKey('action_object_content_type', 'action_object_object_id') user = models.ForeignKey(User, on_delete=models.DO_NOTHING, null=True) - anonymous = models.BooleanField('Usuario no registrado', null=True) + anonymous = models.BooleanField('Usuario no registrado', null=True, blank=True) ip_address = models.GenericIPAddressField('IP usuario', null=True, blank=True) geo = models.PointField('Ubicación aproximada', null=True, blank=True ) - contact = models.BooleanField('Empresa contactada', null=True) - shop = models.BooleanField('Redirigido por botón "Comprar"', null=True) + contact = models.BooleanField('Empresa contactada', null=True, blank=True) + shop = models.BooleanField('Redirigido por botón "Comprar"', null=True, blank=True) # internal created = models.DateTimeField('date of creation', auto_now_add=True) diff --git a/utils/tag_filters.py b/utils/tag_filters.py index 3cc8502..e80d9a6 100644 --- a/utils/tag_filters.py +++ b/utils/tag_filters.py @@ -26,7 +26,7 @@ class ProductTagFilter(django_filters.FilterSet): class Meta: model = Product - fields = ['name', 'tags', 'category', 'attributes'] + fields = ['name', 'tags', 'category', 'attributes', 'company', 'created'] def tag_filter(self, queryset, name, value): return queryset.filter(**{