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 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. # Match only the basename.
  21. if matches_patterns(fn, ignore_patterns):
  22. continue
  23. if location:
  24. fn = os.path.join(location, fn)
  25. # Match the full file path.
  26. if matches_patterns(fn, ignore_patterns):
  27. continue
  28. yield fn
  29. for dir in directories:
  30. if matches_patterns(dir, ignore_patterns):
  31. continue
  32. if location:
  33. dir = os.path.join(location, dir)
  34. yield from get_files(storage, ignore_patterns, dir)
  35. def check_settings(base_url=None):
  36. """
  37. Check if the staticfiles settings have sane values.
  38. """
  39. if base_url is None:
  40. base_url = settings.STATIC_URL
  41. if not base_url:
  42. raise ImproperlyConfigured(
  43. "You're using the staticfiles app "
  44. "without having set the required STATIC_URL setting.")
  45. if settings.MEDIA_URL == base_url:
  46. raise ImproperlyConfigured("The MEDIA_URL and STATIC_URL "
  47. "settings must have different values")
  48. if (settings.DEBUG and settings.MEDIA_URL and settings.STATIC_URL and
  49. settings.MEDIA_URL.startswith(settings.STATIC_URL)):
  50. raise ImproperlyConfigured(
  51. "runserver can't serve media if MEDIA_URL is within STATIC_URL."
  52. )
  53. if ((settings.MEDIA_ROOT and settings.STATIC_ROOT) and
  54. (settings.MEDIA_ROOT == settings.STATIC_ROOT)):
  55. raise ImproperlyConfigured("The MEDIA_ROOT and STATIC_ROOT "
  56. "settings must have different values")