From 6c65361678eec2b9c680cf6b84039f6296d7a4e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Tue, 10 May 2022 08:31:52 -0300 Subject: [PATCH] =?UTF-8?q?Refatora=C3=A7=C3=A3o=20para=20help=20de=20camp?= =?UTF-8?q?os=20com=20placeholders?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/convenios/models.py | 2 +- sigi/apps/home/views.py | 32 +------------------------------- sigi/apps/utils/__init__.py | 27 +++++++++++++++++++++++++++ 3 files changed, 29 insertions(+), 32 deletions(-) diff --git a/sigi/apps/convenios/models.py b/sigi/apps/convenios/models.py index 9dc3e29..c0abdd3 100644 --- a/sigi/apps/convenios/models.py +++ b/sigi/apps/convenios/models.py @@ -14,7 +14,7 @@ from sigi.apps.eventos.models import Evento from sigi.apps.utils import to_ascii from sigi.apps.casas.models import Funcionario, Orgao from sigi.apps.servidores.models import Servidor, Servico -from sigi.apps.home.views import editor_help +from sigi.apps.utils import editor_help class Projeto(models.Model): diff --git a/sigi/apps/home/views.py b/sigi/apps/home/views.py index 8cb36ab..dad1c4e 100644 --- a/sigi/apps/home/views.py +++ b/sigi/apps/home/views.py @@ -8,13 +8,13 @@ from django.db import models from django.db.models import Q, Count from django.http import HttpResponse, HttpResponseForbidden, JsonResponse from django.shortcuts import render, get_object_or_404 -from django.template import Template, Context from django.template.loader import render_to_string from django.utils.translation import gettext as _ from django.views.decorators.cache import never_cache from django_weasyprint.views import WeasyTemplateResponse from sigi.apps.casas.models import TipoOrgao, Orgao from sigi.apps.contatos.models import UnidadeFederativa +from sigi.apps.convenios.models import Convenio, Projeto from sigi.apps.servicos.models import TipoServico from sigi.apps.servidores.models import Servidor from sigi.apps.utils import to_ascii @@ -33,8 +33,6 @@ from django.urls import reverse def openmap(request): - from sigi.apps.convenios.models import Projeto - reptype = request.GET.get("reptype", None) context = site.each_context(request) @@ -479,8 +477,6 @@ def report_sem_convenio(request): def busca_informacoes_camara(): - from sigi.apps.convenios.models import Convenio, Projeto - camaras = Orgao.objects.filter(tipo__sigla="CM") convenios = Convenio.objects.filter(casa_legislativa__tipo__sigla="CM") projetos = Projeto.objects.all() @@ -719,29 +715,3 @@ def color_palete(): ) return (colors, highlights) - - -def editor_help(field_name, Field_list): - placeholders = [] - for name, detail in Field_list: - if type(detail) is str: - placeholders.append([f"{{{{ {name} }}}}", detail]) - else: - placeholders.append( - [ - f"{{{{ {name} }}}}", - detail._meta.verbose_name.capitalize(), - ] - ) - for field in detail._meta.fields: - if field.auto_created or type(field) is models.ForeignKey: - pass # Ignore FK and auto-PK - else: - placeholders.append( - [f"{{{{ {name}.{field.name} }}}}", field.verbose_name] - ) - - return render_to_string( - "home/editor_help_snippet.html", - {"field_name": field_name, "placeholders": placeholders}, - ) diff --git a/sigi/apps/utils/__init__.py b/sigi/apps/utils/__init__.py index b226836..21f28cf 100644 --- a/sigi/apps/utils/__init__.py +++ b/sigi/apps/utils/__init__.py @@ -2,6 +2,7 @@ from unicodedata import normalize from django.contrib import admin from django.core.exceptions import FieldDoesNotExist from django.db import models +from django.template.loader import render_to_string from django.utils.encoding import force_str @@ -62,3 +63,29 @@ def field_label(name, model): label = label + "/" + field_label("__".join(name[1:]), to_model) return label + + +def editor_help(field_name, Field_list): + placeholders = [] + for name, detail in Field_list: + if type(detail) is str: + placeholders.append([f"{{{{ {name} }}}}", detail]) + else: + placeholders.append( + [ + f"{{{{ {name} }}}}", + detail._meta.verbose_name.capitalize(), + ] + ) + for field in detail._meta.fields: + if field.auto_created or type(field) is models.ForeignKey: + pass # Ignore FK and auto-PK + else: + placeholders.append( + [f"{{{{ {name}.{field.name} }}}}", field.verbose_name] + ) + + return render_to_string( + "home/editor_help_snippet.html", + {"field_name": field_name, "placeholders": placeholders}, + )