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-11 09:39:44 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2015-03-02 08:52:07 +00:00
|
|
|
from django.contrib.auth.models import User
|
2015-03-18 15:13:44 +00:00
|
|
|
from accounts.models import UserInstance
|
2015-03-11 09:39:44 +00:00
|
|
|
from instances.models import Instance
|
2015-03-18 15:13:44 +00:00
|
|
|
from accounts.forms import UserAddForm
|
2015-03-02 08:52:07 +00:00
|
|
|
|
|
|
|
|
2015-03-18 15:13:44 +00:00
|
|
|
def profile(request):
|
|
|
|
"""
|
|
|
|
:param request:
|
|
|
|
:return:
|
|
|
|
"""
|
|
|
|
if not request.user.is_authenticated():
|
|
|
|
return HttpResponseRedirect(reverse('index'))
|
|
|
|
|
2015-03-24 12:45:38 +00:00
|
|
|
error_messages = []
|
|
|
|
user = User.objects.get(id=request.user.id)
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
if 'username' in request.POST:
|
|
|
|
username = request.POST.get('username', '')
|
|
|
|
email = request.POST.get('email', '')
|
|
|
|
user.first_name = username
|
|
|
|
user.email = email
|
|
|
|
user.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'oldpasswd' in request.POST:
|
|
|
|
oldpasswd = request.POST.get('oldpasswd', '')
|
|
|
|
password1 = request.POST.get('passwd1', '')
|
|
|
|
password2 = request.POST.get('passwd2', '')
|
2015-03-25 09:36:37 +00:00
|
|
|
if not password1 or not password2:
|
|
|
|
error_messages.append("Passwords didn't enter")
|
2015-03-24 12:45:38 +00:00
|
|
|
if password1 and password2 and password1 != password2:
|
|
|
|
error_messages.append("Passwords don't match")
|
|
|
|
if not user.check_password(oldpasswd):
|
|
|
|
error_messages.append("Old password is wrong!")
|
|
|
|
if not error_messages:
|
|
|
|
user.set_password(password1)
|
|
|
|
user.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'profile.html', locals())
|
2015-03-18 15:13:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
def accounts(request):
|
2015-03-02 08:52:07 +00:00
|
|
|
"""
|
|
|
|
: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-11 12:01:21 +00:00
|
|
|
if 'edit' in request.POST:
|
|
|
|
user_id = request.POST.get('user_id', '')
|
|
|
|
user_pass = request.POST.get('user_pass', '')
|
|
|
|
user_edit = User.objects.get(id=user_id)
|
2015-03-25 09:36:37 +00:00
|
|
|
user_edit.set_password(user_pass)
|
2015-03-11 12:01:21 +00:00
|
|
|
user_edit.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'block' in request.POST:
|
|
|
|
user_id = request.POST.get('user_id', '')
|
|
|
|
user_block = User.objects.get(id=user_id)
|
|
|
|
user_block.is_active = False
|
|
|
|
user_block.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'unblock' in request.POST:
|
|
|
|
user_id = request.POST.get('user_id', '')
|
|
|
|
user_unblock = User.objects.get(id=user_id)
|
|
|
|
user_unblock.is_active = True
|
|
|
|
user_unblock.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
|
|
|
if 'delete' in request.POST:
|
|
|
|
user_id = request.POST.get('user_id', '')
|
|
|
|
try:
|
|
|
|
del_user_inst = UserInstance.objects.filter(user_id=user_id)
|
|
|
|
del_user_inst.delete()
|
|
|
|
finally:
|
|
|
|
user_delete = User.objects.get(id=user_id)
|
|
|
|
user_delete.delete()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-10 14:24:10 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'accounts.html', locals())
|
2015-03-02 08:52:07 +00:00
|
|
|
|
2015-03-11 09:39:44 +00:00
|
|
|
|
2015-03-18 15:13:44 +00:00
|
|
|
def account(request, user_id):
|
2015-03-02 08:52:07 +00:00
|
|
|
"""
|
|
|
|
: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-11 09:39:44 +00:00
|
|
|
error_messages = []
|
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)
|
2015-03-11 09:39:44 +00:00
|
|
|
instances = Instance.objects.all()
|
2015-03-10 14:24:10 +00:00
|
|
|
|
2015-03-26 14:03:12 +00:00
|
|
|
if user.username == request.user.username:
|
2015-03-27 15:12:15 +00:00
|
|
|
return HttpResponseRedirect(reverse('profile'))
|
2015-03-26 14:03:12 +00:00
|
|
|
|
2015-03-10 14:24:10 +00:00
|
|
|
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())
|
2015-03-16 13:46:44 +00:00
|
|
|
if 'permission' in request.POST:
|
2015-03-10 14:24:10 +00:00
|
|
|
user_inst = request.POST.get('user_inst', '')
|
|
|
|
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_change = bool(inst_change)
|
|
|
|
edit_user_inst.is_delete = bool(inst_delete)
|
|
|
|
edit_user_inst.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-11 09:39:44 +00:00
|
|
|
if 'add' in request.POST:
|
|
|
|
inst_id = request.POST.get('inst_id', '')
|
|
|
|
try:
|
|
|
|
check_inst = UserInstance.objects.get(instance_id=int(inst_id))
|
|
|
|
msg = _("Instance already added")
|
|
|
|
error_messages.append(msg)
|
|
|
|
except UserInstance.DoesNotExist:
|
|
|
|
add_user_inst = UserInstance(instance_id=int(inst_id), user_id=user_id)
|
|
|
|
add_user_inst.save()
|
|
|
|
return HttpResponseRedirect(request.get_full_path())
|
2015-03-02 08:52:07 +00:00
|
|
|
|
2015-04-02 13:20:46 +00:00
|
|
|
return render(request, 'account.html', locals())
|