1
0
mirror of https://github.com/aquatix/digimarks.git synced 2025-12-07 03:45:10 +01:00

Refactoring to fastapi, reformatting with ruff

This commit is contained in:
2023-10-30 21:51:55 +01:00
parent 30bd835e41
commit f6f129d67c
3 changed files with 221 additions and 190 deletions

View File

@@ -59,5 +59,14 @@ select = [
"W", "W",
] ]
[tool.ruff.flake8-quotes]
docstring-quotes = "double"
inline-quotes = "single"
multiline-quotes = "double"
[tool.ruff.format]
# Prefer single quotes over double quotes
quote-style = "single"
[tool.ruff.mccabe] [tool.ruff.mccabe]
max-complexity = 10 max-complexity = 10

View File

@@ -1,6 +1,6 @@
-r requirements.in -r requirements.in
black # black
pylint pylint
ruff ruff

View File

@@ -5,26 +5,27 @@ import hashlib
import logging import logging
import os import os
import shutil import shutil
from typing import Optional
from urllib.parse import urljoin, urlparse, urlunparse from urllib.parse import urljoin, urlparse, urlunparse
import bs4 import bs4
import requests import requests
from dateutil import tz from dateutil import tz
# from flask import (Flask, abort, jsonify, make_response, redirect, # from flask import (Flask, abort, jsonify, make_response, redirect,
# render_template, request, url_for) # render_template, request, url_for)
from fastapi import Depends, FastAPI, HTTPException, Request, Response from fastapi import FastAPI, HTTPException, Request, Response
from fastapi.responses import RedirectResponse from fastapi.responses import RedirectResponse
from fastapi.responses import HTMLResponse from fastapi.responses import HTMLResponse
from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware
from fastapi.templating import Jinja2Templates from fastapi.templating import Jinja2Templates
from feedgen.feed import FeedGenerator from feedgen.feed import FeedGenerator
from pydantic import DirectoryPath, FilePath, validator from pydantic import DirectoryPath, FilePath
from pydantic_settings import BaseSettings from pydantic_settings import BaseSettings
from sqlalchemy import VARCHAR, Boolean, Column, DateTime, ForeignKey, Integer, String, Text, create_engine from sqlalchemy import VARCHAR, Boolean, Column, DateTime, Integer, Text, create_engine
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker from sqlalchemy.orm import sessionmaker, Mapped
DIGIMARKS_USER_AGENT = 'digimarks/2.0.0-dev' DIGIMARKS_USER_AGENT = 'digimarks/2.0.0-dev'
@@ -34,7 +35,8 @@ DEFAULT_THEME = 'freshgreen'
class Settings(BaseSettings): class Settings(BaseSettings):
"""Configuration needed for digimarks to find its database, favicons, API integrations""" """Configuration needed for digimarks to find its database, favicons, API integrations"""
database_file: FilePath = './bookmarks.db' # database_file: FilePath = './bookmarks.db'
database_file: FilePath
media_dir: DirectoryPath media_dir: DirectoryPath
media_url: str = '/static/' media_url: str = '/static/'
@@ -44,17 +46,16 @@ class Settings(BaseSettings):
settings = Settings() settings = Settings()
print(settings.model_dump())
engine = create_engine( engine = create_engine(f'sqlite:///{settings.database_file}', connect_args={'check_same_thread': False})
f'sqlite:///{settings.database_file}', connect_args={'check_same_thread': False}
)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine) SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
Base = declarative_base() Base = declarative_base()
app = FastAPI() app = FastAPI()
templates = Jinja2Templates(directory="templates") templates = Jinja2Templates(directory='templates')
logger = logging.getLogger('digimarks') logger = logging.getLogger('digimarks')
if settings.debug: if settings.debug:
@@ -63,10 +64,10 @@ if settings.debug:
# CORS configuration # CORS configuration
app.add_middleware( app.add_middleware(
CORSMiddleware, CORSMiddleware,
allow_origins=["*"], # Allow requests from everywhere allow_origins=['*'], # Allow requests from everywhere
allow_credentials=True, allow_credentials=True,
allow_methods=["*"], allow_methods=['*'],
allow_headers=["*"], allow_headers=['*'],
) )
@@ -113,25 +114,28 @@ def clean_tags(tags_list):
return tags_res return tags_res
magic_dict = { def file_type(filename):
b"\x1f\x8b\x08": "gz", """Try to determine the file type for the file in `filename`.
b"\x42\x5a\x68": "bz2",
b"\x50\x4b\x03\x04": "zip" :param str filename: path to file to check
} :return: zip file type
:rtype: str
"""
magic_dict = {b'\x1f\x8b\x08': 'gz', b'\x42\x5a\x68': 'bz2', b'\x50\x4b\x03\x04': 'zip'}
max_len = max(len(x) for x in magic_dict) max_len = max(len(x) for x in magic_dict)
def file_type(filename): with open(filename, 'rb') as f:
with open(filename, "rb") as f:
file_start = f.read(max_len) file_start = f.read(max_len)
for magic, filetype in magic_dict.items(): for magic, filetype in magic_dict.items():
if file_start.startswith(magic): if file_start.startswith(magic):
return filetype return filetype
return "no match" return 'no match'
class User(Base): class User(Base):
"""User account""" """User account"""
__tablename__ = 'user' __tablename__ = 'user'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
@@ -149,6 +153,7 @@ class User(Base):
class Bookmark(Base): class Bookmark(Base):
"""Bookmark instance, connected to User""" """Bookmark instance, connected to User"""
__tablename__ = 'bookmark' __tablename__ = 'bookmark'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
@@ -164,7 +169,9 @@ class Bookmark(Base):
starred = Column(Boolean, default=False) starred = Column(Boolean, default=False)
# Website (domain) favicon # Website (domain) favicon
favicon = Column(VARCHAR(255), null=True) # favicon = Column(VARCHAR(255), null=True)
# favicon = Column(VARCHAR(255))
favicon: Mapped[Optional[str]]
# Status code: 200 is OK, 404 is not found, for example (showing an error) # Status code: 200 is OK, 404 is not found, for example (showing an error)
HTTP_CONNECTIONERROR = 0 HTTP_CONNECTIONERROR = 0
@@ -177,15 +184,16 @@ class Bookmark(Base):
redirect_uri = None redirect_uri = None
created_date = Column(DateTime, default=datetime.datetime.now) created_date = Column(DateTime, default=datetime.datetime.now)
modified_date = Column(DateTime, null=True) # modified_date = Column(DateTime, null=True)
deleted_date = Column(DateTime, null=True) modified_date: Mapped[Optional[datetime.datetime]]
# deleted_date = Column(DateTime, null=True)
deleted_date: Mapped[Optional[datetime.datetime]]
# Bookmark status; deleting doesn't remove from DB # Bookmark status; deleting doesn't remove from DB
VISIBLE = 0 VISIBLE = 0
DELETED = 1 DELETED = 1
status = Column(Integer, default=VISIBLE) status = Column(Integer, default=VISIBLE)
class Meta: class Meta:
ordering = (('created_date', 'desc'),) ordering = (('created_date', 'desc'),)
@@ -225,7 +233,7 @@ class Bookmark(Base):
'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,
headers={'User-Agent': DIGIMARKS_USER_AGENT}, headers={'User-Agent': DIGIMARKS_USER_AGENT},
timeout=15 timeout=15,
) )
if meta.url[-3:].lower() == 'ico': if meta.url[-3:].lower() == 'ico':
fileextension = '.ico' fileextension = '.ico'
@@ -233,7 +241,7 @@ class Bookmark(Base):
'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 + fileextension)
with open(filename, 'wb') as out_file: with open(filename, 'wb') as out_file:
@@ -255,14 +263,14 @@ class Bookmark(Base):
response = requests.get( response = requests.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 = requests.get( response = requests.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)
@@ -356,6 +364,7 @@ class Bookmark(Base):
class PublicTag(Base): class PublicTag(Base):
"""Publicly shared tag""" """Publicly shared tag"""
__tablename__ = 'publictag' __tablename__ = 'publictag'
id = Column(Integer, primary_key=True) id = Column(Integer, primary_key=True)
@@ -401,15 +410,19 @@ def make_external(request: Request, url):
def _find_bookmarks(userkey, filter_text) -> list[Bookmark]: def _find_bookmarks(userkey, 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 Bookmark.select().where( return (
Bookmark.select()
.where(
Bookmark.userkey == userkey, Bookmark.userkey == userkey,
( (
Bookmark.title.contains(filter_text) | Bookmark.title.contains(filter_text)
Bookmark.url.contains(filter_text) | | Bookmark.url.contains(filter_text)
Bookmark.note.contains(filter_text) | Bookmark.note.contains(filter_text)
), ),
Bookmark.status == Bookmark.VISIBLE Bookmark.status == Bookmark.VISIBLE,
).order_by(Bookmark.created_date.desc()) )
.order_by(Bookmark.created_date.desc())
)
# @app.errorhandler(404) # @app.errorhandler(404)
@@ -426,7 +439,7 @@ def index():
return {} return {}
def get_bookmarks(request: Request, userkey, filtermethod=None, sortmethod=None): def get_bookmarks(request: Request, user_key, filter_method=None, sort_method=None):
"""User homepage, list their bookmarks, optionally filtered and/or sorted""" """User homepage, list their bookmarks, optionally filtered and/or sorted"""
# return object_list('bookmarks.html', Bookmark.select()) # return object_list('bookmarks.html', Bookmark.select())
# user = User.select(key=userkey) # user = User.select(key=userkey)
@@ -436,77 +449,88 @@ def get_bookmarks(request: Request, userkey, filtermethod=None, sortmethod=None)
# else: # else:
# abort(404) # abort(404)
message = request.args.get('message') message = request.args.get('message')
bookmarktags = get_cached_tags(userkey) bookmarktags = get_cached_tags(user_key)
filter_text = '' filter_text = ''
if request.form: if request.form:
filter_text = request.form['filter_text'] filter_text = request.form['filter_text']
filter_starred = False filter_starred = False
if filtermethod and filtermethod.lower() == 'starred': if filter_method and filter_method.lower() == 'starred':
filter_starred = True filter_starred = True
filter_broken = False filter_broken = False
if filtermethod and filtermethod.lower() == 'broken': if filter_method and filter_method.lower() == 'broken':
filter_broken = True filter_broken = True
filter_note = False filter_note = False
if filtermethod and filtermethod.lower() == 'note': if filter_method and filter_method.lower() == 'note':
filter_note = True filter_note = True
if filter_text: if filter_text:
bookmarks = _find_bookmarks(userkey, filter_text) bookmarks = _find_bookmarks(user_key, filter_text)
elif filter_starred: elif filter_starred:
bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, bookmarks = (
Bookmark.starred).order_by(Bookmark.created_date.desc()) Bookmark.select()
.where(Bookmark.userkey == user_key, Bookmark.starred)
.order_by(Bookmark.created_date.desc())
)
elif filter_broken: elif filter_broken:
bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, bookmarks = (
Bookmark.http_status != 200).order_by(Bookmark.created_date.desc()) Bookmark.select()
.where(Bookmark.userkey == user_key, Bookmark.http_status != 200)
.order_by(Bookmark.created_date.desc())
)
elif filter_note: elif filter_note:
bookmarks = Bookmark.select().where(Bookmark.userkey == userkey, bookmarks = (
Bookmark.note != '').order_by(Bookmark.created_date.desc()) Bookmark.select()
.where(Bookmark.userkey == user_key, Bookmark.note != '')
.order_by(Bookmark.created_date.desc())
)
else: else:
bookmarks = Bookmark.select().where( bookmarks = (
Bookmark.userkey == userkey, Bookmark.select()
Bookmark.status == Bookmark.VISIBLE .where(Bookmark.userkey == user_key, Bookmark.status == Bookmark.VISIBLE)
).order_by(Bookmark.created_date.desc()) .order_by(Bookmark.created_date.desc())
)
return bookmarks, bookmarktags, filter_text, message return bookmarks, bookmarktags, filter_text, message
@app.get('/<userkey>', response_class=HTMLResponse) @app.get('/{user_key}', response_class=HTMLResponse)
@app.post('/<userkey>', response_class=HTMLResponse) @app.post('/{user_key}', response_class=HTMLResponse)
@app.route('/<userkey>/filter/<filtermethod>', methods=['GET', 'POST']) @app.route('/<user_key>/filter/<filtermethod>', methods=['GET', 'POST'])
@app.route('/<userkey>/sort/<sortmethod>', methods=['GET', 'POST']) @app.route('/<user_key>/sort/<sortmethod>', methods=['GET', 'POST'])
@app.route('/<userkey>/<show_as>', methods=['GET', 'POST']) @app.route('/<user_key>/<show_as>', methods=['GET', 'POST'])
@app.route('/<userkey>/<show_as>/filter/<filtermethod>', methods=['GET', 'POST']) @app.route('/<user_key>/<show_as>/filter/<filtermethod>', methods=['GET', 'POST'])
@app.route('/<userkey>/<show_as>/sort/<sortmethod>', methods=['GET', 'POST']) @app.route('/<user_key>/<show_as>/sort/<sortmethod>', methods=['GET', 'POST'])
def bookmarks_page(request: Request, userkey, filtermethod=None, sortmethod=None, show_as='cards'): def bookmarks_page(request: Request, user_key, filter_method=None, sort_method=None, show_as='cards'):
bookmarks, bookmarktags, filter_text, message = get_bookmarks(request, userkey, filtermethod, sortmethod) bookmarks, bookmarktags, filter_text, message = get_bookmarks(request, user_key, filter_method, sort_method)
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=bookmarktags, tags=bookmarktags,
filter_text=filter_text, filter_text=filter_text,
message=message, message=message,
theme=theme, theme=theme,
editable=True, # bookmarks can be edited editable=True, # bookmarks can be edited
showtags=True, # tags should be shown with the bookmarks showtags=True, # tags should be shown with the bookmarks
filtermethod=filtermethod, filtermethod=filter_method,
sortmethod=sortmethod, sortmethod=sort_method,
show_as=show_as, # show list of bookmarks instead of cards show_as=show_as, # show list of bookmarks instead of cards
) )
@app.get('/<userkey>/js') @app.get('/{user_key}/js')
def bookmarks_js(userkey): def bookmarks_js(user_key):
"""Return list of bookmarks with their favicons, to be used for autocompletion""" """Return list of bookmarks with their favicons, to be used for autocompletion"""
bookmarks = Bookmark.select().where( bookmarks = (
Bookmark.userkey == userkey, Bookmark.select()
Bookmark.status == Bookmark.VISIBLE .where(Bookmark.userkey == user_key, Bookmark.status == Bookmark.VISIBLE)
).order_by(Bookmark.created_date.desc()) .order_by(Bookmark.created_date.desc())
)
result = [] result = []
for bookmark in bookmarks: for bookmark in bookmarks:
result.append({'title': bookmark.title}) result.append({'title': bookmark.title})
@@ -525,9 +549,7 @@ def bookmark_redirect(userkey, urlhash):
# @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.url_hash == urlhash, Bookmark.userkey == userkey, Bookmark.status == Bookmark.VISIBLE
Bookmark.userkey == userkey,
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')
@@ -557,14 +579,13 @@ def bookmark_json(userkey, urlhash):
"""Serialise bookmark to json""" """Serialise bookmark to json"""
try: try:
bookmark = Bookmark.get( bookmark = Bookmark.get(
Bookmark.url_hash == urlhash, Bookmark.url_hash == urlhash, Bookmark.userkey == userkey, Bookmark.status == Bookmark.VISIBLE
Bookmark.userkey == userkey,
Bookmark.status == Bookmark.VISIBLE
) )
return bookmark.to_dict() return bookmark.to_dict()
except Bookmark.DoesNotExist: except Bookmark.DoesNotExist:
raise HTTPException(status_code=404, detail='Bookmark not found') raise HTTPException(status_code=404, detail='Bookmark not found')
@app.route('/api/v1/<userkey>/search/<filter_text>') @app.route('/api/v1/<userkey>/search/<filter_text>')
def search_bookmark_titles_json(userkey, filter_text): def search_bookmark_titles_json(userkey, filter_text):
"""Serialise bookmark to json""" """Serialise bookmark to json"""
@@ -598,7 +619,7 @@ def editbookmark(request: Request, userkey, urlhash):
message=message, message=message,
formaction='edit', formaction='edit',
tags=tags, tags=tags,
theme=theme theme=theme,
) )
@@ -615,13 +636,7 @@ def addbookmark(request: Request, userkey):
tags = get_cached_tags(userkey) tags = get_cached_tags(userkey)
theme = get_theme(userkey) theme = get_theme(userkey)
return templates.TemplateResponse( return templates.TemplateResponse(
'edit.html', 'edit.html', action='Add bookmark', userkey=userkey, bookmark=bookmark, tags=tags, message=message, theme=theme
action='Add bookmark',
userkey=userkey,
bookmark=bookmark,
tags=tags,
message=message,
theme=theme
) )
@@ -643,7 +658,9 @@ def updatebookmark(request: Request, userkey, urlhash=None):
bookmark, created = Bookmark.get_or_create(url=url, userkey=userkey) bookmark, created = Bookmark.get_or_create(url=url, userkey=userkey)
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(request.url_for('editbookmark', userkey=userkey, urlhash=bookmark.url_hash, message=message)) return RedirectResponse(
request.url_for('editbookmark', userkey=userkey, 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 == userkey, Bookmark.url_hash == urlhash)
@@ -681,25 +698,26 @@ def updatebookmark(request: Request, userkey, urlhash=None):
@app.route('/<userkey>/adding', methods=['GET', 'POST']) @app.route('/<userkey>/adding', methods=['GET', 'POST'])
# @app.route('/<userkey>/adding') # @app.route('/<userkey>/adding')
def addingbookmark(request: Request, userkey): def adding_bookmark(request: Request, user_key):
"""Add the bookmark from form submit by /add""" """Add the bookmark from form submit by /add"""
tags = get_cached_tags(userkey) tags = get_cached_tags(user_key)
if request.method == 'POST': if request.method == 'POST':
bookmark = updatebookmark(request, userkey) bookmark = updatebookmark(request, user_key)
if not bookmark: if not bookmark:
return RedirectResponse(request.url_for('addbookmark', userkey=userkey, message='No url provided', tags=tags)) return RedirectResponse(
request.url_for('addbookmark', userkey=user_key, message='No url provided', tags=tags)
)
if type(bookmark).__name__ == 'Response': if type(bookmark).__name__ == 'Response':
return bookmark return bookmark
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('addbookmark', userkey=userkey, tags=tags)) return RedirectResponse(request.url_for('addbookmark', userkey=user_key, tags=tags))
@app.route('/<userkey>/<urlhash>/editing', methods=['GET', 'POST']) @app.route('/<userkey>/<urlhash>/editing', methods=['GET', 'POST'])
def editingbookmark(request: Request, userkey, urlhash): def editingbookmark(request: Request, userkey, urlhash):
"""Edit the bookmark from form submit""" """Edit the bookmark from form submit"""
if request.method == 'POST': if request.method == 'POST':
bookmark = updatebookmark(request, userkey, urlhash=urlhash) bookmark = updatebookmark(request, userkey, urlhash=urlhash)
all_tags[userkey] = get_tags_for_user(userkey) all_tags[userkey] = get_tags_for_user(userkey)
@@ -713,15 +731,12 @@ def deletingbookmark(request: Request, userkey, urlhash):
query = Bookmark.update(status=Bookmark.DELETED).where(Bookmark.userkey == userkey, Bookmark.url_hash == urlhash) query = Bookmark.update(status=Bookmark.DELETED).where(Bookmark.userkey == userkey, Bookmark.url_hash == urlhash)
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.userkey == userkey, Bookmark.url_hash == urlhash
Bookmark.url_hash == urlhash
) )
query.execute() query.execute()
message = 'Bookmark deleted. <a href="{}">Undo deletion</a>'.format(request.url_for( message = 'Bookmark deleted. <a href="{}">Undo deletion</a>'.format(
'undeletebookmark', request.url_for('undeletebookmark', userkey=userkey, urlhash=urlhash)
userkey=userkey, )
urlhash=urlhash
))
all_tags[userkey] = get_tags_for_user(userkey) all_tags[userkey] = get_tags_for_user(userkey)
return RedirectResponse(request.url_for('bookmarks_page', userkey=userkey, message=message)) return RedirectResponse(request.url_for('bookmarks_page', userkey=userkey, message=message))
@@ -748,11 +763,11 @@ def tags_page(userkey):
except PublicTag.DoesNotExist: except PublicTag.DoesNotExist:
publictag = None publictag = None
total = Bookmark.select().where( total = (
Bookmark.userkey == userkey, Bookmark.select()
Bookmark.tags.contains(tag), .where(Bookmark.userkey == userkey, Bookmark.tags.contains(tag), Bookmark.status == Bookmark.VISIBLE)
Bookmark.status == Bookmark.VISIBLE .count()
).count() )
alltags.append({'tag': tag, 'publictag': publictag, 'total': total}) alltags.append({'tag': tag, 'publictag': publictag, '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 == userkey, Bookmark.status == Bookmark.VISIBLE).count()
@@ -773,18 +788,18 @@ def tags_page(userkey):
totalhttperrorstatus=totalhttperrorstatus, totalhttperrorstatus=totalhttperrorstatus,
totalnotes=totalnotes, totalnotes=totalnotes,
userkey=userkey, userkey=userkey,
theme=theme theme=theme,
) )
@app.get('/<userkey>/tag/<tag>', response_class=HTMLResponse) @app.get('/<userkey>/tag/<tag>', response_class=HTMLResponse)
def tag_page(request: Request, userkey, tag): def tag_page(request: Request, userkey, tag):
"""Overview of all bookmarks with a certain tag""" """Overview of all bookmarks with a certain tag"""
bookmarks = Bookmark.select().where( bookmarks = (
Bookmark.userkey == userkey, Bookmark.select()
Bookmark.tags.contains(tag), .where(Bookmark.userkey == userkey, Bookmark.tags.contains(tag), Bookmark.status == Bookmark.VISIBLE)
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(userkey)
pageheader = 'tag: ' + tag pageheader = 'tag: ' + tag
message = request.args.get('message') message = request.args.get('message')
@@ -813,11 +828,15 @@ def tag_page(request: Request, userkey, tag):
def get_publictag(tagkey): def get_publictag(tagkey):
"""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 == tagkey)
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),
Bookmark.status == Bookmark.VISIBLE Bookmark.status == Bookmark.VISIBLE,
).order_by(Bookmark.created_date.desc()) )
.order_by(Bookmark.created_date.desc())
)
return this_tag, bookmarks return this_tag, bookmarks
@@ -835,7 +854,7 @@ def publictag_page(tagkey):
tag=this_tag.tag, tag=this_tag.tag,
action=this_tag.tag, action=this_tag.tag,
tagkey=tagkey, tagkey=tagkey,
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')
@@ -843,7 +862,7 @@ def publictag_page(tagkey):
@app.route('/api/v1/pub/<tagkey>') @app.route('/api/v1/pub/<tagkey>')
def publictag_json(tagkey): def publictag_json(tagkey):
""" 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 userkey/tag of this PublicTag"""
try: try:
this_tag, bookmarks = get_publictag(tagkey) this_tag, bookmarks = get_publictag(tagkey)
result = { result = {
@@ -867,7 +886,7 @@ async def publictag_feed(request: Request, tagkey: str):
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),
Bookmark.status == Bookmark.VISIBLE Bookmark.status == Bookmark.VISIBLE,
) )
feed = FeedGenerator() feed = FeedGenerator()
@@ -951,7 +970,7 @@ def adduser(systemkey):
all_tags[newuser.key] = [] all_tags[newuser.key] = []
return {'user': f'/{newuser.key.decode("utf-8")}'} return {'user': f'/{newuser.key.decode("utf-8")}'}
else: else:
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('/<systemkey>/refreshfavicons')
@@ -969,7 +988,7 @@ def refreshfavicons(systemkey):
bookmark.set_favicon() bookmark.set_favicon()
return {'message': 'Done refreshing icons'} return {'message': 'Done refreshing icons'}
else: else:
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('/<systemkey>/findmissingfavicons')
@@ -979,7 +998,9 @@ def findmissingfavicons(systemkey):
bookmarks = Bookmark.select() bookmarks = Bookmark.select()
for bookmark in bookmarks: for bookmark in bookmarks:
try: try:
if not bookmark.favicon or not os.path.isfile(os.path.join(settings.media_dir, 'favicons', bookmark.favicon)): if not bookmark.favicon or not os.path.isfile(
os.path.join(settings.media_dir, 'favicons', bookmark.favicon)
):
# This favicon is missing # This favicon is missing
# Clear favicon, so fallback can be used instead of showing a broken image # Clear favicon, so fallback can be used instead of showing a broken image
bookmark.favicon = None bookmark.favicon = None
@@ -991,22 +1012,23 @@ def findmissingfavicons(systemkey):
print(e) print(e)
return {'message': 'Done finding missing icons'} return {'message': 'Done finding missing icons'}
else: else:
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')
# Initialisation == create the bookmark, user and public tag tables if they do not exist # Initialisation == create the bookmark, user and public tag tables if they do not exist
Bookmark.create_table(True) # TODO: switch to alembic migrations
User.create_table(True) # Bookmark.create_table(True)
PublicTag.create_table(True) # User.create_table(True)
# PublicTag.create_table(True)
users = User.select() # users = User.select()
print('Current user keys:') # print('Current user keys:')
for user in users: # for user in users:
all_tags[user.key] = get_tags_for_user(user.key) # all_tags[user.key] = get_tags_for_user(user.key)
usersettings[user.key] = {'theme': user.theme} # usersettings[user.key] = {'theme': user.theme}
print(user.key) # print(user.key)
# Run when called standalone # Run when called standalone
if __name__ == '__main__': # if __name__ == '__main__':
# run the application # run the application
app.run(host='0.0.0.0', port=9999, debug=True) # app.run(host='0.0.0.0', port=9999, debug=True)