def get_value(field, descriptor): """ Gets an attribute value from a Field instance and formats it. """ attrname, options = descriptor value = get_attribute(field, attrname) # Lazy-eval functions get eval'd. if isinstance(value, Promise): value = unicode(value) # If the value is the same as the default, omit it for clarity if "default" in options and value == options['default']: raise IsDefault # If there's an ignore_if, use it if "ignore_if" in options: if get_attribute(field, options['ignore_if']): raise IsDefault # Some default values need to be gotten from an attribute too. if "default_attr" in options: default_value = get_attribute(field, options['default_attr']) if value == default_value: raise IsDefault # Some are made from a formatting string and several attrs (e.g. db_table) if "default_attr_concat" in options: format, attrs = options['default_attr_concat'][0], options[ 'default_attr_concat'][1:] default_value = format % tuple( map(lambda x: get_attribute(field, x), attrs)) if value == default_value: raise IsDefault # Callables get called. if callable(value) and not isinstance(value, ModelBase): # Datetime.datetime.now is special, as we can access it from the eval # context (and because it changes all the time; people will file bugs otherwise). if value == datetime.datetime.now: return "datetime.datetime.now" if value == datetime.datetime.utcnow: return "datetime.datetime.utcnow" if value == datetime.date.today: return "datetime.date.today" # All other callables get called. value = value() # Models get their own special repr() if isinstance(value, ModelBase): # If it's a proxy model, follow it back to its non-proxy parent if getattr(value._meta, "proxy", False): value = value._meta.proxy_for_model return "orm['%s.%s']" % (value._meta.app_label, value._meta.object_name) # As do model instances if isinstance(value, Model): return "orm['%s.%s'].objects.get(pk=%r)" % ( value.__class__._meta.app_label, value.__class__._meta.object_name, value.pk) # Now, apply the converter func if there is one if "converter" in options: value = options['converter'](value) # Return the final value return repr(value)
def get_value(field, descriptor): """ Gets an attribute value from a Field instance and formats it. """ attrname, options = descriptor value = get_attribute(field, attrname) # Lazy-eval functions get eval'd. if isinstance(value, Promise): value = unicode(value) # If the value is the same as the default, omit it for clarity if "default" in options and value == options['default']: raise IsDefault # If there's an ignore_if, use it if "ignore_if" in options: if get_attribute(field, options['ignore_if']): raise IsDefault # Some default values need to be gotten from an attribute too. if "default_attr" in options: default_value = get_attribute(field, options['default_attr']) if value == default_value: raise IsDefault # Some are made from a formatting string and several attrs (e.g. db_table) if "default_attr_concat" in options: format, attrs = options['default_attr_concat'][0], options['default_attr_concat'][1:] default_value = format % tuple(map(lambda x: get_attribute(field, x), attrs)) if value == default_value: raise IsDefault # Callables get called. if callable(value) and not isinstance(value, ModelBase): # Datetime.datetime.now is special, as we can access it from the eval # context (and because it changes all the time; people will file bugs otherwise). if value == datetime.datetime.now: return "datetime.datetime.now" if value == datetime.datetime.utcnow: return "datetime.datetime.utcnow" if value == datetime.date.today: return "datetime.date.today" # All other callables get called. value = value() # Models get their own special repr() if isinstance(value, ModelBase): # If it's a proxy model, follow it back to its non-proxy parent if getattr(value._meta, "proxy", False): value = value._meta.proxy_for_model return "orm['%s.%s']" % (value._meta.app_label, value._meta.object_name) # As do model instances if isinstance(value, Model): return "orm['%s.%s'].objects.get(pk=%r)" % (value.__class__._meta.app_label, value.__class__._meta.object_name, value.pk) # Now, apply the converter func if there is one if "converter" in options: value = options['converter'](value) # Return the final value return repr(value)
def field_dependencies(field, checked_models=None): checked_models = checked_models or set() depends = set() arg_defs, kwarg_defs = modelsinspector.matching_details(field) for attrname, options in arg_defs + kwarg_defs.values(): if options.get("ignore_if_auto_through", False) and auto_through(field): continue if options.get("is_value", False): value = attrname elif attrname == 'rel.through' and hasattr(getattr(field, 'rel', None), 'through_model'): # Hack for django 1.1 and below, where the through model is stored # in rel.through_model while rel.through stores only the model name. value = field.rel.through_model else: try: value = get_attribute(field, attrname) except AttributeError: if options.get("ignore_missing", False): continue raise if isinstance(value, Model): value = value.__class__ if not isinstance(value, ModelBase): continue if getattr(value._meta, "proxy", False): value = value._meta.proxy_for_model if value in checked_models: continue checked_models.add(value) depends.add(value) depends.update(model_dependencies(value, checked_models)) return depends
def get_value(field, descriptor): """ Gets an attribute value from a Field instance and formats it. """ attrname, options = descriptor # If the options say it's not a attribute name but a real value, use that. if options.get('is_value', False): value = attrname else: try: value = get_attribute(field, attrname) except AttributeError: if options.get("ignore_missing", False): raise IsDefault else: raise # Lazy-eval functions get eval'd. if isinstance(value, Promise): value = text_type(value) # If the value is the same as the default, omit it for clarity if "default" in options and value == options['default']: raise IsDefault # If there's an ignore_if, use it if "ignore_if" in options: if get_attribute(field, options['ignore_if']): raise IsDefault # If there's an ignore_if_auto_through which is True, use it if options.get("ignore_if_auto_through", False): if auto_through(field): raise IsDefault # Some default values need to be gotten from an attribute too. if "default_attr" in options: default_value = get_attribute(field, options['default_attr']) if value == default_value: raise IsDefault # Some are made from a formatting string and several attrs (e.g. db_table) if "default_attr_concat" in options: format, attrs = options['default_attr_concat'][0], options[ 'default_attr_concat'][1:] default_value = format % tuple( map(lambda x: get_attribute(field, x), attrs)) if value == default_value: raise IsDefault # Clean and return the value return value_clean(value, options)
def get_value(field, descriptor): """ Gets an attribute value from a Field instance and formats it. """ attrname, options = descriptor # If the options say it's not a attribute name but a real value, use that. if options.get('is_value', False): value = attrname else: try: value = get_attribute(field, attrname) except AttributeError: if options.get("ignore_missing", False): raise IsDefault else: raise # Lazy-eval functions get eval'd. if isinstance(value, Promise): value = unicode(value) # If the value is the same as the default, omit it for clarity if "default" in options and value == options['default']: raise IsDefault # If there's an ignore_if, use it if "ignore_if" in options: if get_attribute(field, options['ignore_if']): raise IsDefault # If there's an ignore_if_auto_through which is True, use it if options.get("ignore_if_auto_through", False): if auto_through(field): raise IsDefault # Some default values need to be gotten from an attribute too. if "default_attr" in options: default_value = get_attribute(field, options['default_attr']) if value == default_value: raise IsDefault # Some are made from a formatting string and several attrs (e.g. db_table) if "default_attr_concat" in options: format, attrs = options['default_attr_concat'][0], options['default_attr_concat'][1:] default_value = format % tuple(map(lambda x: get_attribute(field, x), attrs)) if value == default_value: raise IsDefault # Clean and return the value return value_clean(value, options)
def get_value(field, descriptor): """ Gets an attribute value from a Field instance and formats it. """ attrname, options = descriptor value = get_attribute(field, attrname) # Lazy-eval functions get eval'd. # Annoyingly, we can't do an isinstance() test if isinstance(value, Promise): value = unicode(value) # If the value is the same as the default, omit it for clarity if "default" in options and value == options['default']: raise IsDefault # If there's an ignore_if, use it if "ignore_if" in options: if get_attribute(field, options['ignore_if']): raise IsDefault # Some default values need to be gotten from an attribute too. if "default_attr" in options: default_value = get_attribute(field, options['default_attr']) if value == default_value: raise IsDefault # Some are made from a formatting string and several attrs (e.g. db_table) if "default_attr_concat" in options: format, attrs = options['default_attr_concat'][0], options['default_attr_concat'][1:] default_value = format % tuple(map(lambda x: get_attribute(field, x), attrs)) if value == default_value: raise IsDefault # Models get their own special repr() if isinstance(value, ModelBase): return "orm['%s.%s']" % (value._meta.app_label, value._meta.object_name) # Callables get called. elif callable(value): # Datetime.datetime.now is special, as we can access it from the eval # context (and because it changes all the time; people will file bugs otherwise). if value == datetime.datetime.now: return "datetime.datetime.now" if value == datetime.date.today: return "datetime.date.today" # All other callables get called. return repr(value()) else: return repr(value)
def get_value(field, descriptor): """ Gets an attribute value from a Field instance and formats it. """ attrname, options = descriptor # If the options say it's not a attribute name but a real value, use that. if options.get('is_value', False): value = attrname else: try: value = get_attribute(field, attrname) except AttributeError: if options.get("ignore_missing", False): raise IsDefault else: raise # Lazy-eval functions get eval'd. if isinstance(value, Promise): value = unicode(value) # If the value is the same as the default, omit it for clarity if "default" in options and value == options['default']: raise IsDefault # If there's an ignore_if, use it if "ignore_if" in options: if get_attribute(field, options['ignore_if']): raise IsDefault # If there's an ignore_if_auto_through which is True, use it if options.get("ignore_if_auto_through", False): if auto_through(field): raise IsDefault # Some default values need to be gotten from an attribute too. if "default_attr" in options: default_value = get_attribute(field, options['default_attr']) if value == default_value: raise IsDefault # Some are made from a formatting string and several attrs (e.g. db_table) if "default_attr_concat" in options: format, attrs = options['default_attr_concat'][0], options['default_attr_concat'][1:] default_value = format % tuple(map(lambda x: get_attribute(field, x), attrs)) if value == default_value: raise IsDefault # Callables get called. if callable(value) and not isinstance(value, ModelBase): # Datetime.datetime.now is special, as we can access it from the eval # context (and because it changes all the time; people will file bugs otherwise). if value == datetime.datetime.now: return "datetime.datetime.now" if value == datetime.datetime.utcnow: return "datetime.datetime.utcnow" if value == datetime.date.today: return "datetime.date.today" # All other callables get called. value = value() # Models get their own special repr() if isinstance(value, ModelBase): # If it's a proxy model, follow it back to its non-proxy parent if getattr(value._meta, "proxy", False): value = value._meta.proxy_for_model return "orm['%s.%s']" % (value._meta.app_label, value._meta.object_name) # As do model instances if isinstance(value, Model): if options.get("ignore_dynamics", False): raise IsDefault return "orm['%s.%s'].objects.get(pk=%r)" % (value.__class__._meta.app_label, value.__class__._meta.object_name, value.pk) # Make sure Decimal is converted down into a string if isinstance(value, decimal.Decimal): value = str(value) # in case the value is timezone aware if isinstance(value, (datetime.datetime, datetime.time)): try: from django.utils import timezone except ImportError: pass else: if (getattr(settings, 'USE_TZ', False) and value is not None and timezone.is_aware(value)): default_timezone = timezone.get_default_timezone() value = timezone.make_naive(value, default_timezone) # datetime_safe has an improper repr value if isinstance(value, datetime_safe.datetime): value = datetime.datetime(*value.utctimetuple()[:7]) if isinstance(value, datetime_safe.date): value = datetime.date(*value.timetuple()[:3]) # Now, apply the converter func if there is one if "converter" in options: value = options['converter'](value) # Return the final value return repr(value)