Esempio n. 1
0
def reset_pwd(request):
    """修改密码"""
    res = ''
    if request.method == 'GET':
        return render(request, 'reset_pwd.html', {'msg': res})
    if request.method == 'POST':
        # 获取页面中值
        username = request.POST.get('username')
        pwd = request.POST.get('password')
        new_pwd = request.POST.get('new')

        if pwd == new_pwd:
            res = '新密码和旧密码不能重复!'
            return render(request, 'reset_pwd.html', {'msg': res})
        else:
            # 查询数据库是否有此用户名
            user_list = User.objects.filter(user_name=username)
            if not user_list:
                # 如果没有此用户
                res = '{}用户未注册'.format(username)
                return render(request, 'reset_pwd.html', {'msg': res})
            else:
                # 如果注册过,判断密码对不对
                ret = User.objects.filter(user_name=username).first()
                # 校验密码
                is_pwd_true = check_password(pwd, new_pwd)
                if is_pwd_true:
                    user = User()
                    user.psw = make_password(new_pwd)
                    user.save()
                    res = '密码修改成功!'
                else:
                    res = '密码错误!'
                return render(request, 'reset_pwd.html', {'msg': res})
Esempio n. 2
0
def register(request):
    if request.method=='POST':
        form=UserForm(request.POST,request.FILES)
        if form.is_valid():
            # print form.cleaned_data
            # filePath=file('upload/'+form.cleaned_data['headImg'].name,'wb')
            # s=form.cleaned_data['headImg'].read()
            # filePath.write(s)
            # filePath.close()
            name=form.cleaned_data['name']
            headImg=form.cleaned_data['headImg']
            print name,headImg
            user=User()
            user.name=name
            user.headImg=headImg
            user.save()
            return HttpResponse('ok')
    else:
        form=UserForm()
    return render_to_response('register.html',{'form':form})

# from django.contrib import auth
# def login(request):
#     username=request.POST['username']
#     password=request.POST['password']
#     user=auth.authenticate(username=username,password=password)
#     if user is not None:
#         auth.login(request,user)
#         return HttpResponseRedirect('/hello/')
#     else:
#         return render_to_response('login.html',{'err':'login error'})
Esempio n. 3
0
def index(request):
    # return HttpResponse('hello world')
    # name = 'tom'
    user1 = User(username='******', age=33)
    # user1.save()
    user2 = User(username='******', age=44)
    # user2.save()
    users = User.objects.all()
    return render(request, 'index.html', {'users': users})
Esempio n. 4
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('index'))
    form = RegistrationForm()
    if form.validate_on_submit():
        user = User(username=form.username.data, email=form.email.data)
        user.set_password(form.password.data)
        db.session.add(user)
        db.session.commit()
        flash('Congratulations you are new one!')
        return redirect(url_for('login'))
    return render_template('register.html', title='REGISTER', form=form)
Esempio n. 5
0
def index(request):
    feng = User(username='******', age=18)
    feng.save()
    zhou = User(username='******', age=22)
    zhou.save()
    users = User.objects.all()
    return render(request, 'index.html', {'users': users})
Esempio n. 6
0
def add_user():
    payload = request.json
    if 'username' not in payload or 'dateOfBirth' not in payload:
        abort(HTTPStatus.BAD_REQUEST, "Malformed request")

    username = payload["username"]
    if not utils.validate_username(username):
        abort(
            HTTPStatus.BAD_REQUEST,
            "The username must be a string between 1 and 32 characters (no digits or any other special chars)",
        )
    date_of_birth = payload["dateOfBirth"]

    if not utils.validate_date_of_birth(date_of_birth):
        abort(
            HTTPStatus.BAD_REQUEST,
            "The valid format for the date of birth is YYYY-MM-DD and must be a date before the today date.",
        )

    db_user = get_db_user(username)

    if db_user is None:
        user_schema = UserSchema()
        new_user = User(username, date_of_birth)

        db_session.add(new_user)
        db_session.commit()

        data = user_schema.dump(new_user).data
        return make_response(jsonify(data), HTTPStatus.CREATED)
    else:
        abort(
            HTTPStatus.CONFLICT,
            "Username '{0}' already exists".format(username),
        )
Esempio n. 7
0
def users(request):
    #模拟保存两条数据
    mayun = User(username='******', age = 18)
    mayun.save()
    mahuateng = User(username='******', age=18)
    mahuateng.save()
    #查询返回给前端
    users = User.objects.all()
    return render(request,'user_logo_in.html',{'users':users})
Esempio n. 8
0
def register(request):
    """注册页面"""
    res = ''
    if request.method == 'POST':
        username = request.POST.get('username')
        psw = request.POST.get('password')
        mail = request.POST.get('mail')

        # 查询数据库中是否由此用户名
        user_list = User.objects.filter(user_name=username)
        if user_list:
            # 如果已经注册给提示
            res = '{}用户已经被注册!'.format(username)
            return render(request, 'register.html', {'rename': res})
        else:
            # 如果没有注册过,进行注册
            # 第一种写法 (建议)
            user_new = User()
            user_new.user_name = username
            # 加密方法
            user_new.psw = make_password(psw)
            user_new.mail = mail
            user_new.save()

            # 第二种写法
            # user_new = User(user_name=username,
            #                 psw=psw,
            #                 mail=mail)
            # user_new.save()

            return render(request, 'login.html', {'rename': res})

    return render(request, 'register.html')
Esempio n. 9
0
def register(request):
    '''注册页面'''
    res = ""
    if request.method == "POST":
        username = request.POST.get('username')
        psw = request.POST.get('password')
        mail = request.POST.get('mail')

        # 先查询数据库是否有此用户名
        user_lst = User.objects.filter(user_name=username)
        if user_lst:
            # 如果已经注册过,就给个提示
            res = "%s用户已被注册" % username
            return render(request, 'register.html', {'rename': res})

        else:
            # 如果没被注册,插入数据库

            # 第一种写法 -- 推荐
            user = User()
            user.user_name = username
            user.psw = psw
            user.mail = mail
            user.save()

            # 第二种写法
            # user = User(user_name=username,
            #            psw = psw,
            #            mail = mail,
            #            )
            # user.save()
            return render(request, 'login.html', {'rename': res})
    return render(request, 'register.html')
Esempio n. 10
0
def adduser(request):
    if request.method == 'POST':
        form = UserForm(request.POST)

        if form.is_valid():
            users_list = []
            s = User(username=request.POST.get('username'),
                     password=request.POST.get('password'),
                     sex=request.POST.get('sex'),
                     )
            s.save()
            for i in User.objects.values():
                users_list.append(i)
            return render(request, 'showuser.html', {'users_list':users_list, 'successmessage':'已成功添加用户'})
        else:
            return render(request, 'adduser.html', {'form':form})
    else:
        form = UserForm()
        return render(request, 'adduser.html', {'form':form})
Esempio n. 11
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for('home'))
    form = RegistrationForm()
    if form.validate_on_submit():
        hashed_password = bcrypt.generate_password_hash(form.password.data).decode('utf-8')
        user = User(username=form.username.data, email=form.email.data, password=hashed_password)
        db.session.add(user)
        db.session.commit()
        flash('Your account has been created! You are now able to log in', 'success')
        return redirect(url_for('login'))
    return render_template('register.html', title='Register', form=form)
Esempio n. 12
0
def Add_user(request):
    global count
    count += 1
    user = User()
    user.user_age = count
    user.user_name = random.choice(['Wang', 'Chan', 'Liu', 'Lin'])
    user.user_gender = not random.getrandbits(1)
    user.save()

    return render(request, ('add_user.html'))
Esempio n. 13
0
def reset_psw(request):
    '''修改密码'''
    res = ""
    if request.method == "GET":
        return render(request, 'reset_psw.html', {'msg': res})

    if request.method == "POST":
        username = request.POST.get('username')
        psw = request.POST.get('password')
        new_psw = request.POST.get('new')

        if psw == new_psw:
            res = "新密码和旧密码不能重复"
            return render(request, 'reset_psw.html', {'msg': res})
        else:
            # 先查询数据库是否有此用户名
            user_lst = User.objects.filter(user_name=username)
            if not user_lst:
                # 如果没这个用户
                res = "用户未注册:%s" % username
                return render(request, 'reset_psw.html', {'msg': res})

            else:
                # 如果注册过,判断密码对不对
                ret = User.objects.filter(user_name=username).first()
                # 校验密码
                is_psw_true = check_password(psw, ret.psw)
                print(psw)
                print(ret.psw)
                print(is_psw_true)
                if is_psw_true:
                    user = User()
                    user.psw = make_password(new_psw)
                    user.save()
                    res = "密码修改成功!"
                else:
                    res = "密码错误!"
                return render(request, 'reset_psw.html', {'msg': res})
Esempio n. 14
0
def useradd(request):
    msg = {}
    if request.method == "POST":
        try:
            print(request.POST)
            name = request.POST.get('name', "")
            password = request.POST.get('password', "")
            sex = request.POST.get('sex', "")
            print(name)
            u = User()
            u.name = name
            u.password = password
            u.sex = int(sex)
            u.save()
            msg = {"code": 0, "result": "添加用户成功"}
        except:
            msg = {"code": 1, "errmsg": "添加用户失败: %s" % traceback.format_exc()}
    return render(request, 'hello/useradd.html', {"msg": msg})
Esempio n. 15
0
def register(request):
    message = ''

    if request.method == 'POST':
        form = InputForm(request.POST)
        if form.is_valid():
            user_name = request.POST.get('user_name')
            passwd = request.POST.get('passwd')

            try:
                user = User()
                user.user_name = user_name
                user.passwd = passwd
                user.save()
                message = user_name + "ようこそ!"

            except:
                message = 'すでに同じユーザ名が存在します'
        else:
            message = 'エラー'
    else:
        form = InputForm()

    return render(request, "register.html", {'form': form, 'message': message})
Esempio n. 16
0
 def test_password_verification(self):
     u = User(password='******')
     self.assertTrue(u.verify_password('cat'))
     self.assertFalse(u.verify_password('dog'))
Esempio n. 17
0
def add_user(request):
    """新增数据"""
    test2 = User(user_name='zhm', mail='*****@*****.**', psw='111111')
    test2.save()
    return HttpResponse('数据新增成功!')
Esempio n. 18
0
 def test_password_salts_are_random(self):
     u = User(password='******')
     u2 = User(password='******')
     self.assertTrue(u.password_hash != u2.password_hash)
Esempio n. 19
0
 def test_roles_and_permissions(self):
     Role.insert_roles()
     u = User(email='*****@*****.**', password='******')
     self.assertTrue(u.can(Permission.WRITE))
     self.assertFalse(u.can(Permission.COMMENT))
Esempio n. 20
0
 def test_password_setter(self):
     u = User(password='******')
     self.assertTrue(u.password_hash is not None)
Esempio n. 21
0
 def test_no_password_getter(self):
     u = User(password='******')
     with self.assertRaises(AttributeError):
         u.password