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.

findstatic.py 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import os
  2. from django.contrib.staticfiles import finders
  3. from django.core.management.base import LabelCommand
  4. class Command(LabelCommand):
  5. help = "Finds the absolute paths for the given static file(s)."
  6. label = 'staticfile'
  7. def add_arguments(self, parser):
  8. super().add_arguments(parser)
  9. parser.add_argument(
  10. '--first', action='store_false', dest='all',
  11. help="Only return the first match for each static file.",
  12. )
  13. def handle_label(self, path, **options):
  14. verbosity = options['verbosity']
  15. result = finders.find(path, all=options['all'])
  16. if verbosity >= 2:
  17. searched_locations = (
  18. "\nLooking in the following locations:\n %s" %
  19. "\n ".join(finders.searched_locations)
  20. )
  21. else:
  22. searched_locations = ''
  23. if result:
  24. if not isinstance(result, (list, tuple)):
  25. result = [result]
  26. result = (os.path.realpath(path) for path in result)
  27. if verbosity >= 1:
  28. file_list = '\n '.join(result)
  29. return ("Found '%s' here:\n %s%s" %
  30. (path, file_list, searched_locations))
  31. else:
  32. return '\n'.join(result)
  33. else:
  34. message = ["No matching file found for '%s'." % path]
  35. if verbosity >= 2:
  36. message.append(searched_locations)
  37. if verbosity >= 1:
  38. self.stderr.write('\n'.join(message))