cms.plugins.file.models: 31 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/cms/plugins/file/models.py

Stats: 0 executed, 20 missed, 11 excluded, 26 ignored

  1. from django.db import models
  2. from django.utils.translation import ugettext_lazy as _
  3. from cms.models import CMSPlugin
  4. import os
  5. from django.conf import settings
  6. class File(CMSPlugin):
  7. """
  8. Plugin for storing any type of file.
  9. Default template displays download link with icon (if available) and file size.
  10. Icons are searched for within <MEDIA_ROOT>/<CMS_FILE_ICON_PATH>
  11. (CMS_FILE_ICON_PATH is a plugin-specific setting which defaults to "<CMS_MEDIA_PATH>/images/file_icons")
  12. with filenames of the form <file_ext>.<icon_ext>, where <file_ext> is the extension
  13. of the file itself, and <icon_ext> is one of <CMS_FILE_ICON_EXTENSIONS>
  14. (another plugin specific setting, which defaults to ('gif', 'png'))
  15. This could be updated to use the mimetypes library to determine the type of file rather than
  16. storing a separate icon for each different extension.
  17. The icon search is currently performed within get_icon_url; this is probably a performance concern.
  18. """
  19. file = models.FileField(_("file"), upload_to=CMSPlugin.get_media_path)
  20. title = models.CharField(_("title"), max_length=255, null=True, blank=True)
  21. # CMS_ICON_EXTENSIONS and CMS_ICON_PATH are assumed to be plugin-specific, and not included in cms.settings
  22. # -- they are therefore imported from django.conf.settings
  23. ICON_EXTENSIONS = getattr(settings, "CMS_FILE_ICON_EXTENSIONS", ('gif', 'png'))
  24. ICON_PATH = getattr(settings, "CMS_FILE_ICON_PATH", os.path.join(settings.STATIC_ROOT, "cms", "images", "file_icons"))
  25. ICON_URL = getattr(settings, "CMS_FILE_ICON_URL", "%s%s/%s/%s/" % (settings.STATIC_URL, "cms", "images", "file_icons"))
  26. def get_icon_url(self):
  27. path_base = os.path.join(self.ICON_PATH, self.get_ext())
  28. url_base = '%s%s' % (self.ICON_URL, self.get_ext())
  29. for ext in self.ICON_EXTENSIONS:
  30. if os.path.exists("%s.%s" % (path_base, ext)):
  31. return "%s.%s" % (url_base, ext)
  32. return None
  33. def file_exists(self):
  34. return os.path.exists(self.file.path)
  35. def get_file_name(self):
  36. return os.path.basename(self.file.path)
  37. def get_ext(self):
  38. return os.path.splitext(self.get_file_name())[1][1:].lower()
  39. def __unicode__(self):
  40. if self.title:
  41. return self.title;
  42. elif self.file:
  43. # added if, because it raised attribute error when file wasnt defined
  44. return self.get_file_name();
  45. return "<empty>"
  46. search_fields = ('title',)