Browse Source

Crud Casa Legislativa

pull/486/head
Eduardo Edson Batista Cordeiro Alves 9 years ago
parent
commit
ccc85785f7
  1. 63
      base/forms.py
  2. 10
      base/layouts.yaml
  3. 9
      base/urls.py
  4. 64
      base/views.py
  5. 2
      materia/urls.py
  6. 8
      templates/base/casa_leg_table_aux.html
  7. 38
      templates/base/casalegislativa_list.html

63
base/forms.py

@ -14,7 +14,7 @@ from sapl.settings import MAX_IMAGE_UPLOAD_SIZE
from .models import CasaLegislativa
class CasaLegislativaTabelaAuxForm(ModelForm):
class CasaLegislativaForm(ModelForm):
class Meta:
@ -50,67 +50,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

9
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,9 @@ 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$',
# 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},

64
base/views.py

@ -6,62 +6,34 @@ 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)
# @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 CasaLegislativaCrud(Crud):
model = CasaLegislativa
help_path = ''
class CasaLegislativaTableAuxView(FormView):
class BaseMixin(crud.base.CrudBaseMixin):
list_field_names = ['codigo', 'nome', 'sigla']
template_name = "base/casa_leg_table_aux.html"
class CreateView(crud.base.CrudCreateView):
form_class = CasaLegislativaForm
def get(self, request, *args, **kwargs):
try:
casa = CasaLegislativa.objects.first()
except ObjectDoesNotExist:
form = CasaLegislativaTabelaAuxForm()
else:
form = CasaLegislativaTabelaAuxForm(instance=casa)
class UpdateView(crud.base.CrudUpdateView):
form_class = CasaLegislativaForm
return self.render_to_response({'form': form})
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']]

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 %}
Loading…
Cancel
Save