def user_change(request): if request.user.is_authenticated() and request.user.is_superuser: if request.method=='POST': vm_list=request.POST.getlist('vmSelect') user=request.POST.get('user') user=CustomUser.objects.get(username=user) form = Change(request.POST) if form.is_valid(): user.delete() user=CustomUser.objects.create_user(username=request.POST.get('username'),email='',password=request.POST.get('password')) if request.POST.get('core_num'): user.core_num=int(request.POST.get('core_num')) if request.POST.get('mem_limit'): user.mem_limit=int(request.POST.get('mem_limit')) vms=VM.objects.all() if request.POST.get('is_superuser',''): for vm in vms: user.vms.add(vm) user.is_superuser=True else : user.vms.clear() if vm_list: for vmr in vm_list: for vm in vms: if vm.vmr==vmr: user.vms.add(vm) user.is_superuser=False if request.POST.get('email',''): user.email=request.POST.get('email','') user.save() users=CustomUser.objects.all() message="User changed" return render_to_response('users.html',locals()) else: form=Change({'username':user.username,'email':user.email, 'core_num':user.core_num,'mem_limit':user.mem_limit}) with open(path['vms'],'r') as f: vms=json.load(f) for vm in vms: try: vm2=VM.objects.get(vmr=vm['vmr']) except Exception: vm2=None if not vm2: new_vm=VM.objects.create(name=vm['name'],vmr=vm['vmr']) new_vm.save() action='/user_change/' vms=VM.objects.all() errors = form.errors form.user=user.username return render_to_response('change.html',locals()) if 'username' in request.GET: user=CustomUser.objects.get(username=request.GET['username']) #del user if 'del' in request.GET: user.delete() message="User deleted" users=CustomUser.objects.all() return render_to_response('users.html',locals()) form=Change({'username':user.username,'email':user.email, 'core_num':user.core_num,'mem_limit':user.mem_limit}) form.username=user.username form.core_num=user.core_num form.mem_limit=user.mem_limit form.user=user.username action='/user_change/' with open(path['vms'],'r') as f: vms=json.load(f) for vm in vms: try: vm2=VM.objects.get(vmr=vm['vmr']) except Exception: vm2=None if not vm2: new_vm=VM.objects.create(name=vm['name'],vmr=vm['vmr']) new_vm.save() action='/user_change/' vms=VM.objects.all() return render_to_response('change.html',locals()) else: return HttpResponseRedirect('/user_add/') else: return HttpResponseRedirect('/')
def user_add(request): if request.user.is_authenticated() and request.user.is_superuser: form = Change() if request.method == 'POST': vm_list=request.POST.getlist('vmSelect') form = Change(request.POST) data = request.POST.copy() if form.is_valid(): username=request.POST.get('username','') password=request.POST.get('password','') try: user=CustomUser.objects.get(username=username) except Exception: user=None if not user: new_user = CustomUser.objects.create_user(username=username,email='',password=password) if request.POST.get('core_num'): new_user.core_num=int(request.POST.get('core_num')) if request.POST.get('mem_limit'): new_user.mem_limit=int(request.POST.get('mem_limit')) new_user.save() vms=VM.objects.all() if request.POST.get('is_superuser',''): for vm in vms: new_user.vms.add(vm) new_user.is_superuser=True else: new_user.is_superuser=False if vm_list: for vmr in vm_list: for vm in vms: if vm.vmr==vmr: new_user.vms.add(vm) if request.POST.get('email',''): new_user.email=request.POST.get('email','') new_user.save() else: error="User already exist" action='/user_add/' vms=VM.objects.all() return render_to_response('change.html',locals()) message="User created" users=CustomUser.objects.all() return render_to_response('users.html',locals()) else: with open(path['vms'],'r') as f: vms=json.load(f) for vm in vms: try: vm2=VM.objects.get(vmr=vm['vmr']) except Exception: vm2=None if not vm2: new_vm=VM.objects.create(name=vm['name'],vmr=vm['vmr']) new_vm.save() action='/user_add/' vms=VM.objects.all() error = "form invalid" return render_to_response('change.html',locals()) else: with open(path['vms'],'r') as f: vms=json.load(f) for vm in vms: try: vm2=VM.objects.get(vmr=vm['vmr']) except Exception: vm2=None if not vm2: new_vm=VM.objects.create(name=vm['name'],vmr=vm['vmr']) new_vm.save() action='/user_add/' vms=VM.objects.all() #new user return render_to_response('change.html',locals()) else: return HttpResponseRedirect('/')