first custom tag seriliazer tests

This commit is contained in:
Sam
2021-01-25 11:52:36 +00:00
parent fc0478299b
commit b2c857878b
7 changed files with 61 additions and 9 deletions

View File

@@ -16,7 +16,7 @@ This README aims to document functionality of backend as well as required steps
From inside the project's folder: From inside the project's folder:
- Make migrations: - Make migrations:
``` ```
python manage.py makemigrations core companies products history stats python manage.py makemigrations core geo companies products history stats
python migrate python migrate
``` ```
- Start server in development mode: `python manage.py runserver` - Start server in development mode: `python manage.py runserver`

View File

@@ -44,6 +44,7 @@ INSTALLED_APPS = [
'rest_framework', 'rest_framework',
'django_filters', 'django_filters',
'corsheaders', 'corsheaders',
'taggit_serializer',
# local apps # local apps
'core', 'core',

View File

@@ -1,5 +1,7 @@
from django.contrib.gis.db import models from django.contrib.gis.db import models
from tagulous.models import SingleTagField, TagField
from companies.models import Company from companies.models import Company
# Create your models here. # 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) 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) 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)
# tags = models.ManyToMany(Tag, null=True, blank=True ) tags = TagField( )
# category = models.ForeignKey(Tag, null=true) # main tag category category = SingleTagField() # main tag category
# attributes = models.ManyToMany(Tag, null=True, blank=True ) attributes = TagField()
identifiers = models.TextField('Identificador único de producto', null=True, blank=True) identifiers = models.TextField('Identificador único de producto', null=True, blank=True)
# internal # internal

View File

@@ -1,8 +1,17 @@
from rest_framework import serializers from rest_framework import serializers
from taggit_serializer.serializers import TagListSerializerField, TaggitSerializer
from products.models import Product from products.models import Product
from utils.tag_serializers import SingleTagSerializerField
class ProductSerializer(serializers.ModelSerializer):
class Meta: class ProductSerializer(TaggitSerializer, serializers.ModelSerializer):
model = Product
exclude = ['created', 'updated', 'creator'] tags = TagListSerializerField( )
category = SingleTagSerializerField() # main tag category
attributes = TagListSerializerField()
class Meta:
model = Product
exclude = ['created', 'updated', 'creator']

View File

@@ -6,3 +6,4 @@ django-dotenv==1.4.2
django-filter==2.4.0 django-filter==2.4.0
-e git://github.com/darklow/django-suit/@v2#egg=django-suit -e git://github.com/darklow/django-suit/@v2#egg=django-suit
django-cors-headers==3.5.0 django-cors-headers==3.5.0
django-taggit-serializer==0.1.7

0
utils/__init__.py Normal file
View File

39
utils/tag_serializers.py Normal file
View File

@@ -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