utils.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from taggit.managers import _TaggableManager
  2. def is_taggable(obj):
  3. """
  4. Return True if the instance can have Tags assigned to it; False otherwise.
  5. """
  6. if hasattr(obj, 'tags'):
  7. if issubclass(obj.tags.__class__, _TaggableManager):
  8. return True
  9. return False
  10. def image_upload(instance, filename):
  11. """
  12. Return a path for uploading image attachments.
  13. """
  14. path = 'image-attachments/'
  15. # Rename the file to the provided name, if any. Attempt to preserve the file extension.
  16. extension = filename.rsplit('.')[-1].lower()
  17. if instance.name and extension in ['bmp', 'gif', 'jpeg', 'jpg', 'png']:
  18. filename = '.'.join([instance.name, extension])
  19. elif instance.name:
  20. filename = instance.name
  21. return '{}{}_{}_{}'.format(path, instance.object_type.name, instance.object_id, filename)
  22. def is_script(obj):
  23. """
  24. Returns True if the object is a Script or Report.
  25. """
  26. from .reports import Report
  27. from .scripts import Script
  28. try:
  29. return (issubclass(obj, Report) and obj != Report) or (issubclass(obj, Script) and obj != Script)
  30. except TypeError:
  31. return False
  32. def is_report(obj):
  33. """
  34. Returns True if the given object is a Report.
  35. """
  36. from .reports import Report
  37. try:
  38. return issubclass(obj, Report) and obj != Report
  39. except TypeError:
  40. return False