From 50951f0687ed267a789d5836a6bc510080a3be2e Mon Sep 17 00:00:00 2001 From: LeandroJatai Date: Sat, 30 Jul 2022 16:11:11 -0300 Subject: [PATCH] Insere btn openapi em detail crud e metadata field no serializer da api --- sapl/api/core/__init__.py | 19 +++++++++++++++++-- sapl/api/core/filters.py | 12 +++++++++--- sapl/base/models.py | 3 +++ sapl/crud/base.py | 10 +++++++++- sapl/settings.py | 5 +++-- sapl/templates/crud/detail.html | 13 +++++++++++++ sapl/templates/crud/detail_detail.html | 12 +++++++++++- 7 files changed, 65 insertions(+), 9 deletions(-) diff --git a/sapl/api/core/__init__.py b/sapl/api/core/__init__.py index 5b4aa0a4e..2ecab98fd 100644 --- a/sapl/api/core/__init__.py +++ b/sapl/api/core/__init__.py @@ -22,6 +22,7 @@ from rest_framework.viewsets import ModelViewSet from sapl.api.core.filters import SaplFilterSetMixin from sapl.api.permissions import SaplModelPermissions +from sapl.base.models import Metadata # ATENÇÃO: MUDANÇAS NO CORE DEVEM SER REALIZADAS COM # EXTREMA CAUTELA @@ -100,6 +101,7 @@ class SaplApiViewSetConstrutor(): # criada a classe sapl.api.core.serializers.{model}Serializer class SaplSerializer(_serializer_class): __str__ = SerializerMethodField() + metadata = SerializerMethodField() class Meta(_meta_serializer): if not hasattr(_meta_serializer, 'ref_name'): @@ -114,14 +116,26 @@ class SaplApiViewSetConstrutor(): if not hasattr(_meta_serializer, 'fields'): fields = '__all__' elif _meta_serializer.fields != '__all__': - fields = list( - _meta_serializer.fields) + ['__str__', ] + fields = list(_meta_serializer.fields) + [ + '__str__', 'metadata'] else: fields = _meta_serializer.fields def get___str__(self, obj) -> str: return str(obj) + def get_metadata(self, obj): + try: + metadata = Metadata.objects.get( + content_type=ContentType.objects.get_for_model( + obj._meta.model), + object_id=obj.id + ).metadata + except: + metadata = {} + finally: + return metadata + _meta_filterset = object if not hasattr( _filterset_class, 'Meta') else _filterset_class.Meta @@ -162,6 +176,7 @@ class SaplApiViewSetConstrutor(): return cls + """ 1. Constroi uma rest_framework.viewsets.ModelViewSet para todos os models de todas as apps do sapl diff --git a/sapl/api/core/filters.py b/sapl/api/core/filters.py index 90ef46bfa..0007906c6 100644 --- a/sapl/api/core/filters.py +++ b/sapl/api/core/filters.py @@ -1,18 +1,18 @@ from collections import OrderedDict +from django.contrib.postgres.fields.jsonb import JSONField from django.db.models.fields.files import FileField from django.template.defaultfilters import capfirst +import django_filters from django_filters.constants import ALL_FIELDS from django_filters.filters import CharFilter from django_filters.filterset import FilterSet from django_filters.utils import resolve_field, get_all_model_fields -import django_filters + # ATENÇÃO: MUDANÇAS NO CORE DEVEM SER REALIZADAS COM # EXTREMA CAUTELA E CONSCIENTE DOS IMPACTOS NA API - - class SaplFilterSetMixin(FilterSet): o = CharFilter(method='filter_o') @@ -26,6 +26,12 @@ class SaplFilterSetMixin(FilterSet): 'lookup_expr': 'exact', }, }, + JSONField: { + 'filter_class': django_filters.CharFilter, + 'extra': lambda f: { + 'lookup_expr': 'exact', + }, + }, } def filter_o(self, queryset, name, value): diff --git a/sapl/base/models.py b/sapl/base/models.py index ad8b25fa7..c27db2748 100644 --- a/sapl/base/models.py +++ b/sapl/base/models.py @@ -414,3 +414,6 @@ class Metadata(models.Model): verbose_name = _('Metadado') verbose_name_plural = _('Metadados') unique_together = (('content_type', 'object_id'), ) + + def __str__(self): + return f'Metadata de {self.content_object}' diff --git a/sapl/crud/base.py b/sapl/crud/base.py index 5f080758e..5b0aab24c 100644 --- a/sapl/crud/base.py +++ b/sapl/crud/base.py @@ -25,7 +25,7 @@ from django.views.generic.list import MultipleObjectMixin from sapl.crispy_layout_mixin import CrispyLayoutFormMixin, get_field_display from sapl.crispy_layout_mixin import SaplFormHelper from sapl.rules import (RP_ADD, RP_CHANGE, RP_DELETE, RP_DETAIL, - RP_LIST) + RP_LIST) from sapl.utils import normalize logger = logging.getLogger(settings.BASE_DIR.name) @@ -78,6 +78,7 @@ def make_pagination(index, num_pages): head = from_to(1, PAGINATION_LENGTH - len(tail) - 1) return head + [None] + tail + """ variáveis do crud: help_topic @@ -360,6 +361,13 @@ class CrudBaseMixin(CrispyLayoutFormMixin): if self.request.user.has_perm( self.permission(RP_DELETE)) else '' + @property + def openapi_url(self): + obj = self.crud if hasattr(self, 'crud') else self + o = self.object + url = f'/api/{o._meta.app_label}/{o._meta.model_name}/{o.id}' + return url + def get_template_names(self): names = super(CrudBaseMixin, self).get_template_names() names.append("crud/%s.html" % diff --git a/sapl/settings.py b/sapl/settings.py index a17db0969..2b43d70e7 100644 --- a/sapl/settings.py +++ b/sapl/settings.py @@ -321,7 +321,8 @@ FORM_RENDERER = 'django.forms.renderers.DjangoTemplates' # suprime texto de ajuda default do django-filter FILTERS_HELP_TEXT_FILTER = False -LOGGING_CONSOLE_VERBOSE = config('LOGGING_CONSOLE_VERBOSE', cast=bool, default=False) +LOGGING_CONSOLE_VERBOSE = config( + 'LOGGING_CONSOLE_VERBOSE', cast=bool, default=False) LOGGING = { 'version': 1, @@ -372,7 +373,7 @@ LOGGING = { 'propagate': True, }, 'django': { - 'handlers': ['applogfile'], + 'handlers': ['applogfile'] + ['console_verbose'] if LOGGING_CONSOLE_VERBOSE else [], 'level': 'ERROR', 'propagate': True, }, diff --git a/sapl/templates/crud/detail.html b/sapl/templates/crud/detail.html index 001784c34..42d67e51f 100644 --- a/sapl/templates/crud/detail.html +++ b/sapl/templates/crud/detail.html @@ -36,6 +36,7 @@
{% block editions %} {% if view.update_url or view.delete_url %} +
{% if view.update_url %} {% trans 'Editar' %} @@ -99,6 +100,18 @@ {% endfor %} {% endfor %} {% endblock detail_content %} + + + +
{% block table_content %} diff --git a/sapl/templates/crud/detail_detail.html b/sapl/templates/crud/detail_detail.html index 9dda69713..fc01330f2 100644 --- a/sapl/templates/crud/detail_detail.html +++ b/sapl/templates/crud/detail_detail.html @@ -64,6 +64,16 @@ {% endfor %} {% endblock detail_content %} + +
{% if view.detail_set_create_url %} @@ -74,7 +84,7 @@
- +
{% if not rows %}

{{ NO_ENTRIES_MSG }}