easy_thumbnails.alias: 46 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/easy_thumbnails/alias.py

Stats: 0 executed, 45 missed, 1 excluded, 77 ignored

  1. from easy_thumbnails.conf import settings
  2. class Aliases(object):
  3. """
  4. A container which stores and retrieves named easy-thumbnail options
  5. dictionaries.
  6. """
  7. def __init__(self, populate_from_settings=True):
  8. """
  9. Initialize the Aliases object.
  10. :param populate_from_settings: If ``True`` (default) then populate the
  11. initial aliases from settings. See :meth:`populate_from_settings`.
  12. """
  13. self._aliases = {}
  14. if populate_from_settings:
  15. self.populate_from_settings()
  16. def populate_from_settings(self):
  17. """
  18. Populate the aliases from the ``THUMBNAIL_ALIASES`` setting.
  19. """
  20. settings_aliases = settings.THUMBNAIL_ALIASES
  21. if settings_aliases:
  22. for target, aliases in settings_aliases.items():
  23. target_aliases = self._aliases.setdefault(target, {})
  24. target_aliases.update(aliases)
  25. def set(self, alias, options, target=None):
  26. """
  27. Add an alias.
  28. :param alias: The name of the alias to add.
  29. :param options: The easy-thumbnails options dictonary for this alias
  30. (should include ``size``).
  31. :param target: A field, model, or app to limit this alias to
  32. (optional).
  33. """
  34. target = self._coerce_target(target) or ''
  35. target_aliases = self._aliases.setdefault(target, {})
  36. target_aliases[alias] = options
  37. def get(self, alias, target=None):
  38. """
  39. Get a dictionary of aliased options.
  40. :param alias: The name of the aliased options.
  41. :param target: Get alias for this specific target (optional).
  42. If no matching alias is found, returns ``None``.
  43. """
  44. for target_part in reversed(list(self._get_targets(target))):
  45. options = self._get(target_part, alias)
  46. if options:
  47. return options
  48. return self._get('', alias)
  49. def all(self, target=None, include_global=True):
  50. """
  51. Get a dictionary of all aliases and their options.
  52. :param target: Include aliases for this specific field, model or app
  53. (optional).
  54. :param include_global: Include all non target-specific aliases
  55. (default ``True``).
  56. For example::
  57. >>> aliases.all(target='my_app.MyModel')
  58. {'small': {'size': (100, 100)}, 'large': {'size': (400, 400)}}
  59. """
  60. aliases = {}
  61. for target_part in self._get_targets(target, include_global):
  62. aliases.update(self._aliases.get(target_part, {}))
  63. return aliases
  64. def _get(self, target, alias):
  65. """
  66. Internal method to get a specific alias.
  67. """
  68. if target not in self._aliases:
  69. return
  70. return self._aliases[target].get(alias)
  71. def _get_targets(self, target, include_global=True):
  72. """
  73. Internal iterator to split up a complete target into the possible parts
  74. it may match.
  75. For example::
  76. >>> list(aliases._get_targets('my_app.MyModel.somefield'))
  77. ['', 'my_app', 'my_app.MyModel', 'my_app.MyModel.somefield']
  78. """
  79. target = self._coerce_target(target)
  80. if include_global:
  81. yield ''
  82. if not target:
  83. return
  84. target_bits = target.split('.')
  85. for i in range(len(target_bits)):
  86. yield '.'.join(target_bits[:i + 1])
  87. def _coerce_target(self, target):
  88. """
  89. Internal method to coerce a target to a string.
  90. The assumption is that if it is not ``None`` and not a string, it is
  91. a Django ``FieldFile`` object.
  92. """
  93. if not target or isinstance(target, basestring):
  94. return target
  95. model = target.instance.__class__
  96. return '%s.%s.%s' % (
  97. model._meta.app_label,
  98. model.__name__,
  99. target.field.name,
  100. )
  101. aliases = Aliases()