From 92d8e81accd8f6fbf372a32426e91b12d16a3ced Mon Sep 17 00:00:00 2001 From: Adelson Lima Date: Sat, 7 May 2022 21:28:35 -0300 Subject: [PATCH] =?UTF-8?q?Corre=C3=A7=C3=A3o=20na=20gera=C3=A7=C3=A3o=20d?= =?UTF-8?q?a=20documenta=C3=A7=C3=A3o=20da=20api?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sloth/core/base.py | 15 +++++++++------ sloth/decorators/__init__.py | 3 +++ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/sloth/core/base.py b/sloth/core/base.py index a6da47b4..8f2d9b5b 100644 --- a/sloth/core/base.py +++ b/sloth/core/base.py @@ -1,6 +1,7 @@ from django.apps import apps from django.core.exceptions import FieldDoesNotExist from django.template.loader import render_to_string +from sloth.utils import pretty from sloth.actions import Action from sloth.core.values import ValueSet @@ -301,20 +302,23 @@ def get_api_paths(cls, request): url = '/api/{}/{}/'.format(cls.metaclass().app_label, cls.metaclass().model_name) info = dict() - info[url] = [('get', 'List', 'List objects', {'type': 'string'}, None)] + info[url] = [ + ('get', 'List', 'List objects', {'type': 'string'}, None), + ('post', 'Add', 'Add object', {'type': 'string'}, cls.add_form_cls()), + ('put', 'Edit', 'Edit object', {'type': 'string'}, cls.edit_form_cls()), + ] info['{}{{id}}/'.format(url)] = [ ('get', 'View', 'View object', instance.view().get_api_schema(), None), - ('post', 'Add', 'Add object', {'type': 'string'}, cls.add_form_cls()), - ('put', 'Edit', 'Edit object', {'type': 'string'}, None), ] for name, attr in cls.__dict__.items(): - if hasattr(attr, 'decorated'): + if name.startswith('get_') and not hasattr(attr, 'decorated'): try: v = getattr(instance, name)() except BaseException as e: continue + verbose_name = pretty(name) info['{}{{id}}/{}/'.format(url, name)] = [ - ('get', attr.verbose_name, 'View {}'.format(attr.verbose_name), {'type': 'string'}, None), + ('get', verbose_name, 'View {}'.format(verbose_name), {'type': 'string'}, None), ] if isinstance(v, ValueSet): for action in v.metadata['actions']: @@ -327,7 +331,6 @@ def get_api_paths(cls, request): info['{}{{id}}/{}/{{ids}}/{}/'.format(url, name, action)] = [ ('post', action, 'Execute {}'.format(action), {'type': 'string'}, forms_cls), ] - paths = {} for url, data in info.items(): paths[url] = {} diff --git a/sloth/decorators/__init__.py b/sloth/decorators/__init__.py index 674e01d9..9bd05b13 100644 --- a/sloth/decorators/__init__.py +++ b/sloth/decorators/__init__.py @@ -3,6 +3,7 @@ def verbose_name(name): def decorate(func): + setattr(func, 'decorated', True) setattr(func, '__verbose_name__', name) return func return decorate @@ -10,6 +11,7 @@ def decorate(func): def template(name): def decorate(func): + setattr(func, 'decorated', True) setattr(func, '__template__', name) return func return decorate @@ -17,6 +19,7 @@ def decorate(func): def role(name, username, email=None, password=None, **scopes): def decorate(cls): + setattr(cls, 'decorated', True) if not hasattr(cls, '__roles__'): setattr(cls, '__roles__', []) roles = getattr(cls, '__roles__')