def __init__(self, enum_class): if Enum is None: raise ImproperlyConfigured( "'enum34' package is required to use 'EnumType' in Python " "< 3.4") if not issubclass(enum_class, Enum): raise ImproperlyConfigured( "EnumType needs a class of enum defined.") self.enum_class = enum_class
def __init__( self, type_in=None, key=None, engine=None, padding=None, **kwargs ): """Initialization.""" if not cryptography: raise ImproperlyConfigured( "'cryptography' is required to use EncryptedType" ) super(StringEncryptedType, self).__init__(**kwargs) # set the underlying type if type_in is None: type_in = String() elif isinstance(type_in, type): type_in = type_in() self.underlying_type = type_in self._key = key if not engine: engine = AesEngine self.engine = engine() if isinstance(self.engine, AesEngine): self.engine._set_padding_mechanism(padding)
def __init__(self, raw_number, country_code=None): # Bail if phonenumbers is not found. if phonenumbers is None: raise ImproperlyConfigured( "'phonenumbers' is required to use 'PhoneNumber'") self._phone_number = phonenumbers.parse(raw_number, country_code) super(PhoneNumber, self).__init__( country_code=self._phone_number.country_code, national_number=self._phone_number.national_number, extension=self._phone_number.extension, italian_leading_zero=self._phone_number.italian_leading_zero, raw_input=self._phone_number.raw_input, country_code_source=self._phone_number.country_code_source, preferred_domestic_carrier_code=( self._phone_number.preferred_domestic_carrier_code ) ) self.national = phonenumbers.format_number( self._phone_number, phonenumbers.PhoneNumberFormat.NATIONAL ) self.international = phonenumbers.format_number( self._phone_number, phonenumbers.PhoneNumberFormat.INTERNATIONAL ) self.e164 = phonenumbers.format_number( self._phone_number, phonenumbers.PhoneNumberFormat.E164 )
def __init__(self, *args, **kwargs): if babel is None: raise ImproperlyConfigured( "'babel' package is required to use 'WeekDaysType'" ) super(WeekDaysType, self).__init__(*args, **kwargs)
def __init__(self, max_length=50, *args, **kwargs): if not ip_address: raise ImproperlyConfigured( "'ipaddr' package is required to use 'IPAddressType' " "in python 2") super(IPAddressType, self).__init__(*args, **kwargs) self.impl = types.Unicode(max_length)
def __init__(self, max_length=20, *args, **kwargs): # Fail if colour is not found. if colour is None: raise ImproperlyConfigured( "'colour' package is required to use 'ColorType'") super(ColorType, self).__init__(*args, **kwargs) self.impl = types.Unicode(max_length)
def __init__(self, country_code='US', max_length=20, *args, **kwargs): # Bail if phonenumbers is not found. if phonenumbers is None: raise ImproperlyConfigured( "'phonenumbers' is required to use 'PhoneNumberType'") super(PhoneNumberType, self).__init__(*args, **kwargs) self.country_code = country_code self.impl = types.Unicode(max_length)
def _set_padding_mechanism(self, padding_mechanism=None): """Set the padding mechanism.""" if isinstance(padding_mechanism, six.string_types): if padding_mechanism not in PADDING_MECHANISM.keys(): raise ImproperlyConfigured( "There is not padding mechanism with name {}".format( padding_mechanism)) if padding_mechanism is None: padding_mechanism = 'naive' padding_class = PADDING_MECHANISM[padding_mechanism] self.padding_engine = padding_class(self.BLOCK_SIZE)
def __init__(self, max_length=None, **kwargs): # Fail if passlib is not found. if passlib is None: raise ImproperlyConfigured( "'passlib' is required to use 'PasswordType'") # Construct the passlib crypt context. self.context = CryptContext(**kwargs) if max_length is None: max_length = self.calculate_max_length() # Set the length to the now-calculated max length. self.length = max_length
def __init__(self, type_in=None, key=None, engine=None, **kwargs): """Initialization.""" if not cryptography: raise ImproperlyConfigured( "'cryptography' is required to use EncryptedType") super(EncryptedType, self).__init__(**kwargs) # set the underlying type if type_in is None: type_in = String() self.underlying_type = type_in() self._key = key if not engine: engine = AesEngine self.engine = engine(self._key)
def __init__(self, backend='dateutil'): """ :param backend: Whether to use 'dateutil' or 'pytz' for timezones. """ self.backend = backend if backend == 'dateutil': try: from dateutil.tz import tzfile from dateutil.zoneinfo import gettz self.python_type = tzfile self._to = gettz self._from = lambda x: six.text_type(x._filename) except ImportError: raise ImproperlyConfigured( "'python-dateutil' is required to use the " "'dateutil' backend for 'TimezoneType'") elif backend == 'pytz': try: from pytz import tzfile, timezone self.python_type = tzfile.DstTzInfo self._to = timezone self._from = six.text_type except ImportError: raise ImproperlyConfigured( "'pytz' is required to use the 'pytz' backend " "for 'TimezoneType'") else: raise ImproperlyConfigured( "'pytz' or 'dateutil' are the backends supported for " "'TimezoneType'")
def process_value(cls, value, python_type): """ process_value returns a datetime, date or time object according to a given string value and a python type. """ if not dateutil: raise ImproperlyConfigured( "'python-dateutil' is required to process datetimes") return_value = datetime_parse(value) if issubclass(python_type, datetime.datetime): return return_value elif issubclass(python_type, datetime.time): return return_value.time() elif issubclass(python_type, datetime.date): return return_value.date()
def __init__(self, choices): if not choices: raise ImproperlyConfigured( 'ChoiceType needs list of choices defined.') self.choices_dict = dict(choices)
def __init__(self, *args, **kwargs): if not arrow: raise ImproperlyConfigured( "'arrow' package is required to use 'ArrowType'") super(ArrowType, self).__init__(*args, **kwargs)