From 2f38f398749a43b0e0f66198048ec3c0bfa92053 Mon Sep 17 00:00:00 2001 From: Eduardo Calil Date: Thu, 7 Jan 2016 12:01:42 -0200 Subject: [PATCH] Add CasaLegislativaView --- .../migrations/0002_casalegislativa_codigo.py | 20 +++ base/migrations/0003_auto_20160107_1122.py | 20 +++ base/migrations/0004_auto_20160107_1158.py | 26 +++ base/models.py | 21 ++- base/urls.py | 6 +- base/views.py | 159 ++++++++++++++++++ templates/base/casa_leg_table_aux.html | 8 + templates/sistema.html | 2 +- 8 files changed, 253 insertions(+), 9 deletions(-) create mode 100644 base/migrations/0002_casalegislativa_codigo.py create mode 100644 base/migrations/0003_auto_20160107_1122.py create mode 100644 base/migrations/0004_auto_20160107_1158.py create mode 100644 templates/base/casa_leg_table_aux.html diff --git a/base/migrations/0002_casalegislativa_codigo.py b/base/migrations/0002_casalegislativa_codigo.py new file mode 100644 index 000000000..3275904b6 --- /dev/null +++ b/base/migrations/0002_casalegislativa_codigo.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0001_initial'), + ] + + operations = [ + migrations.AddField( + model_name='casalegislativa', + name='codigo', + field=models.CharField(verbose_name='Codigo', default=1, max_length=100), + preserve_default=False, + ), + ] diff --git a/base/migrations/0003_auto_20160107_1122.py b/base/migrations/0003_auto_20160107_1122.py new file mode 100644 index 000000000..5e930a3c2 --- /dev/null +++ b/base/migrations/0003_auto_20160107_1122.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models +import base.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0002_casalegislativa_codigo'), + ] + + operations = [ + migrations.AlterField( + model_name='casalegislativa', + name='logotipo', + field=models.FileField(null=True, upload_to=base.models.get_casa_media_path, verbose_name='Logotipo', blank=True), + ), + ] diff --git a/base/migrations/0004_auto_20160107_1158.py b/base/migrations/0004_auto_20160107_1158.py new file mode 100644 index 000000000..af51d076f --- /dev/null +++ b/base/migrations/0004_auto_20160107_1158.py @@ -0,0 +1,26 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('base', '0003_auto_20160107_1122'), + ] + + operations = [ + migrations.RemoveField( + model_name='casalegislativa', + name='cor_borda', + ), + migrations.RemoveField( + model_name='casalegislativa', + name='cor_fundo', + ), + migrations.RemoveField( + model_name='casalegislativa', + name='cor_principal', + ), + ] diff --git a/base/models.py b/base/models.py index 61abbbf89..732183537 100644 --- a/base/models.py +++ b/base/models.py @@ -1,10 +1,19 @@ from django.db import models from django.utils.translation import ugettext_lazy as _ +def get_sessao_media_path(instance, subpath, filename): + return './casa/%s/%s/%s' % (instance.numero, subpath, filename) + + +def get_casa_media_path(instance, filename): + import ipdb; ipdb.set_trace() + return get_sessao_media_path(instance, 'Logotipo', filename) class CasaLegislativa(models.Model): # TODO ajustar todos os max_length !!!! # cod_casa => id (pk) + + codigo = models.CharField(max_length=100, verbose_name=_('Codigo')) nome = models.CharField(max_length=100, verbose_name=_('Nome')) sigla = models.CharField(max_length=100, verbose_name=_('Sigla')) endereco = models.CharField(max_length=100, verbose_name=_('Endereço')) @@ -13,13 +22,11 @@ class CasaLegislativa(models.Model): uf = models.CharField(max_length=100, verbose_name=_('UF')) telefone = models.CharField(max_length=100, verbose_name=_('Telefone')) fax = models.CharField(max_length=100, verbose_name=_('Fax')) - cor_fundo = models.CharField( - max_length=100, verbose_name=_('Cor de fundo')) - cor_borda = models.CharField( - max_length=100, verbose_name=_('Cor da borda')) - cor_principal = models.CharField( - max_length=100, verbose_name=_('Cor principal')) - logotipo = models.CharField(max_length=100, verbose_name=_('Logotipo')) + logotipo = models.FileField( + blank=True, + null=True, + upload_to=get_casa_media_path, + verbose_name=_('Logotipo')) endereco_web = models.CharField(max_length=100, verbose_name=_('HomePage')) email = models.CharField(max_length=100, verbose_name=_('E-mail')) informacao_geral = models.CharField( diff --git a/base/urls.py b/base/urls.py index af8b83a6f..0f70ce5e0 100644 --- a/base/urls.py +++ b/base/urls.py @@ -1,11 +1,15 @@ from django.conf.urls import url from django.views.generic.base import TemplateView -from .views import HelpView + +from .views import HelpView, CasaLegislativaTableAuxView urlpatterns = [ url(r'^sistema/', TemplateView.as_view(template_name='sistema.html')), url(r'^ajuda/(?P\w+)$', HelpView.as_view(), name='help_topic'), url(r'^ajuda/', TemplateView.as_view(template_name='ajuda/index.html'), name='help_base'), + url(r'^casa-legislativa$', + CasaLegislativaTableAuxView.as_view(), name='casa_legislativa'), + ] diff --git a/base/views.py b/base/views.py index 5d790c433..37eb8f3ee 100644 --- a/base/views.py +++ b/base/views.py @@ -1,5 +1,17 @@ +import sapl + from django.views.generic.base import TemplateView +from .models import CasaLegislativa +from django.forms import ModelForm +from django import forms +from django.views.generic.edit import FormMixin +from vanilla import GenericView +from crispy_forms.helper import FormHelper +from crispy_forms.layout import (HTML, ButtonHolder, Column, Fieldset, Layout, + Submit) +from django.core.exceptions import ObjectDoesNotExist +from django.core.urlresolvers import reverse class HelpView(TemplateView): # XXX treat non existing template as a 404!!!! @@ -7,3 +19,150 @@ class HelpView(TemplateView): def get_template_names(self): print(self.kwargs['topic']) return ['ajuda/%s.html' % self.kwargs['topic']] + + +ESTADOS = ["AC", + "AL", + "AM", + "AP", + "BA", + "CE", + "DF", + "ES", + "GO", + "MA", + "MG", + "MS", + "MT", + "PA", + "PB", + "PE", + "PI", + "PR", + "RJ", + "RN", + "RO", + "RR", + "RS", + "SC", + "SE", + "SP", + "TO"] + + +class CasaLegislativaTabelaAuxForm(ModelForm): + + uf = forms.ChoiceField(required=False, + label='UF', + choices=[(a,a) for a in ESTADOS], + widget=forms.Select( + attrs={'class': 'selector'})) + + informacao_geral = forms.CharField(widget=forms.Textarea, + label='informacao_geral', + required=True) + + class Meta: + + model = CasaLegislativa + fields = ['codigo', + 'nome', + 'sigla', + 'endereco', + 'cep', + 'municipio', + 'uf', + 'telefone', + 'fax', + 'logotipo', + 'endereco_web', + 'email', + 'informacao_geral'] + + def __init__(self, *args, **kwargs): + + row1 = sapl.layout.to_row( + [('codigo', 2), + ('nome', 5), + ('sigla', 5)]) + + row2 = sapl.layout.to_row( + [('endereco', 8), + ('cep', 4)]) + + row3 = sapl.layout.to_row( + [('municipio', 10), + ('uf', 2)]) + + row4 = sapl.layout.to_row( + [('telefone', 6), + ('fax', 6)]) + + row5 = sapl.layout.to_row( + [('logotipo', 12)]) + + row6 = sapl.layout.to_row( + [('endereco_web', 12)]) + + row7 = sapl.layout.to_row( + [('email', 12)]) + + row8 = sapl.layout.to_row( + [('informacao_geral', 12)]) + + self.helper = FormHelper() + self.helper.layout = Layout( + Fieldset( + 'Dados Básicos', + row1, + row2, + row3, + row4, + row5, + row6, + row7, + row8, + ButtonHolder( + Submit('submit', 'Salvar', + css_class='button primary') + ) + ) + ) + super(CasaLegislativaTabelaAuxForm, self).__init__(*args, **kwargs) + +class CasaLegislativaTableAuxView(FormMixin, GenericView): + + template_name = "base/casa_leg_table_aux.html" + + def get(self, request, *args, **kwargs): + try: + casa = CasaLegislativa.objects.first() + except ObjectDoesNotExist: + form = CasaLegislativaTabelaAuxForm() + else: + form = CasaLegislativaTabelaAuxForm(instance=casa) + + return self.render_to_response({'form': form}) + + def post(self, request, *args, **kwargs): + form = CasaLegislativaTabelaAuxForm(request.POST or request.FILES) + + if form.is_valid(): + try: + casa = CasaLegislativa.objects.first() + except ObjectDoesNotExist: + casa_save = form.save(commit=False) + else: + casa_save = CasaLegislativaTabelaAuxForm(request.POST, instance = casa) + + if 'logotipo' in request.FILES: + casa_save.logotipo = request.FILES['logotipo'] + + casa_save.save() + + return self.form_valid(form) + else: + return self.render_to_response({'form': form}) + + def get_success_url(self): + return reverse('casa_legislativa') diff --git a/templates/base/casa_leg_table_aux.html b/templates/base/casa_leg_table_aux.html new file mode 100644 index 000000000..9ded38567 --- /dev/null +++ b/templates/base/casa_leg_table_aux.html @@ -0,0 +1,8 @@ +{% extends "crud/detail.html" %} +{% load i18n %} +{% load crispy_forms_tags %} +{% block actions %} {% endblock %} + +{% block detail_content %} + {% crispy form %} +{% endblock detail_content %} \ No newline at end of file diff --git a/templates/sistema.html b/templates/sistema.html index 5983123b3..4040f5ed1 100644 --- a/templates/sistema.html +++ b/templates/sistema.html @@ -18,7 +18,7 @@
Configuração Inicial
Módulo Parlamentares