mirror of
https://github.com/Derpy-Leggies/OnlyLegs.git
synced 2025-06-29 03:26:16 +00:00
Switch to SQLAlchemy for database management
Add Favicon
This commit is contained in:
parent
9e87c74c96
commit
2e86bfa072
11 changed files with 341 additions and 192 deletions
157
gallery/db.py
157
gallery/db.py
|
@ -1,38 +1,133 @@
|
|||
import sqlite3
|
||||
import click
|
||||
from flask import current_app, g
|
||||
import os
|
||||
import platformdirs
|
||||
from datetime import datetime
|
||||
|
||||
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime, ForeignKey
|
||||
from sqlalchemy.orm import declarative_base, relationship
|
||||
|
||||
|
||||
@click.command('init-db')
|
||||
def init_db_command():
|
||||
"""Create tables if not already created"""
|
||||
init_db()
|
||||
click.echo('Initialized the database!')
|
||||
path_to_db = os.path.join(platformdirs.user_config_dir('onlylegs'), 'gallery.sqlite')
|
||||
engine = create_engine(f'sqlite:///{path_to_db}', echo=False)
|
||||
base = declarative_base()
|
||||
|
||||
|
||||
def get_db():
|
||||
if 'db' not in g:
|
||||
g.db = sqlite3.connect(current_app.config['DATABASE'],
|
||||
detect_types=sqlite3.PARSE_DECLTYPES)
|
||||
g.db.row_factory = sqlite3.Row
|
||||
class users (base):
|
||||
__tablename__ = 'users'
|
||||
|
||||
return g.db
|
||||
id = Column(Integer, primary_key=True)
|
||||
username = Column(String, unique=True, nullable=False)
|
||||
email = Column(String, unique=True, nullable=False)
|
||||
password = Column(String, nullable=False)
|
||||
created_at = Column(DateTime, nullable=False)
|
||||
|
||||
posts = relationship('posts')
|
||||
groups = relationship('groups')
|
||||
session = relationship('sessions')
|
||||
log = relationship('logs')
|
||||
|
||||
def __init__(self, username, email, password):
|
||||
self.username = username
|
||||
self.email = email
|
||||
self.password = password
|
||||
self.created_at = datetime.now()
|
||||
|
||||
class posts (base):
|
||||
__tablename__ = 'posts'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
file_name = Column(String, unique=True, nullable=False)
|
||||
description = Column(String, nullable=False)
|
||||
alt = Column(String, nullable=False)
|
||||
author_id = Column(Integer, ForeignKey('users.id'))
|
||||
created_at = Column(DateTime, nullable=False)
|
||||
|
||||
junction = relationship('group_junction')
|
||||
|
||||
def __init__(self, file_name, description, alt, author_id):
|
||||
self.file_name = file_name
|
||||
self.description = description
|
||||
self.alt = alt
|
||||
self.author_id = author_id
|
||||
self.created_at = datetime.now()
|
||||
|
||||
class groups (base):
|
||||
__tablename__ = 'groups'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
name = Column(String, nullable=False)
|
||||
description = Column(String, nullable=False)
|
||||
author_id = Column(Integer, ForeignKey('users.id'))
|
||||
created_at = Column(DateTime, nullable=False)
|
||||
|
||||
junction = relationship('group_junction')
|
||||
|
||||
def __init__(self, name, description, author_id):
|
||||
self.name = name
|
||||
self.description = description
|
||||
self.author_id = author_id
|
||||
self.created_at = datetime.now()
|
||||
|
||||
class group_junction (base):
|
||||
__tablename__ = 'group_junction'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
group_id = Column(Integer, ForeignKey('groups.id'))
|
||||
post_id = Column(Integer, ForeignKey('posts.id'))
|
||||
|
||||
def __init__(self, group_id, post_id):
|
||||
self.group_id = group_id
|
||||
self.post_id = post_id
|
||||
|
||||
class sessions (base):
|
||||
__tablename__ = 'sessions'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
user_id = Column(Integer, ForeignKey('users.id'))
|
||||
session_uuid = Column(String, nullable=False)
|
||||
ip = Column(String, nullable=False)
|
||||
user_agent = Column(String, nullable=False)
|
||||
active = Column(Boolean, nullable=False)
|
||||
created_at = Column(DateTime, nullable=False)
|
||||
|
||||
def __init__(self, user_id, session_uuid, ip, user_agent, active):
|
||||
self.user_id = user_id
|
||||
self.session_uuid = session_uuid
|
||||
self.ip = ip
|
||||
self.user_agent = user_agent
|
||||
self.active = active
|
||||
self.created_at = datetime.now()
|
||||
|
||||
class logs (base):
|
||||
__tablename__ = 'logs'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
user_id = Column(Integer, ForeignKey('users.id'))
|
||||
ip = Column(String, nullable=False)
|
||||
code = Column(Integer, nullable=False)
|
||||
msg = Column(String, nullable=False)
|
||||
created_at = Column(DateTime, nullable=False)
|
||||
|
||||
def __init__(self, user_id, ip, code, msg):
|
||||
self.user_id = user_id
|
||||
self.ip = ip
|
||||
self.code = code
|
||||
self.msg = msg
|
||||
self.created_at = datetime.now()
|
||||
|
||||
class bans (base):
|
||||
__tablename__ = 'bans'
|
||||
|
||||
id = Column(Integer, primary_key=True)
|
||||
ip = Column(String, nullable=False)
|
||||
code = Column(Integer, nullable=False)
|
||||
msg = Column(String, nullable=False)
|
||||
created_at = Column(DateTime, nullable=False)
|
||||
|
||||
def __init__(self, ip, code, msg):
|
||||
self.ip = ip
|
||||
self.code = code
|
||||
self.msg = msg
|
||||
self.created_at = datetime.now()
|
||||
|
||||
|
||||
def close_db(e=None):
|
||||
db = g.pop('db', None)
|
||||
|
||||
if db is not None:
|
||||
db.close()
|
||||
|
||||
|
||||
def init_db():
|
||||
db = get_db()
|
||||
|
||||
with current_app.open_resource('schema.sql') as f:
|
||||
db.executescript(f.read().decode('utf8'))
|
||||
|
||||
|
||||
def init_app(app):
|
||||
app.teardown_appcontext(close_db)
|
||||
app.cli.add_command(init_db_command)
|
||||
base.metadata.create_all(engine)
|
Loading…
Add table
Add a link
Reference in a new issue