Browse Source

Fix #101 (mostrar preview de foto de parlamentar)

pull/153/head
Edward Ribeiro 9 years ago
parent
commit
def001ee1a
  1. 27
      parlamentares/views.py

27
parlamentares/views.py

@ -12,6 +12,7 @@ from django.views.generic.edit import FormMixin
from vanilla import GenericView from vanilla import GenericView
import sapl import sapl
import os
from sapl.crud import build_crud from sapl.crud import build_crud
from .models import (CargoMesa, Coligacao, ComposicaoMesa, Dependente, from .models import (CargoMesa, Coligacao, ComposicaoMesa, Dependente,
@ -272,6 +273,11 @@ class ParlamentaresForm (ModelForm):
widget=forms.TextInput( widget=forms.TextInput(
attrs={'class': 'telefone'})) attrs={'class': 'telefone'}))
fotografia = forms.ImageField(label='Fotografia',
required=False,
widget=forms.FileInput
)
class Meta: class Meta:
model = Parlamentar model = Parlamentar
fields = ['nome_parlamentar', fields = ['nome_parlamentar',
@ -357,7 +363,17 @@ class ParlamentaresForm (ModelForm):
Fieldset('Cadastro do Parlamentar', Fieldset('Cadastro do Parlamentar',
row1, row2, row3, row4, row5, row1, row2, row3, row4, row5,
row6, row7, row8, row9, row10, row6, row7, row8, row9, row10,
row11, row12, row13, row14, row11, row12, row13,
HTML("""{% if form.fotografia.value %}
<img class="img-responsive"
src="{{ MEDIA_URL }}{{ form.fotografia.value }}">
<input type="submit"
name="remover"
id="remover"
class="button primary"
value="Remover"/>
{% endif %}""", ),
row14,
ButtonHolder( ButtonHolder(
Submit('submit', 'Salvar', Submit('submit', 'Salvar',
css_class='button primary'), css_class='button primary'),
@ -449,6 +465,15 @@ class ParlamentaresEditarView(FormMixin, GenericView):
elif 'excluir' in request.POST: elif 'excluir' in request.POST:
Mandato.objects.get(parlamentar=parlamentar).delete() Mandato.objects.get(parlamentar=parlamentar).delete()
parlamentar.delete() parlamentar.delete()
elif "remover" in request.POST:
try:
os.unlink(parlamentar.fotografia.path)
except OSError:
pass # Should log this error!!!!!
parlamentar = form.save(commit=False)
parlamentar.fotografia = None
parlamentar.save()
return self.form_valid(form) return self.form_valid(form)
else: else:

Loading…
Cancel
Save