예제 #1
0
class EmailDteInputSerializer(ModelSerializer):
    """
    Serializador del input de correos desde el DTE.
    Agregando el serializador de empresa anida la relación
    en el json de listado del modelo emails, pero en los
    parámetros rest hay que enviarlos.
    """
    xml = FileField(use_url=False, max_length=None, allow_null=True,
                    allow_empty_file=True, required=False)
    pdf = FileField(use_url=False, max_length=None, allow_null=True,
                    allow_empty_file=True, required=False)
    adjunto1 = FileField(use_url=False, max_length=None, allow_null=True,
                         allow_empty_file=True, required=False)
    opcional1 = CharField(allow_null=True, required=False)
    opcional2 = CharField(allow_null=True, required=False)
    opcional3 = CharField(allow_null=True, required=False)

    class Meta:
        model = Email
        fields = (
            'id', 'input_date', 'empresa', 'rut_receptor', 'rut_emisor',
            'tipo_envio', 'tipo_dte', 'numero_folio', 'resolucion_receptor',
            'resolucion_emisor', 'monto', 'fecha_emision', 'fecha_recepcion',
            'estado_documento', 'id_envio', 'tipo_operacion', 'tipo_receptor',
            'nombre_cliente', 'correo', 'asunto', 'html', 'xml', 'pdf',
            'adjunto1', 'opcional1', 'opcional2', 'opcional3'
        )
예제 #2
0
class StorySerializer(BaseSerializer):
    class Meta:
        model = Story
        name = 'story'
        plural_name = 'stories'
        fields = (
            'character',
            'creator',
            'created_at',
            'cover_image',
            'duration',
            'id',
            'recording',
            'recordings',
            'scenes',
            'scene_durations',
            'title',
        )

    recording = FileField()
    recordings = DynamicRelationField(
        'bbook_backend.api.serializers.SceneRecordingSerializer',
        many=True,
    )
    scenes = DynamicRelationField(
        'bbook_backend.api.serializers.SceneSerializer',
        many=True,
    )
    character = DynamicRelationField(
        'bbook_backend.api.serializers.CharacterSerializer',
        required=True,
    )
    cover_image = DynamicMethodField(requires=['recordings.scene.', 'scenes.'])
    duration = DynamicMethodField(requires=['recordings.'])
    title = FileField(required=False)

    def get_cover_image(self, instance):
        image = None
        try:
            if instance.scene_durations is not None:
                scene_id = instance.scene_durations[0]['scene']
                # take advantage of prefetching
                image = next(scene.image for scene in instance.scenes.all()
                             if scene.id == scene_id)
            else:
                image = min(instance.recordings.all(),
                            key=lambda rec: rec.order).scene.image
        except Exception as e:
            print('get_cover_image failed', e)

        return FileField().to_representation(image)

    def get_duration(self, instance):
        try:
            if instance.recording is not None:
                return sum([r['duration'] for r in instance.scene_durations])
            return sum([r.duration for r in instance.recordings.all()])
        except Exception as e:
            print('get_duration failed', e)
예제 #3
0
class UploadMediaSerializer(Serializer):
    video = FileField(allow_empty_file=True,
                      error_messages={'required': 'video key is required'})
    audio = FileField(allow_empty_file=True,
                      error_messages={'required': 'audio key is required'})
    thumb = FileField(allow_empty_file=True,
                      error_messages={'required': 'thumb key is required'})
    image = FileField(allow_empty_file=True,
                      error_messages={'required': 'image key is required'})
예제 #4
0
        class TestDataAdminInstanceSerializer(ModelSerializer):
            test_in = CharField(allow_null=True,
                                max_length=_INPUT_MAX,
                                source='get_test_in',
                                style={'base_template': 'textarea.html'})
            test_out = CharField(allow_null=True,
                                 max_length=_INPUT_MAX,
                                 source='get_test_out',
                                 style={'base_template': 'textarea.html'})
            test_in_file = FileField(write_only=True,
                                     allow_null=True,
                                     required=False)
            test_out_file = FileField(write_only=True,
                                      allow_null=True,
                                      required=False)

            class Meta:
                model = TestData
                fields = '__all__'
                read_only_fields = _RESOURCE_READONLY + (
                    'number_problem', 'meta_problem', 'in_size', 'out_size')

            def update(self, instance, validated_data):
                if 'get_test_in' in validated_data or 'test_in_file' in validated_data:
                    test_in = validated_data.pop('get_test_in', None)
                    test_in_file = validated_data.pop('test_in_file', None)
                    if test_in_file is not None:
                        test_in = b''
                        for b in test_in_file.chunks():
                            test_in += b
                        in_size = test_in_file.size
                    elif test_in is not None:
                        test_in = test_in.encode('utf-8')
                        in_size = len(test_in)
                    else:
                        test_in = None
                        in_size = 0
                    validated_data['test_in'] = test_in
                    validated_data['in_size'] = in_size
                if 'get_test_out' in validated_data or 'test_out_file' in validated_data:
                    test_out = validated_data.pop('get_test_out', None)
                    test_out_file = validated_data.pop('test_out_file', None)
                    if test_out_file is not None:
                        test_out = b''
                        for b in test_out_file.chunks():
                            test_out += b
                        out_size = test_out_file.size
                    elif test_out is not None:
                        test_out = test_out.encode('utf-8')
                        out_size = len(test_out)
                    else:
                        test_out = None
                        out_size = 0
                    validated_data['test_out'] = test_out
                    validated_data['out_size'] = out_size

                return super().update(instance, validated_data)
예제 #5
0
class UploadSerializer(Serializer):
    """serialzer to upload files"""

    file_uploaded = FileField()

    class Meta:
        fields = ["file_uploaded"]
예제 #6
0
class StaticAssetSerializer(ModelSerializer):
    """Serializer for StaticAsset."""

    asset = FileField(use_url=True)
    name = SerializerMethodField()

    class Meta:
        # pylint: disable=missing-docstring
        model = StaticAsset
        fields = (
            'id',
            'asset',
            'name',
        )
        read_only_fields = (
            'id',
            'asset',
            'name',
        )

    @staticmethod
    def get_name(static_asset_obj):
        """Method to get the name of the asset."""
        basepath = STATIC_ASSET_BASEPATH.format(
            org=static_asset_obj.course.org,
            course_number=static_asset_obj.course.course_number,
            run=static_asset_obj.course.run,
        )
        return static_asset_obj.asset.name.replace(basepath, '')
예제 #7
0
    def __init__(self, *args, **kwargs):
        super(MatlabSerializer, self).__init__(*args, **kwargs)

        request = kwargs['context']['request']
        no_of_dependency = request.POST.get('no_of_dependency', False)

        if no_of_dependency:
            for i in range(int(no_of_dependency)):
                self.fields['dependency_' + str(i)] = FileField()
예제 #8
0
class FileContentSerializer(hms):
    file = FileField(use_url=True)

    class Meta:
        model = FileContent
        fields = (
            'file',
            'caption',
        )
예제 #9
0
class MissionVideoSerializer(ModelSerializer):
    mission_file_url = SerializerMethodField()
    mission_file = FileField(write_only=True)

    class Meta:
        model = MissionVideo
        fields = ('id', 'width', 'height', 'mission_file', 'fps', 'mime_type', 'mission_file_url')
        read_only_fields = ('width', 'height', 'fps', 'mime_type')

    def get_mission_file_url(self, obj):
        return f'''{self.context.get("request").scheme}://{self.context.get("request").META.get(
예제 #10
0
class UploadFileSerializer(ModelSerializer):
    files = ListField(child=FileField(
        max_length=100000, allow_empty_file=False, use_url=False))

    class Meta:
        model = UploadFileModel
        fields = [
            'files',
            'encoding',
            'test_choice',
        ]
class BulkUploadCsvSerializer(Serializer):
    file = FileField(required=True)

    def validate(self, attrs):
        if 'file' in attrs:
            if attrs['file'].name.endswith('.csv'):
                # valid
                return attrs

            # the file should be csv
            raise ValidationError({'file': 'file extension should be .csv'})

        # the file is missing
        raise ValidationError({'file': 'file is required'})
예제 #12
0
class EmailTrackRelatedSerializer(ModelSerializer):
    """ Serializa los correos para poder ser desplegados en la tabla
        del menú customsearch o detalle de email.
    """
    tipo_dte = TipoDocumentoSerializer(many=False, read_only=True)
    empresa = EmpresaSerializer(many=False, read_only=True)
    xml = FileField(use_url=False, max_length=None, allow_null=True,
                    allow_empty_file=True, required=False)
    pdf = FileField(use_url=False, max_length=None, allow_null=True,
                    allow_empty_file=True, required=False)
    adjunto1 = FileField(use_url=False, max_length=None, allow_null=True,
                         allow_empty_file=True, required=False)
    resolucion_receptor = IntegerField(allow_null=True, required=False)
    fecha_recepcion = IntegerField(allow_null=True, required=False)
    opcional1 = CharField(allow_null=True, required=False)
    opcional2 = CharField(allow_null=True, required=False)
    opcional3 = CharField(allow_null=True, required=False)

    class Meta:
        model = Email
        fields = (
            'id', 'input_date', 'empresa', 'rut_receptor', 'rut_emisor',
            'tipo_envio', 'tipo_dte', 'numero_folio', 'resolucion_receptor',
            'resolucion_emisor', 'monto', 'fecha_emision', 'fecha_recepcion',
            'estado_documento', 'id_envio', 'tipo_operacion', 'tipo_receptor',
            'nombre_cliente', 'correo', 'asunto', 'xml', 'pdf', 'adjunto1',
            'opcional1', 'opcional2', 'opcional3',
            'processed_date', 'processed_event',
            'delivered_date', 'delivered_event', 'delivered_response',
            'opened_first_date', 'opened_last_date', 'opened_event',
            'opened_ip', 'opened_user_agent', 'opened_count',
            'dropped_date', 'dropped_reason', 'dropped_event',
            'bounce_date', 'bounce_event', 'bounce_reason', 'bounce_status', 'bounce_type',
            'unsubscribe_date', 'unsubscribe_purchase', 'unsubscribe_event',
            'click_ip', 'click_purchase', 'click_useragent', 'click_event',
            'click_email', 'click_date', 'click_url',
        )
예제 #13
0
class SubmissionFileSerializer(DynamicModelSerializer):
    filename = CharField(read_only=True)
    url = URLField(source='file_url', read_only=True)
    file = FileField(max_length=None, allow_empty_file=False, write_only=True)

    # TODO: make submission a non-required field (for detail put)
    # but add a check in create that requires it (for post)

    # TODO: override update() to auto set the updated date
    # (actually should have it call the manager's update method which
    # does the 'auto-setting')

    # TODO: need to ensure unique on owner/submission/filename somehow
    # possibly store filename in db and make unique constraint with it

    class Meta:
        model = SubmissionFile
        fields = (
            'id',
            'owner',
            'filename',
            'url',
            'submission',
            'created_date',
            'updated_date',
            'file'
        )
        read_only_fields = (
            'owner',
            'filename',
            'url',
            'created_date',
            'updated_date'
        )

    def create(self, validated_data):
        # User validation handled by DRY permissions
        user = self.context['request'].user

        # submission = Submission.objects.get(id=submission_id)
        submission = validated_data.pop('submission')
        if user != submission.submitter:
            raise PermissionDenied('You do not own this submission')

        return SubmissionFile.objects.create(
            validated_data,
            owner=user,
            submission=submission
        )
class ContentUploadSerializer(Serializer):
    """
    A serializer class.

    Containing three fields: for file, for link, and for email.
    """

    file_upload = FileField()
    url_upload = URLField()
    email_upload = EmailField()

    class Meta:
        """Meta data."""

        fields = ['file_uploaded', 'url_upload', 'email_upload']
예제 #15
0
    def get_cover_image(self, instance):
        image = None
        try:
            if instance.scene_durations is not None:
                scene_id = instance.scene_durations[0]['scene']
                # take advantage of prefetching
                image = next(scene.image for scene in instance.scenes.all()
                             if scene.id == scene_id)
            else:
                image = min(instance.recordings.all(),
                            key=lambda rec: rec.order).scene.image
        except Exception as e:
            print('get_cover_image failed', e)

        return FileField().to_representation(image)
예제 #16
0
class EmailTrackDTESerializer(ModelSerializer):
    """
    Serializador del objeto de respuesta a las consultas de la traza
    de los correos realizadas desde el DTE.
    """
    xml = FileField(use_url=False, max_length=None, allow_null=True,
                    allow_empty_file=True, required=False)
    pdf = FileField(use_url=False, max_length=None, allow_null=True,
                    allow_empty_file=True, required=False)
    adjunto1 = FileField(use_url=False, max_length=None, allow_null=True,
                         allow_empty_file=True, required=False)
    resolucion_receptor = IntegerField(allow_null=True, required=False)
    fecha_recepcion = IntegerField(allow_null=True, required=False)
    opcional1 = CharField(allow_null=True, required=False)
    opcional2 = CharField(allow_null=True, required=False)
    opcional3 = CharField(allow_null=True, required=False)

    class Meta:
        model = Email
        fields = (
            'id', 'input_date', 'empresa', 'rut_receptor', 'rut_emisor',
            'tipo_envio', 'tipo_dte', 'numero_folio', 'resolucion_receptor',
            'resolucion_emisor', 'monto', 'fecha_emision', 'fecha_recepcion',
            'estado_documento', 'id_envio', 'tipo_operacion', 'tipo_receptor',
            'nombre_cliente', 'correo', 'asunto', 'xml', 'pdf', 'adjunto1',
            'opcional1', 'opcional2', 'opcional3',
            'processed_date', 'processed_event',
            'delivered_date', 'delivered_event', 'delivered_response',
            'opened_first_date', 'opened_last_date', 'opened_event',
            'opened_ip', 'opened_user_agent', 'opened_count',
            'dropped_date', 'dropped_reason', 'dropped_event',
            'bounce_date', 'bounce_event', 'bounce_reason', 'bounce_status', 'bounce_type',
            'unsubscribe_date', 'unsubscribe_purchase', 'unsubscribe_event',
            'click_ip', 'click_purchase', 'click_useragent', 'click_event',
            'click_email', 'click_date', 'click_url',
        )
예제 #17
0
class CharacterSerializer(BaseSerializer):
    class Meta:
        model = Character
        name = 'character'
        fields = (
            'creator',
            'created_at',
            'id',
            'image',
            'name',
        )

    creator = DynamicRelationField(
        'bbook_backend.api.serializers.UserSerializer', )
    image = FileField()
예제 #18
0
class MatlabSerializer(Serializer):
    uuid = CharField(max_length=200)
    file = FileField()
    has_dependency = BooleanField()
    no_of_dependency = IntegerField()

    def __init__(self, *args, **kwargs):
        super(MatlabSerializer, self).__init__(*args, **kwargs)

        request = kwargs['context']['request']
        no_of_dependency = request.POST.get('no_of_dependency', False)

        if no_of_dependency:
            for i in range(int(no_of_dependency)):
                self.fields['dependency_' + str(i)] = FileField()
예제 #19
0
class UploadSerializerFieldsMixin(Serializer):
    """A mixin class that contains fields and methods common to content upload serializers."""

    file = FileField(
        help_text=
        _("An uploaded file that may be turned into the artifact of the content unit."
          ),
        required=False,
        write_only=True,
    )
    repository = DetailRelatedField(
        help_text=
        _("A URI of a repository the new content unit should be associated with."
          ),
        required=False,
        write_only=True,
        view_name_pattern=r"repositories(-.*/.*)-detail",
        queryset=Repository.objects.all(),
    )

    def create(self, validated_data):
        """
        Save a GenericContent unit.

        This must be used inside a task that locks on the Artifact and if given, the repository.
        """

        repository = validated_data.pop("repository", None)
        content = super().create(validated_data)

        if repository:
            repository.cast()
            content_to_add = self.Meta.model.objects.filter(pk=content.pk)

            # create new repo version with uploaded package
            with repository.new_version() as new_version:
                new_version.add_content(content_to_add)
        return content

    class Meta:
        fields = ("file", "repository")
예제 #20
0
class EdocumentSerializer(TagListSerializer,
                          NoteListSerializer,
                          DynamicFieldsModelSerializer):
    download_link = SerializerMethodField()
    edocument = FileField(write_only=True)

    def get_download_link(self, obj):
        result = '{}'.format(reverse('edoc_download',
                                     args=[obj.uuid],
                                     request=self.context['request']))
        return result

    def validate_edoc_type(self, value):
        try:
            doc_type = TypeDef.objects.get(category='file', description=value)
        except TypeDef.DoesNotExist:
            val_types = TypeDef.objects.filter(category='file')
            options = [val.description for val in val_types]
            raise ValidationError(
                f'File type {value} does not exist. Options are: {", ".join(options)}', code='invalid')
        return value

    def create(self, validated_data):
        
        validated_data['filename'] = validated_data['edocument'].name
        validated_data['edocument'] = validated_data['edocument'].read()
        doc_type = TypeDef.objects.get(category='file',
                                       description=validated_data['edoc_type'])
        validated_data['doc_type_uuid'] = doc_type
        edoc = Edocument(**validated_data)
        edoc.save()
        return edoc

    class Meta:
        model = Edocument
        fields = ('url', 'title', 'description', 'filename',
                  'source', 'edoc_type', 'download_link',
                  'actor', 'actor_description', 'tags', 'notes', 'edocument',
                  'ref_edocument_uuid')
        read_only_fields = ['ref_edocument_uuid', 'filename']
예제 #21
0
class SceneSerializer(BaseSerializer):

    class Meta:
        model = Scene
        name = 'scene'
        fields = (
            'character',
            'creator',
            'created_at',
            'id',
            'image',
        )

    character = DynamicRelationField(
        'bbook_backend.api.serializers.CharacterSerializer',
        required=True,
    )
    creator = DynamicRelationField(
        'bbook_backend.api.serializers.UserSerializer',
    )

    image = FileField()
예제 #22
0
class InvoiceUploadSerializer(Serializer):
    file = FileField()

    def __init__(self, *args, **kwargs):
        if kwargs.get('user'):
            self.user = kwargs.pop('user')
        super().__init__(*args, **kwargs)

    def validate(self, data):
        """
            check for pdf extension and file size
        """
        ext = data["file"].name.split('.')[-1]
        if ext != "pdf":
            raise ValidationError("Only pdf files are allowed")
        if data["file"].size > 1000000:  # !MB
            raise ValidationError("File size should not exceed 1 MB")
        self.file = data["file"]
        return True

    def save(self):
        return mockInvoiceParser(self.file, self.user)
예제 #23
0
class SceneRecordingSerializer(BaseSerializer):
    CREATOR_FIELD = None

    class Meta:
        model = SceneRecording
        name = 'scene_recording'
        fields = (
            'created_at',
            'duration',
            'id',
            'order',
            'recording',
            'scene',
            'story',
        )

    scene = DynamicRelationField(
        'bbook_backend.api.serializers.SceneSerializer', )
    story = DynamicRelationField(
        'bbook_backend.api.serializers.StorySerializer', )
    recording = FileField()
    order = IntegerField(required=True)
예제 #24
0
class MediaIn(BaseModelInputSerializer):
    upfile = FileField(source='file')
    token = HiddenField(default=uuid4)

    class Meta:
        model = Media
        fields = (
            'upfile',
            'token'
        )
        output_serializer_class = MediaOut

    def validate(self, attrs):
        validated = super(MediaIn, self).validate(attrs)
        file: UploadedFile = validated['file']
        filename = file.name
        validated['filename'] = filename
        validated['size'] = file.size
        file.name = str(validated['token'])
        validated['file'] = file
        guessed_mime, _ = mimetypes.guess_type(filename)
        validated['mime'] = guessed_mime if guessed_mime else 'application/octet-stream'
        return validated
예제 #25
0
class FileSerializer(ModelSerializer):
    """
    The file model serializer
    """
    file_url = FileField(max_length=None, use_url=True)
    file_id = SerializerMethodField()

    class Meta:
        """
        The file model fields
        """
        model = Files
        fields = [
            'file_id',
            'task',
            'file_url',
        ]

    def get_file_id(self, obj):
        """
        The File model id
        """
        return obj.id
예제 #26
0
 class TestSerializer(Serializer):  # pylint: disable=abstract-method
     boolean_field = BooleanField()
     char_field = CharField()
     choice_field = ChoiceField([])
     date_field = DateField()
     date_time_field = DateTimeField()
     decimal_field = DecimalField(1, 1)
     dict_field = DictField()
     duration_field = DurationField()
     email_field = EmailField()
     file_field = FileField()
     file_path_field = FilePathField('')
     float_field = FloatField()
     image_field = ImageField()
     integer_field = IntegerField()
     ip_address_field = IPAddressField()
     json_field = JSONField()
     string_list_field = ListField(child=CharField())
     int_list_field = ListField(child=IntegerField())
     int_list_list_field = ListField(
         child=ListField(
             child=IntegerField(),
         ),
     )
     multiple_choice_field = MultipleChoiceField([])
     null_boolean_field = NullBooleanField()
     regex_field = RegexField('')
     slug_field = SlugField()
     time_field = TimeField()
     url_field = URLField()
     uuid_field = UUIDField()
     nullable_char_field = CharField(allow_null=True)
     nullable_char_listfield = ListField(
         child=CharField(allow_null=True),
     )
     write_only_field = CharField(write_only=True)
예제 #27
0
class GemContentSerializer(MultipleArtifactContentSerializer):
    """
    A Serializer for GemContent.
    """

    artifact = SingleContentArtifactField(
        help_text=_("Artifact file representing the physical content"),
        required=False,
        write_only=True,
    )
    file = FileField(
        help_text=
        _("An uploaded file that should be turned into the artifact of the content unit."
          ),
        required=False,
        write_only=True,
    )
    repository = HyperlinkedRelatedField(
        help_text=
        _("A URI of a repository the new content unit should be associated with."
          ),
        required=False,
        write_only=True,
        queryset=Repository.objects.all(),
        view_name="repositories-detail",
    )
    name = CharField(help_text=_("Name of the gem"), read_only=True)
    version = CharField(help_text=_("Version of the gem"), read_only=True)

    def __init__(self, *args, **kwargs):
        """Initializer for GemContentSerializer."""
        super().__init__(*args, **kwargs)
        self.fields["artifacts"].read_only = True

    def validate(self, data):
        """Validate the GemContent data."""
        data = super().validate(data)

        if "file" in data:
            if "artifact" in data:
                raise ValidationError(
                    _("Only one of 'file' and 'artifact' may be specified."))
            data["artifact"] = Artifact.init_and_validate(data.pop("file"))
        elif "artifact" not in data:
            raise ValidationError(
                _("One of 'file' and 'artifact' must be specified."))

        if "request" not in self.context:
            data = self.deferred_validate(data)

        return data

    def deferred_validate(self, data):
        """Validate the GemContent data (deferred)."""
        artifact = data.pop("artifact")

        name, version, spec_data = analyse_gem(artifact.file)
        relative_path = os.path.join("gems", name + "-" + version + ".gem")

        spec_artifact = _artifact_from_data(spec_data)
        spec_relative_path = os.path.join("quick/Marshal.4.8",
                                          name + "-" + version + ".gemspec.rz")

        data["name"] = name
        data["version"] = version
        data["artifacts"] = {
            relative_path: artifact,
            spec_relative_path: spec_artifact
        }

        # Validate uniqueness
        content = GemContent.objects.filter(name=name, version=version)
        if content.exists():
            raise ValidationError(
                _("There is already a gem content with name '{name}' and version '{version}'."
                  ).format(name=name, version=version))

        return data

    def create(self, validated_data):
        """Save the GemContent unit.

        This must be used inside a task that locks on the Artifact and if given, the repository.
        """

        repository = validated_data.pop("repository", None)
        content = super().create(validated_data)

        if repository:
            content_to_add = self.Meta.model.objects.filter(pk=content.pk)

            # create new repo version with uploaded package
            with RepositoryVersion.create(repository) as new_version:
                new_version.add_content(content_to_add)
        return content

    class Meta:
        fields = MultipleArtifactContentSerializer.Meta.fields + (
            "artifact",
            "file",
            "repository",
            "name",
            "version",
        )
        model = GemContent
class FileSerializer(ModelSerializer):
    file = FileField()

    class Meta:
        model = File
        fields = "__all__"
예제 #29
0
class UploadSerializer(Serializer):
    file_uploaded = FileField()

    class Meta:
        fields = ['file_uploaded']
예제 #30
0
class FileSerializer(Serializer):
    file = FileField(required=True)