2023-03-04 13:45:26 +00:00
|
|
|
"""
|
2023-03-11 22:14:03 +00:00
|
|
|
OnlyLegs - Database models and functions for SQLAlchemy
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
2023-03-03 00:26:46 +00:00
|
|
|
import os
|
2023-03-04 13:45:26 +00:00
|
|
|
import platformdirs
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
from sqlalchemy import create_engine, Column, Integer, String, Boolean, DateTime, ForeignKey, PickleType
|
2023-03-12 12:29:29 +00:00
|
|
|
from sqlalchemy.orm import declarative_base, relationship
|
|
|
|
|
|
|
|
|
|
|
|
USER_DIR = platformdirs.user_config_dir('onlylegs')
|
|
|
|
DB_PATH = os.path.join(USER_DIR, 'gallery.sqlite')
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
# engine = create_engine('postgresql://username:password@host:port/database_name', echo=False)
|
|
|
|
# engine = create_engine('mysql://username:password@host:port/database_name', echo=False)
|
2023-03-12 12:29:29 +00:00
|
|
|
engine = create_engine(f'sqlite:///{DB_PATH}', echo=False)
|
2023-03-03 00:26:46 +00:00
|
|
|
base = declarative_base()
|
2023-03-01 23:29:34 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
class Users (base): # 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-03-03 00:26:46 +00:00
|
|
|
__tablename__ = 'users'
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
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)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
posts = relationship('Posts', backref='users')
|
|
|
|
groups = relationship('Groups', backref='users')
|
|
|
|
session = relationship('Sessions', backref='users')
|
|
|
|
log = relationship('Logs', backref='users')
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-11 22:14:03 +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-03-03 00:26:46 +00:00
|
|
|
__tablename__ = 'posts'
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
author_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
created_at = Column(DateTime, nullable=False)
|
2023-03-05 16:22:11 +00:00
|
|
|
|
|
|
|
file_name = Column(String, unique=True, nullable=False)
|
|
|
|
file_type = Column(String, nullable=False)
|
|
|
|
|
|
|
|
image_exif = Column(PickleType, nullable=False)
|
|
|
|
image_colours = Column(PickleType, nullable=False)
|
|
|
|
|
|
|
|
post_description = Column(String, nullable=False)
|
|
|
|
post_alt = Column(String, nullable=False)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
junction = relationship('GroupJunction', backref='posts')
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-11 22:14:03 +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-03-03 00:26:46 +00:00
|
|
|
__tablename__ = 'groups'
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
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)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-05 16:22:11 +00:00
|
|
|
junction = relationship('GroupJunction', backref='groups')
|
2023-03-04 13:45:26 +00:00
|
|
|
|
|
|
|
|
2023-03-11 22:14:03 +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-03-03 00:26:46 +00:00
|
|
|
__tablename__ = 'group_junction'
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
2023-03-09 23:31:58 +00:00
|
|
|
date_added = Column(DateTime, nullable=False)
|
2023-03-03 00:26:46 +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-03-11 22:14:03 +00:00
|
|
|
class Sessions (base): # pylint: disable=too-few-public-methods, C0103
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Session table
|
|
|
|
Joins with user
|
|
|
|
"""
|
2023-03-03 00:26:46 +00:00
|
|
|
__tablename__ = 'sessions'
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
|
|
|
session_uuid = Column(String, nullable=False)
|
2023-03-04 13:45:26 +00:00
|
|
|
ip_address = Column(String, nullable=False)
|
2023-03-03 00:26:46 +00:00
|
|
|
user_agent = Column(String, nullable=False)
|
|
|
|
active = Column(Boolean, nullable=False)
|
|
|
|
created_at = Column(DateTime, nullable=False)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
|
|
|
|
2023-03-11 22:14:03 +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-03-03 00:26:46 +00:00
|
|
|
__tablename__ = 'logs'
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
user_id = Column(Integer, ForeignKey('users.id'))
|
2023-03-04 13:45:26 +00:00
|
|
|
ip_address = Column(String, nullable=False)
|
2023-03-03 00:26:46 +00:00
|
|
|
code = Column(Integer, nullable=False)
|
|
|
|
msg = Column(String, nullable=False)
|
|
|
|
created_at = Column(DateTime, nullable=False)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
|
|
|
|
2023-03-11 22:14:03 +00:00
|
|
|
class Bans (base): # pylint: disable=too-few-public-methods, C0103
|
2023-03-04 13:45:26 +00:00
|
|
|
"""
|
|
|
|
Bans table
|
|
|
|
"""
|
2023-03-03 00:26:46 +00:00
|
|
|
__tablename__ = 'bans'
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-03-03 00:26:46 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
2023-03-04 13:45:26 +00:00
|
|
|
ip_address = Column(String, nullable=False)
|
2023-03-03 00:26:46 +00:00
|
|
|
code = Column(Integer, nullable=False)
|
|
|
|
msg = Column(String, nullable=False)
|
|
|
|
created_at = Column(DateTime, nullable=False)
|
2023-03-04 13:45:26 +00:00
|
|
|
|
2023-01-10 12:39:29 +00:00
|
|
|
|
2023-03-12 12:29:29 +00:00
|
|
|
# check if database file exists, if not create it
|
|
|
|
if not os.path.isfile(DB_PATH):
|
2023-03-12 15:52:23 +00:00
|
|
|
base.metadata.create_all(engine)
|
|
|
|
print('Database created')
|