main
parent
9a3bb61dd2
commit
ff9322e3d3
Binary file not shown.
38
main.py
38
main.py
|
@ -2,6 +2,7 @@ from flask import Flask, render_template, redirect, request, session, url_for
|
||||||
from flask_sqlalchemy import SQLAlchemy
|
from flask_sqlalchemy import SQLAlchemy
|
||||||
from flask_admin import Admin
|
from flask_admin import Admin
|
||||||
from flask_admin.contrib.sqla import ModelView
|
from flask_admin.contrib.sqla import ModelView
|
||||||
|
from werkzeug.security import generate_password_hash, check_password_hash
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
app.secret_key = 'bebra'
|
app.secret_key = 'bebra'
|
||||||
|
@ -12,11 +13,11 @@ class User(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
username = db.Column(db.String(100), unique=True, nullable=False)
|
username = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
password = db.Column(db.String(100), nullable=False)
|
password = db.Column(db.String(100), nullable=False)
|
||||||
email = db.Column(db.String(100), nullable=False)
|
email = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
|
|
||||||
class Post(db.Model):
|
class Post(db.Model):
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
date_created = db.Column(db.String(100), unique=True, nullable=False)
|
date_created = db.Column(db.String(100), unique=False, nullable=False)
|
||||||
alias = db.Column(db.String(100), unique=True, nullable=False)
|
alias = db.Column(db.String(100), unique=True, nullable=False)
|
||||||
title = db.Column(db.String(100), nullable=False)
|
title = db.Column(db.String(100), nullable=False)
|
||||||
image = db.Column(db.String(100), nullable=False)
|
image = db.Column(db.String(100), nullable=False)
|
||||||
|
@ -30,26 +31,6 @@ def register_admin_views():
|
||||||
admin.add_view(ModelView(User, db.session))
|
admin.add_view(ModelView(User, db.session))
|
||||||
admin.add_view(ModelView(Post, db.session))
|
admin.add_view(ModelView(Post, db.session))
|
||||||
|
|
||||||
users = {
|
|
||||||
'thegamer': 'gamerpro1',
|
|
||||||
'justuser': 'randompassword'
|
|
||||||
}
|
|
||||||
|
|
||||||
posts = [
|
|
||||||
{"alias": "advices", "title": "Advices for game developers", "image": "advice.png"},
|
|
||||||
{"alias": "design-and-music", "title": "Design and music", "image": "design-and-music.png"},
|
|
||||||
{"alias": "expectations", "title": "Game expectations vs. reality", "image": "developers-be-like.png"},
|
|
||||||
{"alias": "update-1-1", "title": "Game 'Picture Puzzle remake' update 1.1", "image": "skeleton.png"},
|
|
||||||
{"alias": "chatgpt", "title": "ChatGPT as code helper", "image": "chatgpt-meme.png"},
|
|
||||||
{"alias": "difficulties", "title": "Difficulties while making game", "image": "staircase-fail.png"},
|
|
||||||
{"alias": "how-it-was-made", "title": "How the 'Picture Puzzle remake' was made", "image": "coding.png"},
|
|
||||||
{"alias": "update-1", "title": "Game 'Picture Puzzle remake' update 1.0", "image": "baobab.png"},
|
|
||||||
{"alias": "history", "title": "Picture Puzzle history", "image": "picture-puzzle.png"}
|
|
||||||
]
|
|
||||||
|
|
||||||
def fetch_latest_posts():
|
|
||||||
return posts
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index():
|
def index():
|
||||||
latest_posts = Post.query.all()
|
latest_posts = Post.query.all()
|
||||||
|
@ -66,9 +47,9 @@ def about():
|
||||||
|
|
||||||
@app.route("/posts/<alias>")
|
@app.route("/posts/<alias>")
|
||||||
def post(alias):
|
def post(alias):
|
||||||
post_info = next((p for p in posts if p['alias'] == alias), None)
|
post_info = Post.query.filter_by(alias=alias).first()
|
||||||
if post_info:
|
if post_info:
|
||||||
return render_template(f"{alias}.html")
|
return render_template(f"{alias}.html", post_info=post_info)
|
||||||
else:
|
else:
|
||||||
return "Post not found", 404
|
return "Post not found", 404
|
||||||
|
|
||||||
|
@ -77,7 +58,8 @@ def login():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
username = request.form["username"]
|
username = request.form["username"]
|
||||||
password = request.form["password"]
|
password = request.form["password"]
|
||||||
if username in users and users[username] == password:
|
user = User.query.filter_by(username=username, password=password).first()
|
||||||
|
if user:
|
||||||
session["username"] = username
|
session["username"] = username
|
||||||
return redirect(url_for("index"))
|
return redirect(url_for("index"))
|
||||||
return render_template("auth/login.html")
|
return render_template("auth/login.html")
|
||||||
|
@ -91,12 +73,14 @@ def logout():
|
||||||
def register():
|
def register():
|
||||||
if request.method == "POST":
|
if request.method == "POST":
|
||||||
username = request.form["username"]
|
username = request.form["username"]
|
||||||
|
email = request.form["email"]
|
||||||
password = request.form["password"]
|
password = request.form["password"]
|
||||||
users[username] = password
|
new_user = User(username=username, email=email, password=password)
|
||||||
|
db.session.add(new_user)
|
||||||
|
db.session.commit()
|
||||||
return redirect(url_for("login"))
|
return redirect(url_for("login"))
|
||||||
return render_template("auth/register.html")
|
return render_template("auth/register.html")
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
create_tables()
|
|
||||||
register_admin_views()
|
register_admin_views()
|
||||||
app.run(debug=True)
|
app.run(debug=True)
|
|
@ -7,6 +7,8 @@
|
||||||
<form method="post">
|
<form method="post">
|
||||||
<label for="username">Username</label></br>
|
<label for="username">Username</label></br>
|
||||||
<input type="text" name="username" id="username" required></br>
|
<input type="text" name="username" id="username" required></br>
|
||||||
|
<label for="email">Email</label></br>
|
||||||
|
<input type="text" name="email" id="email" required></br>
|
||||||
<label for="password">Password</label></br>
|
<label for="password">Password</label></br>
|
||||||
<input type="password" name="password" id="password" required></br>
|
<input type="password" name="password" id="password" required></br>
|
||||||
<label for="password">Confirm password</label></br>
|
<label for="password">Confirm password</label></br>
|
||||||
|
|
Loading…
Reference in New Issue