示例#1
0
 def clean(self, data, initial=None):
     # If the widget got contradictory inputs, we raise a validation error
     if not type(data) in File.__subclasses__():
         return data
     if data is FILE_INPUT_CONTRADICTION:
         raise ValidationError(self.error_messages[u'contradiction'])
     # False means the field value should be cleared; further validation is
     # not needed.
     if data is False:
         if not self.required:
             return False
         # If the field is required, clearing is not possible (the widget
         # shouldn't return False data in that case anyway). False is not
         # in validators.EMPTY_VALUES; if a False value makes it this far
         # it should be validated from here on out as None (so it will be
         # caught by the required check).
         data = None
     return initial if (not data and initial) else super(
         FileOrAlreadyExistantFilePathField, self
     ).clean(data)
示例#2
0
from django.core.files.base import File
from django.core.files.uploadedfile import (
    InMemoryUploadedFile, TemporaryUploadedFile
)
from django.forms import CharField, MultiValueField, ValidationError, FileField
from django.forms.widgets import FILE_INPUT_CONTRADICTION
from lxml import etree

from . import (
    widgets, LANGUAGES, LANGUAGES_REPLACEMENT,
    LANGUAGES_REQUIRED_TEXT, REQUIRED_ERROR
)

# This list is used to validate file uploads
FILE_FIELD_CLASSES = File.__subclasses__() + [
    TemporaryUploadedFile,
    InMemoryUploadedFile
]


class MultiLingualTextField(MultiValueField):
    u"""The field used by MultiLingualTextField."""
    widget = widgets.MultiLingualTextFieldWidget

    def widget_attrs(self, widget):
        u"""
        Given a Widget instance (*not* a Widget class), returns a dictionary
        of any HTML attributes that should be added to the Widget, based on
        this Field.
        """