def serialize(self, obj, request=None): """ Convert any object into a serializable representation. """ # Request from related serializer. if request is not None: self.request = request if isinstance(obj, (dict, models.Model)): # Model instances & dictionaries return self.serialize_model(obj) elif isinstance( obj, (tuple, list, set, QuerySet, RawQuerySet, types.GeneratorType)): # basic iterables return self.serialize_iter(obj) elif isinstance(obj, models.Manager): # Manager objects return self.serialize_manager(obj) elif inspect.isfunction(obj) and not inspect.getargspec(obj)[0]: # function with no args return self.serialize_func(obj) elif inspect.ismethod(obj) and len(inspect.getargspec(obj)[0]) <= 1: # bound method return self.serialize_func(obj) # Protected types are passed through as is. # (i.e. Primitives like None, numbers, dates, and Decimals.) if is_protected_type(obj): return obj # All other values are converted to string. return self.serialize_fallback(obj)
def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'): """ Similar to smart_bytes, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects. """ # Handle the common case first for performance reasons. if isinstance(s, bytes): if encoding == 'utf-8': return s else: return s.decode('utf-8', errors).encode(encoding, errors) if strings_only and is_protected_type(s): return s if isinstance(s, six.memoryview): return bytes(s) if isinstance(s, Promise): return six.text_type(s).encode(encoding, errors) if not isinstance(s, six.string_types): try: if six.PY3: return six.text_type(s).encode(encoding) else: return bytes(s) except UnicodeEncodeError: if isinstance(s, Exception): # An Exception subclass containing non-ASCII data that doesn't # know how to print itself properly. We shouldn't raise a # further exception. return b' '.join(force_bytes(arg, encoding, strings_only, errors) for arg in s) return six.text_type(s).encode(encoding, errors) else: return s.encode(encoding, errors)
def get_field_value(field, model): if field.remote_field is None: value = field.pre_save(model, add=model.pk is None) # Make datetimes timezone aware # https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1394-L1403 if isinstance(value, datetime.datetime) and settings.USE_TZ: if timezone.is_naive(value): default_timezone = timezone.get_default_timezone() value = timezone.make_aware( value, default_timezone).astimezone(timezone.utc) # convert to UTC value = timezone.localtime(value, timezone.utc) if is_protected_type(value): return value else: if field.verbose_name is 'body': field_dict = json.loads(field.value_to_string(model)) final_content = [] for shelf in field_dict: parse_shelf(shelf, parent=None) if shelf['type'] in SHARED_CONTENT_TYPES: shelf['content'] = ShelfAbstract.objects.get( id=shelf['value']).specific.serializable_data() final_content.append(shelf) else: shelf['content'] = shelf['value'] final_content.append(shelf) return json.dumps(final_content) else: return field.value_to_string(model) else: return getattr(model, field.get_attname())
def handle_fk_field(self, obj, field): foreign_model = field.rel.to # change to field.remote_field.model for django >= 1.9 from aristotle_mdr.fields import ConceptForeignKey if type(field) is ConceptForeignKey: value = [] if getattr(obj, field.get_attname()) is not None: value = foreign_model.objects.get( pk=getattr(obj, field.get_attname())).uuid #value = getattr(obj, field.get_attname()).uuid else: value = None elif self.use_natural_foreign_keys and hasattr( field.remote_field.model, 'natural_key'): related = getattr(obj, field.name) if related: value = related.natural_key() else: value = None else: value = getattr(obj, field.get_attname()) if not is_protected_type(value): value = field.value_to_string(obj) self._current[field.name] = value
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) value = coerce_datetime(value) if is_protected_type(value): self._current[field.name] = value else: self._current[field.name] = field.value_to_string(obj)
def serialize(self, obj): """ Convert any object into a serializable representation. """ if isinstance(obj, (dict, models.Model)): # Model instances & dictionaries return self.serialize_model(obj) elif isinstance(obj, (tuple, list, set, QuerySet, types.GeneratorType)): # basic iterables return self.serialize_iter(obj) elif isinstance(obj, models.Manager): # Manager objects return self.serialize_manager(obj) elif inspect.isfunction(obj) and not inspect.getargspec(obj)[0]: # function with no args return self.serialize_func(obj) elif inspect.ismethod(obj) and len(inspect.getargspec(obj)[0]) <= 1: # bound method return self.serialize_func(obj) # Protected types are passed through as is. # (i.e. Primitives like None, numbers, dates, and Decimals.) if is_protected_type(obj): return obj # All other values are converted to string. return self.serialize_fallback(obj)
def _value_from_field(self, obj, field): value = field.value_from_object(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. return value if is_protected_type(value) else field.value_to_string( obj)
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) if is_protected_type(value): self._current['properties'][field.name] = value elif isinstance(value, GEOSGeometry): self._current['geometry'] = value else: self._current['properties'][field.name] = field.value_to_string(obj)
def handle_field_value(self, value, field): # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): return value else: return smart_text(value)
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) display_method = "get_%s_display" % field.name if hasattr(obj, display_method): self._current[field.name] = getattr(obj, display_method)() elif is_protected_type(value): self._current[field.name] = value else: self._current[field.name] = field.value_to_string(obj)
def serialize(self, obj): """ Serializes the field's value into it's simple representation. """ if is_protected_type(obj): return obj elif hasattr(obj, '__iter__'): return [self.serialize(item) for item in obj] return smart_unicode(obj)
def get_field_value(field, model): if field.rel is None: value = field._get_val_from_obj(model) if is_protected_type(value): return value else: return field.value_to_string(model) else: return getattr(model, field.get_attname())
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._current[field.name] = value else: self._current[field.name] = field.value_to_string(obj)
def handle_field(self, obj, field): value = field.value_from_object(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._current[field.name] = value else: self._current[field.name] = field.value_to_string(obj)
def finish_handle_item(self, field_name, value, attributes=None): # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to unicode string first. if attributes is None: attributes = {} if is_protected_type(value): self._current[field_name] = value else: self._current[field_name] = unicode(value)
def serialize(self, instance): """ Retrieve the value for this field from the given model instance, and return a representation of it that can be included in JSON data """ value = self.field.value_from_object(instance) if not is_protected_type(value): value = self.field.value_to_string(instance) return value
def quote_value(value): if value is None: return 'NULL' if isinstance(value, bool): return str(int(value)) if is_protected_type(value): return str(value) else: return '"%s"' % (smart_text(value).replace('\\', '\\\\').replace( '"', '\\"').replace('\n', '\\n').replace('\r', '\\r'))
def serialize_field(self, obj, field_name): field = self.obj._meta.get_field_by_name(self.field_name)[0] value = field._get_val_from_obj(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. self.field = field if is_protected_type(value): return value else: return field.value_to_string(obj)
def to_native(self, value): """ Converts the field's value into it's simple representation. """ if is_simple_callable(value): value = value() if is_protected_type(value): return value elif hasattr(self, 'model_field'): return self.model_field.value_to_string(self.obj) return smart_unicode(value)
def to_native(self, value): """ Converts the field's value into it's simple representation. """ if is_simple_callable(value): value = value() if is_protected_type(value): return value elif hasattr(value, '__iter__') and not isinstance(value, (dict, basestring)): return [self.to_native(item) for item in value] return smart_unicode(value)
def handle_natural_foreign(obj, field): if hasattr(field.remote_field.model, 'natural_key'): _related = getattr(obj, field.name) if _related: value = _related.natural_key() else: value = None else: value = getattr(obj, field.get_attname()) if not is_protected_type(value): value = field.value_to_string(obj) return value
def handle_field(self, obj, field): """ Called to handle each individual (non-relational) field on an object. """ value = field._get_val_from_obj(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._fields[field.name] = value else: self._fields[field.name] = field.value_to_string(obj)
def quote_value(value): if value is None: return 'NULL' if isinstance(value, bool): return str(int(value)) if is_protected_type(value): return str(value) else: return '"%s"' % (smart_text(value).replace('\\', '\\\\') .replace('"', '\\"') .replace('\n', '\\n') .replace('\r', '\\r'))
def value_to_string(self, obj): """ Serialize this as a JSON object {name: field.value_to_string(...)} for each child field. """ value = self.value_from_object(obj) return json.dumps({ name: (field.value_from_object(value) if is_protected_type( field.value_from_object(value)) else field.value_to_string(value)) for name, field in self.Meta.fields })
def handle_fk_field(self, obj, field): if self.use_natural_foreign_keys and hasattr(field.remote_field.model, 'natural_key'): related = getattr(obj, field.name) if related: value = related.natural_key() else: value = None else: value = getattr(obj, field.get_attname()) if not is_protected_type(value): value = field.value_to_string(obj) self._current[field.name] = value
def handle_field(self, obj, field): if isinstance(field, (models.ImageField, models.FileField)): value = field.value_from_object(obj) if value: self._current[field.name] = value.url else: self._current[field.name] = None else: value = field.value_from_object(obj) if is_protected_type(value): self._current[field.name] = value else: self._current[field.name] = field.value_to_string(obj)
def serialize_field(obj, field): """ Convert non-relational model fields. Protected types (i.e., primitives like None, numbers, dates, and Decimals) are passed through as is. All other values are converted to string first. """ value = getattr(obj, field.name) if is_protected_type(value): return value else: return field.value_to_string(obj)
def _value_from_field(self, obj, field): value = field.value_from_object(obj) # Use display name for test result if str(field) == "dashboard.DiagnosticTest.test_result": return obj.get_test_result_display() # Format test date to human readable format (discard time) elif str(field) == "dashboard.DiagnosticTest.date_test": return obj.date_test.strftime('%d-%m-%Y') elif is_protected_type(value): return value else: return field.value_to_string(obj)
def to_native(self, value): """ Converts the field's value into it's simple representation. """ if is_simple_callable(value): value = value() if is_protected_type(value): return value elif hasattr(value, '__iter__') and not isinstance(value, (dict, six.string_types)): return [self.to_native(item) for item in value] elif isinstance(value, dict): return dict(map(self.to_native, (k, v)) for k, v in value.items()) return smart_text(value)
def handle_properties(self, obj, name): value = getattr(obj, name) if callable(value): value = value() # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._properties[name] = value elif isinstance(value, dict): self._properties[name] = value else: self._properties[name] = unicode(value) return self._properties[name]
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) #If the object has a get_field_display() method, use it. display_method = "get_%s_display" % field.name if hasattr(obj, display_method): self._current[field.name] = getattr(obj, display_method)() # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. elif is_protected_type(value): self._current[field.name] = value else: self._current[field.name] = field.value_to_string(obj)
def force_text(s, encoding='utf-8', strings_only=False, errors='strict'): """ Similar to smart_text, except that lazy instances are resolved to strings, rather than kept as lazy objects. If strings_only is True, don't convert (some) non-string-like objects. """ # Handle the common case first, saves 30-40% when s is an instance of # text_type. This function gets called often in that setting. if isinstance(s, unicode): return s if strings_only and is_protected_type(s): return s try: if not isinstance(s, basestring): if hasattr(s, '__unicode__'): s = s.__unicode__() else: try: s = unicode(bytes(s), encoding, errors) except UnicodeEncodeError: if not isinstance(s, Exception): raise # If we get to here, the caller has passed in an Exception # subclass populated with non-ASCII data without special # handling to display as a string. We need to handle this # without raising a further exception. We do an # approximation to what the Exception's standard str() # output should be. s = ' '.join([force_text(arg, encoding, strings_only, errors) for arg in s]) else: # Note: We use .decode() here, instead of text_type(s, encoding, # errors), so that if s is a SafeBytes, it ends up being a # SafeText at the end. s = s.decode(encoding, errors) except UnicodeDecodeError as e: if not isinstance(s, Exception): raise DjangoUnicodeDecodeError(s, *e.args) else: # If we get to here, the caller has passed in an Exception # subclass populated with non-ASCII bytestring data without a # working unicode method. Try to handle this without raising a # further exception by individually forcing the exception args # to unicode. s = ' '.join([force_text(arg, encoding, strings_only, errors) for arg in s]) return s
def handle_extra_field(self, obj, field_name): value = obj try: for field in field_name.split("."): value = value.__getattribute__(field) if callable(value): value = value() except (AttributeError, ObjectDoesNotExist): return None # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._current[field_name.replace(".","_")] = value else: self._current[field_name.replace(".","_")] = unicode(value)
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._current[field.name] = value else: # Changed to make it possible to get the value from a choice field, the LABEL value!! value = field.value_to_string(obj) if len(field.choices) > 0: # Get the first value founded! value = filter(None, map(lambda x: x[1] if str(x[0]) == value else None, field.choices)) if value: value = value[0] self._current[field.name] = value
def handle_field(self, obj, field): """ Most of this code is codied from Django's python serializer. It removes '_pk' from fields' keys """ value = field._get_val_from_obj(obj) field_name = field.name if field_name[-3:] == '_pk': field_name = field_name[:-3] # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._current[field_name] = value else: self._current[field_name] = field.value_to_string(obj)
def custom_force_unicode(s, encoding='utf-8', strings_only=False, errors='strict'): """ This is a wrapper of django.utils.encoding.force_unicode to return SafeUnicode objects instead of unicode, respecting protected_types and cases order to keep performance. """ # Handle the common case first, saves 30-40% in performance when s # is an instance of unicode. This function gets called often in that # setting. if isinstance(s, unicode): return SafeUnicode(s) if strings_only and is_protected_type(s): return s return SafeUnicode( original_force_unicode(s, encoding, strings_only, errors))
def handle_fk_field(self, obj, field): if self.use_natural_foreign_keys and hasattr(field.rel.to, 'natural_key'): related = getattr(obj, field.name) if related: value = related.natural_key() else: value = None elif hasattr(field.rel, 'to'): sub_modal = field.rel.to.objects.filter( pk=getattr(obj, field.get_attname())) value = DataSerializer().serialize(sub_modal) self._current[field.name] = value else: value = getattr(obj, field.get_attname()) if not is_protected_type(value): value = field.value_to_string(obj) self._current[field.name] = value
def to_native(self, value): """ Converts the field's value into it's simple representation. """ if is_simple_callable(value): value = value() if is_protected_type(value): return value elif is_non_str_iterable(value) and not isinstance(value, (dict, six.string_types)): return [self.to_native(item) for item in value] elif isinstance(value, dict): # Make sure we preserve field ordering, if it exists ret = SortedDict() for key, val in value.items(): ret[key] = self.to_native(val) return ret return force_text(value)
def full_dehydrate(self, obj, for_list=False): """Convert the given object into a dictionary. :param for_list: True when the object is being converted to belong in a list. """ if for_list: allowed_fields = self._meta.list_fields exclude_fields = self._meta.list_exclude else: allowed_fields = self._meta.fields exclude_fields = self._meta.exclude data = {} for field in self._meta.object_class._meta.fields: # Convert the field name to unicode as some are stored in bytes. field_name = str(field.name) # Skip fields that are not allowed. if allowed_fields is not None and field_name not in allowed_fields: continue if exclude_fields is not None and field_name in exclude_fields: continue # Get the value from the field and set it in data. The value # will pass through the dehydrate method if present. field_obj = getattr(obj, field_name) dehydrate_method = getattr(self, "dehydrate_%s" % field_name, None) if dehydrate_method is not None: data[field_name] = dehydrate_method(field_obj) else: value = field.value_from_object(obj) if is_protected_type(value): data[field_name] = value elif isinstance(field, ArrayField): data[field_name] = field.to_python(value) else: data[field_name] = field.value_to_string(obj) # Add permissions that can be performed on this object. data = self._add_permissions(obj, data) # Return the data after the final dehydrate. return self.dehydrate(obj, data, for_list=for_list)
def to_native(self, value): """ Converts the field's value into it's simple representation. """ if is_simple_callable(value): value = value() if is_protected_type(value): return value elif (is_non_str_iterable(value) and not isinstance(value, (dict, six.string_types))): return [self.to_native(item) for item in value] elif isinstance(value, dict): # Make sure we preserve field ordering, if it exists ret = SortedDict() for key, val in value.items(): ret[key] = self.to_native(val) return ret return force_text(value)
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. if is_protected_type(value): self._current[field.name] = value else: # Changed to make it possible to get the value from a choice field, the LABEL value!! value = field.value_to_string(obj) if len(field.choices) > 0: # Get the first value founded! value = filter( None, map(lambda x: x[1] if str(x[0]) == value else None, field.choices)) if value: value = value[0] self._current[field.name] = value
def get_field_value(field, model): if field.remote_field is None: value = field.pre_save(model, add=model.pk is None) # Make datetimes timezone aware # https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1394-L1403 if isinstance(value, datetime.datetime) and settings.USE_TZ: if timezone.is_naive(value): default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone).astimezone(timezone.utc) # convert to UTC value = timezone.localtime(value, timezone.utc) if is_protected_type(value): return value else: return field.value_to_string(model) else: return getattr(model, field.get_attname())
def get_field_value(field, model): if field.rel is None: value = field._get_val_from_obj(model) # Make datetimes timezone aware # https://github.com/django/django/blob/master/django/db/models/fields/__init__.py#L1394-L1403 if isinstance(value, datetime.datetime) and settings.USE_TZ: if timezone.is_naive(value): default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone).astimezone(timezone.utc) # convert to UTC value = timezone.localtime(value, timezone.utc) if is_protected_type(value): return value else: return field.value_to_string(model) else: return getattr(model, field.get_attname())
def handle_field(self, obj, field): value = field._get_val_from_obj(obj) # Protected types (i.e., primitives like None, numbers, dates, # and Decimals) are passed through as is. All other values are # converted to string first. field_json_name = underscore_to_camelcase(field.name) if isinstance(value, datetime.datetime): # convert to milliseconds self._current[field_json_name] = time.mktime( value.timetuple()) * 1000 elif is_protected_type(value): self._current[field_json_name] = value elif isinstance(value, list): self._current[field_json_name] = value elif isinstance(value, Polygon): self._current[field_json_name] = [[ value.extent[0], value.extent[1] ], [value.extent[2], value.extent[3]]] else: self._current[field_json_name] = field.value_to_string(obj)
def serialize_one(obj, selectedFields=None): """ Serialize a model object into a dictionary. The method is modified based on Django's serializer, but omits many to many field support and changes the DateTimeField output format. If selectedFields are specified, only the selected fields will be returned. """ outputObj = { 'pk':obj.pk } concreteModel = obj._meta.concrete_model for field in concreteModel._meta.local_fields: if field.serialize: if field.rel is None: # Normal field if (selectedFields is None or field.attname in selectedFields): value = field._get_val_from_obj(obj) if is_protected_type(value): if isinstance(value, datetime): value = value.strftime(FORMAT_DATETIME) outputObj[field.name] = value else: outputObj[field.name] = field.value_to_string(obj) else: # Foreign Key if (selectedFields is None or field.attname[:-3] in selectedFields): value = getattr(obj, field.name) if hasattr(value, 'name'): valueObj = { 'id':value.id, 'name':value.name } outputObj[field.name] = valueObj elif value is not None: outputObj[field.name] = value.id else: outputObj[field.name] = value return outputObj
def handle_fk_field(self, obj, field): foreign_model = field.rel.to # change to field.remote_field.model for django >= 1.9 from aristotle_mdr.fields import ConceptForeignKey if type(field) is ConceptForeignKey: value = [] if getattr(obj, field.get_attname()) is not None: value = foreign_model.objects.get(pk=getattr(obj, field.get_attname())).uuid #value = getattr(obj, field.get_attname()).uuid else: value = None elif self.use_natural_foreign_keys and hasattr(field.remote_field.model, 'natural_key'): related = getattr(obj, field.name) if related: value = related.natural_key() else: value = None else: value = getattr(obj, field.get_attname()) if not is_protected_type(value): value = field.value_to_string(obj) self._current[field.name] = value
def field_to_native(self, obj, field_name): value = self.model_field._get_val_from_obj(obj) if is_protected_type(value): return value return self.model_field.value_to_string(obj)
def to_representation(self, obj): value = self.model_field._get_val_from_obj(obj) if is_protected_type(value): return value return self.model_field.value_to_string(obj)
def handle_field(self, field): value = field._get_val_from_obj(self.obj) if is_protected_type(value): self.result[field.name] = value else: self.result[field.name] = field.value_to_string(self.obj)