Esempio n. 1
0
def register(request):
    if request.method == "POST":
        uf = UserForm(request.POST)
        if uf.is_valid():
            #获取表单信息
            username = uf.cleaned_data['username']
            password = uf.cleaned_data['password']
            email = uf.cleaned_data['email']
            #将表单写入数据库
            user = User()
            user.username = username
            user.password = password
            user.email = email
            user.save()
            #返回注册成功页面
            return render(request, 'success.html', {'username': username})
    else:
        uf = UserForm()
        return render(request, 'register.html', {'uf': uf})
    #return render(request, 'register.html')
Esempio n. 2
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))

    form = RegistrationForm()
    if form.validate_on_submit():
        #Hashing the password using bcrypt
        hashed_password = bcrypt.generate_password_hash(
            form.password.data).decode('utf-8')

        #Creating the user using the from and insert into the database
        user = User(username=form.username.data,
                    email=form.email.data,
                    password=hashed_password)
        db.session.add(user)
        db.session.commit()

        flash(f'Account created! Now log in', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Esempio n. 3
0
def add_user(request):
    form = UserAddForm(request.POST or None)
    if form.is_valid():
        user = User()
        account = form.save(commit=False)
        password = form.cleaned_data.get('password')
        password_ = form.cleaned_data.get('password_')
        username = form.cleaned_data.get('username')
        if account.employee:
            employee = Account.objects.filter(employee=account.employee)
            if employee.count() >= 1:
                messages.error(request, "Employee already exist")
                return redirect('/users/add/')

        if username:
            user_name = User.objects.filter(username=username)
            if user_name:
                messages.error(request, "Username already exist")
                return redirect('/users/add/')
        if password != password_:
            messages.error(request, "Passwords does not match")
            return redirect('/users/add/')
        if len(password) <= 5:
            messages.error(request,
                           "Passwords must contain at least 6 characters")
            return redirect('/users/add/')
        if (account.employee.level == 'Cleaner') | (account.employee.level
                                                    == 'Other'):
            messages.error(
                request, "This emplyee can't use the system\nCleaner or other")
            return redirect('/users/add')
        user.first_name = form.cleaned_data.get('first_name')
        user.last_name = form.cleaned_data.get('last_name')
        user.username = username
        user.set_password(password)
        user.save()
        account.user = user
        account.save()
        return redirect('/users/')
    context = {'form': form}
    return render(request, 'add_user.html', context)
Esempio n. 4
0
def index(request):
    #
    photos = Photo.objects.all().order_by('position')
    files = Upload.objects.all().order_by('filename')
    # file_types = Upload.objects.all().order_by('file_type')
    # if request.method == 'GET':
    inputMail = request.GET.get('email', '')
    print(inputMail)
    ##验证邮箱的合法性
    isMatch = bool(
        re.match(
            r"^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$",
            inputMail, re.VERBOSE))
    #print(isMatch)
    if isMatch:
        ##邮箱去重
        is_email_exist = User.objects.filter(email=inputMail).exists()
        if is_email_exist:
            # ret=0
            # return HttpResponse(json.dumps({
            #     "ret": ret,
            # }))
            status = send_email(inputMail)
            if status:
                print("邮件发送成功")

        else:
            ##实例化用户表
            user_profile = User()
            user_profile.email = inputMail
            user_profile.is_activate = False
            user_profile.save()
            ##发送邮件
            #status = send_email(inputMail)
            #mess="订阅成功,我们将给你邮箱发送激活链接,注意查收"
            status = send_email(inputMail)
            if status:
                print("邮件发送成功")
    # else:
    #     mess = "邮箱格式不正确"
    return render(request, 'index.html', locals())