Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.py 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import fnmatch
  2. import os
  3. from django.conf import settings
  4. from django.core.exceptions import ImproperlyConfigured
  5. def matches_patterns(path, patterns=None):
  6. """
  7. Return True or False depending on whether the ``path`` should be
  8. ignored (if it matches any pattern in ``ignore_patterns``).
  9. """
  10. return any(fnmatch.fnmatchcase(path, pattern) for pattern in (patterns or []))
  11. def get_files(storage, ignore_patterns=None, location=''):
  12. """
  13. Recursively walk the storage directories yielding the paths
  14. of all files that should be copied.
  15. """
  16. if ignore_patterns is None:
  17. ignore_patterns = []
  18. directories, files = storage.listdir(location)
  19. for fn in files:
  20. if matches_patterns(fn, ignore_patterns):
  21. continue
  22. if location:
  23. fn = os.path.join(location, fn)
  24. yield fn
  25. for dir in directories:
  26. if matches_patterns(dir, ignore_patterns):
  27. continue
  28. if location:
  29. dir = os.path.join(location, dir)
  30. yield from get_files(storage, ignore_patterns, dir)
  31. def check_settings(base_url=None):
  32. """
  33. Check if the staticfiles settings have sane values.
  34. """
  35. if base_url is None:
  36. base_url = settings.STATIC_URL
  37. if not base_url:
  38. raise ImproperlyConfigured(
  39. "You're using the staticfiles app "
  40. "without having set the required STATIC_URL setting.")
  41. if settings.MEDIA_URL == base_url:
  42. raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL "
  43. "settings must have different values")
  44. if ((settings.MEDIA_ROOT and settings.STATIC_ROOT) and
  45. (settings.MEDIA_ROOT == settings.STATIC_ROOT)):
  46. raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT "
  47. "settings must have different values")