utils.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. """
  2. utils.py # Shared helper functions
  3. See schemas.__init__.py for package overview.
  4. """
  5. from django.db import models
  6. from django.utils.translation import gettext_lazy as _
  7. from rest_framework.mixins import RetrieveModelMixin
  8. def is_list_view(path, method, view):
  9. """
  10. Return True if the given path/method appears to represent a list view.
  11. """
  12. if hasattr(view, 'action'):
  13. # Viewsets have an explicitly defined action, which we can inspect.
  14. return view.action == 'list'
  15. if method.lower() != 'get':
  16. return False
  17. if isinstance(view, RetrieveModelMixin):
  18. return False
  19. path_components = path.strip('/').split('/')
  20. if path_components and '{' in path_components[-1]:
  21. return False
  22. return True
  23. def get_pk_description(model, model_field):
  24. if isinstance(model_field, models.AutoField):
  25. value_type = _('unique integer value')
  26. elif isinstance(model_field, models.UUIDField):
  27. value_type = _('UUID string')
  28. else:
  29. value_type = _('unique value')
  30. return _('A {value_type} identifying this {name}.').format(
  31. value_type=value_type,
  32. name=model._meta.verbose_name,
  33. )