コード例 #1
0
    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)
コード例 #2
0
ファイル: utils.py プロジェクト: barszczmm/django-allauth
 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)
コード例 #3
0
ファイル: utils.py プロジェクト: danmorley/nhs-example
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())
コード例 #4
0
    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
コード例 #5
0
 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)
コード例 #6
0
    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)
コード例 #7
0
ファイル: python.py プロジェクト: zc-andy/DbMgrSystem
 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)
コード例 #8
0
ファイル: snippet.py プロジェクト: szabo92/gistable
 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)
コード例 #9
0
 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)
コード例 #10
0
 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)
コード例 #11
0
 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)
コード例 #12
0
ファイル: fields.py プロジェクト: rohanza/django-serializers
 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)
コード例 #13
0
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())
コード例 #14
0
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())
コード例 #15
0
ファイル: python.py プロジェクト: schoonc/django
 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)
コード例 #16
0
ファイル: python.py プロジェクト: atlassian/django
 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)
コード例 #17
0
ファイル: serializers.py プロジェクト: DirectEmployers/MyJobs
 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)
コード例 #18
0
    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
コード例 #19
0
ファイル: compact_csv.py プロジェクト: movermeyer/nicedjango
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'))
コード例 #20
0
ファイル: serializers.py プロジェクト: supervisitor20/MyJobs
 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)
コード例 #21
0
ファイル: fields.py プロジェクト: rohanza/django-serializers
 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)
コード例 #22
0
    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)
コード例 #23
0
ファイル: fields.py プロジェクト: ulmus/django-rest-framework
    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)
コード例 #24
0
ファイル: serializers.py プロジェクト: lroolle/codingpython
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
コード例 #25
0
 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)
コード例 #26
0
ファイル: compact_csv.py プロジェクト: katakumpo/nicedjango
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'))
コード例 #27
0
 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
     })
コード例 #28
0
ファイル: serializers.py プロジェクト: lroolle/CodingInPython
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
コード例 #29
0
ファイル: python.py プロジェクト: MPBAUnofficial/gswebgis
 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)
コード例 #30
0
ファイル: python.py プロジェクト: tousifcs/django
 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
コード例 #31
0
    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)
コード例 #32
0
 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)
コード例 #33
0
ファイル: base.py プロジェクト: prestontimmons/djserializers
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)
コード例 #34
0
    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)
コード例 #35
0
    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)
コード例 #36
0
ファイル: python.py プロジェクト: rosix-ru/django-bwp
 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]
コード例 #37
0
ファイル: fields.py プロジェクト: aitzol/django-admin2
    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)
コード例 #38
0
    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)
コード例 #39
0
    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)
コード例 #40
0
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
コード例 #41
0
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)
コード例 #42
0
ファイル: lib.py プロジェクト: flipjack/mexicof
	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
コード例 #43
0
 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)
コード例 #44
0
ファイル: __init__.py プロジェクト: gijs/django-i18n-helper
 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))
コード例 #45
0
 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))
コード例 #46
0
 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
コード例 #47
0
    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)
コード例 #48
0
    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)
コード例 #49
0
    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)
コード例 #50
0
 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
コード例 #51
0
ファイル: models.py プロジェクト: kahuria00/wagtail-mysite
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())
コード例 #52
0
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())
コード例 #53
0
 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)
コード例 #54
0
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
コード例 #55
0
    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
コード例 #56
0
ファイル: fields.py プロジェクト: aminadha/taiga-back
 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)
コード例 #57
0
 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)
コード例 #58
0
ファイル: serializer.py プロジェクト: feuervogel/simpleapi
 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)