Merge branch 'development' of https://bitbucket.org/enreda/back-latienda into diego
This commit is contained in:
@@ -61,7 +61,6 @@ class Command(BaseCommand):
|
|||||||
logging.info(f"Country instance created for: {name}")
|
logging.info(f"Country instance created for: {name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
# region instances
|
# region instances
|
||||||
logging.info("loading region instances")
|
logging.info("loading region instances")
|
||||||
@@ -83,7 +82,6 @@ class Command(BaseCommand):
|
|||||||
logging.info(f"Region instance created for: {name}")
|
logging.info(f"Region instance created for: {name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
# province instances
|
# province instances
|
||||||
logging.info("loading province instances")
|
logging.info("loading province instances")
|
||||||
@@ -106,7 +104,6 @@ class Command(BaseCommand):
|
|||||||
logging.info(f"Province instance created for: {name}")
|
logging.info(f"Province instance created for: {name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
logging.error(f"[{name}][{type(e)}] {str(e)}")
|
||||||
import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
# city instances
|
# city instances
|
||||||
logging.info("loading city instances")
|
logging.info("loading city instances")
|
||||||
@@ -129,7 +126,6 @@ class Command(BaseCommand):
|
|||||||
logging.debug(f"City instance created for: {name}")
|
logging.debug(f"City instance created for: {name}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logging.error(f"[{type(e)}] {str(e)}")
|
logging.error(f"[{type(e)}] {str(e)}")
|
||||||
# import ipdb; ipdb.set_trace()
|
|
||||||
|
|
||||||
logging.info(f"Country instances created: {country_counter}")
|
logging.info(f"Country instances created: {country_counter}")
|
||||||
logging.info(f"Region instances created: {region_counter}")
|
logging.info(f"Region instances created: {region_counter}")
|
||||||
|
|||||||
@@ -1,11 +1,18 @@
|
|||||||
from django.contrib.gis.db import models
|
from django.contrib.gis.db import models
|
||||||
|
|
||||||
from tagulous.models import SingleTagField, TagField
|
from tagulous.models import SingleTagField, TagField, TagTreeModel
|
||||||
|
|
||||||
from companies.models import Company
|
from companies.models import Company
|
||||||
|
|
||||||
# Create your models here.
|
# Create your models here.
|
||||||
|
|
||||||
|
class MyTreeTags(TagTreeModel):
|
||||||
|
class TagMeta:
|
||||||
|
initial = "colors/blue, colors/red, colors/green"
|
||||||
|
force_lowercase = True
|
||||||
|
# autocomplete_view = 'myapp.views.hobbies_autocomplete'
|
||||||
|
|
||||||
|
|
||||||
class Product(models.Model):
|
class Product(models.Model):
|
||||||
|
|
||||||
SYNCHRONIZED = 'SYNCHRONIZED'
|
SYNCHRONIZED = 'SYNCHRONIZED'
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from rest_framework import serializers
|
from rest_framework import serializers
|
||||||
|
from tagulous.serializers.json import Serializer
|
||||||
|
|
||||||
from products.models import Product
|
from products.models import Product
|
||||||
|
|
||||||
|
|||||||
@@ -80,6 +80,88 @@ class ProductViewSetTest(APITestCase):
|
|||||||
# Assert access is forbidden
|
# Assert access is forbidden
|
||||||
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
self.assertEqual(response.status_code, status.HTTP_200_OK)
|
||||||
|
|
||||||
|
def test_anon_user_can_filter_name(self):
|
||||||
|
# create instances
|
||||||
|
self.factory(name='product1', tags="zapatos, verdes")
|
||||||
|
self.factory(name='product2', tags="rojos")
|
||||||
|
|
||||||
|
url = f"{self.endpoint}?name=product1"
|
||||||
|
|
||||||
|
# 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(1, len(payload))
|
||||||
|
|
||||||
|
def test_anon_user_can_filter_tags(self):
|
||||||
|
# create instances
|
||||||
|
expected_instance = [
|
||||||
|
self.factory(name='product1', tags="zapatos, rojos"),
|
||||||
|
self.factory(name='product2', tags="rojos")
|
||||||
|
]
|
||||||
|
unexpected_instance = [
|
||||||
|
self.factory(name='sadfdsa', tags="zapatos, azules"),
|
||||||
|
self.factory(name='qwerw', tags="xxl")
|
||||||
|
]
|
||||||
|
# prepare url
|
||||||
|
url = f"{self.endpoint}?tags=rojos"
|
||||||
|
|
||||||
|
# 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_filter_attributes(self):
|
||||||
|
# create instances
|
||||||
|
expected_instance = [
|
||||||
|
self.factory(attributes='xxl', tags="zapatos, rojos"),
|
||||||
|
self.factory(attributes='blue, xxl', tags="rojos")
|
||||||
|
]
|
||||||
|
unexpected_instance = [
|
||||||
|
self.factory(name='sadfdsa', tags="zapatos, azules"),
|
||||||
|
self.factory(name='qwerw', tags="xxl")
|
||||||
|
]
|
||||||
|
# prepare url
|
||||||
|
url = f"{self.endpoint}?attributes=xxl"
|
||||||
|
|
||||||
|
# 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_filter_category(self):
|
||||||
|
# create instances
|
||||||
|
expected_instance = [
|
||||||
|
self.factory(category='ropa', tags="zapatos, rojos"),
|
||||||
|
self.factory(category='ropa', tags="rojos")
|
||||||
|
]
|
||||||
|
unexpected_instance = [
|
||||||
|
self.factory(category='roperos', tags="zapatos, azules"),
|
||||||
|
self.factory(category='enropados', tags="xxl")
|
||||||
|
]
|
||||||
|
# prepare url
|
||||||
|
url = f"{self.endpoint}?category=ropa"
|
||||||
|
|
||||||
|
# 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))
|
||||||
|
|
||||||
# authenticated user
|
# authenticated user
|
||||||
def test_auth_user_can_list_instances(self):
|
def test_auth_user_can_list_instances(self):
|
||||||
"""Regular logged-in user can list instance
|
"""Regular logged-in user can list instance
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ from history.models import HistorySync
|
|||||||
from back_latienda.permissions import IsCreator
|
from back_latienda.permissions import IsCreator
|
||||||
from .utils import extract_search_filters
|
from .utils import extract_search_filters
|
||||||
from utils.tag_serializers import TaggitSerializer
|
from utils.tag_serializers import TaggitSerializer
|
||||||
|
from utils.tag_filters import ProductTagFilter
|
||||||
|
|
||||||
|
|
||||||
logging.basicConfig(
|
logging.basicConfig(
|
||||||
filename='logs/product-load.log',
|
filename='logs/product-load.log',
|
||||||
@@ -38,6 +40,8 @@ class ProductViewSet(viewsets.ModelViewSet):
|
|||||||
queryset = Product.objects.all()
|
queryset = Product.objects.all()
|
||||||
serializer_class = ProductSerializer
|
serializer_class = ProductSerializer
|
||||||
permission_classes = [IsAuthenticatedOrReadOnly, IsCreator]
|
permission_classes = [IsAuthenticatedOrReadOnly, IsCreator]
|
||||||
|
filterset_class = ProductTagFilter
|
||||||
|
filterset_fields = ['name', 'tags', 'category', 'attributes']
|
||||||
|
|
||||||
def perform_create(self, serializer):
|
def perform_create(self, serializer):
|
||||||
serializer.save(creator=self.request.user)
|
serializer.save(creator=self.request.user)
|
||||||
|
|||||||
18
utils/tag_filters.py
Normal file
18
utils/tag_filters.py
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
import django_filters
|
||||||
|
from products.models import Product
|
||||||
|
|
||||||
|
|
||||||
|
class ProductTagFilter(django_filters.FilterSet):
|
||||||
|
|
||||||
|
tags = django_filters.CharFilter(method='tag_filter')
|
||||||
|
attributes = django_filters.CharFilter(method='tag_filter')
|
||||||
|
category = django_filters.CharFilter(method='tag_filter')
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
model = Product
|
||||||
|
fields = ['name', 'tags', 'category', 'attributes']
|
||||||
|
|
||||||
|
def tag_filter(self, queryset, name, value):
|
||||||
|
return queryset.filter(**{
|
||||||
|
name: value,
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user