From 6a3fb69d76a1343a1557724e1867d6e1f7ed37d9 Mon Sep 17 00:00:00 2001 From: LeandroRoberto Date: Thu, 7 Apr 2016 13:19:21 -0300 Subject: [PATCH] Refatora Vide para trabalhar com DispositivoSearch --- compilacao/forms.py | 48 ++++++++++++----- compilacao/views.py | 10 ++-- static/js/compilacao.js | 2 +- static/js/compilacao_notas.js | 8 +-- static/js/compilacao_view.js | 10 ++-- .../layout/dispositivo_checkbox_old.html | 54 ------------------- 6 files changed, 52 insertions(+), 80 deletions(-) delete mode 100644 templates/compilacao/layout/dispositivo_checkbox_old.html diff --git a/compilacao/forms.py b/compilacao/forms.py index f6d333309..d1f949cbd 100644 --- a/compilacao/forms.py +++ b/compilacao/forms.py @@ -329,22 +329,28 @@ class DispositivoSearchFragmentForm(ModelForm): super(DispositivoSearchFragmentForm, self).__init__(*args, **kwargs) -class VideForm(DispositivoSearchFragmentForm): +class VideForm(ModelForm): dispositivo_base = forms.ModelChoiceField( queryset=Dispositivo.objects.all(), widget=forms.HiddenInput()) + dispositivo_ref = forms.ModelChoiceField( - queryset=Dispositivo.objects.all(), - widget=forms.HiddenInput()) + label=Vide._meta.get_field( + 'dispositivo_ref').verbose_name, + queryset=Dispositivo.objects.all()) tipo = forms.ModelChoiceField( label=TipoVide._meta.verbose_name, queryset=TipoVide.objects.all(), required=True, error_messages=error_messages) + texto = forms.CharField( + required=False, + label=Vide._meta.get_field( + 'texto').verbose_name, + widget=forms.Textarea()) - pk = forms.IntegerField(widget=forms.HiddenInput(), - required=False) + pk = forms.IntegerField(widget=forms.HiddenInput(), required=False) class Meta: model = Vide @@ -371,26 +377,34 @@ class VideForm(DispositivoSearchFragmentForm): css_class='btn-primary pull-right') ) - fields_form = Div( + dispositivo_ref = Field( + 'dispositivo_ref', + data_sapl_ta='DispositivoSearch', + data_field='dispositivo_ref', + data_type_selection='radio', + template="compilacao/layout/dispositivo_radio.html") + + fields_form = [] + fields_form.append(Div( Row(to_column((Field( 'tipo', placeholder=_('Selecione um Tipo de Vide')), 12))), + Row(to_column((dispositivo_ref, 12))), + Row(to_column((buttons, 12))))) + + fields_form.append(Div( Row(to_column((Field( 'texto', - placeholder=_('Texto Adicional ao Vide')), 12))), - Row(to_column((buttons, 12)))) - - kwargs['fields_search'] = fields_search = Div() + placeholder=_('Texto Adicional ao Vide')), 12))))) self.helper = FormHelper() self.helper.layout = Layout( Div( Div(HTML(_('Vides')), css_class='panel-heading'), Div( - to_column(( - fields_form, 4)), - to_column(( - fields_search, 8)), css_class="panel-body" + to_column((fields_form[0], 6)), + to_column((fields_form[1], 6)), + css_class="panel-body" ), css_class="panel panel-primary" ) @@ -398,6 +412,12 @@ class VideForm(DispositivoSearchFragmentForm): super(VideForm, self).__init__(*args, **kwargs) + self.fields['dispositivo_ref'].choices = [] + if self.instance and self.instance.dispositivo_ref_id: + self.fields['dispositivo_ref'].choices = [ + (self.instance.dispositivo_ref.pk, + self.instance.dispositivo_ref)] + class PublicacaoForm(ModelForm): diff --git a/compilacao/views.py b/compilacao/views.py index 1f14f9685..267f1cfaf 100644 --- a/compilacao/views.py +++ b/compilacao/views.py @@ -1,6 +1,6 @@ -import sys from collections import OrderedDict from datetime import datetime, timedelta +import sys from braces.views import FormMessagesMixin from django import forms @@ -36,6 +36,7 @@ from compilacao.models import (Dispositivo, Nota, from compilacao.utils import DISPOSITIVO_SELECT_RELATED from crud.base import Crud, CrudListView, make_pagination + TipoNotaCrud = Crud.build(TipoNota, 'tipo_nota') TipoVideCrud = Crud.build(TipoVide, 'tipo_vide') TipoPublicacaoCrud = Crud.build(TipoPublicacao, 'tipo_publicacao') @@ -400,8 +401,7 @@ class VideMixin(DispositivoSuccessUrlMixin): def get_initial(self): dispositivo_base = get_object_or_404( Dispositivo, pk=self.kwargs.get('dispositivo_id')) - - initial = {'dispositivo_base': dispositivo_base} + initial = {'dispositivo_base': dispositivo_base, } if 'pk' in self.kwargs: initial['pk'] = self.kwargs.get('pk') @@ -440,7 +440,7 @@ class VideCreateView(VideMixin, CreateView): self.object = None form = self.get_form() return self.render_to_response(self.get_context_data(form=form)) - +""" def get_form_kwargs(self): kwargs = super(VideCreateView, self).get_form_kwargs() @@ -451,7 +451,7 @@ class VideCreateView(VideMixin, CreateView): choice_model_type_foreignkey_in_extenal_views }) - return kwargs + return kwargs""" class VideEditView(VideMixin, UpdateView): diff --git a/static/js/compilacao.js b/static/js/compilacao.js index 058801b65..22093eed5 100644 --- a/static/js/compilacao.js +++ b/static/js/compilacao.js @@ -57,7 +57,7 @@ function DispostivoSearch(opts) { var dpts = field.find('input'); dpts.off() dpts.attr('type', 'hidden'); - $('') + $('') .insertBefore(dpts) .append($('')) .on('click', function() { diff --git a/static/js/compilacao_notas.js b/static/js/compilacao_notas.js index 47409054a..487cfb5ec 100644 --- a/static/js/compilacao_notas.js +++ b/static/js/compilacao_notas.js @@ -23,9 +23,11 @@ function onEventsDneExec(pk, model) { }); } else if (model == 'vide') { - configFormSearchTA('#dne'+pk, 'radio', 'select_for_vide'); - onChangeParamTA(); + DispostivoSearch({ + 'url_form': '/ta/search_form', + 'text_button': 'Definir Dispositivo' + }); } } @@ -38,7 +40,7 @@ var onSubmitEditNVForm = function(event) { var id_dispositivo = $('#id_dispositivo').val(); if (id_dispositivo == null) { // trata-se de um vide - $('#id_dispositivo_ref').remove(); + //$('#id_dispositivo_ref').remove(); id_dispositivo = $('#id_dispositivo_base').val(); model='vide'; } diff --git a/static/js/compilacao_view.js b/static/js/compilacao_view.js index 83303432c..0c15435dd 100644 --- a/static/js/compilacao_view.js +++ b/static/js/compilacao_view.js @@ -136,9 +136,13 @@ $(document).ready(function() { setTimeout(function() { var href = location.href.split('#') if (href.length == 2) { - $('html, body').animate({ - scrollTop: $('#dptt' + href[1] ).offset().top - window.innerHeight / 9 - }, 0); + try { + $('html, body').animate({ + scrollTop: $('#dptt' + href[1] ).offset().top - window.innerHeight / 9 + }, 0); + } + catch(err) { + } } }, 100); diff --git a/templates/compilacao/layout/dispositivo_checkbox_old.html b/templates/compilacao/layout/dispositivo_checkbox_old.html deleted file mode 100644 index ec994877d..000000000 --- a/templates/compilacao/layout/dispositivo_checkbox_old.html +++ /dev/null @@ -1,54 +0,0 @@ -{% load crispy_forms_filters %} -{% load i18n compilacao_filters common_tags%} - - -
- {% include 'bootstrap/layout/field_errors_block.html' %} - - - - - {% for choice, dpt in field.field.choices %} - {% ifchanged dpt.ta%} - {% if not forloop.first %}{% endif %} - {% endif %} - {% endfor %} - - - - {% include 'bootstrap/layout/help_text.html' %} -