utils.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. import collections
  2. from django.db.models import Q
  3. from django.utils.deconstruct import deconstructible
  4. from taggit.managers import _TaggableManager
  5. from extras.constants import EXTRAS_FEATURES
  6. from extras.registry import registry
  7. def is_taggable(obj):
  8. """
  9. Return True if the instance can have Tags assigned to it; False otherwise.
  10. """
  11. if hasattr(obj, 'tags'):
  12. if issubclass(obj.tags.__class__, _TaggableManager):
  13. return True
  14. return False
  15. def image_upload(instance, filename):
  16. """
  17. Return a path for uploading image attchments.
  18. """
  19. path = 'image-attachments/'
  20. # Rename the file to the provided name, if any. Attempt to preserve the file extension.
  21. extension = filename.rsplit('.')[-1].lower()
  22. if instance.name and extension in ['bmp', 'gif', 'jpeg', 'jpg', 'png']:
  23. filename = '.'.join([instance.name, extension])
  24. elif instance.name:
  25. filename = instance.name
  26. return '{}{}_{}_{}'.format(path, instance.content_type.name, instance.object_id, filename)
  27. @deconstructible
  28. class FeatureQuery:
  29. """
  30. Helper class that delays evaluation of the registry contents for the functionality store
  31. until it has been populated.
  32. """
  33. def __init__(self, feature):
  34. self.feature = feature
  35. def __call__(self):
  36. return self.get_query()
  37. def get_query(self):
  38. """
  39. Given an extras feature, return a Q object for content type lookup
  40. """
  41. query = Q()
  42. for app_label, models in registry['model_features'][self.feature].items():
  43. query |= Q(app_label=app_label, model__in=models)
  44. return query
  45. def extras_features(*features):
  46. """
  47. Decorator used to register extras provided features to a model
  48. """
  49. def wrapper(model_class):
  50. # Initialize the model_features store if not already defined
  51. if 'model_features' not in registry:
  52. registry['model_features'] = {
  53. f: collections.defaultdict(list) for f in EXTRAS_FEATURES
  54. }
  55. for feature in features:
  56. if feature in EXTRAS_FEATURES:
  57. app_label, model_name = model_class._meta.label_lower.split('.')
  58. registry['model_features'][feature][app_label].append(model_name)
  59. else:
  60. raise ValueError('{} is not a valid extras feature!'.format(feature))
  61. return model_class
  62. return wrapper