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", "from_comment") read_only_fields = ("owner", "created_date", "modified_date", "sha1")
class AttachmentSerializer(serializers.ModelSerializer): url = serializers.SerializerMethodField("get_url") thumbnail_card_url = serializers.SerializerMethodField( "get_thumbnail_card_url") attached_file = serializers.FileField(required=True) is_video = serializers.SerializerMethodField("get_is_video") markers = serializers.SerializerMethodField("get_markers") 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", "is_video", "markers") read_only_fields = ( "owner", "created_date", "modified_date", "sha1", ) def get_url(self, obj): return obj.attached_file.url def get_thumbnail_card_url(self, obj): return services.get_card_image_thumbnailer_url(obj) def get_is_video(self, obj): if '.mp4' in obj.attached_file.url: return True else: return False def get_sources(self, obj): if self.get_is_video(obj): sources = dict(src=obj.attached_file.url, type='video/mp4') return dict(sources=[ sources, ]) return False def get_markers(self, obj): if self.get_is_video(obj): qs = obj.markers.all() from productships.serializers import MarkerSerializer qs = MarkerSerializer(qs, many=True) return qs.data # return [ # {'time': 9.5, 'text': "this"}, # {'time': 16, 'text': "is"}, # {'time': 23.6, 'text': "so"}, # {'time': 28, 'text': "cool"} # ] return []
class AttachmentSerializer(serializers.ModelSerializer): url = serializers.SerializerMethodField("get_url") attached_file = serializers.FileField(required=True) class Meta: model = models.Attachment fields = ("id", "project", "owner", "name", "attached_file", "size", "url", "description", "is_deprecated", "created_date", "modified_date", "object_id", "order") read_only_fields = ("owner", "created_date", "modified_date") def get_url(self, obj): return obj.attached_file.url