Browse Source

Melhorias no código

pull/1323/head
Eduardo Calil 9 years ago
parent
commit
fc4d625cf0
  1. 28
      sapl/materia/views.py
  2. 9
      sapl/templates/materia/autoria_form.html

28
sapl/materia/views.py

@ -1195,33 +1195,32 @@ def autores_ativos(materia, tipo=None):
return (autor_qs | autores_by_ct['others']).order_by('nome') return (autor_qs | autores_by_ct['others']).order_by('nome')
else: else:
tipo_autor = tipo
if not tipo in autores_by_ct: if not tipo in autores_by_ct:
tipo_autor = 'others' return autores_by_ct['others'].filter(
tipo_id=tipo).order_by('nome')
return autores_by_ct[tipo_autor].filter(tipo_id=tipo).order_by('nome') return autores_by_ct[tipo].order_by('nome')
def atualizar_autores(request): def atualizar_autores(request):
if ('tipo_autor' in request.GET and 'materia_id' in request.GET and if 'materia_id' in request.GET and request.GET['materia_id']:
request.GET['tipo_autor'] and request.GET['materia_id']):
tipo_autor = request.GET['tipo_autor']
materia_id = int(request.GET['materia_id']) materia_id = int(request.GET['materia_id'])
try: try:
materia = MateriaLegislativa.objects.get(id=materia_id) materia = MateriaLegislativa.objects.get(id=materia_id)
except ObjectDoesNotExist: except ObjectDoesNotExist:
autores = [] pass
else: else:
manter_autor = False
if 'tipo_autor' in request.GET and request.GET['tipo_autor']:
tipo_autor = request.GET['tipo_autor']
autores = autores_ativos(materia, tipo=tipo_autor) autores = autores_ativos(materia, tipo=tipo_autor)
autores_list = [(a.id, a.__str__()) for a in autores]
# Se já houver algum autor selecionado (ex: view de update) # Se já houver algum autor selecionado (ex: view de update)
# no campo correspondente e caso o TipoAutor selecionado # no campo correspondente e caso o TipoAutor selecionado
# seja o mesmo do autor que está aualmente marcado # seja o mesmo do autor que está aualmente marcado
# deve ser enviado um sinal (manter autor) para que o javascript # deve ser enviado um sinal (manter autor) para que o
# mantenha este selecionado # javascript mantenha este selecionado
manter_autor = False
if 'autor_id' in request.GET and request.GET['autor_id']: if 'autor_id' in request.GET and request.GET['autor_id']:
try: try:
autor = Autor.objects.get(id=request.GET['autor_id']) autor = Autor.objects.get(id=request.GET['autor_id'])
@ -1231,9 +1230,16 @@ def atualizar_autores(request):
if autor.tipo.id == int(tipo_autor): if autor.tipo.id == int(tipo_autor):
manter_autor = True manter_autor = True
else:
autores = autores_ativos(materia)
autores_list = [(a.id, a.__str__()) for a in autores]
return JsonResponse({'lista_autores': autores_list, return JsonResponse({'lista_autores': autores_list,
'manter_autor': manter_autor}) 'manter_autor': manter_autor})
return JsonResponse({})
class AutoriaCrud(MasterDetailCrud): class AutoriaCrud(MasterDetailCrud):
model = Autoria model = Autoria

9
sapl/templates/materia/autoria_form.html

@ -25,11 +25,12 @@
lista_autores = data['lista_autores']; lista_autores = data['lista_autores'];
// Atualiza a listagem de autores // Atualiza a listagem de autores
for (i = 0; i < lista_autores.length; i++) {
$.each(lista_autores, function(index, value) {
$('#id_autor').append( $('#id_autor').append(
'<option value="' + lista_autores[i][0] + '">' + '<option value="' + value[0] + '">' +
lista_autores[i][1] + '</option>'); value[1] + '</option>');
} });
if (data['manter_autor'] == true){ if (data['manter_autor'] == true){
$('#id_autor').val(autor_id); $('#id_autor').val(autor_id);

Loading…
Cancel
Save