Exemplo n.º 1
0
def register():
    if request.method == 'POST':
        username = request.form.get('username', '').strip()
        password1 = request.form.get('password1', '').strip()
        password2 = request.form.get('password2', '').strip()
        gender = request.form.get('gender', '').strip()
        city = request.form.get('city', '').strip()
        phone = request.form.get('phone', '').strip()
        now = datetime.datetime.now()
        try:
            User.query.filter_by(username=username).one()
            return render_template('register.html', err='用户名已存在')
        except Exception:
            if not password1 or password1 != password2:
                return render_template('register.html', err='密码不一致')

            user = User(username=username,
                        password=make_password(password1),
                        gender=gender,
                        city=city,
                        phone=phone,
                        create_time=now)

            # 保存头像
            avatar_file = request.files.get('avatar', '')
            if avatar_file:
                user.avatar = save_avatar(avatar_file)

            db.session.add(user)
            db.session.commit()
            return redirect('/user/login')

    else:
        return render_template('register.html')
Exemplo n.º 2
0
def register():
    if request.method == 'POST':
        username = request.form.get('username', '').strip()
        password1 = request.form.get('password1', '').strip()
        password2 = request.form.get('password2', '').strip()
        gender = request.form.get('gender', '').strip()
        birthday = request.form.get('birthday', '').strip()
        city = request.form.get('city', '').strip()
        bio = request.form.get('bio', '').strip()
        now = datetime.datetime.now()

        if not password1 or password1 != password2:
            return render_template('register.html', err='密码不符合要求')

        user = USER(username=username, password=make_password(password1), gender=gender, birthday=birthday, city=city,
                    bio=bio, created=now)

        avatar_file = request.files.get('avatar')
        if avatar_file:
            user.avatar = save_avatar(avatar_file)
        try:
            db.session.add(user)
            db.session.commit()
            return redirect('/user/login')
        except IntegrityError:
            db.session.rollback()
            return render_template('register.html', err='昵称被占用')
    else:
        return render_template('register.html')
Exemplo n.º 3
0
def register():
    '''注册'''
    if request.method == 'POST':
        nickname = request.form.get('nickname', '').strip()
        password = request.form.get('password', '').strip()
        gender = request.form.get('gender', 'unknow').strip()
        city = request.form.get('city', '上海').strip()
        avatar = request.files.get('avatar')
        birthday = request.form.get('birthday', '2000-01-01').strip()
        bio = request.form.get('bio', '').strip()

        if not (nickname and password):
            return render_template('register.html', error='昵称或密码不能为空')

        safe_password = utils.make_password(password)  # 安全处理密码
        avatar_url = utils.save_avatar(nickname, avatar)  # 保存头像,并返回头像网址

        user = User(nickname=nickname, password=safe_password, gender=gender,
                    city=city, avatar=avatar_url, birthday=birthday, bio=bio)

        db.session.add(user)
        try:
            db.session.commit()
        except IntegrityError:
            db.session.rollback()  # 操作失败,进行事务回滚
            return render_template('register.html', error='昵称或密码不能为空')
        return redirect('/user/login')
    else:
        return render_template('register.html')
Exemplo n.º 4
0
def modify():
    '''修改用户信息'''
    if request.method == 'POST':
        username = request.form.get('username')
        password = request.form.get('password')
        gender = request.form.get('gender')
        phone = request.form.get('phone')
        city = request.form.get('city')
        hobbit = request.form.get('hobbit')
        des = request.form.get('des')
        if not password:
            return render_template('response.html', msg='密码不能为空')

        photo = request.files.get('photo')
        if photo:
            photo = save_avatar(photo)
        else:
            photo = User.query.filter_by(username=username).one().photo
        old_user = User.query.filter_by(username=username)
        old_user.update({
            'username': username,
            'password': make_password(password),
            'gender': gender,
            'phone': phone,
            'photo': photo,
            'city': city,
            'hobbit': hobbit,
            'des': des
        })
        db.session.commit()
        return render_template('response.html', msg='修改成功')
    else:
        username = session['username']
        user = User.query.filter_by(username=username).one()
        return render_template('modify.html', user=user)
Exemplo n.º 5
0
def register():
    if request.method == "GET":
        return render_template('register.html')
    else:
        username = request.form.get('username')
        password = request.form.get('password')
        password2 = request.form.get('password2')
        tel = request.form.get('tel')
        birthday = request.form.get('birthday')
        gender = request.form.get('gender')
        now = datetime.datetime.now()
        avatar = request.files.get('avatar')

        try:
            Users.query.filter_by(username=username).one()
            return render_template('register.html', error=2)
        except NoResultFound:
            if not password or password != password2:
                return render_template('register.html', error=1)
            else:
                u1 = Users(username=username,
                           password=make_password(password),
                           tel=tel,
                           birthday=birthday,
                           gender=gender,
                           created=now)

                if avatar:
                    u1.avatar = save_avatar(avatar)

                db.session.add(u1)
                db.session.commit()

            return redirect('/user/login')
Exemplo n.º 6
0
def register():
    if request.method == 'POST':
        #get里面没有的空单引号'' 的意义是 如果你没有写里面内容的话,默认是''
        # strip() 的作用是把字符串两边的空白符去掉,但是不会去掉字符串内部的空白符

        nickname = request.form.get('nickname', '').strip()
        password1 = request.form.get('password1', '').strip()
        password2 = request.form.get('password1', '').strip()
        gender = request.form.get('gender', '').strip()
        birthday = request.form.get('birthday', '').strip()
        city = request.form.get('city', '').strip()
        bio = request.form.get('avatar', '').strip()
        now = datetime.datetime.now()  #注册时间

        if not password1 or password1 != password2:
            return render_template('register.html', err='密码不符合要求')

        user = User(
            nickname=nickname,
            password=make_password(password1),
            gender=gender,
            birthday=birthday,
            city=city,
            bio=bio,
            created=now
        )

        #保存头像
        avatar_file = request.files.get('avatar')
        if avatar_file:
            # print(dir(avatar_file.stream))
            # avatar_url = save_avatar(avatar)
            # user.avatar = avatar_url
            user.avatar = save_avatar(avatar_file)

        try:
            #保存到数据库
            db.session.add(user)
            db.session.commit()
            return redirect('/user/login')
        except IntegrityError:
            db.session.rollback()
            # print(e)
            return render_template('register.html', err='您的昵称已被占用')
    else:
        return render_template('register.html')