Exemplo n.º 1
0
def register():
    if current_user.is_authenticated:
        return redirect(url_for("home"))

    if request.method == "GET":
        return render_template("auth/register.html", user=current_user)

    username = request.form.get("username")
    password = request.form.get("password")
    if not (username and len(username) >= 3 and password
            and len(password) >= 4):
        return render_template(
            "auth/register.html",
            error_text="Username has to be at least 3 symbols and pass min 5",
            user=current_user,
        )
    if Session.query(User).filter_by(username=username).count():
        return render_template(
            "auth/register.html",
            error_text=f"Username {username!r} already exists!",
            user=current_user,
        )

    user = User(username, password)
    Session.add(user)

    try:
        Session.commit()
    except Exception as e:
        logger.exception("Error creating user!")
        raise InternalServerError(f"Could not create new user! Error: {e}")

    login_user(user)
    return redirect(url_for("home"))
Exemplo n.º 2
0
def add_post(img_url, thumb_url, username):
    session = Session()
    user = session.query(User).filter_by(username=username).first()
    post = Post(image_url=img_url, thumb_url=thumb_url, user_id=user.id)
    session.add(post)
    session.commit()
    post_id = post.id
    session.close()
    return post_id
Exemplo n.º 3
0
def registration():
    form = RegistrationForm()
    if request.method == "POST" and form.validate():
        user = User(username=form.username.data, password=form.password.data)
        Session.add(user)
        Session.commit()

        flash(message=f'Пользователь {user.username} успешно зарегистрирован',
              category='success')
        return redirect(url_for("auth_app.login"))
    return render_template("auth/registration.html", form=form)
Exemplo n.º 4
0
def create_post():
    form = PostForm()
    if request.method == "POST" and form.validate():
        post = Post(
            title=request.form.get('title'),
            text=request.form.get('text'),
            image_url=request.form.get('image_url'),
            user_id=current_user.id
        )
        Session.add(post)
        Session.commit()
        return redirect(url_for("main"))
    return render_template("posts/create.html", form=form)
Exemplo n.º 5
0
def new_post():
    if not current_user.is_authenticated:
        return redirect(url_for("index.html"))
    if request.method == "GET":
        return render_template("new_post.html")
    form = request.form
    post_text = form['post_text']
    post = Post(post_text, current_user.id)
    Session.add(post)
    try:
        Session.commit()
    except Exception as e:
        logger.exception("Error creating post!")
        raise InternalServerError(f"Could not create new post! Error: {e}")
    return redirect(url_for("index"))
Exemplo n.º 6
0
def sign_in():
    if current_user.is_authenticated:
        return redirect(url_for("index"))

    if request.method == "GET":
        return render_template("sign_in.html")

    username, password = get_username_and_password_from_form(request.form)
    validate_username_unique(username)
    user = User(username, password)
    Session.add(user)

    try:
        Session.commit()
    except Exception as e:
        logger.exception("Error creating user!")
        raise InternalServerError(f"Could not create new user! Error: {e}")
    login_user(user)
    return redirect(url_for("index"))
Exemplo n.º 7
0
def register(username, password1, password2):
    ret = {'msg': 'other error', 'user_id': None}
    if username and password1 and password2:
        if password1 == password2:
            session = Session()
            user = session.query(User).filter_by(username=username).all()
            if user:
                msg = 'username is exists'
            else:
                new_user = User(username=username, password=hash(password1))
                session.add(new_user)
                session.commit()
                ret['user_id'] = new_user.id
                session.close()
                msg = 'ok'
        else:
            msg = 'password1 != password2'
    else:
        msg = 'username or password is empty'
    ret['msg'] = msg
    return ret
Exemplo n.º 8
0
def add_post():

    if request.method == "GET":
        return render_template("post/add_post.html", user=current_user)

    title = request.form["title"]
    text = request.form["text"]
    if not title:
        return render_template(
            "post/add_post.html", error_text="Title can't be empty", user=current_user
        )
    if not text:
        return render_template(
            "post/add_post.html", error_text="Text can't be empty", user=current_user
        )
    new_post = Post(title, text, current_user.id)
    Session.add(new_post)
    try:
        Session.commit()
    except Exception as e:
        logger.exception("Error creating post!")
        raise InternalServerError(f"Could not create new post! Error: {e}")

    return redirect(url_for("home"))