Group permissions select multiple -> checkboxes

- Add handler for CheckboxSelectMultiple in form_field_snippet.html.
- Add template filter "widget_type" to get widget class name.
- Group permissions selection becomes easier.
This commit is contained in:
Aurélien Delobelle 2017-05-19 13:42:41 +02:00
parent e9073e2265
commit ae27065626
5 changed files with 44 additions and 17 deletions

View file

@ -8,7 +8,7 @@ from django.core.exceptions import ValidationError
from django.core.validators import MinLengthValidator
from django.contrib.auth.models import User, Group, Permission
from django.contrib.contenttypes.models import ContentType
from django.forms import modelformset_factory
from django.forms import modelformset_factory, widgets
from django.utils import timezone
from djconfig.forms import ConfigForm
@ -151,10 +151,25 @@ class UserGroupForm(forms.ModelForm):
model = User
fields = ['groups']
class KFetPermissionsField(forms.ModelMultipleChoiceField):
def __init__(self, *args, **kwargs):
queryset = Permission.objects.filter(
content_type__in=ContentType.objects.filter(app_label="kfet"),
)
super().__init__(
queryset=queryset,
widget=widgets.CheckboxSelectMultiple,
*args, **kwargs
)
def label_from_instance(self, obj):
return obj.name
class GroupForm(forms.ModelForm):
permissions = forms.ModelMultipleChoiceField(
queryset= Permission.objects.filter(content_type__in=
ContentType.objects.filter(app_label='kfet')))
permissions = KFetPermissionsField()
def clean_name(self):
name = self.cleaned_data['name']

View file

@ -608,3 +608,10 @@ thead .tooltip {
.ms-choice > div {
top: 4px !important;
}
/* Checkbox select multiple */
.checkbox-select-multiple label {
font-weight: normal;
margin-bottom: 0;
}

View file

@ -42,16 +42,6 @@
<script type="text/javascript">
$(document).ready(function() {
let perms_input = $("#id_permissions");
perms_input.removeClass("form-control");
perms_input.multipleSelect({
width: "100%",
filter: true,
allSelected: "Tou-te-s",
selectAllText: "Tou-te-s",
countSelected: "# sur %"
});
let $name_input = $("#id_name");
let raw_name = $name_input.val();
let prefix = "K-Fêt ";

View file

@ -3,7 +3,19 @@
<div class="form-group">
<label for="{{ field.id_for_label }}" class="col-sm-2 control-label">{{ field.label }}</label>
<div class="col-sm-10">
{{ field|add_class:'form-control' }}
{% if field|widget_type == "checkboxselectmultiple" %}
<ul class="list-unstyled checkbox-select-multiple">
{% for choice in form.permissions %}
<li class="col-sm-6 col-lg-4">
<label for="{{ choice.id_for_label }}">
{{ choice.tag }} {{ choice.choice_label }}
</label>
</li>
{% endfor %}
</ul>
{% else %}
{{ field|add_class:'form-control' }}
{% endif %}
{% if field.errors %}
<span class="help-block">{{field.errors}}</span>
{% endif %}

View file

@ -28,7 +28,6 @@ def highlight_user(user, q):
return highlight_text(text, q)
@register.filter(is_safe=True)
def highlight_clipper(clipper, q):
if clipper.fullname:
@ -38,8 +37,12 @@ def highlight_clipper(clipper, q):
return highlight_text(text, q)
@register.filter()
def ukf(balance, is_cof):
grant = is_cof and (1 + kfet_config.subvention_cof / 100) or 1
return floor(balance * 10 * grant)
@register.filter()
def widget_type(field):
return field.field.widget.__class__.__name__