south.modelsinspector: 193 total statements, 0.0% covered

Generated: Wed 2013-03-13 10:33 CET

Source file: /media/Envs/Envs/filer-gallery/lib/python2.7/site-packages/south/modelsinspector.py

Stats: 0 executed, 178 missed, 15 excluded, 269 ignored

  1. """
  2. Like the old south.modelsparser, but using introspection where possible
  3. rather than direct inspection of models.py.
  4. """
  5. import datetime
  6. import re
  7. import decimal
  8. from south.utils import get_attribute, auto_through
  9. from django.db import models
  10. from django.db.models.base import ModelBase, Model
  11. from django.db.models.fields import NOT_PROVIDED
  12. from django.conf import settings
  13. from django.utils.functional import Promise
  14. from django.contrib.contenttypes import generic
  15. from django.utils.datastructures import SortedDict
  16. from django.utils import datetime_safe
  17. NOISY = False
  18. try:
  19. from django.utils import timezone
  20. except ImportError:
  21. timezone = False
  22. # Define any converter functions first to prevent NameErrors
  23. def convert_on_delete_handler(value):
  24. django_db_models_module = 'models' # relative to standard import 'django.db'
  25. if hasattr(models, "PROTECT"):
  26. if value in (models.CASCADE, models.PROTECT, models.DO_NOTHING, models.SET_DEFAULT):
  27. # straightforward functions
  28. return '%s.%s' % (django_db_models_module, value.__name__)
  29. else:
  30. # This is totally dependent on the implementation of django.db.models.deletion.SET
  31. func_name = getattr(value, '__name__', None)
  32. if func_name == 'set_on_delete':
  33. # we must inspect the function closure to see what parameters were passed in
  34. closure_contents = value.func_closure[0].cell_contents
  35. if closure_contents is None:
  36. return "%s.SET_NULL" % (django_db_models_module)
  37. # simple function we can perhaps cope with:
  38. elif hasattr(closure_contents, '__call__'):
  39. raise ValueError("South does not support on_delete with SET(function) as values.")
  40. else:
  41. # Attempt to serialise the value
  42. return "%s.SET(%s)" % (django_db_models_module, value_clean(closure_contents))
  43. raise ValueError("%s was not recognized as a valid model deletion handler. Possible values: %s." % (value, ', '.join(f.__name__ for f in (models.CASCADE, models.PROTECT, models.SET, models.SET_NULL, models.SET_DEFAULT, models.DO_NOTHING))))
  44. else:
  45. raise ValueError("on_delete argument encountered in Django version that does not support it")
  46. # Gives information about how to introspect certain fields.
  47. # This is a list of triples; the first item is a list of fields it applies to,
  48. # (note that isinstance is used, so superclasses are perfectly valid here)
  49. # the second is a list of positional argument descriptors, and the third
  50. # is a list of keyword argument descriptors.
  51. # Descriptors are of the form:
  52. # [attrname, options]
  53. # Where attrname is the attribute on the field to get the value from, and options
  54. # is an optional dict.
  55. #
  56. # The introspector uses the combination of all matching entries, in order.
  57. introspection_details = [
  58. (
  59. (models.Field, ),
  60. [],
  61. {
  62. "null": ["null", {"default": False}],
  63. "blank": ["blank", {"default": False, "ignore_if":"primary_key"}],
  64. "primary_key": ["primary_key", {"default": False}],
  65. "max_length": ["max_length", {"default": None}],
  66. "unique": ["_unique", {"default": False}],
  67. "db_index": ["db_index", {"default": False}],
  68. "default": ["default", {"default": NOT_PROVIDED, "ignore_dynamics": True}],
  69. "db_column": ["db_column", {"default": None}],
  70. "db_tablespace": ["db_tablespace", {"default": settings.DEFAULT_INDEX_TABLESPACE}],
  71. },
  72. ),
  73. (
  74. (models.ForeignKey, models.OneToOneField),
  75. [],
  76. dict([
  77. ("to", ["rel.to", {}]),
  78. ("to_field", ["rel.field_name", {"default_attr": "rel.to._meta.pk.name"}]),
  79. ("related_name", ["rel.related_name", {"default": None}]),
  80. ("db_index", ["db_index", {"default": True}]),
  81. ("on_delete", ["rel.on_delete", {"default": getattr(models, "CASCADE", None), "is_django_function": True, "converter": convert_on_delete_handler, "ignore_missing": True}])
  82. ])
  83. ),
  84. (
  85. (models.ManyToManyField,),
  86. [],
  87. {
  88. "to": ["rel.to", {}],
  89. "symmetrical": ["rel.symmetrical", {"default": True}],
  90. "related_name": ["rel.related_name", {"default": None}],
  91. "db_table": ["db_table", {"default": None}],
  92. # TODO: Kind of ugly to add this one-time-only option
  93. "through": ["rel.through", {"ignore_if_auto_through": True}],
  94. },
  95. ),
  96. (
  97. (models.DateField, models.TimeField),
  98. [],
  99. {
  100. "auto_now": ["auto_now", {"default": False}],
  101. "auto_now_add": ["auto_now_add", {"default": False}],
  102. },
  103. ),
  104. (
  105. (models.DecimalField, ),
  106. [],
  107. {
  108. "max_digits": ["max_digits", {"default": None}],
  109. "decimal_places": ["decimal_places", {"default": None}],
  110. },
  111. ),
  112. (
  113. (models.SlugField, ),
  114. [],
  115. {
  116. "db_index": ["db_index", {"default": True}],
  117. },
  118. ),
  119. (
  120. (models.BooleanField, ),
  121. [],
  122. {
  123. "default": ["default", {"default": NOT_PROVIDED, "converter": bool}],
  124. "blank": ["blank", {"default": True, "ignore_if":"primary_key"}],
  125. },
  126. ),
  127. (
  128. (models.FilePathField, ),
  129. [],
  130. {
  131. "path": ["path", {"default": ''}],
  132. "match": ["match", {"default": None}],
  133. "recursive": ["recursive", {"default": False}],
  134. },
  135. ),
  136. (
  137. (generic.GenericRelation, ),
  138. [],
  139. {
  140. "to": ["rel.to", {}],
  141. "symmetrical": ["rel.symmetrical", {"default": True}],
  142. "object_id_field": ["object_id_field_name", {"default": "object_id"}],
  143. "content_type_field": ["content_type_field_name", {"default": "content_type"}],
  144. "blank": ["blank", {"default": True}],
  145. },
  146. ),
  147. ]
  148. # Regexes of allowed field full paths
  149. allowed_fields = [
  150. "^django\.db",
  151. "^django\.contrib\.contenttypes\.generic",
  152. "^django\.contrib\.localflavor",
  153. ]
  154. # Regexes of ignored fields (custom fields which look like fields, but have no column behind them)
  155. ignored_fields = [
  156. "^django\.contrib\.contenttypes\.generic\.GenericRelation",
  157. "^django\.contrib\.contenttypes\.generic\.GenericForeignKey",
  158. ]
  159. # Similar, but for Meta, so just the inner level (kwds).
  160. meta_details = {
  161. "db_table": ["db_table", {"default_attr_concat": ["%s_%s", "app_label", "module_name"]}],
  162. "db_tablespace": ["db_tablespace", {"default": settings.DEFAULT_TABLESPACE}],
  163. "unique_together": ["unique_together", {"default": []}],
  164. "ordering": ["ordering", {"default": []}],
  165. "proxy": ["proxy", {"default": False, "ignore_missing": True}],
  166. }
  167. # 2.4 compatability
  168. any = lambda x: reduce(lambda y, z: y or z, x, False)
  169. def add_introspection_rules(rules=[], patterns=[]):
  170. "Allows you to add some introspection rules at runtime, e.g. for 3rd party apps."
  171. assert isinstance(rules, (list, tuple))
  172. assert isinstance(patterns, (list, tuple))
  173. allowed_fields.extend(patterns)
  174. introspection_details.extend(rules)
  175. def add_ignored_fields(patterns):
  176. "Allows you to add some ignore field patterns."
  177. assert isinstance(patterns, (list, tuple))
  178. ignored_fields.extend(patterns)
  179. def can_ignore(field):
  180. """
  181. Returns True if we know for certain that we can ignore this field, False
  182. otherwise.
  183. """
  184. full_name = "%s.%s" % (field.__class__.__module__, field.__class__.__name__)
  185. for regex in ignored_fields:
  186. if re.match(regex, full_name):
  187. return True
  188. return False
  189. def can_introspect(field):
  190. """
  191. Returns True if we are allowed to introspect this field, False otherwise.
  192. ('allowed' means 'in core'. Custom fields can declare they are introspectable
  193. by the default South rules by adding the attribute _south_introspects = True.)
  194. """
  195. # Check for special attribute
  196. if hasattr(field, "_south_introspects") and field._south_introspects:
  197. return True
  198. # Check it's an introspectable field
  199. full_name = "%s.%s" % (field.__class__.__module__, field.__class__.__name__)
  200. for regex in allowed_fields:
  201. if re.match(regex, full_name):
  202. return True
  203. return False
  204. def matching_details(field):
  205. """
  206. Returns the union of all matching entries in introspection_details for the field.
  207. """
  208. our_args = []
  209. our_kwargs = {}
  210. for classes, args, kwargs in introspection_details:
  211. if any([isinstance(field, x) for x in classes]):
  212. our_args.extend(args)
  213. our_kwargs.update(kwargs)
  214. return our_args, our_kwargs
  215. class IsDefault(Exception):
  216. """
  217. Exception for when a field contains its default value.
  218. """
  219. def get_value(field, descriptor):
  220. """
  221. Gets an attribute value from a Field instance and formats it.
  222. """
  223. attrname, options = descriptor
  224. # If the options say it's not a attribute name but a real value, use that.
  225. if options.get('is_value', False):
  226. value = attrname
  227. else:
  228. try:
  229. value = get_attribute(field, attrname)
  230. except AttributeError:
  231. if options.get("ignore_missing", False):
  232. raise IsDefault
  233. else:
  234. raise
  235. # Lazy-eval functions get eval'd.
  236. if isinstance(value, Promise):
  237. value = unicode(value)
  238. # If the value is the same as the default, omit it for clarity
  239. if "default" in options and value == options['default']:
  240. raise IsDefault
  241. # If there's an ignore_if, use it
  242. if "ignore_if" in options:
  243. if get_attribute(field, options['ignore_if']):
  244. raise IsDefault
  245. # If there's an ignore_if_auto_through which is True, use it
  246. if options.get("ignore_if_auto_through", False):
  247. if auto_through(field):
  248. raise IsDefault
  249. # Some default values need to be gotten from an attribute too.
  250. if "default_attr" in options:
  251. default_value = get_attribute(field, options['default_attr'])
  252. if value == default_value:
  253. raise IsDefault
  254. # Some are made from a formatting string and several attrs (e.g. db_table)
  255. if "default_attr_concat" in options:
  256. format, attrs = options['default_attr_concat'][0], options['default_attr_concat'][1:]
  257. default_value = format % tuple(map(lambda x: get_attribute(field, x), attrs))
  258. if value == default_value:
  259. raise IsDefault
  260. # Clean and return the value
  261. return value_clean(value, options)
  262. def value_clean(value, options={}):
  263. "Takes a value and cleans it up (so e.g. it has timezone working right)"
  264. # Lazy-eval functions get eval'd.
  265. if isinstance(value, Promise):
  266. value = unicode(value)
  267. # Callables get called.
  268. if not options.get('is_django_function', False) and callable(value) and not isinstance(value, ModelBase):
  269. # Datetime.datetime.now is special, as we can access it from the eval
  270. # context (and because it changes all the time; people will file bugs otherwise).
  271. if value == datetime.datetime.now:
  272. return "datetime.datetime.now"
  273. elif value == datetime.datetime.utcnow:
  274. return "datetime.datetime.utcnow"
  275. elif value == datetime.date.today:
  276. return "datetime.date.today"
  277. # In case we use Django's own now function, revert to datetime's
  278. # original one since we'll deal with timezones on our own.
  279. elif timezone and value == timezone.now:
  280. return "datetime.datetime.now"
  281. # All other callables get called.
  282. value = value()
  283. # Models get their own special repr()
  284. if isinstance(value, ModelBase):
  285. # If it's a proxy model, follow it back to its non-proxy parent
  286. if getattr(value._meta, "proxy", False):
  287. value = value._meta.proxy_for_model
  288. return "orm['%s.%s']" % (value._meta.app_label, value._meta.object_name)
  289. # As do model instances
  290. if isinstance(value, Model):
  291. if options.get("ignore_dynamics", False):
  292. raise IsDefault
  293. return "orm['%s.%s'].objects.get(pk=%r)" % (value.__class__._meta.app_label, value.__class__._meta.object_name, value.pk)
  294. # Make sure Decimal is converted down into a string
  295. if isinstance(value, decimal.Decimal):
  296. value = str(value)
  297. # in case the value is timezone aware
  298. datetime_types = (
  299. datetime.datetime,
  300. datetime.time,
  301. datetime_safe.datetime,
  302. )
  303. if (timezone and isinstance(value, datetime_types) and
  304. getattr(settings, 'USE_TZ', False) and
  305. value is not None and timezone.is_aware(value)):
  306. default_timezone = timezone.get_default_timezone()
  307. value = timezone.make_naive(value, default_timezone)
  308. # datetime_safe has an improper repr value
  309. if isinstance(value, datetime_safe.datetime):
  310. value = datetime.datetime(*value.utctimetuple()[:7])
  311. # converting a date value to a datetime to be able to handle
  312. # timezones later gracefully
  313. elif isinstance(value, (datetime.date, datetime_safe.date)):
  314. value = datetime.datetime(*value.timetuple()[:3])
  315. # Now, apply the converter func if there is one
  316. if "converter" in options:
  317. value = options['converter'](value)
  318. # Return the final value
  319. if options.get('is_django_function', False):
  320. return value
  321. else:
  322. return repr(value)
  323. def introspector(field):
  324. """
  325. Given a field, introspects its definition triple.
  326. """
  327. arg_defs, kwarg_defs = matching_details(field)
  328. args = []
  329. kwargs = {}
  330. # For each argument, use the descriptor to get the real value.
  331. for defn in arg_defs:
  332. try:
  333. args.append(get_value(field, defn))
  334. except IsDefault:
  335. pass
  336. for kwd, defn in kwarg_defs.items():
  337. try:
  338. kwargs[kwd] = get_value(field, defn)
  339. except IsDefault:
  340. pass
  341. return args, kwargs
  342. def get_model_fields(model, m2m=False):
  343. """
  344. Given a model class, returns a dict of {field_name: field_triple} defs.
  345. """
  346. field_defs = SortedDict()
  347. inherited_fields = {}
  348. # Go through all bases (that are themselves models, but not Model)
  349. for base in model.__bases__:
  350. if hasattr(base, '_meta') and issubclass(base, models.Model):
  351. if not base._meta.abstract:
  352. # Looks like we need their fields, Ma.
  353. inherited_fields.update(get_model_fields(base))
  354. # Now, go through all the fields and try to get their definition
  355. source = model._meta.local_fields[:]
  356. if m2m:
  357. source += model._meta.local_many_to_many
  358. for field in source:
  359. # Can we ignore it completely?
  360. if can_ignore(field):
  361. continue
  362. # Does it define a south_field_triple method?
  363. if hasattr(field, "south_field_triple"):
  364. if NOISY:
  365. print " ( Nativing field: %s" % field.name
  366. field_defs[field.name] = field.south_field_triple()
  367. # Can we introspect it?
  368. elif can_introspect(field):
  369. # Get the full field class path.
  370. field_class = field.__class__.__module__ + "." + field.__class__.__name__
  371. # Run this field through the introspector
  372. args, kwargs = introspector(field)
  373. # Workaround for Django bug #13987
  374. if model._meta.pk.column == field.column and 'primary_key' not in kwargs:
  375. kwargs['primary_key'] = True
  376. # That's our definition!
  377. field_defs[field.name] = (field_class, args, kwargs)
  378. # Shucks, no definition!
  379. else:
  380. if NOISY:
  381. print " ( Nodefing field: %s" % field.name
  382. field_defs[field.name] = None
  383. # If they've used the horrific hack that is order_with_respect_to, deal with
  384. # it.
  385. if model._meta.order_with_respect_to:
  386. field_defs['_order'] = ("django.db.models.fields.IntegerField", [], {"default": "0"})
  387. return field_defs
  388. def get_model_meta(model):
  389. """
  390. Given a model class, will return the dict representing the Meta class.
  391. """
  392. # Get the introspected attributes
  393. meta_def = {}
  394. for kwd, defn in meta_details.items():
  395. try:
  396. meta_def[kwd] = get_value(model._meta, defn)
  397. except IsDefault:
  398. pass
  399. # Also, add on any non-abstract model base classes.
  400. # This is called _ormbases as the _bases variable was previously used
  401. # for a list of full class paths to bases, so we can't conflict.
  402. for base in model.__bases__:
  403. if hasattr(base, '_meta') and issubclass(base, models.Model):
  404. if not base._meta.abstract:
  405. # OK, that matches our terms.
  406. if "_ormbases" not in meta_def:
  407. meta_def['_ormbases'] = []
  408. meta_def['_ormbases'].append("%s.%s" % (
  409. base._meta.app_label,
  410. base._meta.object_name,
  411. ))
  412. return meta_def
  413. # Now, load the built-in South introspection plugins
  414. import south.introspection_plugins