2020-06-16 05:26:58 +00:00
|
|
|
from django.conf import settings
|
2020-07-23 10:16:39 +00:00
|
|
|
from django.contrib import messages
|
|
|
|
from django.contrib.auth import update_session_auth_hash
|
|
|
|
from django.contrib.auth.forms import AdminPasswordChangeForm
|
2020-05-27 12:24:06 +00:00
|
|
|
from django.contrib.auth.models import Group, User
|
|
|
|
from django.core.paginator import Paginator
|
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2020-05-27 12:24:06 +00:00
|
|
|
|
2020-06-16 05:26:58 +00:00
|
|
|
from accounts.models import UserAttributes, UserInstance, Instance
|
2020-09-25 12:57:17 +00:00
|
|
|
from appsettings.settings import app_settings
|
2020-05-27 12:24:06 +00:00
|
|
|
from logs.models import Logs
|
|
|
|
|
|
|
|
from . import forms
|
|
|
|
from .decorators import superuser_only
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def group_list(request):
|
|
|
|
groups = Group.objects.all()
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"admin/group_list.html",
|
2020-05-27 12:24:06 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"groups": groups,
|
2020-05-27 12:24:06 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def group_create(request):
|
|
|
|
form = forms.GroupForm(request.POST or None)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
2020-09-25 12:57:17 +00:00
|
|
|
return redirect("admin:group_list")
|
2020-05-29 09:47:13 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"common/form.html",
|
2020-05-27 12:24:06 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"form": form,
|
|
|
|
"title": _("Create Group"),
|
2020-05-27 12:24:06 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def group_update(request, pk):
|
|
|
|
group = get_object_or_404(Group, pk=pk)
|
|
|
|
form = forms.GroupForm(request.POST or None, instance=group)
|
|
|
|
if form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return redirect('admin:group_list')
|
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"common/form.html",
|
2020-05-27 12:24:06 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"form": form,
|
|
|
|
"title": _("Update Group"),
|
2020-05-27 12:24:06 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def group_delete(request, pk):
|
|
|
|
group = get_object_or_404(Group, pk=pk)
|
2020-09-25 12:57:17 +00:00
|
|
|
if request.method == "POST":
|
2020-05-27 12:24:06 +00:00
|
|
|
group.delete()
|
2020-09-25 12:57:17 +00:00
|
|
|
return redirect("admin:group_list")
|
2020-05-27 12:24:06 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"common/confirm_delete.html",
|
|
|
|
{"object": group},
|
2020-05-27 12:24:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_list(request):
|
|
|
|
users = User.objects.all()
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"admin/user_list.html",
|
2020-05-27 12:24:06 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"users": users,
|
|
|
|
"title": _("Users"),
|
2020-05-27 12:24:06 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_create(request):
|
|
|
|
user_form = forms.UserCreateForm(request.POST or None)
|
|
|
|
attributes_form = forms.UserAttributesForm(request.POST or None)
|
|
|
|
if user_form.is_valid() and attributes_form.is_valid():
|
|
|
|
user = user_form.save()
|
2020-09-25 12:57:17 +00:00
|
|
|
password = user_form.cleaned_data["password"]
|
2020-05-27 12:24:06 +00:00
|
|
|
user.set_password(password)
|
|
|
|
user.save()
|
|
|
|
attributes = attributes_form.save(commit=False)
|
|
|
|
attributes.user = user
|
|
|
|
attributes.save()
|
2020-06-16 05:26:58 +00:00
|
|
|
add_default_instances(user)
|
2020-09-25 12:57:17 +00:00
|
|
|
return redirect("admin:user_list")
|
2020-05-27 12:24:06 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"admin/user_form.html",
|
2020-05-27 12:24:06 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"user_form": user_form,
|
|
|
|
"attributes_form": attributes_form,
|
|
|
|
"title": _("Create User")
|
2020-05-27 12:24:06 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_update(request, pk):
|
|
|
|
user = get_object_or_404(User, pk=pk)
|
|
|
|
attributes = UserAttributes.objects.get(user=user)
|
|
|
|
user_form = forms.UserForm(request.POST or None, instance=user)
|
2020-09-25 12:57:17 +00:00
|
|
|
attributes_form = forms.UserAttributesForm(
|
|
|
|
request.POST or None, instance=attributes)
|
2020-05-27 12:24:06 +00:00
|
|
|
if user_form.is_valid() and attributes_form.is_valid():
|
|
|
|
user_form.save()
|
|
|
|
attributes_form.save()
|
2020-09-25 12:57:17 +00:00
|
|
|
return redirect("admin:user_list")
|
2020-05-27 12:24:06 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"admin/user_form.html",
|
2020-05-27 12:24:06 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"user_form": user_form,
|
|
|
|
"attributes_form": attributes_form,
|
|
|
|
"title": _("Update User")
|
2020-05-27 12:24:06 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2020-09-25 12:57:17 +00:00
|
|
|
|
2020-07-23 10:16:39 +00:00
|
|
|
@superuser_only
|
|
|
|
def user_update_password(request, pk):
|
|
|
|
user = get_object_or_404(User, pk=pk)
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = AdminPasswordChangeForm(user, request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
user = form.save()
|
|
|
|
update_session_auth_hash(request, user) # Important!
|
2020-09-25 12:57:17 +00:00
|
|
|
messages.success(request, _(
|
|
|
|
"User password changed: {}".format(user.username)))
|
|
|
|
return redirect("admin:user_list")
|
2020-07-23 10:16:39 +00:00
|
|
|
else:
|
2020-09-25 12:57:17 +00:00
|
|
|
messages.error(request, _("Wrong Data Provided"))
|
2020-07-23 10:16:39 +00:00
|
|
|
else:
|
|
|
|
form = AdminPasswordChangeForm(user)
|
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"accounts/change_password_form.html",
|
2020-07-23 10:16:39 +00:00
|
|
|
{
|
2020-09-25 12:57:17 +00:00
|
|
|
"form": form,
|
|
|
|
"user": user.username
|
2020-07-23 10:16:39 +00:00
|
|
|
}
|
|
|
|
)
|
2020-05-27 12:24:06 +00:00
|
|
|
|
2020-09-25 12:57:17 +00:00
|
|
|
|
2020-05-27 12:24:06 +00:00
|
|
|
@superuser_only
|
|
|
|
def user_delete(request, pk):
|
|
|
|
user = get_object_or_404(User, pk=pk)
|
2020-09-25 12:57:17 +00:00
|
|
|
if request.method == "POST":
|
2020-05-27 12:24:06 +00:00
|
|
|
user.delete()
|
2020-09-25 12:57:17 +00:00
|
|
|
return redirect("admin:user_list")
|
2020-05-27 12:24:06 +00:00
|
|
|
|
|
|
|
return render(
|
|
|
|
request,
|
2020-09-25 12:57:17 +00:00
|
|
|
"common/confirm_delete.html",
|
|
|
|
{"object": user},
|
2020-05-27 12:24:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_block(request, pk):
|
|
|
|
user: User = get_object_or_404(User, pk=pk)
|
|
|
|
user.is_active = False
|
|
|
|
user.save()
|
2020-09-25 12:57:17 +00:00
|
|
|
return redirect("admin:user_list")
|
2020-05-27 12:24:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def user_unblock(request, pk):
|
|
|
|
user: User = get_object_or_404(User, pk=pk)
|
|
|
|
user.is_active = True
|
|
|
|
user.save()
|
2020-09-25 12:57:17 +00:00
|
|
|
return redirect("admin:user_list")
|
2020-05-27 12:24:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
@superuser_only
|
|
|
|
def logs(request):
|
2020-09-25 12:57:17 +00:00
|
|
|
l = Logs.objects.order_by("-date")
|
|
|
|
paginator = Paginator(l, int(app_settings.LOGS_PER_PAGE))
|
|
|
|
page = request.GET.get("page", 1)
|
2020-05-27 12:24:06 +00:00
|
|
|
logs = paginator.page(page)
|
2020-09-25 12:57:17 +00:00
|
|
|
return render(request, "admin/logs.html", {"logs": logs})
|
2020-06-16 05:26:58 +00:00
|
|
|
|
|
|
|
|
|
|
|
def add_default_instances(user):
|
|
|
|
"""
|
|
|
|
Adds instances listed in NEW_USER_DEFAULT_INSTANCES to user
|
|
|
|
"""
|
|
|
|
existing_instances = UserInstance.objects.filter(user=user)
|
|
|
|
if not existing_instances:
|
|
|
|
for instance_name in settings.NEW_USER_DEFAULT_INSTANCES:
|
|
|
|
instance = Instance.objects.get(name=instance_name)
|
|
|
|
user_instance = UserInstance(user=user, instance=instance)
|
|
|
|
user_instance.save()
|