From b2c857878b8d69465ebb24303b8b7f58e46cee40 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 25 Jan 2021 11:52:36 +0000 Subject: [PATCH] first custom tag seriliazer tests --- README.md | 2 +- back_latienda/settings/base.py | 1 + products/models.py | 8 ++++--- products/serializers.py | 17 +++++++++++---- requirements.txt | 3 ++- utils/__init__.py | 0 utils/tag_serializers.py | 39 ++++++++++++++++++++++++++++++++++ 7 files changed, 61 insertions(+), 9 deletions(-) create mode 100644 utils/__init__.py create mode 100644 utils/tag_serializers.py diff --git a/README.md b/README.md index 9325056..d41beae 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ This README aims to document functionality of backend as well as required steps From inside the project's folder: - Make migrations: ``` -python manage.py makemigrations core companies products history stats +python manage.py makemigrations core geo companies products history stats python migrate ``` - Start server in development mode: `python manage.py runserver` diff --git a/back_latienda/settings/base.py b/back_latienda/settings/base.py index add8b2b..ff94f1e 100644 --- a/back_latienda/settings/base.py +++ b/back_latienda/settings/base.py @@ -44,6 +44,7 @@ INSTALLED_APPS = [ 'rest_framework', 'django_filters', 'corsheaders', + 'taggit_serializer', # local apps 'core', diff --git a/products/models.py b/products/models.py index 0e5bdf3..8afd717 100644 --- a/products/models.py +++ b/products/models.py @@ -1,5 +1,7 @@ from django.contrib.gis.db import models +from tagulous.models import SingleTagField, TagField + from companies.models import Company # Create your models here. @@ -30,9 +32,9 @@ class Product(models.Model): 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) - # tags = models.ManyToMany(Tag, null=True, blank=True ) - # category = models.ForeignKey(Tag, null=true) # main tag category - # attributes = models.ManyToMany(Tag, null=True, blank=True ) + tags = TagField( ) + category = SingleTagField() # main tag category + attributes = TagField() identifiers = models.TextField('Identificador único de producto', null=True, blank=True) # internal diff --git a/products/serializers.py b/products/serializers.py index 0572cea..d39b689 100644 --- a/products/serializers.py +++ b/products/serializers.py @@ -1,8 +1,17 @@ from rest_framework import serializers + +from taggit_serializer.serializers import TagListSerializerField, TaggitSerializer from products.models import Product +from utils.tag_serializers import SingleTagSerializerField -class ProductSerializer(serializers.ModelSerializer): - class Meta: - model = Product - exclude = ['created', 'updated', 'creator'] + +class ProductSerializer(TaggitSerializer, serializers.ModelSerializer): + + tags = TagListSerializerField( ) + category = SingleTagSerializerField() # main tag category + attributes = TagListSerializerField() + + class Meta: + model = Product + exclude = ['created', 'updated', 'creator'] diff --git a/requirements.txt b/requirements.txt index d7e94a0..c2f2832 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,4 +5,5 @@ factory-boy==3.1.0 django-dotenv==1.4.2 django-filter==2.4.0 -e git://github.com/darklow/django-suit/@v2#egg=django-suit -django-cors-headers==3.5.0 \ No newline at end of file +django-cors-headers==3.5.0 +django-taggit-serializer==0.1.7 \ No newline at end of file diff --git a/utils/__init__.py b/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/utils/tag_serializers.py b/utils/tag_serializers.py new file mode 100644 index 0000000..b5893a6 --- /dev/null +++ b/utils/tag_serializers.py @@ -0,0 +1,39 @@ +from rest_framework import serializers + + + +class SingleTag(str): + + def __init__(self, *args, **kwargs): + pass + + def __str__(self): + return self + + +class SingleTagSerializerField(serializers.Field): + child = serializers.CharField() + default_error_messages = { + 'not_a_str': 'Expected a string but got type "{input_type}".', + } + order_by = None + + def __init__(self, **kwargs): + super(SingleTagSerializerField, self).__init__(**kwargs) + + def to_internal_value(self, value): + if isinstance(value, str): + if not value: + value = "" + + if not isinstance(value, str): + self.fail('not_a_str', input_type=type(value).__name__) + + return value + + def to_representation(self, value): + if not isinstance(value, SingleTag): + if not isinstance(value, str): + value = value.name + value = SingleTag(value) + return value