[pyar] Validator Django Rest

Alan Rizzo alan.rizzo en gmail.com
Jue Jun 22 16:58:17 ART 2017


Buenas estoy tratando de generar un validador para permitir solo cierto
tipo de extensiones de archivo. El tema es que cuando corro el servidor, no
esta ni siequiera entrando al validador.
Alguna idea para ver que puede estar pasando?

restricted_file.py


import os
from django.utils.deconstruct import deconstructible
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _


@deconstructible
class FileExtensionValidator(object):
    message = _(
        "File extension '%(extension)s' is not allowed. "
        "Allowed extensions are: '%(allowed_extensions)s'."
    )
    code = 'invalid_extension'

    def __init__(self, allowed_extensions=None, message=None, code=None):
        self.allowed_extensions = allowed_extensions
        if message is not None:
            self.message = message
        if code is not None:
            self.code = code

    def __call__(self, value):
        extension = os.path.splitext(value.name)[1][1:].lower()
        if self.allowed_extensions is not None and extension not in
self.allowed_extensions:
            raise ValidationError(
                self.message,
                code=self.code,
                params={
                    'extension': extension,
                    'allowed_extensions': ', '.join(self.allowed_extensions)
                }
            )

    def __eq__(self, other):
        return (
            isinstance(other, self.__class__) and
            self.allowed_extensions == other.allowed_extensions and
            self.message == other.message and
            self.code == other.code
        )


candidate.py

from django.db import models
from contacts.models import contact_model
from .restricted_file import FileExtensionValidator


class CandidateContact(contact_model.BaseContact):
    """
    Contains candidate information such as Contact,Email, Linkedin,
    Portfolio plus resume(Attachment).
    """

    location = models.CharField(max_length=40, blank=True)
    linkedin_link = models.URLField(blank=True)
    portfolio_link = models.URLField(blank=True)
    content_type = FileExtensionValidator(allowed_extensions=['pdf'])
    resume = models.FileField(validators=[content_type],
                              upload_to="docs/%d-%m-%Y", blank=True, null=True)
------------ próxima parte ------------
Se ha borrado un adjunto en formato HTML...
URL: <http://listas.python.org.ar/pipermail/pyar/attachments/20170622/306d6f27/attachment.html>


Más información sobre la lista de distribución pyar