Added Logout

This commit is contained in:
Michael Rodin 2023-10-20 15:27:25 +02:00
parent 3c79c3c688
commit 76d43d57d9
8 changed files with 106 additions and 38 deletions

View file

@ -12,7 +12,7 @@ db.startup()
app = Flask(__name__)
## CUSTOM FILTERS
########## CUSTOM FILTERS
@app.template_filter('ctime')
def timectime(s):
return datetime.utcfromtimestamp(s).strftime('%Y-%m-%d %H:%M')
@ -27,7 +27,7 @@ def convsize(s):
return str("%.2f" % s)+sizes[n]
## WEB FRONTEND
########## WEB FRONTEND
@app.route('/')
def homepage():
# try to get userdata, else logout state
@ -95,6 +95,15 @@ def loginpage():
else:
return render_template("login.html", title="Login")
@app.route('/logout')
def logout():
sesskey=request.cookies.get('session')
logged_in,userdata=get_login_info(sesskey)
if not logged_in:
return make_response(redirect('/login'))
db.logout_user(sesskey)
return make_response(redirect('/'))
@app.route('/view/<int:archid>')
def viewpage(archid:int):
logged_in,userdata=get_login_info(request.cookies.get('session'))
@ -137,7 +146,7 @@ def labeleditpage(archid:int):
# GET: return normal labels page
labels_name_list=[]
for i in labels:
labels_name_list.append(i[0])
labels_name_list.append(i[1])
return render_template("labels.html", title="Edit Labels",userdata=userdata,login=logged_in,archive=archive,res_labels=label_dict,labels_names=labels_name_list)
@app.route('/search')
@ -152,8 +161,10 @@ def searchpage():
sorttype="time"
try:
category=request.args['category']
label_dict=db.get_label_labeltypes(int(category))
except Exception as e:
category=0
label_dict={}
try:
keywords=request.args['q']
keywords="".join(keywords).split(" ")
@ -163,13 +174,23 @@ def searchpage():
count=request.args['count']
except Exception as e:
count=20
archives=db.get_n_archives(sorttype,category,keywords,count)
labels=[]
try:
for i in request.args:
try:
int(i)
labels.append(i)
except Exception as e:
continue
except Exception as e:
labels=[]
archives=db.get_n_archives(sorttype,category,keywords,count,labels)
htmlcatlist=get_category_selection()
return render_template("search.html", title="Advanced Search",categories=htmlcatlist,userdata=userdata,login=logged_in,archives=archives)
return render_template("search.html", title="Advanced Search",categories=htmlcatlist,userdata=userdata,login=logged_in,archives=archives,res_labels=label_dict,labels=labels)
## FUNCTIONS
########## FUNCTIONS
def errorpage(message):
return "<h2>ERROR: " + str(message) + "</h2>Go back and try again"

View file

@ -89,7 +89,7 @@ class db:
def check_sesskey(self, sesskey:str):
self.cur.execute(f"SELECT SESSKEY,USERID FROM Sessions WHERE SESSKEY='{sesskey}'")
entry=self.cur.fetchone()
if sesskey in entry:
if entry and sesskey in entry:
return True, entry[1]
else:
return False, ""
@ -98,6 +98,9 @@ class db:
def set_sesskey(self, sesskey:str, userid:int, lifetime:int):
self.cur.execute(f"INSERT INTO Sessions(SESSKEY,USERID,CREATED,LIFE) VALUES('{sesskey}',{userid},{time.time()},{lifetime})")
def logout_user(self, sesskey:str):
self.cur.execute(f"DELETE FROM Sessions WHERE SESSKEY='{sesskey}'")
## Gets and returns all user info about one (1) user
## OUTPUT: tuple=(ID:int,UNAME:str,DNAME:str,CREATED:int,STATE:text,PASSHASH:text)
def get_user_info(self, userid:int):
@ -132,7 +135,7 @@ class db:
## Returns all relevant information about one (1) archive
## OUTPUT: archive:tuple=(ID:int,NAME:str,HASH:str,SIZE:int,IMPORTED[UNIX]:int,CATEGORY.ID:int,CATEGORY,str,CATEGORY.DESCRIPTION:str,USER.ID:int,DNAME:str),
## category:tuple=(ID:int,CATEGORY:str,DESCRIPTION:str,PID:int,PCAT:str,PDESC:str)
## labels:array=[…,(LABEL:str,LABTYPE:int,LABDESC:str),…]
## labels:list=[…,(ID:int,LABEL:str,LABTYPE:int,LABDESC:str),…]
def get_archive_info(self, archid:int):
# get info about archive itself
self.cur.execute(f"""SELECT Archs.ID,Archs.NAME,Archs.HASH,Archs.SIZE,Archs.IMPORTED,Cats.ID,Cats.CATEGORY,Cats.DESCRIPTION,Users.ID,Users.DNAME FROM Archs
@ -145,13 +148,17 @@ class db:
WHERE c.ID={archive[5]} AND c.PARENT=p.ID""")
category=self.cur.fetchone()
# get info about labels of archive
self.cur.execute(f"""SELECT Labs.LABEL,LabType.ID AS LABTYPE,LabType.DESCRIPTION AS LABDESC FROM ArchLab
labels=self.get_label_info(archid)
return archive, category, labels
def get_label_info(self, archid:int):
self.cur.execute(f"""SELECT Labs.ID,Labs.LABEL,LabType.ID AS LABTYPE,LabType.DESCRIPTION AS LABDESC FROM ArchLab
JOIN Archs ON Archs.ID=ArchLab.ARCHID
JOIN Labs ON Labs.ID=ArchLab.LABID
JOIN LabType ON Labs.TYPE=LabType.ID
WHERE ARCHID={archid};""")
labels=self.cur.fetchall()
return archive, category, labels
return self.cur.fetchall()
## Returns all categories.
## OUTPUT: array=[…,(ID:int,CATEGORY:str,PARENT:int,DESCRIPTION:str),…]
@ -220,7 +227,7 @@ class db:
## Returns n archives, sorted by (imported )time or size
## OUTPUT: archives:array=[…,(ID:int,NAME:str,SIZE:str,IMPORTED[UNIX]:int),…]
def get_n_archives(self, sorttype:str="time",category:int=0, keywords:list=[], count:int=20):
def get_n_archives(self, sorttype:str="time",category:int=0, keywords:list=[], count:int=20,labels:list=[]): # TODO: CLEANN!!!!!
match sorttype:
case "size":
sorttype="SIZE DESC"
@ -238,7 +245,7 @@ class db:
if len(keywords) == 1:
keyword_string+=f"OR HASH = '{keywords[0]}' "
# Get all children of category (if exist) and put into query string
# get all children of category (if exist) and put into query string
categories=self.get_categories()
catlist=[str(category)]
for i in categories:
@ -246,11 +253,27 @@ class db:
catlist.append(str(i[0]))
categories="(" + ",".join(catlist) + ")"
self.cur.execute(f"""SELECT ID,NAME,SIZE,IMPORTED FROM Archs
{"WHERE 1=1" if category==0 else " WHERE CATEGORY IN " + categories}
self.cur.execute(f"""SELECT Archs.ID,Archs.NAME,Archs.SIZE,Archs.IMPORTED FROM Archs
{"WHERE 1=1" if category==0 else "WHERE CATEGORY IN " + categories}
{keyword_string}
ORDER BY {sorttype} LIMIT {count};""")
ORDER BY {sorttype} LIMIT {count if count else 20}""")
archives=self.cur.fetchall()
## WARNING: JANK
#positive_archives=[]
#print("LABELS:", labels)
#if len(labels) >= 1:
# for arch in archives:
# archid=arch[0]
# archive_labels=self.get_label_info(archid)
# success=True
# for label in archive_labels:
# if not label[0] in labels:
# success=False
# if success:
# positive_archives.append(arch)
# if len(positive_archives) >= count:
# break
return archives
if __name__ == "__main__":

View file

@ -34,6 +34,7 @@ header > div#container {
.big-button {
height: 2em;
min-width: 2em;
margin: auto 0.1em;
border: none;
border-radius: 0.1em;

View file

@ -4,5 +4,5 @@ div.flex-container {
}
div.flex-item {
display: inline-block;
display: block;
}

View file

@ -0,0 +1,11 @@
div.grid-selection {
display: grid;
grid-template-columns: max-content auto;
}
div.select-container {
display: inline-grid;
margin: 0.2em auto;
grid-template-columns: min-content min-content;
grid-template-rows: min-content min-content;
}

View file

@ -13,17 +13,16 @@
<a href="/"><img src="/static/favicon.ico" alt="favicon"></a>
<span id="title">{{title}}</span>
<div id="container">
<!-- BUTTONS IF USER LOGGED IN -->
{% if login %}
<a href="/add"><button class="big-button"><b>&#x2B</b></button></a>
{% endif %}
<!------------------------------->
<form action="/search" method="get">
<input type="text" name="q" placeholder="search">
<input type="submit" value="&#x1F50E;" class="big-button" id="search-button">
</form>
{% if login %}
<a href="/user/{{userdata[0]}}"><button class="big-button">{{userdata[2]}}</button></a>
<a href="/logout"><button class="big-button">Logout</button></a>
{% else %}
<a href="/login"><button class="big-button">Login</button></a>
{% endif %}

View file

@ -2,12 +2,14 @@
{% block meta %}
<link rel="stylesheet" href="/static/search.css" />
<link rel="stylesheet" href="/static/labels.css" />
<link rel="stylesheet" href="/static/home.css" />
{% endblock %}
{% block content %}
<form action="/search" method="get">
<div class="grid-selection">
<div class="select-container">
<b>Sort by:</b>
<select name="sort">
<option value="time" {% if "time" == request.args.get('sort') %}selected="selected"{% endif %}>Import Time</option>
@ -15,14 +17,26 @@
<option value="za" {% if "za" == request.args.get('sort') %}selected="selected"{% endif %}>Z-A</option>
<option value="size" {% if "size" == request.args.get('sort') %}selected="selected"{% endif %}>Archive Size</option>
</select>
</div>
<div class="grid-selection">
<b>Category:</b>
<select name="category">
<option value="0">None</option>
{% for i in categories %}
<option value="{{i[0]}}" {% if i[0] == request.args.get('category')|int %}selected="selected"{% endif %}>{{i[1]}}</option>
{% endfor %}
</select>
<b>Count: </b><input type="number" name="count" step="1" placeholder="Number of items" {% if request.args.get('count') %}value="{{request.args.get('count')}}"{% else %} value="20"{% endif %}>
</div>
{# <!-- Can't get that to work
<div class="labels-container">
{% for ltype in res_labels %}
<div class="flex-item"><b>{{ltype}}</b>
{% for for_temp in res_labels[ltype] %}
<input type="checkbox" id="{{for_temp[0]}}" name="{{for_temp[0]}}" {% if for_temp[0]|string in labels %}checked{% endif %}><label for="{{for_temp[0]}}">{{for_temp[1]}}</label>
{% endfor %}
</div>
{% endfor %}
</div>-->
#}
</div>
{% if request.args.get('q') %}
<input type="text" name="q" value="{{request.args.get('q')}}" placeholder="Keywords">
@ -30,7 +44,6 @@
<input type="text" name="q" placeholder="Keywords">
{% endif %}
<input type="submit" value="Search!">
<div class="grid-selection"></div>
</form>
{% if archives|length > 0 %}
<div class="grid-container">

View file

@ -16,7 +16,7 @@
<b>Labels: {% if login and userdata[0] == archive[8] %}<a href="/labels/{{archive[0]}}">Edit</a>{% endif %}</b>
<div>
{% for lab in labels %}
<div class="label">{{lab[0]}}</div>
<div class="label">{{lab[1]}}</div>
{% endfor %}
</div>
</div>