diff --git a/sigi/apps/pesquisas/__init__.py b/sigi/apps/pesquisas/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/pesquisas/admin.py b/sigi/apps/pesquisas/admin.py new file mode 100644 index 0000000..65ef533 --- /dev/null +++ b/sigi/apps/pesquisas/admin.py @@ -0,0 +1,15 @@ +# -*- coding: utf-8 -*- + +from django.contrib import admin +from sigi.forms_builder.forms.admin import FieldAdmin, FormAdmin +from sigi.apps.pesquisas.models import Pesquisa, Pergunta, Formulario, Resposta + +class PerguntaAdmin(FieldAdmin): + model = Pergunta + +class PesquisaAdmin(FormAdmin): + formentry_model = Formulario + fieldentry_model = Resposta + inlines = (PerguntaAdmin,) + +admin.site.register(Pesquisa, PesquisaAdmin) \ No newline at end of file diff --git a/sigi/apps/pesquisas/forms.py b/sigi/apps/pesquisas/forms.py new file mode 100644 index 0000000..c4ba969 --- /dev/null +++ b/sigi/apps/pesquisas/forms.py @@ -0,0 +1,11 @@ +# -*- coding: utf-8 -*- + +from sigi.forms_builder.forms.forms import FormForForm +from sigi.apps.pesquisas.models import Formulario, Resposta + +class PesquisaForm(FormForForm): + field_entry_model = Resposta + + class Meta(FormForForm.Meta): + model = Formulario + fields = ['casa_legislativa',] \ No newline at end of file diff --git a/sigi/apps/pesquisas/models.py b/sigi/apps/pesquisas/models.py new file mode 100644 index 0000000..7854b66 --- /dev/null +++ b/sigi/apps/pesquisas/models.py @@ -0,0 +1,44 @@ +# -*- coding: utf-8 -*- + +from django.db import models +from django.utils.translation import ungettext, ugettext_lazy as _ +from sigi.forms_builder.forms.models import AbstractForm, AbstractField, AbstractFormEntry, AbstractFieldEntry +from sigi.apps.casas.models import CasaLegislativa +from sigi.apps.servidores.models import Servidor + +class Pesquisa(AbstractForm): + class Meta: + verbose_name = _("Pesquisa") + verbose_name_plural = _("Pesquisas") + +class Pergunta(AbstractField): + """ + Implements automated field ordering. + """ + + form = models.ForeignKey("Pesquisa", related_name="fields") + order = models.IntegerField(_("Order"), null=True, blank=True) + + class Meta(AbstractField.Meta): + ordering = ("order",) + + def save(self, *args, **kwargs): + if self.order is None: + self.order = self.form.fields.count() + super(Pergunta, self).save(*args, **kwargs) + + def delete(self, *args, **kwargs): + fields_after = self.form.fields.filter(order__gte=self.order) + fields_after.update(order=models.F("order") - 1) + super(Pergunta, self).delete(*args, **kwargs) + +class Formulario(AbstractFormEntry): + form = models.ForeignKey("Pesquisa", related_name="entries") + casa_legislativa = models.ForeignKey(CasaLegislativa, verbose_name=u"Casa legislativa") + #operador = models.ForeignKey(Servidor, verbose_name=u"Operador") + + class Meta(AbstractFormEntry.Meta): + unique_together = ('form', 'casa_legislativa') + +class Resposta(AbstractFieldEntry): + entry = models.ForeignKey("Formulario", related_name="fields") \ No newline at end of file diff --git a/sigi/apps/pesquisas/templates/admin/forms/change_form.html b/sigi/apps/pesquisas/templates/admin/forms/change_form.html new file mode 100644 index 0000000..a52c31b --- /dev/null +++ b/sigi/apps/pesquisas/templates/admin/forms/change_form.html @@ -0,0 +1,30 @@ +{% extends "admin/change_form.html" %} + +{% load i18n %} +{% load url from future %} + +{% block object-tools %} +{% if change %}{% if not is_popup %} +
{{ message }}
{% endif %} +{{ field }}: | +{{ value|linebreaks }} | +
{{ form.response }}
+ {% endif %} + diff --git a/sigi/apps/pesquisas/templates/forms/includes/built_form.html b/sigi/apps/pesquisas/templates/forms/includes/built_form.html new file mode 100644 index 0000000..0d18fe1 --- /dev/null +++ b/sigi/apps/pesquisas/templates/forms/includes/built_form.html @@ -0,0 +1,11 @@ +{{ form.intro }}
+ {% endif %} + diff --git a/sigi/apps/pesquisas/templatetags/__init__.py b/sigi/apps/pesquisas/templatetags/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/sigi/apps/pesquisas/templatetags/forms_builder_tags.py b/sigi/apps/pesquisas/templatetags/forms_builder_tags.py new file mode 100644 index 0000000..d26181c --- /dev/null +++ b/sigi/apps/pesquisas/templatetags/forms_builder_tags.py @@ -0,0 +1,33 @@ +from django import template +from sigi.forms_builder.forms.templatetags.forms_builder_tags import BuiltFormNode +from sigi.apps.pesquisas.models import Pesquisa +from sigi.apps.pesquisas.forms import PesquisaForm + +register = template.Library() + +class BuiltPesquisaNode(BuiltFormNode): + form_class = Pesquisa + form_for_form_class = PesquisaForm + +@register.tag +def render_built_form(parser, token): + """ + render_build_form takes one argument in one of the following formats: + + {% render_build_form form_instance %} + {% render_build_form form=form_instance %} + {% render_build_form id=form_instance.id %} + {% render_build_form slug=form_instance.slug %} + + """ + try: + _, arg = token.split_contents() + if "=" not in arg: + arg = "form=" + arg + name, value = arg.split("=", 1) + if name not in ("form", "id", "slug"): + raise ValueError + except ValueError: + e = () + raise template.TemplateSyntaxError(render_built_form.__doc__) + return BuiltPesquisaNode(name, value) diff --git a/sigi/apps/pesquisas/tests.py b/sigi/apps/pesquisas/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/sigi/apps/pesquisas/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/sigi/apps/pesquisas/urls.py b/sigi/apps/pesquisas/urls.py new file mode 100644 index 0000000..136b8a4 --- /dev/null +++ b/sigi/apps/pesquisas/urls.py @@ -0,0 +1,9 @@ +from __future__ import unicode_literals + +from django.conf.urls import patterns, url + + +urlpatterns = patterns("apps.pesquisas.views", + url(r"(?P