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