From b99591b86f0d56b741f9ed0fae8f926a3d533a97 Mon Sep 17 00:00:00 2001 From: Gilson Filho Date: Mon, 28 Nov 2011 20:26:14 +0000 Subject: [PATCH] =?UTF-8?q?Ordenando=20as=20perguntas=20e=20criando=20o=20?= =?UTF-8?q?filtro=20para=20definir=20os=20question=C3=A1rios=20como=20publ?= =?UTF-8?q?icados.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sigi/apps/diagnosticos/admin.py | 9 +++++++++ sigi/apps/diagnosticos/forms.py | 23 ++++++++++++++++++----- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/sigi/apps/diagnosticos/admin.py b/sigi/apps/diagnosticos/admin.py index 00cab18..22deedf 100644 --- a/sigi/apps/diagnosticos/admin.py +++ b/sigi/apps/diagnosticos/admin.py @@ -21,9 +21,17 @@ class AnexoAdmin(admin.ModelAdmin): search_fields = ('descricao', 'diagnostico__id', 'arquivo', 'diagnostico__casa_legislativa__nome') + +# Ação de alterar o status das publicações no modo Draft. +def alterar_status_publicacao(modeladmin, request, queryset): + queryset.update(status=True) +alterar_status_publicacao.short_description = u""" + Publicar diagnósticos selecionados""" + class DiagnosticoAdmin(BaseEntityAdmin): form = DiagnosticoForm date_hierarchy = 'data_questionario' + actions = [alterar_status_publicacao] inlines = (EquipeInline, AnexosInline) raw_id_fields = ('casa_legislativa', 'responsavel') @@ -56,6 +64,7 @@ class DiagnosticoAdmin(BaseEntityAdmin): 'classes': ['collapse'] })) + class PerguntaAdmin (BaseSchemaAdmin): search_fields = ('title', 'help_text', 'name',) list_display = ('title', 'categoria', 'datatype', 'help_text', 'required') diff --git a/sigi/apps/diagnosticos/forms.py b/sigi/apps/diagnosticos/forms.py index 90ecf66..36c4bcd 100644 --- a/sigi/apps/diagnosticos/forms.py +++ b/sigi/apps/diagnosticos/forms.py @@ -3,6 +3,7 @@ from copy import deepcopy from models import Diagnostico +from django.forms.forms import BoundField from eav.forms import BaseDynamicEntityForm @@ -26,21 +27,28 @@ class DiagnosticoMobileForm(BaseDynamicEntityForm): super(BaseDynamicEntityForm, self).__init__(data, *args, **kwargs) self._build_dynamics_fields(category) + def __iter__(self): + fields_by_label = [ (field.label, field) for field in self.fields.values()] + for label, field in sorted(fields_by_label): + yield BoundField(self, field, field.label) + def _build_dynamics_fields(self, category): """Método da classe ``BaseDynamicEntityForm`` sobrescrita, para que as perguntas sejam agrupadas dentro das suas categorias. + * category = ID da Categoria """ # Caso seja as duas primeiras categorias, utilize # os campos do modelo - if category in (0, 1, ): + if int(category) in (0, 1, ): self.fields = deepcopy(self.base_fields) else: - self.field = dict() + self.fields = dict() # Se determinada pergunta é da categoria pesquisada, # então, gere o campo no formulário. for schema in self.instance.get_schemata(): + if not schema.categoria_id == int(category): continue @@ -55,6 +63,12 @@ class DiagnosticoMobileForm(BaseDynamicEntityForm): choices = getattr(self.instance, schema.name) defaults.update({'queryset': schema.get_choices(), 'initial': [x.pk for x in choices]}) + elif datatype == schema.TYPE_ONE: + choice = getattr(self.instance, schema.name) + defaults.update({'queryset': schema.get_choices(), + 'initial': choice.pk if choice else None, + # if schema is required remove --------- from ui + 'empty_label' : None if schema.required else u"---------"}) extra = self.FIELD_EXTRA.get(datatype, {}) if hasattr(extra, '__call__'): @@ -66,7 +80,6 @@ class DiagnosticoMobileForm(BaseDynamicEntityForm): # fill initial data (if attribute was already defined) value = getattr(self.instance, schema.name) - - # m2m is already done above - if value and not datatype == schema.TYPE_MANY: + if value and not datatype in (schema.TYPE_ONE, schema.TYPE_MANY): # choices are already done above self.initial[schema.name] = value +