예제 #1
0
파일: topic.py 프로젝트: gosiaboch/forum
def topic_edit(topic_id):
    topic = db.query(Topic).get(int(topic_id))

    if request.method == "GET":
        return render_template("topic/topic_edit.html", topic=topic)

    elif request.method == "POST":
        title = request.form.get("title")
        text = request.form.get("text")

        session_token = request.cookies.get("session_token")
        user = db.query(User).filter_by(session_token=session_token).first()

        if not user:
            return redirect(url_for('auth.login'))
        elif topic.author.id != user.id:
            return "Nie jestes autorem posta!!!"
        else:
            topic.title = title
            topic.text = text
            db.add(topic)
            db.commit()

            return redirect(
                url_for('topic.topic_details', topic=topic, topic_id=topic_id))
예제 #2
0
파일: main.py 프로젝트: Zirubihara/wd2forum
def topic_details(topic_id):
    topic = db.query(Topic).get(int(topic_id))

    session_token = request.cookies.get("session_token")
    user = db.query(User).filter_by(session_token=session_token).first()

    return render_template("topic_details.html", topic=topic, user=user)
예제 #3
0
파일: topic.py 프로젝트: gosiaboch/forum
def index():
    session_token = request.cookies.get("session_token")

    user = db.query(User).filter_by(session_token=session_token).first()

    topics = db.query(Topic).all()

    return render_template("topic/index.html", user=user, topics=topics)
예제 #4
0
파일: topic.py 프로젝트: gosiaboch/forum
def topic_details(topic_id):
    topic = db.query(Topic).get(int(topic_id))

    if os.getenv('REDIS_URL'):
        get_random_num

    session_token = request.cookies.get("session_token")
    user = db.query(User).filter_by(session_token=session_token).first()

    comments = db.query(Comment).filter_by(topic=topic).all()

    return render_template("topic/topic_details.html",
                           topic=topic,
                           user=user,
                           csrf_token=create_csrf_token(user.username),
                           comments=comments)
예제 #5
0
파일: topic.py 프로젝트: gosiaboch/forum
def topic_create():
    session_token = request.cookies.get("session_token")
    user = db.query(User).filter_by(session_token=session_token).first()

    if not user:
        return redirect(url_for('auth.login'))

    if request.method == "GET":
        csrf_token = create_csrf_token(user.username)
        return render_template("topic/topic_create.html",
                               user=user,
                               csrf_token=csrf_token)

    elif request.method == "POST":
        csrf = request.form.get("csrf")

        if validate_csrf(csrf, user.username):
            title = request.form.get("title")
            text = request.form.get("text")

            topic = Topic.create(title=title, text=text, author=user)
            print(topic)
            return redirect(url_for('topic.index'))
        else:
            return "CSRF token is not valid!"
예제 #6
0
def login():
    if request.method == "GET":
        return render_template("auth/login.html")
    elif request.method == "POST":
        username = request.form.get("username")
        password = request.form.get("password")

        user = db.query(User).filter_by(username=username).first()

        if not user:
            return "Bledne haslo lub nazwa uzytkownika"
        else:
            password_hash = hashlib.sha256(password.encode()).hexdigest()
            if password_hash == user.password_hash:
                user.session_token = str(uuid.uuid4())
                db.add(user)
                db.commit()

                response = make_response(redirect(url_for('topic.index')))
                response.set_cookie("session_token",
                                    user.session_token,
                                    httponly=True,
                                    samesite='Strict')

                return response

            else:
                return "Bledne haslo lub nazwa uzytkownika"
예제 #7
0
def comment_create(topic_id):
    session_token = request.cookies.get("session_token")
    user = db.query(User).filter_by(session_token=session_token).first()

    if not user:
        return redirect(url_for('auth.login'))

    csrf = request.form.get("csrf")

    if validate_csrf(csrf, user.username):
        text = request.form.get("text")
        topic = db.query(Topic).get(int(topic_id))
        comment = Comment.create(topic=topic, text=text, author=user)

        return redirect(url_for('topic.topic_details', topic_id=topic_id, csrf_token=create_csrf_token(user.username)))

    else:
        return "CSRF token jest bledny!!!"
예제 #8
0
파일: topic.py 프로젝트: gosiaboch/forum
def topic_delete(topic_id):
    topic = db.query(Topic).get(int(topic_id))

    if request.method == "GET":
        return render_template("topic/topic_delete.html", topic=topic)

    elif request.method == "POST":
        session_token = request.cookies.get("session_token")
        user = db.query(User).filter_by(session_token=session_token).first()

        if not user:
            return redirect(url_for('auth.login'))
        elif topic.author.id != user.id:
            return "Nie jestes autorem!!!"
        else:
            db.delete(topic)
            db.commit()
            return redirect(url_for('topic.index'))
예제 #9
0
파일: main.py 프로젝트: Zirubihara/wd2forum
def topic_create():
    if request.method == "GET":
        return render_template("topic_create.html")
    elif request.method == "POST":
        title = request.form.get("title")
        text = request.form.get("text")

        session_token = request.cookies.get("session_token")
        user = db.query(User).filter_by(session_token=session_token).first()

        if not user:
            return redirect(url_for('login'))

        topic = Topic.create(title=title, text=text, author=user)
        print(topic)
        return redirect(url_for('index'))