def user_add(request): """ Add a user using a subclass of Userena's SignupForm, which takes care of Profile creation, adding necessary user permissions, password generation, and sending an activation email. The only reason this doesn't use userena.views.signup is that userena.views.signup logs out the current user (the admin user) after a user is added. (That makes sense for creating an account for yourself, but not for creating someone else's account.) """ form = UserAddForm() if request.method == 'POST': form = UserAddForm(request.POST, request.FILES) if form.is_valid(): user = form.save() redirect_to = reverse( 'userena_signup_complete', kwargs={'username': user.username} ) return redirect(redirect_to) return render_to_response('accounts/user_add_form.html', { 'form': form, }, context_instance=RequestContext(request) )
def accounts(request): """ :param request: :return: """ if not request.user.is_authenticated(): return HttpResponseRedirect(reverse('index')) if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) error_messages = [] users = User.objects.filter(is_staff=False, is_superuser=False) 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()) 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) user_edit.set_password(user_pass) 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()) return render(request, 'accounts.html', locals())
def user_add(request): if request.method == "POST": userform = UserAddForm(request.POST) if userform.is_valid(): user = userform.save(commit=False) user.set_password(userform.cleaned_data['password']) userform.save() return HttpResponseRedirect(reverse('user_list')) else: userform = UserAddForm() return render(request, 'account/user_add.html', locals())
def post(self, request): ret = {"result": 0, "msg": None} ## ajax 请求的权限验证 if not request.user.has_perm(self.permission_required): ret["result"] = 1 ret["msg"] = "Sorry,你没有权限,请联系运维!" return JsonResponse(ret) user_form = UserAddForm(request.POST) if not user_form.is_valid(): ret["result"] = 1 error_msg = json.loads(user_form.errors.as_json(escape_html=False)) ret["msg"] = '\n'.join( [i["message"] for v in error_msg.values() for i in v]) return JsonResponse(ret) try: user_info = user_form.cleaned_data ue_obj = UserExtend() user = User.objects.create_user(user_info.get('username'), user_info.get("email"), user_info.get("password")) ue_obj.user = user user.userextend.cn_name = user_info.get('cn_name') user.userextend.phone = user_info.get('phone') user.userextend.role = user_info.get('role') user.userextend.save() except Exception as e: ret["result"] = 1 ret["msg"] = e.args else: ret["msg"] = "新用户 %s 注册成功" % (user_info.get('username')) return JsonResponse(ret)
def user_add(request): if request.method == 'POST': JSON_RESULT = {'status': 200, 'message': '', 'error': '', 'data': []} forms = UserAddForm(request, data=request.POST) if forms.is_valid(): user = forms.save(commit=False) user.set_password(forms.cleaned_data['password']) user.save() JSON_RESULT['message'] = '添加成功' return JsonResponse(JSON_RESULT) JSON_RESULT['status'] = 201 JSON_RESULT['error'] = forms.errors.as_json() return JsonResponse(JSON_RESULT) else: forms = UserAddForm(request) kwargs = { 'html_title': '添加用户', 'cancel': reverse('accounts:user-list'), 'col_md': 'col-md-3', 'forms': forms } return render(request, 'accounts/user-editor.html', kwargs)
def accounts(request): """ :param request: :return: """ def create_missing_userattributes(users): for user in users: try: userattributes = user.userattributes except UserAttributes.DoesNotExist: userattributes = UserAttributes(user=user) userattributes.save() if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) error_messages = [] users = User.objects.all().order_by('username') create_missing_userattributes(users) allow_empty_password = settings.ALLOW_EMPTY_PASSWORD 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()) 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) user_edit.set_password(user_pass) user_edit.is_staff = request.POST.get('user_is_staff', False) user_edit.is_superuser = request.POST.get('user_is_superuser', False) user_edit.save() userattributes = user_edit.userattributes userattributes.can_clone_instances = request.POST.get('userattributes_can_clone_instances', False) userattributes.max_instances = request.POST.get('userattributes_max_instances', 0) userattributes.max_cpus = request.POST.get('userattributes_max_cpus', 0) userattributes.max_memory = request.POST.get('userattributes_max_memory', 0) userattributes.max_disk_size = request.POST.get('userattributes_max_disk_size', 0) userattributes.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()) return render(request, 'accounts.html', locals())
def accounts(request): """ :param request: :return: """ if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) error_messages = [] users = User.objects.all().order_by('username') allow_empty_password = settings.ALLOW_EMPTY_PASSWORD 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() UserAttributes.configure_user(new_user) return HttpResponseRedirect(request.get_full_path()) if 'edit' in request.POST: CHECKBOX_MAPPING = {'on': True, 'off': False, } user_id = request.POST.get('user_id', '') user_pass = request.POST.get('user_pass', '') user_edit = User.objects.get(id=user_id) if user_pass != '': user_edit.set_password(user_pass) user_edit.is_staff = CHECKBOX_MAPPING.get(request.POST.get('user_is_staff', 'off')) user_edit.is_superuser = CHECKBOX_MAPPING.get(request.POST.get('user_is_superuser', 'off')) user_edit.save() UserAttributes.create_missing_userattributes(user_edit) user_edit.userattributes.can_clone_instances = CHECKBOX_MAPPING.get(request.POST.get('userattributes_can_clone_instances', 'off')) user_edit.userattributes.max_instances = request.POST.get('userattributes_max_instances', 0) user_edit.userattributes.max_cpus = request.POST.get('userattributes_max_cpus', 0) user_edit.userattributes.max_memory = request.POST.get('userattributes_max_memory', 0) user_edit.userattributes.max_disk_size = request.POST.get('userattributes_max_disk_size', 0) try: user_edit.userattributes.clean_fields() except ValidationError as exc: error_messages.append(exc) else: user_edit.userattributes.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()) accounts_template_file = 'accounts.html' if settings.VIEW_ACCOUNTS_STYLE == "list": accounts_template_file = 'accounts-list.html' return render(request, accounts_template_file, locals())
def accounts(request): """ :param request: :return: """ if not request.user.is_superuser: return HttpResponseRedirect(reverse('index')) error_messages = [] users = User.objects.all().order_by('username') allow_empty_password = settings.ALLOW_EMPTY_PASSWORD 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()) 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) user_edit.set_password(user_pass) user_edit.is_staff = request.POST.get('user_is_staff', False) user_edit.is_superuser = request.POST.get('user_is_superuser', False) user_edit.save() userattributes = user_edit.userattributes userattributes.can_clone_instances = request.POST.get( 'userattributes_can_clone_instances', False) userattributes.max_instances = request.POST.get( 'userattributes_max_instances', 0) userattributes.max_cpus = request.POST.get( 'userattributes_max_cpus', 0) userattributes.max_memory = request.POST.get( 'userattributes_max_memory', 0) userattributes.max_disk_size = request.POST.get( 'userattributes_max_disk_size', 0) userattributes.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()) return render(request, 'accounts.html', locals())