From c68744ef73526121d7ed0e9040087e3a6dfecbc3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ses=C3=B3stris=20Vieira?= Date: Wed, 16 Oct 2013 15:33:36 -0300 Subject: [PATCH] =?UTF-8?q?Formul=C3=A1rio=20para=20manifesta=C3=A7=C3=A3o?= =?UTF-8?q?=20de=20uso=20de=20servi=C3=A7os=20do=20Interlegis?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/servicos/models.py | 18 ++- sigi/apps/servicos/views.py | 123 ++++++++++++++- sigi/templates/servicos/casa_manifesta.html | 156 ++++++++++++++++++++ 3 files changed, 294 insertions(+), 3 deletions(-) create mode 100644 sigi/templates/servicos/casa_manifesta.html diff --git a/sigi/apps/servicos/models.py b/sigi/apps/servicos/models.py index cdd44c5..7e4b688 100644 --- a/sigi/apps/servicos/models.py +++ b/sigi/apps/servicos/models.py @@ -185,4 +185,20 @@ class CasaAtendida(CasaLegislativa): for servico in self.servico_set.all(): result.append(unicode(servico)) - return ", ".join(result) \ No newline at end of file + return ", ".join(result) + +class CasaManifesta(models.Model): + casa_legislativa = models.OneToOneField(CasaLegislativa) + data_manifestacao = models.DateTimeField(auto_now_add=True) + data_atualizacao = models.DateTimeField(auto_now=True) + informante = models.CharField(u'Nome do informante', max_length=100) + cargo = models.CharField(u'Cargo do informante', max_length=100) + +class ServicoManifesto(models.Model): + casa_manifesta = models.ForeignKey(CasaManifesta) + servico = models.ForeignKey(TipoServico) + url = models.URLField(blank=True) + hospedagem_interlegis = models.BooleanField(u'Hospedagem no Interlegis?') + + class Meta: + unique_together = ('casa_manifesta', 'servico') \ No newline at end of file diff --git a/sigi/apps/servicos/views.py b/sigi/apps/servicos/views.py index 73129f0..83edc73 100644 --- a/sigi/apps/servicos/views.py +++ b/sigi/apps/servicos/views.py @@ -1,8 +1,18 @@ # -*- coding: utf-8 -*- +from django import forms from django.http import HttpResponse from django.utils import simplejson +from django.shortcuts import render_to_response, get_object_or_404 from django.db.models import Q -from sigi.apps.servicos.models import CasaAtendida +from sigi.apps.servicos.models import TipoServico, CasaAtendida, CasaManifesta, ServicoManifesto +from sigi.apps.contatos.models import UnidadeFederativa +from sigi.apps.casas.models import CasaLegislativa +from django.template.context import RequestContext +from django.utils.encoding import force_unicode +from django.forms.forms import BoundField +from django.utils.html import conditional_escape +from django.utils.safestring import mark_safe +from django.contrib.admin.helpers import AdminForm def municipios_atendidos(self, servico): municipios = [] @@ -25,4 +35,113 @@ def municipios_atendidos(self, servico): 'servicos': "",} municipios.append(municipio) - return HttpResponse(simplejson.dumps(municipios), mimetype="application/json") \ No newline at end of file + return HttpResponse(simplejson.dumps(municipios), mimetype="application/json") + +class CasaManifestaProtoForm(forms.Form): + fieldsets = None + informante = forms.CharField(max_length=100, required=False) + cargo = forms.CharField(max_length=100, required=False) + + def set_fieldsets(self, fieldsets): + result = [] + for name, lines in fieldsets: + field_lines = [] + for line in lines: + if isinstance(line, str): + line = (line,) + field_line = [] + for field_name in line: + field = self.fields[field_name] + bf = BoundField(self, field, field_name) + field_line.append(bf) + field_lines.append(field_line) + result.append({'name': name, 'lines': field_lines},) + self.fieldsets = result + +def casa_manifesta_view(request): + if 'casa_id' in request.GET: + casa_id = request.GET.get('casa_id') + casa = get_object_or_404(CasaLegislativa, pk=casa_id) + + # Criar um formulário dinâmico + + campos = {} + fieldsets = ((None, ('informante', 'cargo'),),) + + for ts in TipoServico.objects.all(): + campos['possui_%s' % ts.pk] = forms.BooleanField(label='Possui o serviço de %s' % ts.nome, required=False) + campos['url_%s' % ts.pk] = forms.URLField(label='Informe a URL', required=False) + campos['hospedagem_interlegis_%s' % ts.pk] = forms.BooleanField(label=u'Serviço está hospedado no Interlegis', required=False) + fieldsets += ((ts.nome, ('possui_%s' % ts.pk, 'url_%s' % ts.pk, 'hospedagem_interlegis_%s' % ts.pk )),) + + CasaManifestaForm = type('', (CasaManifestaProtoForm,), campos) + + if request.method == 'POST': + cmf = CasaManifestaForm(request.POST) + if cmf.is_valid(): + thanks = [] + cm, created = CasaManifesta.objects.get_or_create(casa_legislativa=casa) + cm.informante = cmf.cleaned_data['informante'] + cm.cargo = cmf.cleaned_data['cargo'] + cm.save() + thanks.append(('Informante', cmf.cleaned_data['informante'])) + thanks.append(('Cargo', cmf.cleaned_data['cargo'])) + for ts in TipoServico.objects.all(): + if cmf.cleaned_data['possui_%s' % ts.pk]: + sm, created = ServicoManifesto.objects.get_or_create(casa_manifesta=cm, servico=ts) + sm.url = cmf.cleaned_data['url_%s' % ts.pk] + sm.hospedagem_interlegis = cmf.cleaned_data['hospedagem_interlegis_%s' % ts.pk] + sm.save() + thanks.append((ts.nome, 'Possui o serviço acessível em %s %s' % (sm.url, 'hospedado no Interlegis' if + sm.hospedagem_interlegis else ''))) + else: + ServicoManifesto.objects.filter(casa_manifesta=cm, servico=ts).delete() + thanks.append((ts.nome, 'Não possui')) + extra_context = {'casa': casa, 'thanks': thanks} + else: + extra_context = {'casa': casa, 'cmf': cmf} + else: + try: + cm = casa.casamanifesta + values = { + 'informante': cm.informante, + 'cargo': cm.cargo, + } + for sm in cm.servicomanifesto_set.all(): + values['possui_%s' % sm.servico.pk] = True + values['url_%s' % sm.servico.pk] = sm.url + values['hospedagem_interlegis_%s' % sm.servico.pk] = sm.hospedagem_interlegis + cmf = CasaManifestaForm(values) + except: + cmf = CasaManifestaForm() + + cmf.set_fieldsets(fieldsets) + + extra_context = {'casa': casa, 'cmf': cmf} + elif 'uf' in request.GET: + uf = request.GET.get('uf') + extra_context = {'casa_list': CasaLegislativa.objects.filter(municipio__uf__sigla=uf)} + else: + extra_context = {'uf_list': UnidadeFederativa.objects.all()} + + return render_to_response('servicos/casa_manifesta.html', extra_context, context_instance=RequestContext(request)) + + # Monta formulário dinâmico dos serviços +# campos = {} +# for ts in TipoServico.objects.all(): +# campos['possui_%s' % ts.pk] = forms.BooleanField(label=ts.nome) +# campos['url_%s' % ts.pk] = forms.URLField(label='URL do %s' % ts.nome) +# campos['hospedagem_interlegis_%s' % ts.pk] = forms.BooleanField(label=u'Hospedado no Interlegis') +# ServicoManifestoForm = type('', (forms.Form,), campos) +# +# if request.method == 'POST': +# cm_form = CasaManifestaForm(request.POST) +# sm_form = ServicoManifestoForm(request.POST) +# +# if cm_form.is_valid() and sm_form.is_valid(): +# return render_to_response('servicos/casa_manifesta.html', {'cm_form': cm_form, 'sm_form': sm_form}, +# context_instance=RequestContext(request)) +# else: +# cm_form = CasaManifestaForm() +# sm_form = ServicoManifestoForm() + return render_to_response('casa_manifesta.html', {'cm_form': cm_form, 'sm_form': sm_form}) \ No newline at end of file diff --git a/sigi/templates/servicos/casa_manifesta.html b/sigi/templates/servicos/casa_manifesta.html new file mode 100644 index 0000000..c0fbea5 --- /dev/null +++ b/sigi/templates/servicos/casa_manifesta.html @@ -0,0 +1,156 @@ +{% extends "admin/change_form.html" %} +{% load i18n admin_modify adminmedia %} + +{% block extrahead %}{{ block.super }} +{% url admin:jsi18n as jsi18nurl %} + + +{{ media }} +{% endblock %} + +{% block coltype %}colMS{% endblock %} +{% block bodyclass %}casa-manifesta change-form{% endblock %} + +{% block nav-global %} + +{% endblock %} + +{% block breadcrumbs %} + +{% endblock %} + +{% block content %} +
+{% if uf_list %} +

1. Informe a Unidade da Federação onde se localiza sua Casa Legislativa

+ +{% endif %} + +{% if casa_list %} +

2. Identifique sua Casa Legislativa

+
+ +{% endif %} + +{% if cmf %} +

3. Informe os dados para a {{ casa.nome }} - {{ casa.municipio.uf }}

+
{% csrf_token %} + {% for fieldset in cmf.fieldsets %} + {% comment %} +
+ {% if fieldset.name %}

{{ fieldset.name }}

{% endif %} + {% for line in fieldset.lines %} +
+ {% for field in line %} +
+ {% if not field.is_readonly %}{{ field.errors }}{% endif %} + {% if field.is_checkbox %} + {{ field }}{{ field.label_tag }} + {% else %} + {{ field.label_tag }} + {% if field.is_readonly %} +

{{ field.contents }}

+ {% else %} + {{ field }} + {% endif %} + {% endif %} + {% if field.field.help_text %} +

{{ field.field.help_text|safe }}

+ {% endif %} +
+ {% endfor %} +
+ {% endfor %} +
+ {% endcomment %} + {% if forloop.first %} +
+ {% if fieldset.name %}

{{ fieldset.name }}

{% endif %} + {% for line in fieldset.lines %} +
+ {% for field in line %} +
+ {% if not field.is_readonly %}{{ field.errors }}{% endif %} + {% if field.is_checkbox %} + {{ field }}{{ field.label_tag }} + {% else %} + {{ field.label_tag }} + {% if field.is_readonly %} +

{{ field.contents }}

+ {% else %} + {{ field }} + {% endif %} + {% endif %} + {% if field.field.help_text %} +

{{ field.field.help_text|safe }}

+ {% endif %} +
+ {% endfor %} +
+ {% endfor %} +
+
+

Serviços e produtos Interlegis

+ + + + + + + + {% else %} + + + {% for line in fieldset.lines %} + {% for field in line %} + + {% endfor %} + {% endfor %} + + {% endif %} + {% endfor %} +
Nome do serviço ou produtoMarque se a Casa utiliza este serviço ou produtoInforme a URL onde se localiza o serviço ou produtoMarque se o serviço ou produto está hospedado no Interlegis
{{ fieldset.name }}{{ field }}
+
+
+ +
+
+{% endif %} + +{% if thanks %} +

Obrigado por responder à nossa pesquisa!

+

Quadro resumo das informações prestadas:

+ + {% for row in thanks %} + + {% endfor %} +
{{ row.0 }}{{ row.1|default:"Não informado" }}
+

Se desejar, você poderá alterar esses dados repetindo o processo

+{% endif %} + +{% endblock %}