Browse Source

Merge pull request #486 from interlegis/484-crud-casa-legislativa

Fix #484 crud casa legislativa
pull/432/head
Edward 9 years ago
parent
commit
110f3bdcfc
  1. 69
      base/forms.py
  2. 10
      base/layouts.yaml
  3. 8
      base/urls.py
  4. 69
      base/views.py
  5. 3
      comissoes/tests/test_comissoes.py
  6. 2
      materia/urls.py
  7. 8
      templates/base/casa_leg_table_aux.html
  8. 38
      templates/base/casalegislativa_list.html
  9. 2
      templates/sistema.html

69
base/forms.py

@ -1,20 +1,14 @@
from crispy_forms.helper import FormHelper
from crispy_forms.layout import HTML, Fieldset, Layout
from django import forms
from django.contrib.auth.forms import AuthenticationForm
from django.core.exceptions import ValidationError
from django.forms import ModelForm
from django.utils.translation import ugettext_lazy as _
import crispy_layout_mixin
import sapl
from crispy_layout_mixin import form_actions
from sapl.settings import MAX_IMAGE_UPLOAD_SIZE
from .models import CasaLegislativa
class CasaLegislativaTabelaAuxForm(ModelForm):
class CasaLegislativaForm(ModelForm):
class Meta:
@ -50,67 +44,6 @@ class CasaLegislativaTabelaAuxForm(ModelForm):
raise ValidationError("Imagem muito grande. ( > 2mb )")
return logotipo
def __init__(self, *args, **kwargs):
row1 = crispy_layout_mixin.to_row(
[('codigo', 2),
('nome', 5),
('sigla', 5)])
row2 = crispy_layout_mixin.to_row(
[('endereco', 8),
('cep', 4)])
row3 = crispy_layout_mixin.to_row(
[('municipio', 10),
('uf', 2)])
row4 = crispy_layout_mixin.to_row(
[('telefone', 6),
('fax', 6)])
row5 = crispy_layout_mixin.to_row(
[('logotipo', 12)])
row6 = crispy_layout_mixin.to_row(
[('endereco_web', 12)])
row7 = crispy_layout_mixin.to_row(
[('email', 12)])
row8 = crispy_layout_mixin.to_row(
[('informacao_geral', 12)])
self.helper = FormHelper()
self.helper.layout = Layout(
Fieldset(
_('Dados Básicos'),
row1,
row2,
row3,
row4,
row5,
HTML("""
<div class="col-md-12">
{% if not form.fotografia.errors and form.fotografia.value %}
<img class="img-responsive" width="225" height="300"
src="{{ MEDIA_URL }}{{ form.logotipo.value }}">
<br />
<input type="submit"
name="remover"
id="remover"
class="btn btn-warning"
value="Remover Logo"/>
{% endif %}
</div>"""),
row6,
row7,
row8,
form_actions()
)
)
super(CasaLegislativaTabelaAuxForm, self).__init__(*args, **kwargs)
class LoginForm(AuthenticationForm):
username = forms.CharField(label="Username", max_length=30,

10
base/layouts.yaml

@ -0,0 +1,10 @@
CasaLegislativa:
Casa Legislativa:
- codigo:2 nome sigla
- endereco:8 cep
- municipio:10 uf
- telefone fax
- logotipo
- endereco_web
- email
- informacao_geral

8
base/urls.py

@ -1,11 +1,11 @@
from django.conf.urls import url
from django.conf.urls import include, url
from django.contrib.auth import views
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
from django.views.generic.base import TemplateView
from .apps import AppConfig
from .forms import LoginForm
from .views import CasaLegislativaTableAuxView, HelpView
from .views import CasaLegislativaCrud, HelpView
app_name = AppConfig.name
@ -15,8 +15,8 @@ urlpatterns = [
url(r'^ajuda/(?P<topic>\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'),
url(r'^casa_legislativa/', include(CasaLegislativaCrud.get_urls())),
url(r'^login/$', views.login, {
'template_name': 'base/login.html', 'authentication_form': LoginForm},

69
base/views.py

@ -1,67 +1,32 @@
import os
from functools import lru_cache
from django.core.exceptions import ObjectDoesNotExist
from django.core.urlresolvers import reverse
from django.views.generic import FormView
from django.views.generic.base import TemplateView
from .forms import CasaLegislativaTabelaAuxForm
import crud.base
from crud.base import Crud
from .forms import CasaLegislativaForm
from .models import CasaLegislativa
@lru_cache(maxsize=1)
def get_casalegislativa():
return CasaLegislativa.objects.first()
class HelpView(TemplateView):
# XXX treat non existing template as a 404!!!!
def get_template_names(self):
return ['ajuda/%s.html' % self.kwargs['topic']]
class CasaLegislativaTableAuxView(FormView):
class CasaLegislativaCrud(Crud):
model = CasaLegislativa
help_path = ''
template_name = "base/casa_leg_table_aux.html"
class BaseMixin(crud.base.CrudBaseMixin):
list_field_names = ['codigo', 'nome', 'sigla']
def get(self, request, *args, **kwargs):
try:
casa = CasaLegislativa.objects.first()
except ObjectDoesNotExist:
form = CasaLegislativaTabelaAuxForm()
else:
form = CasaLegislativaTabelaAuxForm(instance=casa)
class CreateView(crud.base.CrudCreateView):
form_class = CasaLegislativaForm
return self.render_to_response({'form': form})
class UpdateView(crud.base.CrudUpdateView):
form_class = CasaLegislativaForm
def post(self, request, *args, **kwargs):
form = CasaLegislativaTabelaAuxForm(request.POST, request.FILES)
if form.is_valid():
casa = CasaLegislativa.objects.first()
if casa:
if ("remover" in request.POST or
(form.cleaned_data['logotipo'] and casa.logotipo)):
try:
os.unlink(casa.logotipo.path)
except OSError:
pass # Should log this error!!!!!
casa.logotipo = None
CasaLegislativaTabelaAuxForm(
request.POST,
request.FILES,
instance=casa
).save()
else:
form.save()
# Invalida cache de consulta
get_casalegislativa.cache_clear()
return self.form_valid(form)
else:
return self.render_to_response({'form': form})
class HelpView(TemplateView):
# XXX treat non existing template as a 404!!!!
def get_success_url(self):
return reverse('base:casa_legislativa')
def get_template_names(self):
return ['ajuda/%s.html' % self.kwargs['topic']]

3
comissoes/tests/test_comissoes.py

@ -2,8 +2,7 @@ import pytest
from django.core.urlresolvers import reverse
from model_mommy import mommy
from comissoes.models import (CargoComissao, Comissao, Composicao,
Participacao, Periodo, TipoComissao)
from comissoes.models import Comissao, Composicao, Periodo, TipoComissao
from parlamentares.models import Filiacao, Parlamentar, Partido

2
materia/urls.py

@ -30,7 +30,7 @@ urlpatterns = [
RelatoriaCrud.get_urls() +
DocumentoAcessorioCrud.get_urls())),
url(r'proposicao/', include(ProposicaoCrud.get_urls())),
url(r'^proposicao/', include(ProposicaoCrud.get_urls())),
# Integração com Compilação
url(r'^materia/(?P<pk>[0-9]+)/ta$',

8
templates/base/casa_leg_table_aux.html

@ -1,8 +0,0 @@
{% extends "crud/detail.html" %}
{% load i18n %}
{% load crispy_forms_tags %}
{% block actions %} {% endblock %}
{% block detail_content %}
{% crispy form %}
{% endblock detail_content %}

38
templates/base/casalegislativa_list.html

@ -0,0 +1,38 @@
{% extends "crud/list.html" %}
{% load i18n %}
{% block base_content %}
{% if not rows %}
<p>{{ NO_ENTRIES_MSG }}</p>
<div class="actions btn-group pull-right" role="group">
<a href="{{ view.create_url }}" class="btn btn-default">
{% blocktrans with verbose_name=view.verbose_name %} Adicionar {{ verbose_name }} {% endblocktrans %}
</a>
</div>
{% else %}
<table class="table table-striped table-hover">
<thead>
<tr>
{% for name in headers %}
<th>{{ name }}</th>
{% endfor %}
</tr>
</thead>
<tbody>
{% for value_list in rows %}
<tr>
{% for value, href in value_list %}
<td>
{% if href %}
<a href="{{ href }}">{{ value }}</a>
{% else %}
{{ value|safe }}
{% endif %}
</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
{% endif %}
{% endblock %}

2
templates/sistema.html

@ -5,7 +5,7 @@
<h2>Configuração Inicial</h2>
<div class="row">
<div class="col-md-6"><a href="{% url 'base:casa_legislativa' %}" class="btn btn-link">Casa Legislativa</a></div>
<div class="col-md-6"><a href="{% url 'base:casalegislativa_list' %}" class="btn btn-link">Casa Legislativa</a></div>
</div>
<h2>Módulo Parlamentares</h2>

Loading…
Cancel
Save