2015-03-02 08:52:07 +00:00
|
|
|
from django.shortcuts import render
|
2015-03-03 09:34:09 +00:00
|
|
|
from django.http import HttpResponseRedirect
|
|
|
|
from django.core.urlresolvers import reverse
|
2015-03-02 08:52:07 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-03-10 14:24:10 +00:00
|
|
|
from users.models import UserInstance
|
|
|
|
from users.forms import UserAddForm
|
2015-03-02 08:52:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
def users(request):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-03-03 09:34:09 +00:00
|
|
|
if not request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect(reverse('index'))
|
|
|
|
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
return HttpResponseRedirect(reverse('index'))
|
|
|
|
|
2015-03-10 14:24:10 +00:00
|
|
|
error_messages = []
|
2015-03-02 08:52:07 +00:00
|
|
|
users = User.objects.filter(is_staff=False, is_superuser=False)
|
|
|
|
|
2015-03-10 14:24:10 +00:00
|
|
|
if request.method == 'POST':
|
|
|
|
if 'create' in request.POST:
|
|
|
|
form = UserAddForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
data = form.cleaned_data
|
|
|
|
else:
|
|
|
|
for msg_err in form.errors.values():
|
|
|
|
error_messages.append(msg_err.as_text())
|
|
|
|
if not error_messages:
|
|
|
|
new_user = User.objects.create_user(data['name'], None, data['password'])
|
|
|
|
new_user.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
|
2015-03-02 08:52:07 +00:00
|
|
|
return render(request, 'users.html', locals())
|
|
|
|
|
|
|
|
def user(request, user_id):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
|
2015-03-03 09:34:09 +00:00
|
|
|
if not request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect(reverse('index'))
|
|
|
|
|
|
|
|
if not request.user.is_superuser:
|
|
|
|
return HttpResponseRedirect(reverse('index'))
|
|
|
|
|
2015-03-02 08:52:07 +00:00
|
|
|
user = User.objects.get(id=user_id)
|
2015-03-10 14:24:10 +00:00
|
|
|
user_insts = UserInstance.objects.filter(user_id=user_id)
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'delete' in request.POST:
|
|
|
|
user_inst = request.POST.get('user_inst', '')
|
|
|
|
del_user_inst = UserInstance.objects.get(id=user_inst)
|
|
|
|
del_user_inst.delete()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'edit' in request.POST:
|
|
|
|
user_inst = request.POST.get('user_inst', '')
|
|
|
|
inst_block = request.POST.get('inst_block', '')
|
|
|
|
inst_change = request.POST.get('inst_change', '')
|
|
|
|
inst_delete = request.POST.get('inst_delete', '')
|
|
|
|
edit_user_inst = UserInstance.objects.get(id=user_inst)
|
|
|
|
edit_user_inst.is_block = bool(inst_block)
|
|
|
|
edit_user_inst.is_change = bool(inst_change)
|
|
|
|
edit_user_inst.is_delete = bool(inst_delete)
|
|
|
|
edit_user_inst.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-02 08:52:07 +00:00
|
|
|
|
|
|
|
return render(request, 'user.html', locals())
|