Implement bootstrap checkboxes
This commit is contained in:
parent
0b13fe4d7a
commit
8365ba777e
@ -1,6 +1,7 @@
|
||||
from django import forms
|
||||
|
||||
from .models import LdapUser, LdapGroup
|
||||
from django.forms import modelformset_factory
|
||||
|
||||
|
||||
class AddLDAPUserForm(forms.Form):
|
||||
@ -24,9 +25,10 @@ class UserDeleteListForm(forms.Form):
|
||||
|
||||
|
||||
class AddLDAPGroupForm(forms.Form):
|
||||
name = forms.CharField(label='name', max_length=400)
|
||||
name = forms.CharField(label='Name', max_length=400)
|
||||
# TODO show only allowed user
|
||||
members = forms.ModelMultipleChoiceField(widget=forms.CheckboxSelectMultiple, queryset=LdapUser.objects.all())
|
||||
members = forms.ModelMultipleChoiceField(label='Mitglieder', widget=forms.CheckboxSelectMultiple,
|
||||
queryset=LdapUser.objects.all(), )
|
||||
|
||||
|
||||
class RealmAddForm(forms.Form):
|
||||
@ -45,3 +47,10 @@ class RealmUpdateForm(forms.Form):
|
||||
admin_group = forms.ModelChoiceField(label='Admin Gruppe',
|
||||
help_text="Die Mitglieder dieser Gruppe darf den Bereich administieren",
|
||||
queryset=LdapGroup.objects.all())
|
||||
|
||||
|
||||
UserFormset = modelformset_factory(
|
||||
LdapUser,
|
||||
fields=('dn',),
|
||||
extra=1
|
||||
)
|
||||
|
||||
12
account_manager/utils/dbldap.py
Normal file
12
account_manager/utils/dbldap.py
Normal file
@ -0,0 +1,12 @@
|
||||
from django.db import connections
|
||||
from ldapdb.backends.ldap.compiler import SQLCompiler, query_as_ldap
|
||||
|
||||
|
||||
def get_filterstr(qs):
|
||||
connection = connections['ldap']
|
||||
compiler = SQLCompiler(
|
||||
query=qs.query,
|
||||
connection=connection,
|
||||
using=None,
|
||||
)
|
||||
return query_as_ldap(qs.query, compiler, connection).filterstr
|
||||
@ -1,10 +1,13 @@
|
||||
import re
|
||||
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import render, redirect
|
||||
|
||||
from account_helper.models import Realm
|
||||
from account_manager.forms import AddLDAPGroupForm
|
||||
from account_manager.models import LdapGroup, LdapUser
|
||||
from account_manager.main_views import is_realm_admin
|
||||
from account_manager.models import LdapGroup, LdapUser
|
||||
|
||||
|
||||
@login_required
|
||||
@ -58,6 +61,10 @@ def group_update(request, realm_id, group_dn):
|
||||
group = LdapGroup.objects.get(dn=group_dn)
|
||||
LdapUser.base_dn = LdapUser.ROOT_DN
|
||||
if request.method == 'POST':
|
||||
# user_ids = list(map(int, request.POST.getlist('members')))
|
||||
# user_formset = UserFormset(request.POST)
|
||||
# if user_formset and user_formset.is_valid():
|
||||
# print(user_formset)
|
||||
# create a form instance and populate it with data from the request:
|
||||
form = AddLDAPGroupForm(request.POST)
|
||||
# check whether it's valid:
|
||||
@ -71,10 +78,19 @@ def group_update(request, realm_id, group_dn):
|
||||
# if a GET (or any other method) we'll create a blank form
|
||||
else:
|
||||
# TODO: Automatic checkbox selection
|
||||
data = {'name': group.name, 'members': group.members}
|
||||
members = LdapUser.objects.none()
|
||||
if group.members:
|
||||
group_members = [re.compile('uid=([a-zA-Z0-9_]*),(ou=[a-zA-Z_]*),(.*)').match(member).group(1) for member in
|
||||
group.members]
|
||||
query = Q(username=group_members.pop())
|
||||
for member in group_members:
|
||||
query = query | Q(username=member)
|
||||
members = LdapUser.objects.filter(query)
|
||||
data = {'name': group.name, 'members': members}
|
||||
form = AddLDAPGroupForm(initial=data)
|
||||
|
||||
return render(request, 'group/group_detail.jinja2', {'form': form, 'realm': realm, 'group': group})
|
||||
return render(request, 'group/group_detail.jinja2',
|
||||
{'form': form, 'realm': realm, 'group': group})
|
||||
|
||||
|
||||
def group_delete(request, realm_id, group_dn):
|
||||
|
||||
@ -5,8 +5,8 @@
|
||||
<form action="{{ url('realm-group-add', args=[realm.id]) }}" method="post">
|
||||
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
|
||||
{{ mform.text_input(form.name) }}
|
||||
<p>Mitglieder</p>
|
||||
{{ form.members }}
|
||||
{{ form.members.label }}
|
||||
{{ mform.checkbox_input_for_choice_fields(form.members) }}
|
||||
<div class="d-flex mt-4">
|
||||
<button type="submit" class="btn btn-primary mr-auto p-2">Speichern</button>
|
||||
<a href="{{ url('realm-group-list', args = [realm.id]) }}"
|
||||
|
||||
@ -24,17 +24,9 @@
|
||||
{% else %}
|
||||
<form method="post">
|
||||
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
|
||||
{{ form.as_p()|safe }}
|
||||
{{ mform.text_input(form.name) }}
|
||||
{% for choice_value, choice_label in form.members.field.choices %}
|
||||
<p>{{ form.members.html_name }} | {{ choice_value }} | {{ choice_label }} | {{ form.members.value.value }}</p>
|
||||
{# {{ mform.checkbox_input_for_choice_fields(choice_id, choice_label ) }}#}
|
||||
{% endfor %}
|
||||
{# {% for value in form.members.value %}#}
|
||||
{# <p>{{ value }}</p>#}
|
||||
{# {{ mform.checkbox_input_for_choice_fields(choice_id, choice_label ) }}#}
|
||||
{# {% endfor %}#}
|
||||
|
||||
{{ form.members.label }}
|
||||
{{ mform.checkbox_input_for_choice_fields(form.members) }}
|
||||
<div class="d-flex mt-4">
|
||||
<button type="submit" class="btn btn-primary mr-auto p-2">Speichern</button>
|
||||
<a href="{{ url('realm-group-detail', args = [realm.id, group.dn]) }}"
|
||||
|
||||
@ -143,34 +143,25 @@
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
{% macro checkbox_input_for_choice_fields(label, value, id) -%}
|
||||
{% macro checkbox_input_for_choice_fields(field) -%}
|
||||
<div class="form-check">
|
||||
{# <label class="{{ type }}{% if display == "inline" or field.field.widget.attrs.inline %} inline{% endif %}">#}
|
||||
{# <input#}
|
||||
{# type="checkbox"#}
|
||||
{# name="{{ field.html_name }}"#}
|
||||
{# value="{{ choice_id }}"#}
|
||||
{# {% if field.field.widget.allow_multiple_selected %}#}
|
||||
{# {% if choice_id|safe in field.value|safeseq %}checked{% endif %}#}
|
||||
{# {% else %}#}
|
||||
{# {% if field.value|safe == choice_id|safe %}checked{% endif %}#}
|
||||
{# {% endif %}#}
|
||||
{# {{ field.field.widget.attrs|html_attrs }}#}
|
||||
{# />#}
|
||||
{# {{ choice_label }}#}
|
||||
{# </label>#}
|
||||
<input type="checkbox"
|
||||
class="form-check-input"
|
||||
placeholder="{{ field.label }}"
|
||||
aria-describedby="{{ field.id_for_label }}_help"
|
||||
name="{{ field.html_name }}"
|
||||
id="{{ field.id_for_label }}"
|
||||
{% if field.value() != None %}value="{{ field.value() }}"{% endif %}
|
||||
maxlength="{{ field.field.max_length }}"
|
||||
{% if field.field.required %}required{% endif %}>
|
||||
<label id="{{ field.id_for_label }}" class="form-check-label" for="{{ field.id_for_label }}">
|
||||
{{ field.label }}
|
||||
</label>
|
||||
<ul>
|
||||
{% for choice_value, choice_label in field.field.choices %}
|
||||
<li>
|
||||
<input type="checkbox"
|
||||
class="custom-control-input"
|
||||
aria-describedby="{{ choice_label }}_help"
|
||||
name="{{ field.html_name }}"
|
||||
id="{{ choice_label }}_{{ loop.index }}"
|
||||
value="{{ choice_value }}"
|
||||
{% if field.value() and choice_value in field.value() %}checked{% endif %}
|
||||
>
|
||||
<label class="custom-control-label" for="{{ choice_label }}_{{ loop.index }}">
|
||||
{{ choice_label }}
|
||||
</label>
|
||||
</li>
|
||||
{% endfor %}
|
||||
</ul>
|
||||
</div>
|
||||
{% endmacro %}
|
||||
|
||||
|
||||
@ -33,7 +33,7 @@
|
||||
<a href="{{ url('user-update', args = [user.dn, realm.id]) }}"
|
||||
class="btn btn-primary mr-auto p-2">Update
|
||||
User</a>
|
||||
<a href="{{ url('user-delete', args = [user.dn, realm.id]) }}"
|
||||
<a href="{{ url('user-delete-confirm', args = [user.dn, realm.id]) }}"
|
||||
class="btn btn-danger p-2">Delete
|
||||
User</a>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user