92 lines
2.7 KiB
Python
92 lines
2.7 KiB
Python
from flask import Flask, redirect, render_template, request, session
|
|
from lib.post import *
|
|
|
|
app = Flask(__name__)
|
|
app.secret_key = "minggkingkki"
|
|
|
|
# 게시판 기능은
|
|
# ~~글쓰기,~~
|
|
# ~~글조회,~~
|
|
# ~~글 수정,~~
|
|
# ~~글 삭제,~~
|
|
# ~~회원가입,~~
|
|
# ~~로그인,~~
|
|
# ~~글 검색~~)
|
|
|
|
@app.route("/")
|
|
def index():
|
|
query = request.args.get("q")
|
|
if query:
|
|
return render_template("index.html", posts=get_posts_search(query))
|
|
|
|
return render_template("index.html", posts=get_posts())
|
|
|
|
@app.route("/login", methods=["GET", "POST"])
|
|
def login():
|
|
if request.method == "POST":
|
|
username = request.form.get("username")
|
|
password = request.form.get("password")
|
|
try:
|
|
if check_user(username, password):
|
|
session["username"] = username
|
|
return redirect("/")
|
|
else:
|
|
return "Invalid username or password"
|
|
except Exception as e:
|
|
return str(e)
|
|
return render_template("login.html")
|
|
|
|
@app.route("/register", methods=["GET", "POST"])
|
|
def register():
|
|
if request.method == "POST":
|
|
username = request.form["username"]
|
|
password = request.form["password"]
|
|
try:
|
|
create_user(username, password)
|
|
return redirect("/")
|
|
except Exception as e:
|
|
return str(e)
|
|
return render_template("register.html")
|
|
|
|
@app.route("/post", methods=["POST"])
|
|
def create_post_endpoint():
|
|
# spec : {"title": "", "content": ""}
|
|
title = request.form["title"]
|
|
content = request.form["content"]
|
|
|
|
if not title or not content:
|
|
return "title and content are required"
|
|
|
|
username = session["username"]
|
|
if not username:
|
|
return "You must be logged in to create a post"
|
|
|
|
post_id = create_post(title, username, content)
|
|
|
|
return redirect(f"/post/{post_id}")
|
|
|
|
@app.route("/edit/<post_id>", methods=["GET", "POST"])
|
|
def edit_post(post_id):
|
|
if request.method == "POST":
|
|
title = request.form["title"]
|
|
content = request.form["content"]
|
|
if not title or not content:
|
|
return "title and content are required"
|
|
author = session["username"]
|
|
if not author:
|
|
return "You must be logged in to update a post"
|
|
update_post(post_id, title, author, content)
|
|
return redirect(f"/post/{post_id}")
|
|
return render_template("edit.html", post_id=post_id, post=get_post(post_id))
|
|
|
|
@app.route("/post/<post_id>", methods=["GET", "DELETE"])
|
|
def post(post_id):
|
|
if request.method == "DELETE":
|
|
delete_post(post_id)
|
|
return redirect("/")
|
|
|
|
return render_template("post.html", post_id=post_id, post=get_post(post_id))
|
|
|
|
if __name__ == "__main__":
|
|
init()
|
|
app.run(port=5555, debug=True)
|