1
0
mirror of https://github.com/aquatix/digimarks.git synced 2025-12-06 22:05:09 +01:00

More refactoring

This commit is contained in:
2024-08-08 17:30:15 +02:00
parent c695972a94
commit 533e21141f

View File

@@ -13,8 +13,6 @@ import bs4
import httpx import httpx
from dateutil import tz from dateutil import tz
# from flask import (Flask, abort, jsonify, make_response, redirect,
# render_template, request, url_for)
from fastapi import FastAPI, HTTPException, Request, Response from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import HTMLResponse, RedirectResponse from fastapi.responses import HTMLResponse, RedirectResponse
@@ -41,6 +39,8 @@ class Settings(BaseSettings):
mashape_api_key: str mashape_api_key: str
system_key: str
debug: bool = False debug: bool = False
@@ -125,7 +125,7 @@ def clean_tags(tags_list):
return tags_res return tags_res
def file_type(filename): def file_type(filename: str):
"""Try to determine the file type for the file in `filename`. """Try to determine the file type for the file in `filename`.
:param str filename: path to file to check :param str filename: path to file to check
@@ -234,13 +234,13 @@ class Bookmark(Base):
result = request.app.requests_client.head(self.url, headers={'User-Agent': DIGIMARKS_USER_AGENT}, timeout=30) result = request.app.requests_client.head(self.url, headers={'User-Agent': DIGIMARKS_USER_AGENT}, timeout=30)
self.http_status = result.status_code self.http_status = result.status_code
except httpx.HTTPError as e: except httpx.HTTPError as e:
logger.error(f'Failed to do head info fetching for {self.url}: {e}') logger.error('Failed to do head info fetching for %s: %s', self.url, str(e))
self.http_status = self.HTTP_CONNECTIONERROR self.http_status = self.HTTP_CONNECTIONERROR
return self.http_status return self.http_status
def _set_favicon_with_iconsbetterideaorg(self, request: Request, domain): def _set_favicon_with_iconsbetterideaorg(self, request: Request, domain):
"""Fetch favicon for the domain.""" """Fetch favicon for the domain."""
fileextension = '.png' file_extension = '.png'
meta = request.app.requests_client.head( meta = request.app.requests_client.head(
'http://icons.better-idea.org/icon?size=60&url=' + domain, 'http://icons.better-idea.org/icon?size=60&url=' + domain,
allow_redirects=True, allow_redirects=True,
@@ -248,41 +248,41 @@ class Bookmark(Base):
timeout=15, timeout=15,
) )
if meta.url[-3:].lower() == 'ico': if meta.url[-3:].lower() == 'ico':
fileextension = '.ico' file_extension = '.ico'
response = request.app.requests_client.get( response = request.app.requests_client.get(
'http://icons.better-idea.org/icon?size=60&url=' + domain, 'http://icons.better-idea.org/icon?size=60&url=' + domain,
stream=True, stream=True,
headers={'User-Agent': DIGIMARKS_USER_AGENT}, headers={'User-Agent': DIGIMARKS_USER_AGENT},
timeout=15, timeout=15,
) )
filename = os.path.join(settings.media_dir, 'favicons/', domain + fileextension) filename = os.path.join(settings.media_dir, 'favicons/', domain + file_extension)
with open(filename, 'wb') as out_file: with open(filename, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file) shutil.copyfileobj(response.raw, out_file)
del response del response
filetype = file_type(filename) filetype = file_type(str(filename))
if filetype == 'gz': if filetype == 'gz':
# decompress # decompress
orig = gzip.GzipFile(filename, 'rb') orig = gzip.GzipFile(filename, 'rb')
origcontent = orig.read() orig_content = orig.read()
orig.close() orig.close()
os.remove(filename) os.remove(filename)
with open(filename, 'wb') as new: with open(filename, 'wb') as new:
new.write(origcontent) new.write(orig_content)
self.favicon = domain + fileextension self.favicon = domain + file_extension
def _set_favicon_with_realfavicongenerator(self, request: Request, domain: str): def _set_favicon_with_realfavicongenerator(self, request: Request, domain: str):
"""Fetch favicon for the domain.""" """Fetch favicon for the domain."""
response = request.app.requests_client.get( response = request.app.requests_client.get(
'https://realfavicongenerator.p.rapidapi.com/favicon/icon?platform=android_chrome&site=' + domain, 'https://realfavicongenerator.p.rapidapi.com/favicon/icon?platform=android_chrome&site=' + domain,
stream=True, stream=True,
headers={'User-Agent': DIGIMARKS_USER_AGENT, 'X-Mashape-Key': settings.MASHAPE_API_KEY}, headers={'User-Agent': DIGIMARKS_USER_AGENT, 'X-Mashape-Key': settings.mashape_api_key},
) )
if response.status_code == 404: if response.status_code == 404:
# Fall back to desktop favicon # Fall back to desktop favicon
response = request.app.requests_client.get( response = request.app.requests_client.get(
'https://realfavicongenerator.p.rapidapi.com/favicon/icon?platform=desktop&site=' + domain, 'https://realfavicongenerator.p.rapidapi.com/favicon/icon?platform=desktop&site=' + domain,
stream=True, stream=True,
headers={'User-Agent': DIGIMARKS_USER_AGENT, 'X-Mashape-Key': settings.MASHAPE_API_KEY}, headers={'User-Agent': DIGIMARKS_USER_AGENT, 'X-Mashape-Key': settings.mashape_api_key},
) )
# Debug for the moment # Debug for the moment
print(domain) print(domain)
@@ -292,16 +292,16 @@ class Bookmark(Base):
print('Skipping this favicon, needs fallback') print('Skipping this favicon, needs fallback')
return return
# Default to 'image/png' # Default to 'image/png'
fileextension = '.png' file_extension = '.png'
if response.headers['content-type'] == 'image/jpeg': if response.headers['content-type'] == 'image/jpeg':
fileextension = '.jpg' file_extension = '.jpg'
if response.headers['content-type'] == 'image/x-icon': if response.headers['content-type'] == 'image/x-icon':
fileextension = '.ico' file_extension = '.ico'
filename = os.path.join(settings.media_dir, 'favicons/', domain + fileextension) filename = os.path.join(settings.media_dir, 'favicons/', domain + file_extension)
with open(filename, 'wb') as out_file: with open(filename, 'wb') as out_file:
shutil.copyfileobj(response.raw, out_file) shutil.copyfileobj(response.raw, out_file)
del response del response
filetype = file_type(filename) filetype = file_type(str(filename))
if filetype == 'gz': if filetype == 'gz':
# decompress # decompress
orig = gzip.GzipFile(filename, 'rb') orig = gzip.GzipFile(filename, 'rb')
@@ -310,7 +310,7 @@ class Bookmark(Base):
os.remove(filename) os.remove(filename)
with open(filename, 'wb') as new: with open(filename, 'wb') as new:
new.write(origcontent) new.write(origcontent)
self.favicon = domain + fileextension self.favicon = domain + file_extension
def set_favicon(self, request: Request): def set_favicon(self, request: Request):
"""Fetch favicon for the domain.""" """Fetch favicon for the domain."""
@@ -393,42 +393,42 @@ class PublicTag(Base):
self.tagkey = binascii.hexlify(os.urandom(16)) self.tagkey = binascii.hexlify(os.urandom(16))
def get_tags_for_user(userkey): def get_tags_for_user(user_key: str):
"""Extract all tags from the bookmarks.""" """Extract all tags from the bookmarks."""
bookmarks = Bookmark.select().filter(Bookmark.userkey == userkey, Bookmark.status == Bookmark.VISIBLE) bookmarks = Bookmark.select().filter(Bookmark.userkey == user_key, Bookmark.status == Bookmark.VISIBLE)
tags = [] tags = []
for bookmark in bookmarks: for bookmark in bookmarks:
tags += bookmark.tags_list tags += bookmark.tags_list
return clean_tags(tags) return clean_tags(tags)
def get_cached_tags(userkey): def get_cached_tags(user_key: str):
"""Fail-safe way to get the cached tags for `userkey`.""" """Fail-safe way to get the cached tags for `user_key`."""
try: try:
return all_tags[userkey] return all_tags[user_key]
except KeyError: except KeyError:
return [] return []
def get_theme(userkey): def get_theme(user_key: str):
themes = {DEFAULT_THEME: {}} themes = {DEFAULT_THEME: {}}
try: try:
usertheme = usersettings[userkey]['theme'] usertheme = usersettings[user_key]['theme']
return themes[usertheme] return themes[usertheme]
except KeyError: except KeyError:
return themes[DEFAULT_THEME] # default return themes[DEFAULT_THEME] # default
def make_external(request: Request, url): def make_external(request: Request, url: str):
return urljoin(request.url_root, url) return urljoin(request.url_root, url)
def _find_bookmarks(userkey, filter_text) -> list[Bookmark]: def _find_bookmarks(user_key: str, filter_text) -> list[Bookmark]:
"""Look up bookmark for `userkey` which contains `filter_text` in its properties.""" """Look up bookmark for `userkey` which contains `filter_text` in its properties."""
return ( return (
Bookmark.select() Bookmark.select()
.where( .where(
Bookmark.userkey == userkey, Bookmark.userkey == user_key,
( (
Bookmark.title.contains(filter_text) Bookmark.title.contains(filter_text)
| Bookmark.url.contains(filter_text) | Bookmark.url.contains(filter_text)
@@ -519,7 +519,7 @@ def get_bookmarks(request: Request, user_key, filter_method=None, sort_method=No
@app.route('/<user_key>/<show_as>', methods=['GET', 'POST']) @app.route('/<user_key>/<show_as>', methods=['GET', 'POST'])
@app.route('/<user_key>/<show_as>/filter/<filtermethod>', methods=['GET', 'POST']) @app.route('/<user_key>/<show_as>/filter/<filtermethod>', methods=['GET', 'POST'])
@app.route('/<user_key>/<show_as>/sort/<sortmethod>', methods=['GET', 'POST']) @app.route('/<user_key>/<show_as>/sort/<sortmethod>', methods=['GET', 'POST'])
def bookmarks_page(request: Request, user_key, filter_method=None, sort_method=None, show_as='cards'): def bookmarks_page(request: Request, user_key: str, filter_method=None, sort_method=None, show_as='cards'):
bookmarks, bookmarktags, filter_text, message = get_bookmarks(request, user_key, filter_method, sort_method) bookmarks, bookmarktags, filter_text, message = get_bookmarks(request, user_key, filter_method, sort_method)
theme = get_theme(user_key) theme = get_theme(user_key)
return templates.TemplateResponse( return templates.TemplateResponse(
@@ -558,24 +558,24 @@ def bookmarks_js(user_key):
return result return result
@app.get('/r/<userkey>/<urlhash>', response_class=HTMLResponse) @app.get('/r/<user_key>/<url_hash>', response_class=HTMLResponse)
def bookmark_redirect(userkey, urlhash): def bookmark_redirect(user_key, url_hash):
"""Securely redirect a bookmark to its url, stripping referrer (if browser plays nice).""" """Securely redirect a bookmark to its url, stripping referrer (if browser plays nice)."""
# @TODO: add counter to this bookmark # @TODO: add counter to this bookmark
try: try:
bookmark = Bookmark.get( bookmark = Bookmark.get(
Bookmark.url_hash == urlhash, Bookmark.userkey == userkey, Bookmark.status == Bookmark.VISIBLE Bookmark.url_hash == url_hash, Bookmark.userkey == user_key, Bookmark.status == Bookmark.VISIBLE
) )
except Bookmark.DoesNotExist: except Bookmark.DoesNotExist:
raise HTTPException(status_code=404, detail='Bookmark not found') raise HTTPException(status_code=404, detail='Bookmark not found')
return templates.TemplateResponse('redirect.html', url=bookmark.url) return templates.TemplateResponse('redirect.html', url=bookmark.url)
@app.route('/api/v1/<userkey>', methods=['GET', 'POST']) @app.route('/api/v1/<user_key>', methods=['GET', 'POST'])
@app.route('/api/v1/<userkey>/filter/<filtermethod>', methods=['GET', 'POST']) @app.route('/api/v1/<user_key>/filter/<filtermethod>', methods=['GET', 'POST'])
@app.route('/api/v1/<userkey>/sort/<sortmethod>', methods=['GET', 'POST']) @app.route('/api/v1/<user_key>/sort/<sortmethod>', methods=['GET', 'POST'])
def bookmarks_json(request: Request, userkey, filtermethod=None, sortmethod=None): def bookmarks_json(request: Request, user_key: str, filtermethod=None, sortmethod=None):
bookmarks, bookmarktags, filter_text, message = get_bookmarks(request, userkey, filtermethod, sortmethod) bookmarks, bookmarktags, filter_text, message = get_bookmarks(request, user_key, filtermethod, sortmethod)
bookmarkslist = [i.serialize for i in bookmarks] bookmarkslist = [i.serialize for i in bookmarks]
@@ -584,17 +584,17 @@ def bookmarks_json(request: Request, userkey, filtermethod=None, sortmethod=None
'tags': bookmarktags, 'tags': bookmarktags,
'filter_text': filter_text, 'filter_text': filter_text,
'message': message, 'message': message,
'userkey': userkey, 'userkey': user_key,
} }
return the_data return the_data
@app.route('/api/v1/<userkey>/<urlhash>') @app.get('/api/v1/<user_key>/<urlhash>')
def bookmark_json(userkey, urlhash): def bookmark_json(user_key: str, url_hash):
"""Serialise bookmark to json.""" """Serialise bookmark to json."""
try: try:
bookmark = Bookmark.get( bookmark = Bookmark.get(
Bookmark.url_hash == urlhash, Bookmark.userkey == userkey, Bookmark.status == Bookmark.VISIBLE Bookmark.url_hash == url_hash, Bookmark.userkey == user_key, Bookmark.status == Bookmark.VISIBLE
) )
return bookmark.to_dict() return bookmark.to_dict()
except Bookmark.DoesNotExist: except Bookmark.DoesNotExist:
@@ -611,8 +611,8 @@ def search_bookmark_titles_json(userkey, filter_text):
return result return result
@app.get('/<userkey>/<urlhash>', response_class=HTMLResponse) @app.get('/<user_key>/<urlhash>', response_class=HTMLResponse)
@app.get('/<userkey>/<urlhash>/edit', response_class=HTMLResponse) @app.get('/<user_key>/<urlhash>/edit', response_class=HTMLResponse)
def edit_bookmark(request: Request, userkey, urlhash): def edit_bookmark(request: Request, userkey, urlhash):
"""Bookmark edit form.""" """Bookmark edit form."""
# bookmark = getbyurlhash() # bookmark = getbyurlhash()
@@ -638,8 +638,8 @@ def edit_bookmark(request: Request, userkey, urlhash):
) )
@app.get('/<userkey>/add', response_class=HTMLResponse) @app.get('/<user_key>/add', response_class=HTMLResponse)
def add_bookmark(request: Request, userkey): def add_bookmark(request: Request, user_key: str):
"""Bookmark add form.""" """Bookmark add form."""
url = request.args.get('url') url = request.args.get('url')
if not url: if not url:
@@ -648,14 +648,14 @@ def add_bookmark(request: Request, userkey):
url = request.referrer url = request.referrer
bookmark = Bookmark(title='', url=url, tags='') bookmark = Bookmark(title='', url=url, tags='')
message = request.args.get('message') message = request.args.get('message')
tags = get_cached_tags(userkey) tags = get_cached_tags(user_key)
theme = get_theme(userkey) theme = get_theme(user_key)
return templates.TemplateResponse( return templates.TemplateResponse(
'edit.html', action='Add bookmark', userkey=userkey, bookmark=bookmark, tags=tags, message=message, theme=theme 'edit.html', action='Add bookmark', userkey=user_key, bookmark=bookmark, tags=tags, message=message, theme=theme
) )
def update_bookmark(request: Request, userkey, urlhash=None): def update_bookmark(request: Request, user_key: str, url_hash: str=None):
"""Add (no urlhash) or edit (urlhash is set) a bookmark.""" """Add (no urlhash) or edit (urlhash is set) a bookmark."""
title = request.form.get('title') title = request.form.get('title')
url = request.form.get('url') url = request.form.get('url')
@@ -668,17 +668,17 @@ def update_bookmark(request: Request, userkey, urlhash=None):
if request.form.get('strip'): if request.form.get('strip'):
strip_params = True strip_params = True
if url and not urlhash: if url and not url_hash:
# New bookmark # New bookmark
bookmark, created = Bookmark.get_or_create(url=url, userkey=userkey) bookmark, created = Bookmark.get_or_create(url=url, userkey=user_key)
if not created: if not created:
message = 'Existing bookmark, did not overwrite with new values' message = 'Existing bookmark, did not overwrite with new values'
return RedirectResponse( return RedirectResponse(
request.url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash, message=message) request.url_for('editbookmark', userkey=user_key, urlhash=bookmark.url_hash, message=message)
) )
elif url: elif url:
# Existing bookmark, get from DB # Existing bookmark, get from DB
bookmark = Bookmark.get(Bookmark.userkey == userkey, Bookmark.url_hash == urlhash) bookmark = Bookmark.get(Bookmark.userkey == user_key, Bookmark.url_hash == url_hash)
# Editing this bookmark, set modified_date to now # Editing this bookmark, set modified_date to now
bookmark.modified_date = datetime.datetime.now() bookmark.modified_date = datetime.datetime.now()
else: else:
@@ -711,7 +711,7 @@ def update_bookmark(request: Request, userkey, urlhash=None):
return bookmark return bookmark
@app.route('/<userkey>/adding', methods=['GET', 'POST']) @app.route('/<user_key>/adding', methods=['GET', 'POST'])
# @app.route('/<userkey>/adding') # @app.route('/<userkey>/adding')
def adding_bookmark(request: Request, user_key): def adding_bookmark(request: Request, user_key):
"""Add the bookmark from form submit by /add.""" """Add the bookmark from form submit by /add."""
@@ -730,68 +730,68 @@ def adding_bookmark(request: Request, user_key):
return RedirectResponse(request.url_for('addbookmark', userkey=user_key, tags=tags)) return RedirectResponse(request.url_for('addbookmark', userkey=user_key, tags=tags))
@app.route('/<userkey>/<urlhash>/editing', methods=['GET', 'POST']) @app.route('/<user_key>/<url_hash>/editing', methods=['GET', 'POST'])
def editing_bookmark(request: Request, userkey, urlhash): def editing_bookmark(request: Request, user_key: str, url_hash: str):
"""Edit the bookmark from form submit.""" """Edit the bookmark from form submit."""
if request.method == 'POST': if request.method == 'POST':
bookmark = update_bookmark(request, userkey, urlhash=urlhash) bookmark = update_bookmark(request, user_key, url_hash=url_hash)
all_tags[userkey] = get_tags_for_user(userkey) all_tags[user_key] = get_tags_for_user(user_key)
return RedirectResponse(request.url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash)) return RedirectResponse(request.url_for('editbookmark', userkey=user_key, urlhash=bookmark.url_hash))
return RedirectResponse(request.url_for('editbookmark', userkey=userkey, urlhash=urlhash)) return RedirectResponse(request.url_for('editbookmark', userkey=user_key, urlhash=url_hash))
@app.route('/<userkey>/<urlhash>/delete', methods=['GET', 'POST']) @app.route('/<user_key>/<url_hash>/delete', methods=['GET', 'POST'])
def deleting_bookmark(request: Request, userkey, urlhash): def deleting_bookmark(request: Request, user_key: str, url_hash: str):
"""Delete the bookmark from form submit by <urlhash>/delete.""" """Delete the bookmark from form submit by <urlhash>/delete."""
query = Bookmark.update(status=Bookmark.DELETED).where(Bookmark.userkey == userkey, Bookmark.url_hash == urlhash) query = Bookmark.update(status=Bookmark.DELETED).where(Bookmark.userkey == user_key, Bookmark.url_hash == url_hash)
query.execute() query.execute()
query = Bookmark.update(deleted_date=datetime.datetime.now()).where( query = Bookmark.update(deleted_date=datetime.datetime.now()).where(
Bookmark.userkey == userkey, Bookmark.url_hash == urlhash Bookmark.userkey == user_key, Bookmark.url_hash == url_hash
) )
query.execute() query.execute()
message = 'Bookmark deleted. <a href="{}">Undo deletion</a>'.format( message = 'Bookmark deleted. <a href="{}">Undo deletion</a>'.format(
request.url_for('undeletebookmark', userkey=userkey, urlhash=urlhash) request.url_for('undeletebookmark', userkey=user_key, urlhash=url_hash)
) )
all_tags[userkey] = get_tags_for_user(userkey) all_tags[user_key] = get_tags_for_user(user_key)
return RedirectResponse(request.url_for('bookmarks_page', userkey=userkey, message=message)) return RedirectResponse(request.url_for('bookmarks_page', userkey=user_key, message=message))
@app.get('/<userkey>/<urlhash>/undelete') @app.get('/<user_key>/<url_hash>/undelete')
def undelete_bookmark(request: Request, userkey, urlhash): def undelete_bookmark(request: Request, user_key: str, url_hash: str):
"""Undo deletion of the bookmark identified by urlhash.""" """Undo deletion of the bookmark identified by urlhash."""
query = Bookmark.update(status=Bookmark.VISIBLE).where(Bookmark.userkey == userkey, Bookmark.url_hash == urlhash) query = Bookmark.update(status=Bookmark.VISIBLE).where(Bookmark.userkey == user_key, Bookmark.url_hash == url_hash)
query.execute() query.execute()
message = 'Bookmark restored' message = 'Bookmark restored'
all_tags[userkey] = get_tags_for_user(userkey) all_tags[user_key] = get_tags_for_user(user_key)
return RedirectResponse(request.url_for('bookmarks_page', userkey=userkey, message=message)) return RedirectResponse(request.url_for('bookmarks_page', userkey=user_key, message=message))
@app.get('/<userkey>/tags', response_class=HTMLResponse) @app.get('/<user_key>/tags', response_class=HTMLResponse)
def tags_page(userkey): def tags_page(user_key):
"""Overview of all tags used by user.""" """Overview of all tags used by user."""
tags = get_cached_tags(userkey) tags = get_cached_tags(user_key)
# public_tags = PublicTag.select().where(Bookmark.userkey == userkey) # public_tags = PublicTag.select().where(Bookmark.userkey == userkey)
alltags = [] alltags = []
for tag in tags: for tag in tags:
try: try:
public_tag = PublicTag.get(PublicTag.userkey == userkey, PublicTag.tag == tag) public_tag = PublicTag.get(PublicTag.userkey == user_key, PublicTag.tag == tag)
except PublicTag.DoesNotExist: except PublicTag.DoesNotExist:
public_tag = None public_tag = None
total = ( total = (
Bookmark.select() Bookmark.select()
.where(Bookmark.userkey == userkey, Bookmark.tags.contains(tag), Bookmark.status == Bookmark.VISIBLE) .where(Bookmark.userkey == user_key, Bookmark.tags.contains(tag), Bookmark.status == Bookmark.VISIBLE)
.count() .count()
) )
alltags.append({'tag': tag, 'public_tag': public_tag, 'total': total}) alltags.append({'tag': tag, 'public_tag': public_tag, 'total': total})
totaltags = len(alltags) totaltags = len(alltags)
totalbookmarks = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.status == Bookmark.VISIBLE).count() totalbookmarks = Bookmark.select().where(Bookmark.userkey == user_key, Bookmark.status == Bookmark.VISIBLE).count()
totalpublic = PublicTag.select().where(PublicTag.userkey == userkey).count() totalpublic = PublicTag.select().where(PublicTag.userkey == user_key).count()
totalstarred = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.starred).count() totalstarred = Bookmark.select().where(Bookmark.userkey == user_key, Bookmark.starred).count()
totaldeleted = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.status == Bookmark.DELETED).count() totaldeleted = Bookmark.select().where(Bookmark.userkey == user_key, Bookmark.status == Bookmark.DELETED).count()
totalnotes = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.note != '').count() totalnotes = Bookmark.select().where(Bookmark.userkey == user_key, Bookmark.note != '').count()
totalhttperrorstatus = Bookmark.select().where(Bookmark.userkey == userkey, Bookmark.http_status != 200).count() totalhttperrorstatus = Bookmark.select().where(Bookmark.userkey == user_key, Bookmark.http_status != 200).count()
theme = get_theme(userkey) theme = get_theme(user_key)
return templates.TemplateResponse( return templates.TemplateResponse(
'tags.html', 'tags.html',
tags=alltags, tags=alltags,
@@ -802,33 +802,33 @@ def tags_page(userkey):
totalstarred=totalstarred, totalstarred=totalstarred,
totalhttperrorstatus=totalhttperrorstatus, totalhttperrorstatus=totalhttperrorstatus,
totalnotes=totalnotes, totalnotes=totalnotes,
userkey=userkey, userkey=user_key,
theme=theme, theme=theme,
) )
@app.get('/<userkey>/tag/<tag>', response_class=HTMLResponse) @app.get('/<user_key>/tag/<tag>', response_class=HTMLResponse)
def tag_page(request: Request, userkey, tag): def tag_page(request: Request, user_key: str, tag: str):
"""Overview of all bookmarks with a certain tag.""" """Overview of all bookmarks with a certain tag."""
bookmarks = ( bookmarks = (
Bookmark.select() Bookmark.select()
.where(Bookmark.userkey == userkey, Bookmark.tags.contains(tag), Bookmark.status == Bookmark.VISIBLE) .where(Bookmark.userkey == user_key, Bookmark.tags.contains(tag), Bookmark.status == Bookmark.VISIBLE)
.order_by(Bookmark.created_date.desc()) .order_by(Bookmark.created_date.desc())
) )
tags = get_cached_tags(userkey) tags = get_cached_tags(user_key)
pageheader = 'tag: ' + tag pageheader = 'tag: ' + tag
message = request.args.get('message') message = request.args.get('message')
try: try:
public_tag = PublicTag.get(PublicTag.userkey == userkey, PublicTag.tag == tag) public_tag = PublicTag.get(PublicTag.userkey == user_key, PublicTag.tag == tag)
except PublicTag.DoesNotExist: except PublicTag.DoesNotExist:
public_tag = None public_tag = None
theme = get_theme(userkey) theme = get_theme(user_key)
return templates.TemplateResponse( return templates.TemplateResponse(
'bookmarks.html', 'bookmarks.html',
bookmarks=bookmarks, bookmarks=bookmarks,
userkey=userkey, userkey=user_key,
tags=tags, tags=tags,
tag=tag, tag=tag,
public_tag=public_tag, public_tag=public_tag,
@@ -840,9 +840,9 @@ def tag_page(request: Request, userkey, tag):
) )
def get_public_tag(tagkey): def get_public_tag(tag_key):
"""Return tag and bookmarks in this public tag collection.""" """Return tag and bookmarks in this public tag collection."""
this_tag = PublicTag.get(PublicTag.tagkey == tagkey) this_tag = PublicTag.get(PublicTag.tagkey == tag_key)
bookmarks = ( bookmarks = (
Bookmark.select() Bookmark.select()
.where( .where(
@@ -855,12 +855,12 @@ def get_public_tag(tagkey):
return this_tag, bookmarks return this_tag, bookmarks
@app.get('/pub/<tagkey>', response_class=HTMLResponse) @app.get('/pub/<tag_key>', response_class=HTMLResponse)
def public_tag_page(tagkey): def public_tag_page(tag_key):
"""Read-only overview of the bookmarks in the userkey/tag of this PublicTag.""" """Read-only overview of the bookmarks in the user_key/tag of this PublicTag."""
# this_tag = get_object_or_404(PublicTag.select().where(PublicTag.tagkey == tagkey)) # this_tag = get_object_or_404(PublicTag.select().where(PublicTag.tagkey == tagkey))
try: try:
this_tag, bookmarks = get_public_tag(tagkey) this_tag, bookmarks = get_public_tag(tag_key)
# theme = themes[DEFAULT_THEME] # theme = themes[DEFAULT_THEME]
theme = {} theme = {}
return templates.TemplateResponse( return templates.TemplateResponse(
@@ -868,21 +868,21 @@ def public_tag_page(tagkey):
bookmarks=bookmarks, bookmarks=bookmarks,
tag=this_tag.tag, tag=this_tag.tag,
action=this_tag.tag, action=this_tag.tag,
tagkey=tagkey, tagkey=tag_key,
theme=theme, theme=theme,
) )
except PublicTag.DoesNotExist: except PublicTag.DoesNotExist:
raise HTTPException(status_code=404, detail='Public tag not found') raise HTTPException(status_code=404, detail='Public tag not found')
@app.route('/api/v1/pub/<tagkey>') @app.route('/api/v1/pub/<tag_key>')
def public_tag_json(tagkey): def public_tag_json(tag_key):
"""Json representation of the Read-only overview of the bookmarks in the userkey/tag of this PublicTag.""" """Json representation of the Read-only overview of the bookmarks in the user_key/tag of this PublicTag."""
try: try:
this_tag, bookmarks = get_public_tag(tagkey) this_tag, bookmarks = get_public_tag(tag_key)
result = { result = {
# 'tag': this_tag, # 'tag': this_tag,
'tagkey': tagkey, 'tagkey': tag_key,
'count': len(bookmarks), 'count': len(bookmarks),
'items': [], 'items': [],
} }
@@ -893,11 +893,11 @@ def public_tag_json(tagkey):
raise HTTPException(status_code=404, detail='Public tag not found') raise HTTPException(status_code=404, detail='Public tag not found')
@app.get('/pub/<tagkey>/feed') @app.get('/pub/<tag_key>/feed')
async def public_tag_feed(request: Request, tagkey: str): async def public_tag_feed(request: Request, tag_key: str):
"""rss/atom representation of the Read-only overview of the bookmarks in the userkey/tag of this PublicTag.""" """rss/atom representation of the Read-only overview of the bookmarks in the user_key/tag of this PublicTag."""
try: try:
this_tag = PublicTag.get(PublicTag.tagkey == tagkey) this_tag = PublicTag.get(PublicTag.tagkey == tag_key)
bookmarks = Bookmark.select().where( bookmarks = Bookmark.select().where(
Bookmark.userkey == this_tag.userkey, Bookmark.userkey == this_tag.userkey,
Bookmark.tags.contains(this_tag.tag), Bookmark.tags.contains(this_tag.tag),
@@ -908,7 +908,7 @@ async def public_tag_feed(request: Request, tagkey: str):
feed.title(this_tag.tag) feed.title(this_tag.tag)
feed.id(request.url) feed.id(request.url)
feed.link(href=request.url, rel='self') feed.link(href=request.url, rel='self')
feed.link(href=make_external(request, app.url_path_for('public_tag_page', tagkey=tagkey))) feed.link(href=make_external(request, app.url_path_for('public_tag_page', tagkey=tag_key)))
for bookmark in bookmarks: for bookmark in bookmarks:
entry = feed.add_entry() entry = feed.add_entry()
@@ -936,21 +936,21 @@ async def public_tag_feed(request: Request, tagkey: str):
raise HTTPException(status_code=404, detail='Tag not found') raise HTTPException(status_code=404, detail='Tag not found')
@app.get('/<userkey>/<tag>/makepublic') @app.get('/<user_key>/<tag>/makepublic')
@app.post('/<userkey>/<tag>/makepublic') @app.post('/<user_key>/<tag>/makepublic')
async def add_public_tag(userkey: str, tag: str): async def add_public_tag(user_key: str, tag: str):
try: try:
User.get(User.key == userkey) User.get(User.key == user_key)
except User.DoesNotExist: except User.DoesNotExist:
raise HTTPException(status_code=404, detail='User not found') raise HTTPException(status_code=404, detail='User not found')
try: try:
public_tag = PublicTag.get(PublicTag.userkey == userkey, PublicTag.tag == tag) public_tag = PublicTag.get(PublicTag.userkey == user_key, PublicTag.tag == tag)
except PublicTag.DoesNotExist: except PublicTag.DoesNotExist:
public_tag = None public_tag = None
if not public_tag: if not public_tag:
new_public_tag = PublicTag() new_public_tag = PublicTag()
new_public_tag.generate_key() new_public_tag.generate_key()
new_public_tag.userkey = userkey new_public_tag.userkey = user_key
new_public_tag.tag = tag new_public_tag.tag = tag
new_public_tag.save() new_public_tag.save()
@@ -961,23 +961,23 @@ async def add_public_tag(userkey: str, tag: str):
message = 'Public link already existed' message = 'Public link already existed'
success = False success = False
# return redirect(url_for('tag_page', userkey=userkey, tag=tag, message=message)) # return redirect(url_for('tag_page', userkey=userkey, tag=tag, message=message))
url = app.url_path_for('tag_page', userkey=userkey, tag=tag, message=message) url = app.url_path_for('tag_page', userkey=user_key, tag=tag, message=message)
return {'success': success, 'message': message, 'url': url} return {'success': success, 'message': message, 'url': url}
@app.route('/<userkey>/<tag>/removepublic/<tagkey>', methods=['GET', 'POST']) @app.route('/<user_key>/<tag>/removepublic/<tag_key>', methods=['GET', 'POST'])
def remove_public_tag(request: Request, userkey, tag, tagkey): def remove_public_tag(request: Request, user_key, tag, tag_key: str):
q = PublicTag.delete().where(PublicTag.userkey == userkey, PublicTag.tag == tag, PublicTag.tagkey == tagkey) q = PublicTag.delete().where(PublicTag.userkey == user_key, PublicTag.tag == tag, PublicTag.tagkey == tag_key)
q.execute() q.execute()
message = f'Public link {tagkey} has been deleted' message = f'Public link {tag_key} has been deleted'
url = request.url_for('tag_page', userkey=userkey, tag=tag) url = request.url_for('tag_page', userkey=user_key, tag=tag)
return {'message': message, 'url': url} return {'message': message, 'url': url}
@app.route('/<systemkey>/adduser') @app.route('/<system_key>/adduser')
def add_user(systemkey): def add_user(system_key):
"""Add user endpoint, convenience.""" """Add user endpoint, convenience."""
if systemkey == settings.SYSTEMKEY: if system_key == settings.system_key:
new_user = User() new_user = User()
new_user.generate_key() new_user.generate_key()
new_user.username = 'Nomen Nescio' new_user.username = 'Nomen Nescio'
@@ -987,10 +987,10 @@ def add_user(systemkey):
raise HTTPException(status_code=404, detail='I can\'t let you do that Dave') raise HTTPException(status_code=404, detail='I can\'t let you do that Dave')
@app.route('/<systemkey>/refreshfavicons') @app.route('/<system_key>/refreshfavicons')
def refresh_favicons(systemkey): def refresh_favicons(system_key):
"""Add user endpoint, convenience.""" """Add user endpoint, convenience."""
if systemkey == settings.SYSTEMKEY: if system_key == settings.system_key:
bookmarks = Bookmark.select() bookmarks = Bookmark.select()
for bookmark in bookmarks: for bookmark in bookmarks:
if bookmark.favicon: if bookmark.favicon:
@@ -1004,10 +1004,10 @@ def refresh_favicons(systemkey):
raise HTTPException(status_code=404, detail='I can\'t let you do that Dave') raise HTTPException(status_code=404, detail='I can\'t let you do that Dave')
@app.route('/<systemkey>/findmissingfavicons') @app.route('/<system_key>/findmissingfavicons')
def find_missing_favicons(request: Request, systemkey: str): def find_missing_favicons(request: Request, system_key: str):
"""Add user endpoint, convenience.""" """Add user endpoint, convenience."""
if systemkey == settings.SYSTEMKEY: if system_key == settings.system_key:
bookmarks = Bookmark.select() bookmarks = Bookmark.select()
for bookmark in bookmarks: for bookmark in bookmarks:
try: try: