mirror of
https://github.com/simon987/od-database.git
synced 2025-04-18 18:06:44 +00:00
Added admin blacklist control in dashboard
This commit is contained in:
parent
35837463cd
commit
7400bdc2a9
27
app.py
27
app.py
@ -224,7 +224,7 @@ def try_enqueue(url):
|
|||||||
if not od_util.is_valid_url(url):
|
if not od_util.is_valid_url(url):
|
||||||
return "<strong>Error:</strong> Invalid url. Make sure to include the appropriate scheme.", "danger"
|
return "<strong>Error:</strong> Invalid url. Make sure to include the appropriate scheme.", "danger"
|
||||||
|
|
||||||
if od_util.is_blacklisted(url):
|
if db.is_blacklisted(url):
|
||||||
|
|
||||||
return "<strong>Error:</strong> " \
|
return "<strong>Error:</strong> " \
|
||||||
"Sorry, this website has been blacklisted. If you think " \
|
"Sorry, this website has been blacklisted. If you think " \
|
||||||
@ -326,12 +326,35 @@ def admin_dashboard():
|
|||||||
if "username" in session:
|
if "username" in session:
|
||||||
|
|
||||||
tokens = db.get_tokens()
|
tokens = db.get_tokens()
|
||||||
|
blacklist = db.get_blacklist()
|
||||||
|
|
||||||
return render_template("dashboard.html", api_tokens=tokens)
|
return render_template("dashboard.html", api_tokens=tokens, blacklist=blacklist)
|
||||||
else:
|
else:
|
||||||
return abort(403)
|
return abort(403)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/blacklist/add", methods=["POST"])
|
||||||
|
def admin_blacklist_add():
|
||||||
|
if "username" in session:
|
||||||
|
|
||||||
|
url = request.form.get("url")
|
||||||
|
db.add_blacklist_website(url)
|
||||||
|
flash("Added item to blacklist", "success")
|
||||||
|
return redirect("/dashboard")
|
||||||
|
|
||||||
|
else:
|
||||||
|
return abort(403)
|
||||||
|
|
||||||
|
|
||||||
|
@app.route("/blacklist/<int:blacklist_id>/delete")
|
||||||
|
def admin_blacklist_remove(blacklist_id):
|
||||||
|
if "username" in session:
|
||||||
|
|
||||||
|
db.remove_blacklist_website(blacklist_id)
|
||||||
|
flash("Removed blacklist item", "success")
|
||||||
|
return redirect("/dashboard")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/generate_token", methods=["POST"])
|
@app.route("/generate_token", methods=["POST"])
|
||||||
def admin_generate_token():
|
def admin_generate_token():
|
||||||
if "username" in session:
|
if "username" in session:
|
||||||
|
45
database.py
45
database.py
@ -1,6 +1,6 @@
|
|||||||
import sqlite3
|
import sqlite3
|
||||||
import datetime
|
import datetime
|
||||||
import json
|
from urllib.parse import urlparse
|
||||||
import os
|
import os
|
||||||
import bcrypt
|
import bcrypt
|
||||||
import uuid
|
import uuid
|
||||||
@ -10,6 +10,12 @@ class InvalidQueryException(Exception):
|
|||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class BlacklistedWebsite:
|
||||||
|
def __init__(self, blacklist_id, url):
|
||||||
|
self.id = blacklist_id
|
||||||
|
self.netloc = url
|
||||||
|
|
||||||
|
|
||||||
class Website:
|
class Website:
|
||||||
|
|
||||||
def __init__(self, url, logged_ip, logged_useragent, last_modified=None, website_id=None):
|
def __init__(self, url, logged_ip, logged_useragent, last_modified=None, website_id=None):
|
||||||
@ -228,6 +234,43 @@ class Database:
|
|||||||
|
|
||||||
yield doc
|
yield doc
|
||||||
|
|
||||||
|
def add_blacklist_website(self, url):
|
||||||
|
|
||||||
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
parsed_url = urlparse(url)
|
||||||
|
url = parsed_url.scheme + "://" + parsed_url.netloc
|
||||||
|
cursor.execute("INSERT INTO BlacklistedWebsite (url) VALUES (?)", (url, ))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
def remove_blacklist_website(self, blacklist_id):
|
||||||
|
|
||||||
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
cursor.execute("DELETE FROM BlacklistedWebsite WHERE id=?", (blacklist_id, ))
|
||||||
|
conn.commit()
|
||||||
|
|
||||||
|
def is_blacklisted(self, url):
|
||||||
|
|
||||||
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
parsed_url = urlparse(url)
|
||||||
|
url = parsed_url.scheme + "://" + parsed_url.netloc
|
||||||
|
print(url)
|
||||||
|
cursor.execute("SELECT id FROM BlacklistedWebsite WHERE url LIKE ? LIMIT 1", (url, ))
|
||||||
|
|
||||||
|
return cursor.fetchone() is not None
|
||||||
|
|
||||||
|
def get_blacklist(self):
|
||||||
|
|
||||||
|
with sqlite3.connect(self.db_path) as conn:
|
||||||
|
cursor = conn.cursor()
|
||||||
|
|
||||||
|
cursor.execute("SELECT * FROM BlacklistedWebsite")
|
||||||
|
return [BlacklistedWebsite(r[0], r[1]) for r in cursor.fetchall()]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -18,3 +18,8 @@ CREATE TABLE ApiToken (
|
|||||||
token TEXT PRIMARY KEY NOT NULL,
|
token TEXT PRIMARY KEY NOT NULL,
|
||||||
description TEXT
|
description TEXT
|
||||||
);
|
);
|
||||||
|
|
||||||
|
CREATE TABLE BlacklistedWebsite (
|
||||||
|
id INTEGER PRIMARY KEY NOT NULL,
|
||||||
|
url TEXT
|
||||||
|
);
|
||||||
|
@ -4,7 +4,6 @@ from bs4 import BeautifulSoup
|
|||||||
import os
|
import os
|
||||||
import validators
|
import validators
|
||||||
import re
|
import re
|
||||||
import mimetypes
|
|
||||||
from ftplib import FTP
|
from ftplib import FTP
|
||||||
|
|
||||||
|
|
||||||
@ -198,11 +197,3 @@ def is_od(url):
|
|||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def is_blacklisted(url):
|
|
||||||
with open("blacklist.txt", "r") as f:
|
|
||||||
for line in f.readlines():
|
|
||||||
if url.startswith(line.strip()):
|
|
||||||
return True
|
|
||||||
|
|
||||||
return False
|
|
||||||
|
@ -43,11 +43,42 @@
|
|||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<br>
|
||||||
|
<hr>
|
||||||
|
<h3>Blacklist</h3>
|
||||||
|
<table class="table table-striped">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Netloc</th>
|
||||||
|
<th>Action</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for item in blacklist %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ item.netloc }}</td>
|
||||||
|
<td><a class="btn btn-danger" href="/blacklist/{{ item.id }}/delete">Delete</a></td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<form class="form" action="/blacklist/add" method="POST">
|
||||||
|
<div class="form-row">
|
||||||
|
<div class="col col-md-10">
|
||||||
|
<input class="form-control" name="url" placeholder="Url">
|
||||||
|
</div>
|
||||||
|
<div class="col col-md-2">
|
||||||
|
<input type="submit" class="form-control btn btn-primary" value="Add">
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</form>
|
||||||
|
|
||||||
<br>
|
<br>
|
||||||
<hr>
|
<hr>
|
||||||
<h3>Misc actions</h3>
|
<h3>Misc actions</h3>
|
||||||
|
|
||||||
<a class="btn btn-danger" href="/website/delete_empty">Delete websites with no associated files that are not queued</a>
|
<a class="btn btn-danger" href="/website/delete_empty">Delete websites with no associated files that are
|
||||||
|
not queued</a>
|
||||||
|
|
||||||
<hr>
|
<hr>
|
||||||
<a class="btn btn-info" href="/logout">Logout</a>
|
<a class="btn btn-info" href="/logout">Logout</a>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user