mirror of https://github.com/snachodog/mybuddy.git
Handle read only setting in users form
This commit is contained in:
parent
cd44a473cf
commit
94bc12973a
|
@ -1,12 +1,20 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
from django import forms
|
from django import forms
|
||||||
from django.contrib.auth.forms import PasswordChangeForm, UserCreationForm
|
|
||||||
from django.contrib.auth import get_user_model
|
from django.contrib.auth import get_user_model
|
||||||
|
from django.contrib.auth.forms import PasswordChangeForm, UserCreationForm
|
||||||
|
from django.contrib.auth.models import Group
|
||||||
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from .models import Settings
|
from .models import Settings
|
||||||
|
|
||||||
|
|
||||||
class UserAddForm(UserCreationForm):
|
class BabyBuddyUserForm(forms.ModelForm):
|
||||||
|
is_readonly = forms.BooleanField(
|
||||||
|
required=False,
|
||||||
|
label=_("Read only"),
|
||||||
|
help_text=_("Restricts user to viewing data only."),
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = get_user_model()
|
model = get_user_model()
|
||||||
fields = [
|
fields = [
|
||||||
|
@ -15,29 +23,41 @@ class UserAddForm(UserCreationForm):
|
||||||
"last_name",
|
"last_name",
|
||||||
"email",
|
"email",
|
||||||
"is_staff",
|
"is_staff",
|
||||||
|
"is_readonly",
|
||||||
"is_active",
|
"is_active",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def __init__(self, *args, **kwargs):
|
||||||
|
user = kwargs["instance"]
|
||||||
|
if user:
|
||||||
|
kwargs["initial"].update(
|
||||||
|
{"is_readonly": user.groups.filter(name="read_only").exists()}
|
||||||
|
)
|
||||||
|
super(BabyBuddyUserForm, self).__init__(*args, **kwargs)
|
||||||
|
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
user = super(UserAddForm, self).save(commit=False)
|
user = super(BabyBuddyUserForm, self).save(commit=False)
|
||||||
# All Baby Buddy users are superusers.
|
is_readonly = self.cleaned_data["is_readonly"]
|
||||||
user.is_superuser = True
|
if is_readonly:
|
||||||
|
user.is_superuser = False
|
||||||
|
else:
|
||||||
|
user.is_superuser = True
|
||||||
if commit:
|
if commit:
|
||||||
user.save()
|
user.save()
|
||||||
|
readonly_group = Group.objects.get(name="read_only")
|
||||||
|
if is_readonly:
|
||||||
|
user.groups.add(readonly_group.id)
|
||||||
|
else:
|
||||||
|
user.groups.remove(readonly_group.id)
|
||||||
return user
|
return user
|
||||||
|
|
||||||
|
|
||||||
class UserUpdateForm(forms.ModelForm):
|
class UserAddForm(BabyBuddyUserForm, UserCreationForm):
|
||||||
class Meta:
|
pass
|
||||||
model = get_user_model()
|
|
||||||
fields = [
|
|
||||||
"username",
|
class UserUpdateForm(BabyBuddyUserForm):
|
||||||
"first_name",
|
pass
|
||||||
"last_name",
|
|
||||||
"email",
|
|
||||||
"is_staff",
|
|
||||||
"is_active",
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
class UserForm(forms.ModelForm):
|
class UserForm(forms.ModelForm):
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
{% extends 'babybuddy/page.html' %}
|
{% extends 'babybuddy/page.html' %}
|
||||||
{% load bootstrap i18n widget_tweaks %}
|
{% load babybuddy_tags bootstrap i18n widget_tweaks %}
|
||||||
|
|
||||||
{% block title %}{% trans "Users" %}{% endblock %}
|
{% block title %}{% trans "Users" %}{% endblock %}
|
||||||
|
|
||||||
|
@ -18,6 +18,7 @@
|
||||||
<th>{% trans "First Name" %}</th>
|
<th>{% trans "First Name" %}</th>
|
||||||
<th>{% trans "Last Name" %}</th>
|
<th>{% trans "Last Name" %}</th>
|
||||||
<th>{% trans "Email" %}</th>
|
<th>{% trans "Email" %}</th>
|
||||||
|
<th>{% trans "Read only" %}</th>
|
||||||
<th>{% trans "Staff" %}</th>
|
<th>{% trans "Staff" %}</th>
|
||||||
<th>{% trans "Active" %}</th>
|
<th>{% trans "Active" %}</th>
|
||||||
<th class="text-center">{% trans "Actions" %}</th>
|
<th class="text-center">{% trans "Actions" %}</th>
|
||||||
|
@ -30,6 +31,8 @@
|
||||||
<td>{{ object.first_name }}</td>
|
<td>{{ object.first_name }}</td>
|
||||||
<td>{{ object.last_name }}</td>
|
<td>{{ object.last_name }}</td>
|
||||||
<td>{{ object.email }}</td>
|
<td>{{ object.email }}</td>
|
||||||
|
{% user_is_read_only object as is_readonly %}
|
||||||
|
<td>{{ is_readonly|bool_icon }}</td>
|
||||||
<td>{{ object.is_staff|bool_icon }}</td>
|
<td>{{ object.is_staff|bool_icon }}</td>
|
||||||
<td>{{ object.is_active|bool_icon }}</td>
|
<td>{{ object.is_active|bool_icon }}</td>
|
||||||
<td class="text-center">
|
<td class="text-center">
|
||||||
|
|
|
@ -63,3 +63,8 @@ def make_absolute_url(context, url):
|
||||||
request = context["request"]
|
request = context["request"]
|
||||||
abs_url = request.build_absolute_uri(url)
|
abs_url = request.build_absolute_uri(url)
|
||||||
return abs_url
|
return abs_url
|
||||||
|
|
||||||
|
|
||||||
|
@register.simple_tag()
|
||||||
|
def user_is_read_only(user):
|
||||||
|
return user.groups.filter(name="read_only").exists()
|
||||||
|
|
Loading…
Reference in New Issue