2017-09-14 11:46:34 +03:00
|
|
|
"""
|
|
|
|
utils.py # Shared helper functions
|
|
|
|
|
|
|
|
See schemas.__init__.py for package overview.
|
|
|
|
"""
|
2019-05-13 17:07:03 +03:00
|
|
|
from django.db import models
|
2019-12-05 01:14:43 +03:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2019-05-13 17:07:03 +03:00
|
|
|
|
2017-10-05 21:41:14 +03:00
|
|
|
from rest_framework.mixins import RetrieveModelMixin
|
2017-09-14 11:46:34 +03:00
|
|
|
|
|
|
|
|
|
|
|
def is_list_view(path, method, view):
|
|
|
|
"""
|
|
|
|
Return True if the given path/method appears to represent a list view.
|
|
|
|
"""
|
|
|
|
if hasattr(view, 'action'):
|
|
|
|
# Viewsets have an explicitly defined action, which we can inspect.
|
|
|
|
return view.action == 'list'
|
|
|
|
|
|
|
|
if method.lower() != 'get':
|
|
|
|
return False
|
2017-10-05 21:41:14 +03:00
|
|
|
if isinstance(view, RetrieveModelMixin):
|
|
|
|
return False
|
2017-09-14 11:46:34 +03:00
|
|
|
path_components = path.strip('/').split('/')
|
|
|
|
if path_components and '{' in path_components[-1]:
|
|
|
|
return False
|
|
|
|
return True
|
2019-05-13 17:07:03 +03:00
|
|
|
|
|
|
|
|
|
|
|
def get_pk_description(model, model_field):
|
|
|
|
if isinstance(model_field, models.AutoField):
|
|
|
|
value_type = _('unique integer value')
|
|
|
|
elif isinstance(model_field, models.UUIDField):
|
|
|
|
value_type = _('UUID string')
|
|
|
|
else:
|
|
|
|
value_type = _('unique value')
|
|
|
|
|
|
|
|
return _('A {value_type} identifying this {name}.').format(
|
|
|
|
value_type=value_type,
|
|
|
|
name=model._meta.verbose_name,
|
|
|
|
)
|