Update to reccommended file structure

Use reccommended database structure
Switch to SQLite and update scheme along with it
This commit is contained in:
Michał Gdula 2023-01-10 12:39:29 +00:00
parent 29d204f95e
commit a499e6c840
41 changed files with 544 additions and 342 deletions

View file

@ -0,0 +1,91 @@
import datetime
now = datetime.datetime.now()
import sys
import os
class DBmanager():
def __init__(self):
try:
import mysql.connector
from mysql.connector import Error
from dotenv import load_dotenv
except ImportError:
print("Error: could not import required packages")
sys.exit(1)
env_path = os.path.join('usr', '.env')
if not os.path.exists(env_path):
print("Error: could not find .env file")
sys.exit(1)
load_dotenv(env_path)
print(f"{now.hour}:{now.minute}:{now.second} - Connecting to database...")
try:
database = mysql.connector.connect(host=os.environ.get('DB_HOST'),
port=os.environ.get('DB_PORT'),
database=os.environ.get('DB_NAME'),
user=os.environ.get('DB_USER'),
password=os.environ.get('DB_PASS')
)
if database.is_connected():
db_Info = database.get_server_info()
print("Connected to MySQL Server version:", db_Info)
cursor = database.cursor()
cursor.execute("select database();")
record = cursor.fetchone()
print("Connected to database:", record[0])
print(f"{now.hour}:{now.minute}:{now.second} - Done!\n")
except Error as e:
print("Error while connecting to Database!\nFull error:", e)
sys.exit(1)
self.database = database
def initialize(self):
if not os.path.exists(os.path.join('packages', 'tables', 'generate.sql')):
print("Error: could not find tables directory")
sys.exit(1)
else:
print(f"{now.hour}:{now.minute}:{now.second} - Initializing tables...")
with open(os.path.join('packages', 'tables', 'generate.sql'), 'r') as sql:
cursor = self.database.cursor()
query = cursor.execute(sql.read(), multi=True)
i = 0
for res in query:
print(f"Query {i+1}: Affected {res.rowcount} rows")
i += 1
if not os.path.exists(os.path.join('packages', 'tables', 'junctions.sql')):
print("Error: could not find junctions directory")
sys.exit(1)
else:
print(f"{now.hour}:{now.minute}:{now.second} - Initializing junctions...")
with open(os.path.join('packages', 'tables', 'junctions.sql'), 'r') as sql:
cursor = self.database.cursor()
query = cursor.execute(sql.read(), multi=True)
i = 0
for res in query:
print(f"Query {i+1}: Affected {res.rowcount} rows")
i += 1
self.database.commit()
print(f"{now.hour}:{now.minute}:{now.second} - Done!\n")
def getImage(self, id):
sql = "SELECT * FROM posts WHERE id = %s"
img = (id,)
cursor = self.database.cursor()
cursor.execute(sql, img)
return cursor.fetchone()

View file

@ -0,0 +1,64 @@
import datetime
now = datetime.datetime.now()
import sys
import shutil
import os
class Sassy():
def __init__(self, theme):
print("### OnlyLegs Theme Manager ###")
print(f"{now.hour}:{now.minute}:{now.second} - Loading theme...")
try:
import sass
except ImportError:
print("Could not find libsass!")
sys.exit(1)
theme_path = os.path.join('usr', 'themes', theme, 'style.scss')
if os.path.exists(theme_path):
print(f"Theme '{theme}' found at:", theme_path)
self.sass = sass
self.loadTheme(theme_path)
else:
print("No theme found!")
sys.exit(1)
font_path = os.path.join('usr', 'themes', theme, 'fonts')
if os.path.exists(font_path):
print("Fonts found at:", font_path)
self.loadFonts(font_path)
else:
print("No fonts found!")
print(f"{now.hour}:{now.minute}:{now.second} - Done!\n")
def loadTheme (self, theme):
with open('static/theme/style.css', 'w') as f:
try:
f.write(self.sass.compile(filename=theme, output_style='compressed'))
print("Compiled successfully to:", f.name)
except self.sass.CompileError as e:
print("Failed to compile!\nFull error:", e)
sys.exit(1)
def loadFonts (self, font_path):
dest = os.path.join('static', 'theme', 'fonts')
if os.path.exists(dest):
print("Removing old fonts...")
try:
shutil.rmtree(dest)
print("Removed old fonts!")
except Exception as e:
print("Failed to remove old fonts!\nFull error:", e)
sys.exit(1)
try:
shutil.copytree(font_path, dest)
print("Copied fonts to:", dest)
except Exception as e:
print("Failed to copy fonts!\nFull error:", e)
sys.exit(1)

View file

@ -0,0 +1,67 @@
CREATE TABLE IF NOT EXISTS users (
id INT(69) NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS posts (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
file_name VARCHAR(255) NOT NULL UNIQUE,
author_id INT(69) NOT NULL,
description TEXT NOT NULL,
alt TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS groups (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
author_id INT(69) NOT NULL,
name VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS permissions (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
user_id INT(69) NOT NULL,
admin BOOLEAN NOT NULL DEFAULT FALSE,
create_posts BOOLEAN NOT NULL DEFAULT TRUE,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS devices (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
user_id INT(69) NOT NULL,
device_id VARCHAR(255) NOT NULL,
cookie VARCHAR(255) NOT NULL,
ip VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS tokens (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
token VARCHAR(255) NOT NULL UNIQUE,
is_used BOOLEAN NOT NULL DEFAULT FALSE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS logs (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
ip VARCHAR(255) NOT NULL,
user_id INT(69) DEFAULT NULL,
code INT(69) NOT NULL,
note TEXT DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
CREATE TABLE IF NOT EXISTS bans (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
ip VARCHAR(255) NOT NULL,
code INT(69) NOT NULL,
note TEXT DEFAULT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

View file

@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS group_junction (
id INT(69) PRIMARY KEY AUTO_INCREMENT,
group_id INT(69) NOT NULL,
image_id INT(69) NOT NULL
);