restructured website into folders

This commit is contained in:
2020-04-07 16:29:51 +00:00
parent f86f15069a
commit bcb3b1bbac
13 changed files with 21 additions and 432 deletions

38
database/db.py Normal file
View File

@ -0,0 +1,38 @@
# http://flask.pocoo.org/docs/1.0/tutorial/database/
import sqlite3
import click
from flask import current_app, g
from flask.cli import with_appcontext
def get_db():
if "db" not in g:
g.db = sqlite3.connect(
"database/sqlite_db", detect_types=sqlite3.PARSE_DECLTYPES
)
g.db.row_factory = sqlite3.Row
return g.db
def close_db(e=None):
db = g.pop("db/db", None)
if db is not None:
db.close()
def init_db():
db = get_db()
with current_app.open_resource("database/schema.sql") as f:
db.executescript(f.read().decode("utf8"))
@click.command("init-db")
@with_appcontext
def init_db_command():
"""Clear the existing data and create new tables."""
init_db()
click.echo("Initialized the database.")
def init_app(app):
app.teardown_appcontext(close_db)
app.cli.add_command(init_db_command)

6
database/schema.sql Normal file
View File

@ -0,0 +1,6 @@
CREATE TABLE user (
id TEXT PRIMARY KEY,
name TEXT NOT NULL,
email TEXT UNIQUE NOT NULL,
profile_pic TEXT NOT NULL
);

BIN
database/sqlite_db Normal file

Binary file not shown.

38
database/user.py Normal file
View File

@ -0,0 +1,38 @@
from flask_login import UserMixin
from pathlib import Path
from database.db import get_db
class User(UserMixin):
def __init__(self, id_, name, email, profile_pic):
self.id = id_
self.name = name
self.email = email
self.profile_pic = profile_pic
@staticmethod
def get(user_id):
db = get_db()
user = db.execute(
"SELECT * FROM user WHERE id = ?", (user_id,)
).fetchone()
if not user:
return None
user = User(
id_=user[0], name=user[1], email=user[2], profile_pic=user[3]
)
return user
@staticmethod
def create(id_, name, email, profile_pic):
db = get_db()
db.execute(
"INSERT INTO user (id, name, email, profile_pic) "
"VALUES (?, ?, ?, ?)",
(id_, name, email, profile_pic),
)
db.commit()
Path(f"userinfo/{id_}").mkdir(parents=True, exist_ok=True)