Пример #1
0
def create_user():
    """
    form = MyFlaskForm()
    if form.validate_on_submit():
        return redirect('/list/')
    """
    user = User()
    data = {"form_errors": []}
    success = False
    UserForm = model_form(User)

    if request.method == 'POST':
        form = UserForm(request.form, obj=user)
        if form.validate():
            form.populate_obj(user)
            db.session.add(user)
            db.session.commit()
            success = True
        else:
            print("FORM ERRORS: ", form.errors)
            data["form_errors"] = form.errors
    else:
        form = UserForm(obj=user)

    data["success"] = success
    data["form"] = form

    return render_template('users/create.html', data=data)
Пример #2
0
def login():
    form = LoginUserForm(request.form)
    if request.method == 'POST' and form.validate():
        username = form.data['username']
        login_user(User.get_by_username(username))
        flash(f'Welcome {username}!')
        return redirect('/')
    return render_template('forms/login.html', form=form)
Пример #3
0
def list_users():
    user = User()

    x = User.query.all()
    print("")
    print("")
    print(x)
    print("")
    print("")
    data = {"users": db.session.query(User)}
    return render_template('users/list_users.html', data=data)
Пример #4
0
 def validate(self):
     success = super().validate()
     if not success:
         return False
     user = User.get_by_username(self.data['username'])
     if not user:
         self.errors['errors'] = ['User not found.']
         return False
     elif not user.check_password(password=self.data['password']):
         self.password.errors.append('Password does not match')
         return False
     return True
Пример #5
0
def sign_up():

    new_user = request.get_json()

    response = {'success': True, 'data': None, 'msg': ""}

    raw_pw = new_user.get("password", None)
    if raw_pw is not None:
        hashed_pw = bcrypt.hashpw(raw_pw.encode('UTF-8'), bcrypt.gensalt())
    else:
        return jsonify(response), 400

    new_user_data = {
        "hashed_password": hashed_pw,
        "name": new_user.get("name", "default_user_name"),
        "email": new_user.get("email", "defualt_user_email"),
        "profile": new_user.get("profile", ""),
        "created_at": datetime.now()
    }

    if any(value == None for value in new_user_data.values()):
        response['success'] = False
        response['msg'] = "There are missing values in sing-up form"
        return jsonify(response), 400

    user = User(**new_user_data)

    try:
        isdup = db.session.query(User).filter(User.email == user.email).one()
    except NoResultFound:
        isdup = None

    if isdup:
        return "User Email Already Exists.", 400

    db.session.add(user)
    db.session.commit()

    new_user_data.pop("hashed_password")
    response['data'] = new_user_data
    return jsonify(response), 200
Пример #6
0
 def save(self, cleaned_data):
     return User.activate(
         code=cleaned_data['code'],
         password=cleaned_data['password']
     )
Пример #7
0
 def save(self, cleaned_data):
     return User.create_user(**cleaned_data)
Пример #8
0
def seed_db():
    user = User(email="*****@*****.**", first_name="Mike")
    db.session.add(user)
    db.session.commit()