Migrating attachments serializers

remotes/origin/issue/4795/notification_even_they_are_disabled
Jesús Espino 2016-07-05 23:25:42 +02:00 committed by David Barragán Merino
parent 4f5a4f1314
commit 04102e3b9f
4 changed files with 67 additions and 16 deletions

View File

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

View File

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

View File

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

View File

@ -0,0 +1,33 @@
# -*- coding: utf-8 -*-
# Copyright (C) 2014-2016 Andrey Antukh <niwi@niwi.nz>
# Copyright (C) 2014-2016 Jesús Espino <jespinog@gmail.com>
# Copyright (C) 2014-2016 David Barragán <bameda@dbarragan.com>
# Copyright (C) 2014-2016 Alejandro Alonso <alejandro.alonso@kaleidos.net>
# 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 <http://www.gnu.org/licenses/>.
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")