Browse Source

refatora AutorForm para seleção de usuários

pull/3371/head
Leandro Roberto 5 years ago
parent
commit
f1d87d2edd
  1. 53
      sapl/base/forms.py
  2. 1
      sapl/templates/base/autor_form.html
  3. 34
      sapl/templates/bootstrap4/layout/checkboxselectmultiple.html

53
sapl/base/forms.py

@ -1,7 +1,7 @@
import logging
import os
from _pytest.tmpdir import get_user
from crispy_forms.bootstrap import FieldWithButtons, InlineRadios, StrictButton, FormActions
from crispy_forms.layout import HTML, Button, Div, Field, Fieldset, Layout, Row, Submit
from django import forms
@ -22,7 +22,8 @@ from sapl.audiencia.models import AudienciaPublica
from sapl.base.models import Autor, TipoAutor, OperadorAutor
from sapl.comissoes.models import Reuniao
from sapl.crispy_layout_mixin import (form_actions, to_column, to_row,
SaplFormHelper, SaplFormLayout)
SaplFormHelper, SaplFormLayout,
to_fieldsets)
from sapl.materia.models import (DocumentoAcessorio, MateriaEmTramitacao,
MateriaLegislativa, UnidadeTramitacao,
StatusTramitacao)
@ -422,6 +423,15 @@ class TipoAutorForm(ModelForm):
'por ser equivalente a um tipo já existente'))
class UserCheckboxSelectMultiple(forms.CheckboxSelectMultiple):
#template_name = 'base/checkbox_select.html'
#option_template_name = 'base/radio_option.html'
def render(self, name, value, attrs=None, renderer=None):
super().render(name, value, attrs, renderer)
class AutorForm(ModelForm):
logger = logging.getLogger(__name__)
@ -454,13 +464,22 @@ class AutorForm(ModelForm):
required=False,
widget=forms.RadioSelect())
operadores = forms.ModelMultipleChoiceField(
queryset=get_user_model().objects.filter(is_active=True),
widget=UserCheckboxSelectMultiple(),
label=_('Usuários do SAPL ligados ao autor acima selecionado'),
required=False,
)
class Meta:
model = Autor
fields = ['tipo',
'nome',
'cargo',
'autor_related',
'q', ]
'q',
'operadores'
]
def __init__(self, *args, **kwargs):
@ -486,12 +505,38 @@ class AutorForm(ModelForm):
Field('autor_related'),
css_class='radiogroup-autor-related hidden'),
12)))
operadores_select = to_row(
[
('operadores', 12)
]
)
self.helper = SaplFormHelper()
self.helper.layout = SaplFormLayout(autor_select)
self.helper.layout = SaplFormLayout(autor_select, *operadores_select)
super(AutorForm, self).__init__(*args, **kwargs)
self.fields['operadores'].choices = [
(
u.id,
u.username,
u
)
for u in get_user_model().objects.all().order_by(
get_user_model().USERNAME_FIELD
)
]
if self.instance.pk:
if self.instance.autor_related:
self.fields['autor_related'].choices = [
(self.instance.autor_related.pk,
self.instance.autor_related)]
self.fields['q'].initial = ''
self.fields['autor_related'].initial = self.instance.autor_related
def valida_igualdade(self, texto1, texto2, msg):
if texto1 != texto2:
self.logger.warning(

1
sapl/templates/base/autor_form.html

@ -89,6 +89,7 @@ $(document).ready(function(){
if (flag_create)
$('input[name=autor_related]').closest('.radio').remove();
var pk = $('#id_tipo').val();
if (pk)
update_search(pk, $('#id_q').val().length > 0)

34
sapl/templates/bootstrap4/layout/checkboxselectmultiple.html

@ -0,0 +1,34 @@
{% load crispy_forms_filters common_tags %}
{% load l10n %}
<div class="{% if inline_class %}form-check{% endif %}{% if field_class %} {{ field_class }}{% endif %}"{% if flat_attrs %} {{ flat_attrs|safe }}{% endif %}>
{% include 'bootstrap4/layout/field_errors_block.html' %}
{% for choice in field.field.choices %}
{% if choice.2 %}
<div class="form-check">
{% if choice.2|meta_model_value:'label' == 'auth.User' %}
<label class="form-check-label d-flex align-items-center py-2" id="id_{{ field.id_for_label }}_{{ forloop.counter }}" for="id_{{ field.html_name }}_{{ forloop.counter }}">
<input type="checkbox" class="form-check-input"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|default_if_none:""|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
<div class="d-inline-block">
{{choice.2.get_full_name}}
<i>({{choice.2}})</i><br>
<small>{{choice.2.email}}</small>
</div>
</label>
{% endif %}
</div>
{% else %}
{% if not inline_class %}<div class="form-check">{% endif %}
<label id="id_{{ field.id_for_label }}_{{ forloop.counter }}" class="form-check-{% if inline_class %}{{ inline_class }}{% else %}label{% endif %}" for="id_{{ field.html_name }}_{{ forloop.counter }}">
<input type="checkbox" class="form-check-input"{% if choice.0 in field.value or choice.0|stringformat:"s" in field.value or choice.0|stringformat:"s" == field.value|default_if_none:""|stringformat:"s" %} checked="checked"{% endif %} name="{{ field.html_name }}" id="id_{{ field.html_name }}_{{ forloop.counter }}" value="{{ choice.0|unlocalize }}" {{ field.field.widget.attrs|flatatt }}>
{{ choice.1|unlocalize }}
</label>
{% if not inline_class %}</div>{% endif %}
{% endif %}
{% endfor %}
{% include 'bootstrap4/layout/help_text.html' %}
</div>
Loading…
Cancel
Save