Ejemplo n.º 1
0
def user_add(request):
    user = request.user
    error = ''
    if user.is_superuser:
        if request.method == 'POST':
            form = forms.UserRequestForm(request.POST)
            if form.is_valid():
                email = form.cleaned_data['email']
                user_get = User.objects.filter(username=email)
                if user_get:
                    error = '用户已存在'
                else:
                    userregist_get = models.UserRequest.objects.filter(
                        email=email)
                    if userregist_get.count() > 2:
                        error = '用户已多次添加'
                    else:
                        area = form.cleaned_data['area']
                        request_type = form.cleaned_data['request_type']
                        urlarg = strtopsd(email)
                        models.UserRequest.objects.get_or_create(
                            email=email,
                            urlarg=urlarg,
                            area=area,
                            request_type=request_type,
                            is_check=True,
                            status='1',
                            action_user=user,
                        )
                        res = mails.sendregistmail(email, urlarg)
                        if res:
                            error = '添加成功,已向该员工发送邮件'
                        else:
                            error = '添加成功,邮件发送失败,请重试'
            else:
                error = '请检查输入'
        else:
            form = forms.UserRequestForm()
    else:
        error = '请检查权限是否正确'
    return render(request, 'formedit.html', {
        'form': form,
        'post_url': 'useradd',
        'error': error
    })
Ejemplo n.º 2
0
def regist(request, argu):
    error = ''
    if argu == 'regist':
        if request.method == 'POST':
            form = forms.UserRequestForm(request.POST)
            if form.is_valid():
                email = form.cleaned_data['email']
                user_get = User.objects.filter(username=email)
                if user_get:
                    error = '用户已存在'
                else:
                    userregist_get = models.UserRequest.objects.filter(
                        email=email)
                    if userregist_get.count() > 2:
                        error = '用户已多次添加'
                    else:
                        area = form.cleaned_data['area']
                        request_type = form.cleaned_data['request_type']
                        urlarg = strtopsd(email)
                        models.UserRequest.objects.get_or_create(
                            email=email,
                            urlarg=urlarg,
                            area=area,
                            request_type=request_type,
                        )
                        #res = mails.sendregistmail(email, urlarg)
                        error = '申请成功,审批通过后会向您发送邮箱'
            else:
                error = '请检查输入'
        else:
            form = forms.UserRequestForm()
        return render(request, 'RBAC/registrequest.html', {
            'form': form,
            'error': error
        })
    else:
        regist_get = get_object_or_404(models.UserRequest,
                                       urlarg=argu,
                                       is_use=False)
        if request.method == 'POST':
            form = forms.Account_Reset_Form(request.POST)
            if form.is_valid():
                email = form.cleaned_data['email']
                firstname = form.cleaned_data['firstname']
                lastname = form.cleaned_data['lastname']
                password = form.cleaned_data['password']
                repassword = form.cleaned_data['repassword']
                username = email.split("@")[0]
                check_res = checkpsd(password)
                if check_res:
                    if regist_get.email == email:
                        if password == repassword:
                            user_create = auth.authenticate(username=username,
                                                            password=password)
                            if user_create:
                                error = '用户已存在'
                            else:
                                user_create = User.objects.create_user(
                                    first_name=firstname,
                                    last_name=lastname,
                                    username=username,
                                    password=password,
                                    email=email,
                                )
                                user_create.profile.roles.add(
                                    regist_get.request_type)
                                user_create.profile.area = regist_get.area
                                user_create.save()
                                regist_get.is_use = True
                                regist_get.save()
                                return HttpResponseRedirect('/view/')
                        else:
                            error = '两次密码不一致'
                    else:
                        error = '密码必须6位以上且包含字母、数字'
                else:
                    error = '恶意注册是不对滴'
            else:
                error = '请检查输入'
        else:
            form = forms.Account_Reset_Form()
        return render(request, 'RBAC/regist.html', {
            'form': form,
            'error': error
        })