From 04102e3b9f417cc2d06e33c065d1166054d1145a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Espino?= Date: Tue, 5 Jul 2016 23:25:42 +0200 Subject: [PATCH] Migrating attachments serializers --- taiga/base/fields.py | 17 +++++++++--- taiga/projects/attachments/api.py | 2 ++ taiga/projects/attachments/serializers.py | 31 ++++++++++++--------- taiga/projects/attachments/validators.py | 33 +++++++++++++++++++++++ 4 files changed, 67 insertions(+), 16 deletions(-) create mode 100644 taiga/projects/attachments/validators.py diff --git a/taiga/base/fields.py b/taiga/base/fields.py index f0cf4ee2..30be6b60 100644 --- a/taiga/base/fields.py +++ b/taiga/base/fields.py @@ -22,9 +22,11 @@ from taiga.base.api import serializers import serpy + #################################################################### -# Serializer fields +# DRF Serializer fields (OLD) #################################################################### +# NOTE: This should be in other place, for example taiga.base.api.serializers class JsonField(serializers.WritableField): @@ -74,6 +76,10 @@ class WatchersField(serializers.WritableField): return data +#################################################################### +# Serpy fields (NEW) +#################################################################### + class Field(serpy.Field): pass @@ -82,13 +88,13 @@ class MethodField(serpy.MethodField): pass -class I18NField(serpy.Field): +class I18NField(Field): def to_value(self, value): ret = super(I18NField, self).to_value(value) return _(ret) -class I18NJsonField(serpy.Field): +class I18NJsonField(Field): """ Json objects serializer. """ @@ -118,3 +124,8 @@ class I18NJsonField(serpy.Field): def to_native(self, obj): i18n_obj = self.translate_values(obj) return i18n_obj + + +class FileField(Field): + def to_value(self, value): + return value.name diff --git a/taiga/projects/attachments/api.py b/taiga/projects/attachments/api.py index f7b223e2..27f7ebe1 100644 --- a/taiga/projects/attachments/api.py +++ b/taiga/projects/attachments/api.py @@ -34,6 +34,7 @@ from taiga.projects.history.mixins import HistoryResourceMixin from . import permissions from . import serializers +from . import validators from . import models @@ -42,6 +43,7 @@ class BaseAttachmentViewSet(HistoryResourceMixin, WatchedResourceMixin, model = models.Attachment serializer_class = serializers.AttachmentSerializer + validator_class = validators.AttachmentValidator filter_fields = ["project", "object_id"] content_type = None diff --git a/taiga/projects/attachments/serializers.py b/taiga/projects/attachments/serializers.py index 45e6be45..ce8893b7 100644 --- a/taiga/projects/attachments/serializers.py +++ b/taiga/projects/attachments/serializers.py @@ -19,24 +19,29 @@ from django.conf import settings from taiga.base.api import serializers -from taiga.base.fields import MethodField +from taiga.base.fields import MethodField, Field, FileField from taiga.base.utils.thumbnails import get_thumbnail_url from . import services -from . import models -class AttachmentSerializer(serializers.ModelSerializer): - url = serializers.SerializerMethodField("get_url") - thumbnail_card_url = serializers.SerializerMethodField("get_thumbnail_card_url") - attached_file = serializers.FileField(required=True) - - class Meta: - model = models.Attachment - fields = ("id", "project", "owner", "name", "attached_file", "size", - "url", "thumbnail_card_url", "description", "is_deprecated", - "created_date", "modified_date", "object_id", "order", "sha1") - read_only_fields = ("owner", "created_date", "modified_date", "sha1") +class AttachmentSerializer(serializers.LightSerializer): + id = Field() + project = Field(attr="project_id") + owner = Field(attr="owner_id") + name = Field() + attached_file = FileField() + size = Field() + url = Field() + description = Field() + is_deprecated = Field() + created_date = Field() + modified_date = Field() + object_id = Field() + order = Field() + sha1 = Field() + url = MethodField("get_url") + thumbnail_card_url = MethodField("get_thumbnail_card_url") def get_url(self, obj): return obj.attached_file.url diff --git a/taiga/projects/attachments/validators.py b/taiga/projects/attachments/validators.py new file mode 100644 index 00000000..72355ce4 --- /dev/null +++ b/taiga/projects/attachments/validators.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# Copyright (C) 2014-2016 Andrey Antukh +# Copyright (C) 2014-2016 Jesús Espino +# Copyright (C) 2014-2016 David Barragán +# Copyright (C) 2014-2016 Alejandro Alonso +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU Affero General Public License as +# published by the Free Software Foundation, either version 3 of the +# License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public License +# along with this program. If not, see . + +from taiga.base.api import serializers +from taiga.base.api import validators + +from . import models + + +class AttachmentValidator(validators.ModelValidator): + attached_file = serializers.FileField(required=True) + + class Meta: + model = models.Attachment + fields = ("id", "project", "owner", "name", "attached_file", "size", + "description", "is_deprecated", "created_date", + "modified_date", "object_id", "order", "sha1") + read_only_fields = ("owner", "created_date", "modified_date", "sha1")