Migrating attachments serializers
parent
4f5a4f1314
commit
04102e3b9f
|
@ -22,9 +22,11 @@ from taiga.base.api import serializers
|
||||||
|
|
||||||
import serpy
|
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):
|
class JsonField(serializers.WritableField):
|
||||||
|
@ -74,6 +76,10 @@ class WatchersField(serializers.WritableField):
|
||||||
return data
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
####################################################################
|
||||||
|
# Serpy fields (NEW)
|
||||||
|
####################################################################
|
||||||
|
|
||||||
class Field(serpy.Field):
|
class Field(serpy.Field):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -82,13 +88,13 @@ class MethodField(serpy.MethodField):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
class I18NField(serpy.Field):
|
class I18NField(Field):
|
||||||
def to_value(self, value):
|
def to_value(self, value):
|
||||||
ret = super(I18NField, self).to_value(value)
|
ret = super(I18NField, self).to_value(value)
|
||||||
return _(ret)
|
return _(ret)
|
||||||
|
|
||||||
|
|
||||||
class I18NJsonField(serpy.Field):
|
class I18NJsonField(Field):
|
||||||
"""
|
"""
|
||||||
Json objects serializer.
|
Json objects serializer.
|
||||||
"""
|
"""
|
||||||
|
@ -118,3 +124,8 @@ class I18NJsonField(serpy.Field):
|
||||||
def to_native(self, obj):
|
def to_native(self, obj):
|
||||||
i18n_obj = self.translate_values(obj)
|
i18n_obj = self.translate_values(obj)
|
||||||
return i18n_obj
|
return i18n_obj
|
||||||
|
|
||||||
|
|
||||||
|
class FileField(Field):
|
||||||
|
def to_value(self, value):
|
||||||
|
return value.name
|
||||||
|
|
|
@ -34,6 +34,7 @@ from taiga.projects.history.mixins import HistoryResourceMixin
|
||||||
|
|
||||||
from . import permissions
|
from . import permissions
|
||||||
from . import serializers
|
from . import serializers
|
||||||
|
from . import validators
|
||||||
from . import models
|
from . import models
|
||||||
|
|
||||||
|
|
||||||
|
@ -42,6 +43,7 @@ class BaseAttachmentViewSet(HistoryResourceMixin, WatchedResourceMixin,
|
||||||
|
|
||||||
model = models.Attachment
|
model = models.Attachment
|
||||||
serializer_class = serializers.AttachmentSerializer
|
serializer_class = serializers.AttachmentSerializer
|
||||||
|
validator_class = validators.AttachmentValidator
|
||||||
filter_fields = ["project", "object_id"]
|
filter_fields = ["project", "object_id"]
|
||||||
|
|
||||||
content_type = None
|
content_type = None
|
||||||
|
|
|
@ -19,24 +19,29 @@
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
|
|
||||||
from taiga.base.api import serializers
|
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 taiga.base.utils.thumbnails import get_thumbnail_url
|
||||||
|
|
||||||
from . import services
|
from . import services
|
||||||
from . import models
|
|
||||||
|
|
||||||
|
|
||||||
class AttachmentSerializer(serializers.ModelSerializer):
|
class AttachmentSerializer(serializers.LightSerializer):
|
||||||
url = serializers.SerializerMethodField("get_url")
|
id = Field()
|
||||||
thumbnail_card_url = serializers.SerializerMethodField("get_thumbnail_card_url")
|
project = Field(attr="project_id")
|
||||||
attached_file = serializers.FileField(required=True)
|
owner = Field(attr="owner_id")
|
||||||
|
name = Field()
|
||||||
class Meta:
|
attached_file = FileField()
|
||||||
model = models.Attachment
|
size = Field()
|
||||||
fields = ("id", "project", "owner", "name", "attached_file", "size",
|
url = Field()
|
||||||
"url", "thumbnail_card_url", "description", "is_deprecated",
|
description = Field()
|
||||||
"created_date", "modified_date", "object_id", "order", "sha1")
|
is_deprecated = Field()
|
||||||
read_only_fields = ("owner", "created_date", "modified_date", "sha1")
|
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):
|
def get_url(self, obj):
|
||||||
return obj.attached_file.url
|
return obj.attached_file.url
|
||||||
|
|
|
@ -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")
|
Loading…
Reference in New Issue