def _to_native(self): if self.value in v.VALIDATORS_EMPTY_VALUES: return u'' if self.upper: return py2to3._unicode(self.value).upper() else: return py2to3._unicode(self.value)
def strftime(self, value): if self._serialize_format: return value.strftime(self._serialize_format) elif self._current_format: return value.strftime(self._current_format) else: return py2to3._unicode(value.isoformat())
def _pre(self, objects, limit=None, offset=None, sort=None): if offset is None: offset = 0 try: offset = int(offset) limit = int(limit) except Exception: limit = None if sort is not None and not isinstance(sort, list): sort = [str(sort)] _sort = [] if objects and sort and len(sort) > 0: model_fields = self.get_model_field_list(objects.model) serializer_fieldnames = self._serializer_cls.get_fieldnames() for sort_item in sort: sort_field_name = str(sort_item) sort_prefix = '' if sort_field_name.startswith('-'): sort_prefix = '-' sort_field_name = sort_field_name[1:] if sort_field_name in serializer_fieldnames: sort_field_name = serializer_fieldnames[sort_field_name] if sort_field_name in model_fields: _sort.append('{}{}'.format(sort_prefix, sort_field_name)) try: if objects and len(_sort) > 0: _sort = [py2to3._unicode(item).replace('.', '__') for item in _sort] objects = objects.order_by(*_sort) if limit: objects = objects[offset:(offset + limit)] except Exception: return objects.model.objects.none() else: return objects
def _pre(self, objects, limit=None, offset=None, sort=None): if offset is None: offset = 0 try: offset = int(offset) limit = int(limit) except Exception: limit = None if sort: if not isinstance(sort, list): sort = [py2to3._unicode(sort)] def get_key(item, k): if isinstance(item, dict): return item.get(k, None) return getattr(item, k, None) for s in reversed(sort): reverse = False if s.startswith('-'): reverse = True s = s[1:] objects = list( sorted(objects, key=lambda item: get_key(item, s), reverse=reverse)) try: if limit: objects = objects[offset:(offset + limit)] except Exception: return {} else: return objects
def _pre(self, objects, limit=None, offset=None, sort=None): if offset is None: offset = 0 try: offset = int(offset) limit = int(limit) except Exception: limit = None if sort: if not isinstance(sort, list): sort = [py2to3._unicode(sort)] def get_key(item, k): if isinstance(item, dict): return item.get(k, None) return getattr(item, k, None) for s in reversed(sort): reverse = False if s.startswith('-'): reverse = True s = s[1:] objects = list(sorted(objects, key=lambda item:get_key(item, s), reverse=reverse)) try: if limit: objects = objects[offset:(offset + limit)] except Exception: return {} else: return objects
def _to_python(self): if self.value in v.VALIDATORS_EMPTY_VALUES: return None if self.binary and not isinstance(self.value, uuid.UUID): self.value = uuid.UUID(str(self.value)) if not self.binary: return py2to3._unicode(self.value).lower() return self.value
def initial(self, source): if isinstance(source, py2to3.string): if not isinstance(source, py2to3.text): source = py2to3._unicode(source, 'utf-8') try: self.obj = json.loads(source) except ValueError: self.obj = object() else: self.obj = source self._attribute_names = None
def get_model_field_list(self, model, parent_name=None, result=None): if result is None: result = [] for item, i in model._meta.get_fields_with_model(): if parent_name: result.append('{}.{}'.format(parent_name, item.name)) else: result.append(py2to3._unicode(item.name)) if item.rel is not None: if parent_name: item_name = '{}.{}'.format(parent_name, item.name) else: item_name = item.name self.get_model_field_list(item.rel.to, item_name, result) return result
def get_django_model_field_list(model, parent_name=None, result=None): if result is None: result = [] for field in get_fields(model): if parent_name: result.append('{}.{}'.format(parent_name, field.name)) else: result.append(py2to3._unicode(field.name)) if is_relation_field(field): if parent_name: item_name = '{}.{}'.format(parent_name, field.name) else: item_name = field.name get_django_model_field_list(get_related_model_from_field(field), item_name, result) return result
def to_native(self): return py2to3._unicode(self.name)
def to_python(self): return py2to3._unicode(self.name)
def street_clean_value(self, value): if py2to3._unicode(value).startswith('sesame'): return 'Street for kids' else: return value
def to_unicode(value): if value in v.VALIDATORS_EMPTY_VALUES: return u'' return py2to3._unicode(value)
class MaxStringLengthValidator(CompareValidator): compare = lambda self, a, b: len(py2to3._unicode(a)) > b message = 'Value is too long. Maximum value length is %(compare_value)s.' code = 'max_length'
class MinStringLengthValidator(CompareValidator): compare = lambda self, a, b: len(py2to3._unicode(a)) < b message = 'Value is too short. Minimum value length is %(compare_value)s.' code = 'min_length'
def validate_email(value): if not RE_EMAIL.search(py2to3._unicode(value)): raise SerializerValidatorError('Enter a valid email.', error_code='invalid')
def validate_uuid(value): if not isinstance(value, uuid.UUID): if not RE_UUID.search(py2to3._unicode(value)): raise SerializerValidatorError('Enter a valid uuid.', error_code='invalid')
def validate_url(value): if not RE_URL.search(py2to3._unicode(value)): raise SerializerValidatorError('Enter a valid url.', error_code='invalid')
def _to_native(self): if self.value in v.VALIDATORS_EMPTY_VALUES: return None if isinstance(self.value, datetime): return self.strftime(self.value) return py2to3._unicode(self.value)