From cf6aad83d30d85c515f5e12cd66e322e2951d833 Mon Sep 17 00:00:00 2001 From: Eduardo Calil Date: Tue, 1 Aug 2017 12:43:39 -0300 Subject: [PATCH] =?UTF-8?q?Ajusta=20mudan=C3=A7as=20para=20a=20tela=20de?= =?UTF-8?q?=20update?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sapl/materia/views.py | 25 +++++++++++++++++++++++- sapl/templates/materia/autoria_form.html | 9 +++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/sapl/materia/views.py b/sapl/materia/views.py index b76bb6784..94eae9a57 100644 --- a/sapl/materia/views.py +++ b/sapl/materia/views.py @@ -1215,7 +1215,23 @@ def atualizar_autores(request): autores = autores_ativos(materia, tipo=tipo_autor) autores_list = [(a.id, a.__str__()) for a in autores] - return JsonResponse({'lista_autores': autores_list}) + # Se já houver algum autor selecionado (ex: view de update) + # no campo correspondente e caso o TipoAutor selecionado + # seja o mesmo do autor que está aualmente marcado + # deve ser enviado um sinal (manter autor) para que o javascript + # mantenha este selecionado + manter_autor = False + if 'autor_id' in request.GET and request.GET['autor_id']: + try: + autor = Autor.objects.get(id=request.GET['autor_id']) + except ObjectDoesNotExist: + pass + else: + if autor.tipo.id == int(tipo_autor): + manter_autor = True + + return JsonResponse({'lista_autores': autores_list, + 'manter_autor': manter_autor}) class AutoriaCrud(MasterDetailCrud): @@ -1253,6 +1269,13 @@ class AutoriaCrud(MasterDetailCrud): def layout_key(self): return 'AutoriaUpdate' + def get_initial(self): + return { + 'tipo_autor': self.object.autor.tipo.id, + 'autor': self.object.autor.id, + 'primeiro_autor': self.object.primeiro_autor + } + def get_context_data(self, **kwargs): context = super(UpdateView, self).get_context_data(**kwargs) diff --git a/sapl/templates/materia/autoria_form.html b/sapl/templates/materia/autoria_form.html index b37de27db..7a35884d8 100644 --- a/sapl/templates/materia/autoria_form.html +++ b/sapl/templates/materia/autoria_form.html @@ -9,13 +9,14 @@ function atualizar_autores() { var tipo_autor = $("#id_tipo_autor").val(); - var materia_id = {{ root_pk }} + var materia_id = {{ root_pk }}; + var autor_id = $("#id_autor").val(); // Limpa a listagem para atualizar $("#id_autor").find("option").remove(); $.get("/materia/autoria/atualizar-autores", - {tipo_autor: tipo_autor, materia_id: materia_id}, + {tipo_autor: tipo_autor, materia_id: materia_id, autor_id: autor_id}, function(data) { // Caso não venha nenhum dado da requisição, retorna null if ($.isEmptyObject(data)){ @@ -29,6 +30,10 @@ ''); } + + if (data['manter_autor'] == true){ + $('#id_autor').val(autor_id); + } }); } $("#id_tipo_autor").change(function () {