python-gallery/gallery/db.py

154 lines
4.2 KiB
Python
Raw Normal View History

2023-03-04 13:45:26 +00:00
"""
OnlyLegs - Database models and ions for SQLAlchemy
2023-03-04 13:45:26 +00:00
"""
from uuid import uuid4
import os
2023-04-05 16:57:07 +00:00
import platformdirs
2023-04-07 12:35:30 +00:00
from sqlalchemy import (
create_engine,
Column,
Integer,
String,
DateTime,
ForeignKey,
PickleType,
func,
)
from sqlalchemy.orm import declarative_base, relationship
from flask_login import UserMixin
2023-04-07 12:35:30 +00:00
USER_DIR = platformdirs.user_config_dir("onlylegs")
DB_PATH = os.path.join(USER_DIR, "instance", "gallery.sqlite3")
2023-03-26 20:58:17 +00:00
# In the future, I want to add support for other databases
2023-04-07 12:35:30 +00:00
engine = create_engine(f"sqlite:///{DB_PATH}", echo=False)
base = declarative_base()
2023-04-07 12:35:30 +00:00
class Users(base, UserMixin): # pylint: disable=too-few-public-methods, C0103
2023-03-04 13:45:26 +00:00
"""
User table
Joins with post, groups, session and log
"""
2023-04-07 12:35:30 +00:00
__tablename__ = "users"
# Gallery used information
id = Column(Integer, primary_key=True)
alt_id = Column(String, unique=True, nullable=False, default=str(uuid4()))
profile_picture = Column(String, nullable=True, default=None)
username = Column(String, unique=True, nullable=False)
email = Column(String, unique=True, nullable=False)
password = Column(String, nullable=False)
2023-04-07 12:35:30 +00:00
joined_at = Column(
2023-04-08 19:38:16 +00:00
DateTime, nullable=False, server_default=func.now() # pylint: disable=E1102
)
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
posts = relationship("Posts", backref="users")
groups = relationship("Groups", backref="users")
log = relationship("Logs", backref="users")
def get_id(self):
return str(self.alt_id)
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
class Posts(base): # pylint: disable=too-few-public-methods, C0103
2023-03-04 13:45:26 +00:00
"""
Post table
Joins with group_junction
"""
2023-04-07 12:35:30 +00:00
__tablename__ = "posts"
2023-03-04 13:45:26 +00:00
id = Column(Integer, primary_key=True)
2023-04-07 12:35:30 +00:00
author_id = Column(Integer, ForeignKey("users.id"))
created_at = Column(
2023-04-08 19:38:16 +00:00
DateTime, nullable=False, server_default=func.now() # pylint: disable=E1102
)
filename = Column(String, unique=True, nullable=False)
mimetype = Column(String, nullable=False)
exif = Column(PickleType, nullable=False)
colours = Column(PickleType, nullable=False)
description = Column(String, nullable=False)
alt = Column(String, nullable=False)
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
junction = relationship("GroupJunction", backref="posts")
2023-04-07 12:35:30 +00:00
class Groups(base): # pylint: disable=too-few-public-methods, C0103
2023-03-04 13:45:26 +00:00
"""
Group table
Joins with group_junction
"""
2023-04-07 12:35:30 +00:00
__tablename__ = "groups"
2023-03-04 13:45:26 +00:00
id = Column(Integer, primary_key=True)
name = Column(String, nullable=False)
description = Column(String, nullable=False)
2023-04-07 12:35:30 +00:00
author_id = Column(Integer, ForeignKey("users.id"))
created_at = Column(
2023-04-08 19:38:16 +00:00
DateTime, nullable=False, server_default=func.now() # pylint: disable=E1102
)
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
junction = relationship("GroupJunction", backref="groups")
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
class GroupJunction(base): # pylint: disable=too-few-public-methods, C0103
2023-03-04 13:45:26 +00:00
"""
Junction table for posts and groups
Joins with posts and groups
"""
2023-04-07 12:35:30 +00:00
__tablename__ = "group_junction"
2023-03-04 13:45:26 +00:00
id = Column(Integer, primary_key=True)
2023-04-07 12:35:30 +00:00
date_added = Column(
2023-04-08 19:38:16 +00:00
DateTime, nullable=False, server_default=func.now() # pylint: disable=E1102
)
2023-04-07 12:35:30 +00:00
group_id = Column(Integer, ForeignKey("groups.id"))
post_id = Column(Integer, ForeignKey("posts.id"))
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
class Logs(base): # pylint: disable=too-few-public-methods, C0103
2023-03-04 13:45:26 +00:00
"""
Log table
Joins with user
"""
2023-04-07 12:35:30 +00:00
__tablename__ = "logs"
2023-03-04 13:45:26 +00:00
id = Column(Integer, primary_key=True)
2023-04-07 12:35:30 +00:00
user_id = Column(Integer, ForeignKey("users.id"))
2023-03-04 13:45:26 +00:00
ip_address = Column(String, nullable=False)
code = Column(Integer, nullable=False)
note = Column(String, nullable=False)
2023-04-07 12:35:30 +00:00
created_at = Column(
2023-04-08 19:38:16 +00:00
DateTime, nullable=False, server_default=func.now() # pylint: disable=E1102
)
2023-03-04 13:45:26 +00:00
2023-04-07 12:35:30 +00:00
class Bans(base): # pylint: disable=too-few-public-methods, C0103
2023-03-04 13:45:26 +00:00
"""
Bans table
"""
2023-04-07 12:35:30 +00:00
__tablename__ = "bans"
2023-03-04 13:45:26 +00:00
id = Column(Integer, primary_key=True)
2023-03-04 13:45:26 +00:00
ip_address = Column(String, nullable=False)
code = Column(Integer, nullable=False)
note = Column(String, nullable=False)
2023-04-07 12:35:30 +00:00
banned_at = Column(
2023-04-08 19:38:16 +00:00
DateTime, nullable=False, server_default=func.now() # pylint: disable=E1102
)
2023-03-04 13:45:26 +00:00
# check if database file exists, if not create it
if not os.path.isfile(DB_PATH):
2023-04-05 19:50:28 +00:00
base.metadata.create_all(engine)
2023-04-07 12:35:30 +00:00
print("Database created")