diff --git a/compilacao/forms.py b/compilacao/forms.py index 2e048cb8f..e7c27e8ca 100644 --- a/compilacao/forms.py +++ b/compilacao/forms.py @@ -1,3 +1,4 @@ +import sys from crispy_forms.helper import FormHelper from crispy_forms_foundation.layout import (HTML, Column, Div, Fieldset, diff --git a/compilacao/urls.py b/compilacao/urls.py index 148fb0a5b..1fae0972b 100644 --- a/compilacao/urls.py +++ b/compilacao/urls.py @@ -62,6 +62,13 @@ urlpatterns_compilacao = [ name='search_dispositivo'), + url(r'^config/tipo-textoarticulado$', + views.TipoTaListView.as_view(), name='tipo_ta_list'), + url(r'^config/tipo-textoarticulado/create$', + views.TipoTaCreateView.as_view(), name='tipo_ta_create'), + url(r'^config/tipo-textoarticulado/(?P[0-9]+)$', + views.TipoTaDetailView.as_view(), name='tipo_ta_detail'), + url(r'^config/tipo-nota/', include(tipo_nota_crud.urls)), url(r'^config/tipo-vide/', diff --git a/compilacao/views.py b/compilacao/views.py index 8738544e1..63ab63601 100644 --- a/compilacao/views.py +++ b/compilacao/views.py @@ -3,11 +3,15 @@ from datetime import datetime, timedelta import sys from braces.views import FormMessagesMixin +from crispy_forms.helper import FormHelper +from crispy_forms_foundation.layout.containers import Fieldset +from django import forms from django.contrib.auth.decorators import login_required from django.contrib.contenttypes.models import ContentType from django.core.signing import Signer from django.core.urlresolvers import reverse, reverse_lazy from django.db.models import Q +from django.forms.models import ModelForm from django.http.response import (HttpResponse, HttpResponseRedirect, JsonResponse) from django.shortcuts import get_object_or_404, redirect @@ -19,13 +23,15 @@ from django.views.generic.detail import DetailView from django.views.generic.edit import CreateView, DeleteView, UpdateView from django.views.generic.list import ListView -from compilacao import forms, utils +from compilacao import utils +from compilacao.forms import TaForm, NotaForm, VideForm from compilacao.models import (Dispositivo, Nota, PerfilEstruturalTextoArticulado, TextoArticulado, TipoDispositivo, TipoNota, TipoTextoArticulado, Vide, TipoVide, - TipoPublicacao, VeiculoPublicacao) -from compilacao.utils import build_crud + TipoPublicacao, VeiculoPublicacao, + PARTICIPACAO_SOCIAL_CHOICES) +from compilacao.utils import build_crud, to_row, FormLayout DISPOSITIVO_SELECT_RELATED = ( @@ -70,6 +76,160 @@ veiculo_publicacao_crud = build_crud( ]) +class IntegracaoTaView(TemplateView): + + def get(self, *args, **kwargs): + item = get_object_or_404(self.model, pk=kwargs['pk']) + related_object_type = ContentType.objects.get_for_model(item) + + ta = TextoArticulado.objects.filter( + object_id=item.pk, + content_type=related_object_type) + + if not ta.exists(): + ta = TextoArticulado() + tipo_ta = TipoTextoArticulado.objects.filter( + model=item.__class__.__name__.lower())[:1] + if tipo_ta.exists(): + ta.tipo_ta = tipo_ta[0] + ta.content_object = item + else: + ta = ta[0] + + if hasattr(item, 'ementa') and item.ementa: + ta.ementa = item.ementa + else: + ta.ementa = 'Integração com %s sem ementa.' % item + + if hasattr(item, 'observacao') and item.observacao: + ta.observacao = item.observacao + else: + ta.observacao = 'Integração com %s sem observacao.' % item + + if hasattr(item, 'numero') and item.numero: + ta.numero = item.numero + else: + ta.numero = int('%s%s%s' % ( + int(datetime.now().year), + int(datetime.now().month), + int(datetime.now().day))) + + if hasattr(item, 'ano') and item.ano: + ta.ano = item.ano + else: + ta.ano = datetime.now().year + + if hasattr(item, 'data_apresentacao'): + ta.data = item.data_apresentacao + elif hasattr(item, 'data'): + ta.data = item.data + else: + ta.data = datetime.now() + + ta.save() + + return redirect(to=reverse_lazy('ta_text', kwargs={'ta_id': ta.pk})) + + class Meta: + abstract = True + + +def get_integrations_view_names(): + result = [] + modules = sys.modules + for key, value in modules.items(): + if key.endswith('.views'): + for v in value.__dict__.values(): + if hasattr(v, '__bases__'): + for base in v.__bases__: + if base == IntegracaoTaView: + result.append(v) + return result + + +def choice_extenal_views(): + integrations_view_names = get_integrations_view_names() + result = [] + for item in integrations_view_names: + ct = ContentType.objects.filter( + model=item.model.__name__.lower(), + app_label=item.model._meta.app_label) + if ct.exists(): + result.append(( + ct[0].pk, + item.model._meta.verbose_name_plural)) + return result + + +class TipoTaForm(ModelForm): + sigla = forms.CharField(label='Sigla') + descricao = forms.CharField(label='Descrição') + + participacao_social = forms.NullBooleanField( + label=_('Participação Social'), + widget=forms.Select(choices=PARTICIPACAO_SOCIAL_CHOICES), + required=False) + + class Meta: + model = TipoTextoArticulado + fields = ['sigla', + 'descricao', + 'model', + 'participacao_social', + ] + + def __init__(self, *args, **kwargs): + + row1 = to_row([ + ('sigla', 2), + ('descricao', 4), + ('model', 3), + ('participacao_social', 3), + ]) + + self.helper = FormHelper() + self.helper.layout = FormLayout( + Fieldset(_('Identificação Básica'), + row1, css_class="large-12")) + super(TipoTaForm, self).__init__(*args, **kwargs) + + +class TipoTaListView(ListView): + model = TipoTextoArticulado + paginate_by = 10 + verbose_name = model._meta.verbose_name + title = model._meta.verbose_name_plural + create_url = reverse_lazy('tipo_ta_create') + + +class TipoTaCreateView(FormMessagesMixin, CreateView): + model = TipoTextoArticulado + form_class = TipoTaForm + template_name = "compilacao/form.html" + form_valid_message = _('Registro criado com sucesso!') + form_invalid_message = _('O registro não foi criado.') + + def get(self, request, *args, **kwargs): + self.object = None + form = self.get_form() + form.fields['model'] = forms.ChoiceField( + choices=choice_extenal_views(), + label='Associação', required=False) + + return self.render_to_response(self.get_context_data(form=form)) + + def get_success_url(self): + return reverse_lazy('tipo_ta_detail', kwargs={'pk': self.object.id}) + + +class TipoTaDetailView(DetailView): + model = TipoTextoArticulado + + @property + def title(self): + return self.get_object() + + class TaListView(ListView): model = TextoArticulado paginate_by = 10 @@ -102,7 +262,7 @@ class TaDetailView(DetailView): class TaCreateView(FormMessagesMixin, CreateView): model = TextoArticulado - form_class = forms.TaForm + form_class = TaForm template_name = "compilacao/form.html" form_valid_message = _('Registro criado com sucesso!') form_invalid_message = _('O registro não foi criado.') @@ -113,7 +273,7 @@ class TaCreateView(FormMessagesMixin, CreateView): class TaUpdateView(UpdateView): model = TextoArticulado - form_class = forms.TaForm + form_class = TaForm template_name = "compilacao/form.html" @property @@ -143,64 +303,6 @@ class TaDeleteView(DeleteView): return reverse_lazy('ta_list') -class IntegracaoTaView(TemplateView): - - def get(self, *args, **kwargs): - item = get_object_or_404(self.model, pk=kwargs['pk']) - related_object_type = ContentType.objects.get_for_model(item) - - ta = TextoArticulado.objects.filter( - object_id=item.pk, - content_type=related_object_type) - - if not ta.exists(): - ta = TextoArticulado() - tipo_ta = TipoTextoArticulado.objects.filter( - model=item.__class__.__name__.lower())[:1] - if tipo_ta.exists(): - ta.tipo_ta = tipo_ta[0] - ta.content_object = item - else: - ta = ta[0] - - if hasattr(item, 'ementa') and item.ementa: - ta.ementa = item.ementa - else: - ta.ementa = 'Integração com %s sem ementa.' % item - - if hasattr(item, 'observacao') and item.observacao: - ta.observacao = item.observacao - else: - ta.observacao = 'Integração com %s sem observacao.' % item - - if hasattr(item, 'numero') and item.numero: - ta.numero = item.numero - else: - ta.numero = int('%s%s%s' % ( - int(datetime.now().year), - int(datetime.now().month), - int(datetime.now().day))) - - if hasattr(item, 'ano') and item.ano: - ta.ano = item.ano - else: - ta.ano = datetime.now().year - - if hasattr(item, 'data_apresentacao'): - ta.data = item.data_apresentacao - elif hasattr(item, 'data'): - ta.data = item.data - else: - ta.data = datetime.now() - - ta.save() - - return redirect(to=reverse_lazy('ta_text', kwargs={'ta_id': ta.pk})) - - class Meta: - abstract = True - - class TextView(ListView): template_name = 'compilacao/text_list.html' @@ -1243,7 +1345,7 @@ class NotaMixin(DispositivoSuccessUrlMixin): class NotasCreateView(NotaMixin, CreateView): template_name = 'compilacao/ajax_form.html' - form_class = forms.NotaForm + form_class = NotaForm def get(self, request, *args, **kwargs): flag_action, modelo_nota = self.get_modelo_nota(request) @@ -1256,7 +1358,7 @@ class NotasCreateView(NotaMixin, CreateView): try: ta_id = kwargs.pop('ta_id') dispositivo_id = kwargs.pop('dispositivo_id') - form = forms.NotaForm(request.POST, request.FILES, **kwargs) + form = NotaForm(request.POST, request.FILES, **kwargs) kwargs['ta_id'] = ta_id kwargs['dispositivo_id'] = dispositivo_id @@ -1276,7 +1378,7 @@ class NotasCreateView(NotaMixin, CreateView): class NotasEditView(NotaMixin, UpdateView): model = Nota template_name = 'compilacao/ajax_form.html' - form_class = forms.NotaForm + form_class = NotaForm def get(self, request, *args, **kwargs): flag_action, modelo_nota = self.get_modelo_nota(request) @@ -1315,14 +1417,14 @@ class VideMixin(DispositivoSuccessUrlMixin): class VideCreateView(VideMixin, CreateView): model = Vide template_name = 'compilacao/ajax_form.html' - form_class = forms.VideForm + form_class = VideForm def post_old(self, request, *args, **kwargs): try: self.object = None ta_id = kwargs.pop('ta_id') dispositivo_id = kwargs.pop('dispositivo_id') - form = forms.VideForm(request.POST, request.FILES, **kwargs) + form = VideForm(request.POST, request.FILES, **kwargs) kwargs['ta_id'] = ta_id kwargs['dispositivo_id'] = dispositivo_id @@ -1341,7 +1443,7 @@ class VideCreateView(VideMixin, CreateView): class VideEditView(VideMixin, UpdateView): model = Vide template_name = 'compilacao/ajax_form.html' - form_class = forms.VideForm + form_class = VideForm class VideDeleteView(VideMixin, TemplateView): diff --git a/materia/views.py b/materia/views.py index 11f4f054e..e20783b4a 100644 --- a/materia/views.py +++ b/materia/views.py @@ -15,12 +15,12 @@ from django.views.generic import ListView from django.views.generic.edit import FormMixin from vanilla.views import GenericView -import sapl from comissoes.models import Comissao, Composicao from compilacao.views import IntegracaoTaView from norma.models import LegislacaoCitada, NormaJuridica, TipoNormaJuridica from parlamentares.models import Parlamentar from sapl.crud import build_crud +import sapl from .models import (Anexada, Autor, Autoria, DespachoInicial, DocumentoAcessorio, MateriaLegislativa, Numeracao, Orgao, @@ -29,6 +29,7 @@ from .models import (Anexada, Autor, Autoria, DespachoInicial, TipoFimRelatoria, TipoMateriaLegislativa, TipoProposicao, Tramitacao, UnidadeTramitacao) + origem_crud = build_crud( Origem, 'origem', [ diff --git a/norma/views.py b/norma/views.py index 655b8e89a..f7c64dabb 100644 --- a/norma/views.py +++ b/norma/views.py @@ -12,14 +12,15 @@ from django.utils.translation import ugettext_lazy as _ from django.views.generic.edit import FormMixin from vanilla.views import GenericView -import sapl from compilacao.views import IntegracaoTaView from materia.models import MateriaLegislativa, TipoMateriaLegislativa from sapl.crud import build_crud +import sapl from .models import (AssuntoNorma, LegislacaoCitada, NormaJuridica, TipoNormaJuridica) + assunto_norma_crud = build_crud( AssuntoNorma, 'assunto_norma_juridica', [ diff --git a/templates/compilacao/textoarticulado_detail.html b/templates/compilacao/textoarticulado_detail.html index 4a1b0d48f..5aba9e39a 100644 --- a/templates/compilacao/textoarticulado_detail.html +++ b/templates/compilacao/textoarticulado_detail.html @@ -8,7 +8,8 @@
  • {% trans 'Excluir' %}