First functions
Added first functions in backend The homepage can now show the most recent archives
This commit is contained in:
parent
a2f9b4f978
commit
d4016ce80c
8 changed files with 187 additions and 30 deletions
26
flask/app.py
26
flask/app.py
|
@ -1,17 +1,31 @@
|
|||
|
||||
# Importing flask module in the project is mandatory
|
||||
# An object of Flask class is our WSGI application.
|
||||
from flask import Flask,redirect,url_for,request,render_template
|
||||
|
||||
# Flask constructor takes the name of
|
||||
# current module (__name__) as argument.
|
||||
from datetime import datetime
|
||||
## Import db class from func.py and initialise it
|
||||
from func import db
|
||||
db=db()
|
||||
|
||||
app = Flask(__name__)
|
||||
|
||||
## CUSTOM FILTERS
|
||||
@app.template_filter('ctime')
|
||||
def timectime(s):
|
||||
return datetime.utcfromtimestamp(s).strftime('%Y-%m-%d %H:%M')
|
||||
@app.template_filter('spacer')
|
||||
def timectime(s):
|
||||
sizes=("B","KB","MB","GB","TB")
|
||||
n=0
|
||||
while s > 1000:
|
||||
n+=1
|
||||
s=s/1000
|
||||
return str("%.2f" % s)+sizes[n]
|
||||
|
||||
|
||||
## WEB FRONTEND
|
||||
@app.route('/')
|
||||
def homepage():
|
||||
return render_template("home.html", title="Homepage")
|
||||
archives=db.get_n_archives()
|
||||
return render_template("home.html", title="Homepage",archives=archives)
|
||||
|
||||
## API CALLS
|
||||
|
||||
|
|
106
flask/func.py
Normal file
106
flask/func.py
Normal file
|
@ -0,0 +1,106 @@
|
|||
## MAIN FUNCTIONS FILE FOR BACK-BACKEND OF FLASK
|
||||
import mariadb as sql
|
||||
from os import environ
|
||||
|
||||
## params populated with environment variables, defaults can be changed for a permanent solution
|
||||
conn_params={
|
||||
"user" : environ.get('MARIADB_USER') if environ.get('MARIADB_USER') else "rar_index_app",
|
||||
"password" : environ.get('MARIADB_PASSWORD') if environ.get('MARIADB_PASSWORD') else "password",
|
||||
"host" : environ.get('MARIADB_HOST') if environ.get('MARIADB_HOST') else "marcelsite.com",
|
||||
"database" : environ.get('MARIADB_DB') if environ.get('MARIADB_DB') else "rar_index"
|
||||
}
|
||||
|
||||
class db:
|
||||
def __init__(self):
|
||||
self.conn=sql.connect(**conn_params)
|
||||
self.cur=self.conn.cursor()
|
||||
|
||||
## Creates all archives, if they don't exist already
|
||||
## Called only on startup, hence the name
|
||||
def startup(self):
|
||||
self.cur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS Archs(
|
||||
ID int PRIMARY KEY AUTO_INCREMENT,
|
||||
NAME text NOT NULL,
|
||||
HASH text NOT NULL,
|
||||
SIZE int NOT NULL,
|
||||
IMPORTED int,
|
||||
CATEGORY int,
|
||||
OWNER int
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS Users(
|
||||
ID int PRIMARY KEY AUTO_INCREMENT,
|
||||
UNAME text NOT NULL,
|
||||
DNAME text NOT NULL,
|
||||
CREATED int NOT NULL,
|
||||
STATE text,
|
||||
PASSHASH text NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS Sessions(
|
||||
ID int PRIMARY KEY AUTO_INCREMENT,
|
||||
SESSKEY text NOT NULL,
|
||||
CREATED int NOT NULL,
|
||||
LIFE int
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS Cats(
|
||||
ID int PRIMARY KEY AUTO_INCREMENT,
|
||||
CATEGORY text NOT NULL,
|
||||
PARENT int,
|
||||
DESCRIPTION text
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS ArchLab(
|
||||
ID int PRIMARY KEY AUTO_INCREMENT,
|
||||
ARCHID int NOT NULL,
|
||||
LABID int NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS Labs(
|
||||
ID int PRIMARY KEY AUTO_INCREMENT,
|
||||
LABEL text NOT NULL,
|
||||
CATEGORY text,
|
||||
TYPE int NOT NULL
|
||||
);
|
||||
CREATE TABLE IF NOT EXISTS LabType(
|
||||
ID int PRIMARY KEY AUTO_INCREMENT,
|
||||
NAME text NOT NULL,
|
||||
DESCRIPTION text
|
||||
);
|
||||
""")
|
||||
|
||||
## Returns all relevant information about one (1) archive
|
||||
## OUTPUT: archive:tuple=(NAME:str,HASH:str,IMPORTED[UNIX]:int,CATEGORY,str,CATEGORY.DESCRIPTION:str,UNAME:str,DNAME:str)
|
||||
## labels:array=[…,(LABEL:str,CATEGORY:str,CATDESC:str,LABTYPE:str,LABDESC:str),…]
|
||||
def get_archive_info(self, hash:str):
|
||||
#global cur
|
||||
self.cur.execute(f"""SELECT Archs.NAME,Archs.HASH,Archs.IMPORTED,Cats.CATEGORY,Cats.DESCRIPTION,Users.UNAME,Users.DNAME FROM Archs
|
||||
JOIN Cats ON Cats.ID=Archs.CATEGORY
|
||||
JOIN Users ON Users.ID=Archs.OWNER
|
||||
WHERE hash='{hash}'""")
|
||||
archive=self.cur.fetchone()
|
||||
self.cur.execute(f"""SELECT Labs.LABEL,Cats.CATEGORY,Cats.DESCRIPTION AS CATDESC,LabType.NAME AS LABTYPE,LabType.DESCRIPTION AS LABDESC FROM ArchLab
|
||||
JOIN Archs ON Archs.ID=ArchLab.ARCHID
|
||||
JOIN Labs ON Labs.ID=ArchLab.LABID
|
||||
JOIN Cats ON Labs.CATEGORY=Cats.ID
|
||||
JOIN LabType ON Labs.TYPE=LabType.ID
|
||||
WHERE ARCHID=1;""")
|
||||
labels=self.cur.fetchall()
|
||||
return archive, labels
|
||||
|
||||
## Returns n archives, sorted by (imported )time or size
|
||||
## OUTPUT: archives:array=[…,(NAME:str,SIZE:str,IMPORTED[UNIX]:int),…]
|
||||
def get_n_archives(self, sorttype:str="time",category:int=0, count:int=20):
|
||||
global cur
|
||||
match sorttype:
|
||||
case "size":
|
||||
sorttype="SIZE"
|
||||
case _:
|
||||
sorttype="IMPORTED"
|
||||
self.cur.execute(f"""SELECT NAME,SIZE,IMPORTED FROM Archs{"" if category==0 else " WHERE CATEGORY=" + category} ORDER BY {sorttype} DESC LIMIT {count};""")
|
||||
archives=self.cur.fetchall()
|
||||
return archives
|
||||
|
||||
if __name__ == "__main__":
|
||||
#startup()
|
||||
db=db(conn_params)
|
||||
db.cur.close()
|
||||
db.conn.close()
|
||||
exit()
|
11
flask/static/home.css
Normal file
11
flask/static/home.css
Normal file
|
@ -0,0 +1,11 @@
|
|||
div.grid-container {
|
||||
display: grid;
|
||||
grid-template-columns: auto auto auto;
|
||||
}
|
||||
div.grid-item {
|
||||
border: 2px black solid;
|
||||
}
|
||||
|
||||
div.clickable:hover {
|
||||
background: grey;
|
||||
}
|
|
@ -2,6 +2,8 @@
|
|||
<html>
|
||||
<head>
|
||||
<link rel="stylesheet" href="/static/base.css" />
|
||||
{% block meta %}
|
||||
{% endblock %}
|
||||
<title>RAR-Index – {{title}}</title>
|
||||
</head>
|
||||
|
||||
|
|
|
@ -1,5 +1,20 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% block meta %}
|
||||
<link rel="stylesheet" href="/static/home.css" />
|
||||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<p>Test</p>
|
||||
{% if archives|length > 0 %}
|
||||
<div class="grid-container">
|
||||
{% for arch in archives %}
|
||||
<div class="grid-item clickable"><b>{{arch[0]}}</b></div>
|
||||
<div class="grid-item"><p>{{arch[1]|spacer}}</p></div>
|
||||
<div class="grid-item"><p>{{arch[2]|ctime}}</p></div>
|
||||
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% else %}
|
||||
<p>No matching archives</p>
|
||||
{% endif %}
|
||||
{% endblock %}
|
Loading…
Add table
Add a link
Reference in a new issue