From 78d938e266b0ca7c9c42a2c2d555c04bb8f1eb9a Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Sat, 21 May 2022 01:56:46 +0200 Subject: [PATCH 01/51] Version advance, remove upload table, add backlinks, add (optional) math with markdown_katex (experimental) --- app.py | 137 ++++++++++++++++++++------------------- templates/backlinks.html | 27 ++++++++ templates/history.html | 3 +- templates/view.html | 1 + 4 files changed, 102 insertions(+), 66 deletions(-) create mode 100644 templates/backlinks.html diff --git a/app.py b/app.py index 28bfe8c..918c874 100644 --- a/app.py +++ b/app.py @@ -31,8 +31,12 @@ try: from slugify import slugify except ImportError: slugify = None +try: + import markdown_katex +except ImportError: + markdown_katex = None -__version__ = '0.5' +__version__ = '0.6-dev' #### CONSTANTS #### @@ -41,8 +45,7 @@ APP_BASE_DIR = os.path.dirname(__file__) FK = ForeignKeyField SLUG_RE = r'[a-z0-9]+(?:-[a-z0-9]+)*' -MAGIC_RE = r'\{\{\s*(' + SLUG_RE + ')\s*:\s*(.*?)\s*\}\}' -REDIRECT_RE = r'\{\{\s*redirect\s*:\s*(\d+)\s*\}\}' +ILINK_RE = r'\]\(/(p/\d+|' + SLUG_RE + ')/?\)' upload_types = {'jpeg': 1, 'jpg': 1, 'png': 2} upload_types_rev = {1: 'jpg', 2: 'png'} @@ -139,7 +142,8 @@ class Page(BaseModel): def get_url(self): return '/' + self.url + '/' if self.url else '/p/{}/'.format(self.id) def short_desc(self): - text = remove_tags(self.latest.text, convert = not _getconf('site', 'simple_remove_tags', False)) + full_text = self.latest.text + text = remove_tags(full_text, convert = not '```math' in full_text and not '$`' in full_text and not _getconf('appearance', 'simple_remove_tags', False)) return text[:200] + ('\u2026' if len(text) > 200 else '') def change_tags(self, new_tags): old_tags = set(x.name for x in self.tags) @@ -339,78 +343,71 @@ class PagePolicy(BaseModel): (('page', 'key'), True), ) -# DEPRECATED -# It will be possibly removed in v0.6. -# Use external image URL instead. -class Upload(BaseModel): - name = CharField(256) - url_name = CharField(256, null=True) - filetype = SmallIntegerField() - filesize = IntegerField() - upload_date = DateTimeField(index=True) - md5 = CharField(32, index=True) - @property - def filepath(self): - return '{0}/{1}/{2}{3}.{4}'.format(self.md5[:1], self.md5[:2], self.id, - '-' + self.url_name if self.url_name else '', upload_types_rev[self.filetype]) - @property - def url(self): - return '/media/' + self.filepath - def get_content(self, check=True): - with open(os.path.join(UPLOAD_DIR, self.filepath)) as f: - content = f.read() - if check: - if len(content) != self.filesize: - raise AssertionError('file is corrupted') - if hashlib.md5(content).hexdigest() != self.md5: - raise AssertionError('file is corrupted') - return content - @classmethod - def create_content(cls, name, ext, content): - ext = ext.lstrip('.') - if ext not in upload_types: - raise ValueError('invalid file type') - filetype = upload_types[ext] - name = name[:256] - if slugify: - url_name = slugify(name)[:256] - else: - url_name = None - filemd5 = hashlib.md5(content).hexdigest() - basepath = os.path.join(UPLOAD_DIR, filemd5[:1], filemd5[:2]) - if not os.path.exists(basepath): - os.makedirs(basepath) - obj = cls.create( - name=name, - url_name=url_name, - filetype=filetype, - filesize=len(content), - upload_date=datetime.datetime.now(), - md5=filemd5 + +# Link table for caching purposes. +class PageLink(BaseModel): + from_page = FK(Page, backref='forward_links') + to_page = FK(Page, backref='back_links') + + class Meta: + indexes = ( + (('from_page', 'to_page'), True), ) - try: - with open(os.path.join(basepath, '{0}{1}.{2}'.format(obj.id, - '-' + url_name if url_name else '', upload_types_rev[filetype] - )), 'wb') as f: - f.write(content) - except OSError: - cls.delete_by_id(obj.id) - raise - return obj + + @classmethod + def parse_links(cls, from_page, text, erase=True): + with database.atomic(): + old_links = list(cls.select().where(cls.from_page == from_page)) + for mo in re.finditer(ILINK_RE, text): + try: + pageurl = mo.group(1) + if pageurl.startswith('p/'): + to_page = Page[int(pageurl[2:])] + else: + to_page = Page.get(Page.url == pageurl) + + linkobj, created = PageLink.get_or_create( + from_page = from_page, + to_page = to_page) + if linkobj in old_links: + old_links.remove(linkobj) + except Exception: + continue + if erase: + for linkobj in old_links: + linkobj.delete_instance() + + # The actual ULTIMATE method to refresh all links + # To be called from a maintenance script only! + @classmethod + def refresh_all_links(cls): + for p in Page.select(): + cls.parse_links(p, p.latest.text) + def init_db(): - database.create_tables([Page, PageText, PageRevision, PageTag, PageProperty, PagePolicyKey, PagePolicy, Upload]) + database.create_tables([Page, PageText, PageRevision, PageTag, PageProperty, PagePolicyKey, PagePolicy, PageLink]) #### WIKI SYNTAX #### -def md(text, expand_magic=False, toc=True): +def md(text, expand_magic=False, toc=True, math=True): if expand_magic: # DEPRECATED seeking for a better solution. warnings.warn('Magic words are no more supported.', DeprecationWarning) extensions = ['tables', 'footnotes', 'fenced_code', 'sane_lists', StrikethroughExtension(), SpoilerExtension()] + extension_configs = {} if toc: extensions.append('toc') - return markdown.Markdown(extensions=extensions).convert(text) + if math and markdown_katex and ('$`' in text or '```math' in text): + extensions.append('markdown_katex') + extension_configs['markdown_katex'] = { + 'no_inline_svg': True, + 'insert_fonts_css': True + } + try: + return markdown.Markdown(extensions=extensions, extension_configs=extension_configs).convert(text) + except Exception as e: + return '

There was an error during rendering: {e.__class__.__name__}: {e}

'.format(e=e) def remove_tags(text, convert=True, headings=True): if headings: @@ -457,7 +454,7 @@ forbidden_urls = [ 'create', 'edit', 'p', 'ajax', 'history', 'manage', 'static', 'media', 'accounts', 'tags', 'init-config', 'upload', 'upload-info', 'about', 'stats', 'terms', 'privacy', 'easter', 'search', 'help', 'circles', - 'protect', 'kt', 'embed' + 'protect', 'kt', 'embed', 'backlinks' ] app = Flask(__name__) @@ -573,6 +570,7 @@ def create(): pub_date=datetime.datetime.now(), length=len(request.form['text']) ) + PageLink.parse_links(p, request.form['text']) return redirect(p.get_url()) return render_template('edit.html', pl_url=request.args.get('url')) @@ -609,6 +607,7 @@ def edit(id): pub_date=datetime.datetime.now(), length=len(request.form['text']) ) + PageLink.parse_links(p, request.form['text']) return redirect(p.get_url()) return render_template('edit.html', pl_url=p.url, pl_title=p.title, pl_text=p.latest.text, pl_tags=','.join(x.name for x in p.tags)) @@ -718,6 +717,14 @@ def view_old(revisionid): p = rev.page return render_template('viewold.html', p=p, rev=rev) +@app.route('/backlinks//') +def backlinks(id): + try: + p = Page[id] + except Page.DoesNotExist: + abort(404) + return render_template('backlinks.html', p=p, backlinks=Page.select().join(PageLink, on=PageLink.to_page).where(PageLink.from_page == p)) + @app.route('/search/', methods=['GET', 'POST']) def search(): if request.method == 'POST': diff --git a/templates/backlinks.html b/templates/backlinks.html new file mode 100644 index 0000000..a064af9 --- /dev/null +++ b/templates/backlinks.html @@ -0,0 +1,27 @@ +{% extends "base.html" %} + +{% block title %}Pages linking to “{{ p.title }}” - {{ app_name }}{% endblock %} + +{% block meta %} + +{% endblock %} + +{% block content %} +

{{ p.title }}

+
Backlinks
+ +{% if backlinks %} + +{% else %} +

No other pages linking here. Is this page orphan?

+{% endif %} + +

Back to {{ p.title }}.

+{% endblock %} + diff --git a/templates/history.html b/templates/history.html index c07c228..9eb6552 100644 --- a/templates/history.html +++ b/templates/history.html @@ -7,7 +7,8 @@ {% endblock %} {% block content %} -

Page history for "{{ p.title }}"

+

{{ p.title }}

+
Page history
-

{{ T('no-account-sign-up') }} {{ T("sign-up") }}

+

{{ T('no-account-sign-up') }} {{ T("sign-up") }}

{% endblock %} \ No newline at end of file diff --git a/templates/register.html b/templates/register.html new file mode 100644 index 0000000..4e34b07 --- /dev/null +++ b/templates/register.html @@ -0,0 +1,36 @@ +{% extends "base.html" %} + +{% block title %}{{ T('sign-up') }} – {{ app_name }}{% endblock %} + +{% block content %} +

{{ T('sign-up') }}

+ +
+ +
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ + +
+
+ +
+
+ +

{{ T('already-have-account') }} {{ T("login") }}

+{% endblock %} \ No newline at end of file From f4d536dc4762f20807dbabf45a7fe18af6e1cf77 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Sat, 7 Jan 2023 23:04:06 +0100 Subject: [PATCH 18/51] Improved users, now /create and /edit require user login, changed savepoint() helper --- app.py | 86 +++++++++++++++++++----------- i18n/salvi.en.json | 3 +- i18n/salvi.it.json | 3 +- templates/base.html | 8 ++- templates/edit.html | 24 +++++++-- templates/history.html | 2 + templates/internalservererror.html | 9 ++++ templates/register.html | 6 +-- templates/view.html | 3 +- 9 files changed, 102 insertions(+), 42 deletions(-) create mode 100644 templates/internalservererror.html diff --git a/app.py b/app.py index abf0e80..aadc83e 100644 --- a/app.py +++ b/app.py @@ -210,6 +210,13 @@ class Page(BaseModel): return PagePropertyDict(self) def is_editable(self): return not self.is_locked + + def can_edit(self, user): + if self.is_locked: + return user.id == self.owner.id + return True + def is_owned_by(self, user): + return user.id == self.owner.id class PageText(BaseModel): @@ -508,12 +515,16 @@ def error_404(body): def error_403(body): return render_template('forbidden.html'), 403 -@app.errorhandler(500) +@app.errorhandler(400) def error_400(body): return render_template('badrequest.html'), 400 +@app.errorhandler(500) +def error_500(body): + return render_template('internalservererror.html'), 500 + # Middle point during page editing. -def savepoint(form, is_preview=False, pageid=None): +def savepoint(form, is_preview=False, pageobj=None): if is_preview: preview = md(form['text'], math='enablemath' in form) else: @@ -522,7 +533,7 @@ def savepoint(form, is_preview=False, pageid=None): pl_js_info['editing'] = dict( original_text = None, # TODO preview_text = form['text'], - page_id = pageid + page_id = pageobj.id if pageobj else None ) return render_template( 'edit.html', @@ -531,11 +542,15 @@ def savepoint(form, is_preview=False, pageid=None): pl_text=form['text'], pl_tags=form['tags'], pl_enablemath='enablemath' in form, + pl_is_locked='lockpage' in form, + pl_owner_is_current_user=pageobj.is_owned_by(current_user) if pageobj else True, preview=preview, - pl_js_info=pl_js_info + pl_js_info=pl_js_info, + pl_readonly=not pageobj.can_edit(current_user) if pageobj else False ) @app.route('/create/', methods=['GET', 'POST']) +@login_required def create(): if request.method == 'POST': if request.form.get('preview'): @@ -559,7 +574,9 @@ def create(): title=request.form['title'], is_redirect=False, touched=datetime.datetime.now(), - is_math_enabled='enablemath' in request.form + owner=current_user, + is_math_enabled='enablemath' in request.form, + is_locked = 'lockpage' in request.form ) p.change_tags(p_tags) except IntegrityError as e: @@ -567,7 +584,7 @@ def create(): return savepoint(request.form) pr = PageRevision.create( page=p, - user_id=0, + user_id=p.owner.id, comment='', textref=PageText.create_content(request.form['text']), pub_date=datetime.datetime.now(), @@ -575,37 +592,49 @@ def create(): ) PageLink.parse_links(p, request.form['text']) return redirect(p.get_url()) - return render_template('edit.html', pl_url=request.args.get('url')) + return savepoint({ + "url": request.args.get("url"), + "title": "", + "text": "", + "tags": "" + }) @app.route('/edit//', methods=['GET', 'POST']) +@login_required def edit(id): p = Page[id] if request.method == 'POST': + # check if page is locked first! + if not p.can_edit(current_user): + flash('You are trying to edit a locked page!') + abort(403) + if request.form.get('preview'): - return savepoint(request.form, is_preview=True, pageid=id) + return savepoint(request.form, is_preview=True, pageobj=p) p_url = request.form['url'] or None if p_url: if not is_valid_url(p_url): flash('Invalid URL. Valid URLs contain only letters, numbers and hyphens.') - return savepoint(request.form, pageid=id) + return savepoint(request.form, pageobj=p) elif not is_url_available(p_url) and p_url != p.url: flash('This URL is not available.') - return savepoint(request.form, pageid=id) + return savepoint(request.form, pageobj=p) p_tags = [x.strip().lower().replace(' ', '-').replace('_', '-').lstrip('#') for x in request.form.get('tags', '').split(',')] p_tags = [x for x in p_tags if x] if any(not re.fullmatch(SLUG_RE, x) for x in p_tags): flash('Invalid tags text. Tags contain only letters, numbers and hyphens, and are separated by comma.') - return savepoint(request.form, pageid=id) + return savepoint(request.form, pageobj=p) p.url = p_url p.title = request.form['title'] p.touched = datetime.datetime.now() p.is_math_enabled = 'enablemath' in request.form + p.is_locked = 'lockpage' in request.form p.save() p.change_tags(p_tags) pr = PageRevision.create( page=p, - user_id=0, + user_id=current_user.id, comment='', textref=PageText.create_content(request.form['text']), pub_date=datetime.datetime.now(), @@ -613,22 +642,19 @@ def edit(id): ) PageLink.parse_links(p, request.form['text']) return redirect(p.get_url()) - pl_js_info = dict() - pl_js_info['editing'] = dict( - original_text = None, # TODO - preview_text = None, - page_id = id - ) - return render_template( - 'edit.html', - pl_url=p.url, - pl_title=p.title, - pl_text=p.latest.text, - pl_tags=','.join(x.name for x in p.tags), - pl_js_info=pl_js_info, - pl_enablemath = p.is_math_enabled - ) + form = { + "url": p.url, + "title": p.title, + "text": p.latest.text, + "tags": ','.join(x.name for x in p.tags) + } + if p.is_math_enabled: + form["enablemath"] = "1" + if p.is_locked: + form["lockpage"] = "1" + + return savepoint(form, pageobj=p) @app.route("/__sync_start") def __sync_start(): @@ -805,6 +831,8 @@ def theme_switch(): @app.route('/accounts/login/', methods=['GET','POST']) def accounts_login(): + if current_user.is_authenticated: + return redirect(request.args.get('next', '/')) if request.method == 'POST': try: username = request.form['username'] @@ -826,8 +854,6 @@ def accounts_login(): @app.route('/accounts/register/', methods=['GET','POST']) def accounts_register(): - if current_user.is_authenticated: - return redirect(request.args.get('next', '/')) if request.method == 'POST': username = request.form['username'] password = request.form['password'] @@ -980,7 +1006,7 @@ class Importer(object): rev = PageRevision.create( page = p, - user = self.owner.id, + user_id = self.owner.id, textref = textref, comment = revobj.get('comment'), pub_date = datetime.datetime.fromtimestamp(revobj['timestamp']), diff --git a/i18n/salvi.en.json b/i18n/salvi.en.json index c33dab1..2640b81 100644 --- a/i18n/salvi.en.json +++ b/i18n/salvi.en.json @@ -54,6 +54,7 @@ "privacy-policy": "Privacy Policy", "already-have-account": "Already have an account?", "logged-in-as": "Logged in as", - "not-logged-in": "Not logged in" + "not-logged-in": "Not logged in", + "owner": "Owner" } } \ No newline at end of file diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index a7b061a..6f7614e 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -45,6 +45,7 @@ "notes-count-with-url": "Numero di note con URL impostato", "revision-count": "Numero di revisioni", "revision-count-per-page": "Media di revisioni per pagina", - "remember-me-for": "Ricordami per" + "remember-me-for": "Ricordami per", + "owner": "Proprietario" } } \ No newline at end of file diff --git a/templates/base.html b/templates/base.html index 05b2850..6192923 100644 --- a/templates/base.html +++ b/templates/base.html @@ -45,7 +45,13 @@ diff --git a/templates/edit.html b/templates/edit.html index 8440b28..0439228 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -11,7 +11,7 @@ {% endblock %} -{% block json_info %}{% endblock %} +{% block json_info %}{% endblock %} {% block content %} @@ -35,25 +35,32 @@
- +
- +
+ {% if not pl_readonly %}

This editor is using Markdown for text formatting (e.g. bold, italic, headers and tables). More info on Markdown.

- {% if math_version %} + {% if math_version and pl_enablemath %}

Math with KaTeX is enabled. KaTeX guide

{% endif %}
- + {% else %} +
+

This page was locked by the owner, and is therefore not editable. You can still copy the source text.

+
+ {% endif %} +
+ {% if not pl_readonly %}
@@ -63,6 +70,13 @@
+ {% if pl_owner_is_current_user %} +
+ + +
+ {% endif %} + {% endif %} {% endblock %} diff --git a/templates/history.html b/templates/history.html index 9eb6552..1ef859c 100644 --- a/templates/history.html +++ b/templates/history.html @@ -16,6 +16,8 @@ #{{ rev.id }} · {{ rev.pub_date.strftime("%B %-d, %Y %H:%M:%S") }} + by + {{ rev.user.username }} {% endfor %} diff --git a/templates/internalservererror.html b/templates/internalservererror.html new file mode 100644 index 0000000..53ae91f --- /dev/null +++ b/templates/internalservererror.html @@ -0,0 +1,9 @@ +{% extends "base.html" %} + +{% block title %}Internal Server Error - {{ app_name }}{% endblock %} + +{% block content %} +

Internal Server Error

+ +

We’re sorry, an unexpected error occurred. Try refreshing the page.

+{% endblock %} diff --git a/templates/register.html b/templates/register.html index 4e34b07..cf6012f 100644 --- a/templates/register.html +++ b/templates/register.html @@ -17,18 +17,18 @@
- +
- +
- +
diff --git a/templates/view.html b/templates/view.html index 262c161..696ebc1 100644 --- a/templates/view.html +++ b/templates/view.html @@ -38,7 +38,8 @@ {{ T('action-history') }} - Backlinks - {{ T('last-changed') }} - -{{ T('page-id') }}: {{ p.id }} +{{ T('page-id') }}: {{ p.id }} - +{{ T('owner') }}: {{ p.owner.username }} {% endblock %} {% block scripts %} From d2cef14c38c6d8b7ec26b98c985d0f7d204f160f Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Sun, 8 Jan 2023 17:59:52 +0100 Subject: [PATCH 19/51] Added edit comments, user contributions and improved history page --- app.py | 43 +++++++++++++++++++++++------------- i18n/salvi.en.json | 4 +++- i18n/salvi.it.json | 3 ++- templates/contributions.html | 30 +++++++++++++++++++++++++ templates/edit.html | 2 ++ templates/history.html | 21 +++++++++++++----- 6 files changed, 80 insertions(+), 23 deletions(-) create mode 100644 templates/contributions.html diff --git a/app.py b/app.py index aadc83e..72c3efb 100644 --- a/app.py +++ b/app.py @@ -21,8 +21,8 @@ from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.routing import BaseConverter from peewee import * from playhouse.db_url import connect as dbconnect -import csv, datetime, hashlib, html, importlib, json, markdown, os, random, \ - re, sys, uuid, warnings +import datetime, hashlib, html, importlib, json, markdown, os, random, \ + re, sys, warnings from functools import lru_cache, partial from urllib.parse import quote from configparser import ConfigParser @@ -250,7 +250,7 @@ class PageText(BaseModel): class PageRevision(BaseModel): page = FK(Page, backref='revisions', index=True) - user = ForeignKeyField(User, null=True) + user = ForeignKeyField(User, backref='contributions', null=True) comment = CharField(1024, default='') textref = FK(PageText) pub_date = DateTimeField(index=True) @@ -439,7 +439,7 @@ forbidden_urls = [ 'create', 'edit', 'p', 'ajax', 'history', 'manage', 'static', 'media', 'accounts', 'tags', 'init-config', 'upload', 'upload-info', 'about', 'stats', 'terms', 'privacy', 'easter', 'search', 'help', 'circles', - 'protect', 'kt', 'embed', 'backlinks' + 'protect', 'kt', 'embed', 'backlinks', 'u' ] app = Flask(__name__) @@ -541,6 +541,7 @@ def savepoint(form, is_preview=False, pageobj=None): pl_title=form['title'], pl_text=form['text'], pl_tags=form['tags'], + pl_comment=form['comment'], pl_enablemath='enablemath' in form, pl_is_locked='lockpage' in form, pl_owner_is_current_user=pageobj.is_owned_by(current_user) if pageobj else True, @@ -596,7 +597,8 @@ def create(): "url": request.args.get("url"), "title": "", "text": "", - "tags": "" + "tags": "", + "comment": get_string(g.lang, "page-created") }) @app.route('/edit//', methods=['GET', 'POST']) @@ -632,22 +634,24 @@ def edit(id): p.is_locked = 'lockpage' in request.form p.save() p.change_tags(p_tags) - pr = PageRevision.create( - page=p, - user_id=current_user.id, - comment='', - textref=PageText.create_content(request.form['text']), - pub_date=datetime.datetime.now(), - length=len(request.form['text']) - ) - PageLink.parse_links(p, request.form['text']) + if request.form['text'] != p.latest.text: + pr = PageRevision.create( + page=p, + user_id=current_user.id, + comment=request.form["comment"], + textref=PageText.create_content(request.form['text']), + pub_date=datetime.datetime.now(), + length=len(request.form['text']) + ) + PageLink.parse_links(p, request.form['text']) return redirect(p.get_url()) form = { "url": p.url, "title": p.title, "text": p.latest.text, - "tags": ','.join(x.name for x in p.tags) + "tags": ','.join(x.name for x in p.tags), + "comment": "" } if p.is_math_enabled: form["enablemath"] = "1" @@ -766,6 +770,15 @@ def history(id): abort(404) return render_template('history.html', p=p, history=p.revisions.order_by(PageRevision.pub_date.desc())) +@app.route('/u//') +def contributions(username): + try: + user = User.get(User.username == username) + except User.DoesNotExist: + abort(404) + return render_template('contributions.html', u=user, contributions=user.contributions.order_by(PageRevision.pub_date.desc())) + + @app.route('/history/revision//') def view_old(revisionid): try: diff --git a/i18n/salvi.en.json b/i18n/salvi.en.json index 2640b81..5fb2b7c 100644 --- a/i18n/salvi.en.json +++ b/i18n/salvi.en.json @@ -55,6 +55,8 @@ "already-have-account": "Already have an account?", "logged-in-as": "Logged in as", "not-logged-in": "Not logged in", - "owner": "Owner" + "owner": "Owner", + "page-created": "Page created", + "write-a-comment": "Write a comment…" } } \ No newline at end of file diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index 6f7614e..e5b5783 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -46,6 +46,7 @@ "revision-count": "Numero di revisioni", "revision-count-per-page": "Media di revisioni per pagina", "remember-me-for": "Ricordami per", - "owner": "Proprietario" + "owner": "Proprietario", + "write-a-comment": "Scrivi un commento…" } } \ No newline at end of file diff --git a/templates/contributions.html b/templates/contributions.html new file mode 100644 index 0000000..f94509f --- /dev/null +++ b/templates/contributions.html @@ -0,0 +1,30 @@ +{% extends "base.html" %} + +{% block title %}Contributions of {{ u.username }} - {{ app_name }}{% endblock %} + +{% block meta %} + +{% endblock %} + +{% block content %} +

Contributions of {{ u.username }} + + +{% endblock %} diff --git a/templates/edit.html b/templates/edit.html index 0439228..791bef8 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -64,6 +64,7 @@
+

Advanced options

@@ -78,6 +79,7 @@ {% endif %} {% endif %} + {% endblock %} {% block scripts %} diff --git a/templates/history.html b/templates/history.html index 1ef859c..8760ec4 100644 --- a/templates/history.html +++ b/templates/history.html @@ -12,13 +12,22 @@ + +

{{ T("back-to") }} {{ p.title }}.

{% endblock %} From 6f53cd38368b3dfd0a5b8627844863132844768d Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Fri, 10 Feb 2023 14:15:21 +0100 Subject: [PATCH 20/51] Implemented calendar --- CHANGELOG.md | 7 +- app.py | 33 ++++-- extensions/instagram.py | 173 -------------------------------- i18n/salvi.en.json | 1 + i18n/salvi.it.json | 1 + static/style.css | 5 + templates/calendar.html | 33 ++++++ templates/edit.html | 5 + templates/home.html | 6 ++ templates/includes/nl_item.html | 6 ++ templates/listrecent.html | 13 ++- templates/listtag.html | 6 ++ templates/month.html | 32 ++++++ templates/search.html | 2 + templates/view.html | 4 + 15 files changed, 145 insertions(+), 182 deletions(-) delete mode 100644 extensions/instagram.py create mode 100644 templates/calendar.html create mode 100644 templates/month.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f6b301..17b4aa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,12 @@ + Added `User` table. + Added `Flask-Login` and `Flask-WTF` dependencies in order to implement user logins. + Added `python-i18n` as a dependency. Therefore, i18n changed format, using JSON files now. -+ Now you can export pages in a JSON format. Coming soon: importing. ++ Login is now required for creating and editing. ++ Now you can leave a comment while changing a page’s text. Moreover, a new revision is created now + only in case of an effective text change. ++ Now a page can be dated in the calendar. ++ Now you can export and import pages in a JSON format. Importing can be done by admin users only. ++ Improved page history view, and added user contributions page. + Like it or not, now gzip library is required. + Added CSS variables in the site style. diff --git a/app.py b/app.py index 72c3efb..9460d11 100644 --- a/app.py +++ b/app.py @@ -21,7 +21,7 @@ from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.routing import BaseConverter from peewee import * from playhouse.db_url import connect as dbconnect -import datetime, hashlib, html, importlib, json, markdown, os, random, \ +import calendar, datetime, hashlib, html, importlib, json, markdown, os, random, \ re, sys, warnings from functools import lru_cache, partial from urllib.parse import quote @@ -436,10 +436,10 @@ def is_url_available(url): return url not in forbidden_urls and not Page.select().where(Page.url == url).exists() forbidden_urls = [ - 'create', 'edit', 'p', 'ajax', 'history', 'manage', 'static', 'media', - 'accounts', 'tags', 'init-config', 'upload', 'upload-info', 'about', - 'stats', 'terms', 'privacy', 'easter', 'search', 'help', 'circles', - 'protect', 'kt', 'embed', 'backlinks', 'u' + 'about', 'accounts', 'ajax', 'backlinks', 'calendar', 'circles', 'create', + 'easter', 'edit', 'embed', 'help', 'history', 'init-config', 'kt', + 'manage', 'media', 'p', 'privacy', 'protect', 'search', 'static', 'stats', + 'tags', 'terms', 'u', 'upload', 'upload-info' ] app = Flask(__name__) @@ -547,6 +547,7 @@ def savepoint(form, is_preview=False, pageobj=None): pl_owner_is_current_user=pageobj.is_owned_by(current_user) if pageobj else True, preview=preview, pl_js_info=pl_js_info, + pl_calendar=('usecalendar' in form) and form['calendar'], pl_readonly=not pageobj.can_edit(current_user) if pageobj else False ) @@ -575,7 +576,8 @@ def create(): title=request.form['title'], is_redirect=False, touched=datetime.datetime.now(), - owner=current_user, + owner_id=current_user.id, + calendar=datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None, is_math_enabled='enablemath' in request.form, is_locked = 'lockpage' in request.form ) @@ -632,6 +634,7 @@ def edit(id): p.touched = datetime.datetime.now() p.is_math_enabled = 'enablemath' in request.form p.is_locked = 'lockpage' in request.form + p.calendar = datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None p.save() p.change_tags(p_tags) if request.form['text'] != p.latest.text: @@ -657,6 +660,12 @@ def edit(id): form["enablemath"] = "1" if p.is_locked: form["lockpage"] = "1" + if p.calendar: + form["usecalendar"] = "1" + try: + form["calendar"] = p.calendar.isoformat().split("T")[0] + except Exception: + form["calendar"] = p.calendar return savepoint(form, pageobj=p) @@ -778,6 +787,18 @@ def contributions(username): abort(404) return render_template('contributions.html', u=user, contributions=user.contributions.order_by(PageRevision.pub_date.desc())) +@app.route('/calendar/') +def calendar_view(): + return render_template('calendar.html') + +@app.route('/calendar//') +def calendar_month(y, m): + notes = Page.select().where( + (datetime.date(y, m, 1) <= Page.calendar) & + (Page.calendar < datetime.date(y+1 if m==12 else y, 1 if m==12 else m+1, 1)) + ).order_by(Page.calendar) + + return render_template('month.html', d=datetime.date(y, m, 1), notes=notes) @app.route('/history/revision//') def view_old(revisionid): diff --git a/extensions/instagram.py b/extensions/instagram.py deleted file mode 100644 index 431a239..0000000 --- a/extensions/instagram.py +++ /dev/null @@ -1,173 +0,0 @@ -from flask import Blueprint, render_template -from peewee import * -import instagram_private_api, json, os, sys, random, codecs - -database = SqliteDatabase('instagram.sqlite') - -class BaseModel(Model): - class Meta: - database = database - -class InstagramProfile(BaseModel): - p_id = IntegerField() - p_username = CharField(30) - p_full_name = CharField(30) - p_biography = CharField(150) - posts_count = IntegerField() - followers_count = IntegerField() - following_count = IntegerField() - flags = BitField() - pub_date = DateTimeField() - is_verified = flags.flag(1) - is_private = flags.flag(2) - -class InstagramMedia(BaseModel): - user = IntegerField() - pub_date = DateTimeField() - media_url = TextField() - description = CharField(2200) - -def init_db(): - database.create_tables([InstagramProfile, InstagramMedia]) - -def bytes_to_json(python_object): - if isinstance(python_object, bytes): - return {'__class__': 'bytes', - '__value__': codecs.encode(python_object, 'base64').decode()} - raise TypeError(repr(python_object) + ' is not JSON serializable') - -def bytes_from_json(json_object): - if '__class__' in json_object and json_object['__class__'] == 'bytes': - return codecs.decode(json_object['__value__'].encode(), 'base64') - return json_object - -SETTINGS_PATH = 'ig_api_settings' - -def load_settings(username): - with open(os.path.join(SETTINGS_PATH, username + '.json')) as f: - settings = json.load(f, object_hook=bytes_from_json) - return settings - -def save_settings(username, settings): - with open(os.path.join(SETTINGS_PATH, username + '.json'), 'w') as f: - json.dump(settings, f, default=bytes_to_json) - -CLIENTS = [] - -def load_clients(): - try: - with open(os.path.join(SETTINGS_PATH, 'config.txt')) as f: - conf = f.read() - except OSError: - print('Config file not found.') - return - for up in conf.split('\n'): - try: - up = up.split('#')[0].strip() - if not up: - continue - username, password = up.split(':') - try: - settings = load_settings(username) - except Exception: - settings = None - try: - if settings: - device_id = settings.get('device_id') - api = instagram_private_api.Client( - username, password, - settings=settings - ) - else: - api = instagram_private_api.Client( - username, password, - on_login=lambda x: save_settings(username, x.settings) - ) - except (instagram_private_api.ClientCookieExpiredError, - instagram_private_api.ClientLoginRequiredError) as e: - api = instagram_private_api.Client( - username, password, - device_id=device_id, - on_login=lambda x: save_settings(username, x.settings) - ) - CLIENTS.append(api) - except Exception: - sys.excepthook(*sys.exc_info()) - continue - -def make_request(method_name, *args, **kwargs): - exc = None - usable_clients = list(range(len(CLIENTS))) - while usable_clients: - ci = random.choice(usable_clients) - client = CLIENTS[ci] - usable_clients.remove(ci) - try: - method = getattr(client, method_name) - except AttributeError: - raise ValueError('client has no method called {!r}'.format(method_name)) - if not callable(method): - raise ValueError('client has no method called {!r}'.format(method_name)) - try: - return method(*args, **kwargs) - except Exception as e: - exc = e - if exc: - raise exc - else: - raise RuntimeError('no active clients') - -N_FORCE = 0 -N_FALLBACK_CACHE = 1 -N_PREFER_CACHE = 2 -N_OFFLINE = 3 - -def choose_method(online, offline, network): - if network == N_FORCE: - return online() - elif network == N_FALLBACK_CACHE: - try: - return online() - except Exception: - return offline() - elif network == N_PREFER_CACHE: - try: - return offline() - except Exception: - return online() - elif network == N_OFFLINE: - return offline() - -def get_profile_info(username_or_id, network=N_FALLBACK_CACHE): - if isinstance(username_or_id, str): - username, userid = username_or_id, None - elif isinstance(username_or_id, int): - username, userid = None, username_or_id - else: - raise TypeError('invalid username or id') - def online(): - if userid: - data = make_request('user_info', userid) - else: - data = make_request('username_info', username) - return InstagramProfile.create( - p_id = data['user']['pk'], - p_username = data['user']['username'], - p_full_name = data['user']['full_name'], - p_biography = data['user']['biography'], - posts_count = data['user']['media_count'], - followers_count = data['user']['follower_count'], - following_count = data['user']['following_count'], - is_verified = data['user']['is_verified'], - is_private = data['user']['is_private'], - pub_date = datetime.datetime.now() - ) - def offline(): - if userid: - q = InstagramProfile.select().where(InstagramProfile.p_id == userid) - else: - q = InstagramProfile.select().where(InstagramProfile.p_username == username) - return q.order_by(InstagramProfile.pub_date.desc())[0] - return choose_method(online, offline, network) - -load_clients() diff --git a/i18n/salvi.en.json b/i18n/salvi.en.json index 5fb2b7c..b49cb7a 100644 --- a/i18n/salvi.en.json +++ b/i18n/salvi.en.json @@ -23,6 +23,7 @@ "random-page": "Random page", "search": "Search", "year": "Year", + "month": "Month", "calculate": "Calculate", "show-all": "Show all", "just-now": "just now", diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index e5b5783..a01ce65 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -23,6 +23,7 @@ "random-page": "Pagina casuale", "search": "Cerca", "year": "Anno", + "month": "Mese", "calculate": "Calcola", "show-all": "Mostra tutto", "just-now": "poco fa", diff --git a/static/style.css b/static/style.css index ba6f187..49e1a57 100644 --- a/static/style.css +++ b/static/style.css @@ -70,8 +70,13 @@ input[type="submit"],input[type="reset"],input[type="button"],button{font-family .page-tags .tag-count{color: var(--btn-success);font-size:smaller;font-weight:600} .search-wrapper {display:flex;width:90%;margin:auto} .search-wrapper > input {flex:1} +.calendar-subtitle {text-align: center; margin-top: -1em} .preview-subtitle {text-align: center; margin-top: -1em} textarea {background-color: var(--bg-sharp); color: var(--fg-sharp)} +ul.inline {margin:0; padding:0; display: inline} +ul.inline > li {display: inline-block;} +ul.inline > li::after {content: "·"} +ul.inline > li:last-child::after {content: ""} /* Circles extension */ .circles-red{color: #e14} diff --git a/templates/calendar.html b/templates/calendar.html new file mode 100644 index 0000000..f3e5c23 --- /dev/null +++ b/templates/calendar.html @@ -0,0 +1,33 @@ +{% extends "base.html" %} + +{% block title %}Calendar – {{ app_name }}{% endblock %} + +{% block content %} +

Calendar

+ +
+
+ Calendar view + +
+ +
    + {% set view_choices = ["month"] %} + {% for vch in view_choices %} +
  • + {% if vch == viewas %} + {{ T(vch) }} + {% else %} + {{ T(vch) }} + {% endif %} +
  • + {% endfor %} +
+
+ +
+ +
+
+
+{% endblock %} \ No newline at end of file diff --git a/templates/edit.html b/templates/edit.html index 791bef8..bceabbc 100644 --- a/templates/edit.html +++ b/templates/edit.html @@ -77,6 +77,11 @@
{% endif %} +
+ + + +
{% endif %} diff --git a/templates/home.html b/templates/home.html index c0d4691..f776700 100644 --- a/templates/home.html +++ b/templates/home.html @@ -25,6 +25,12 @@ {% endfor %}

{% endif %} + {% if n.calendar %} +

+ calendar_today + +

+ {% endif %} {% endfor %}
  • {{ T('show-all') }}
  • diff --git a/templates/includes/nl_item.html b/templates/includes/nl_item.html index a9f01c6..c898901 100644 --- a/templates/includes/nl_item.html +++ b/templates/includes/nl_item.html @@ -12,3 +12,9 @@ {% endif %} {% endfor %}

    +{% if n.calendar %} +

    + calendar_today + +

    +{% endif %} diff --git a/templates/listrecent.html b/templates/listrecent.html index f8b7188..01bb1ff 100644 --- a/templates/listrecent.html +++ b/templates/listrecent.html @@ -13,11 +13,20 @@
  • {{ n.title }}

    {{ n.short_desc() }}

    -

    Tags: + {% if n.tags %} +

    {{ T('tags') }}: {% for tag in n.tags %} - #{{ tag.name }} + {% set tn = tag.name %} + #{{ tn }} {% endfor %}

    + {% endif %} + {% if n.calendar %} +

    + calendar_today + +

    + {% endif %}
  • {% endfor %} {% if page_n <= total_count // 20 %} diff --git a/templates/listtag.html b/templates/listtag.html index 5742b86..68ef780 100644 --- a/templates/listtag.html +++ b/templates/listtag.html @@ -25,6 +25,12 @@ #{{ tn }} {% endif %} {% endfor %} + {% if n.calendar %} +

    + calendar_today + +

    + {% endif %}

    {% endfor %} diff --git a/templates/month.html b/templates/month.html new file mode 100644 index 0000000..9211ade --- /dev/null +++ b/templates/month.html @@ -0,0 +1,32 @@ +{% extends "base.html" %} + +{% block title %}{{ d.strftime("%B %Y") }} – {{ app_name }}{% endblock %} + +{% block content %} +

    {{ d.strftime("%B %Y") }}

    + +
      + {% for n in notes %} +
    • + {{ n.title }} +

      {{ n.short_desc() }}

      + {% if n.tags %} +

      {{ T('tags') }}: + {% for tag in n.tags %} + {% set tn = tag.name %} + #{{ tn }} + {% endfor %} +

      + {% endif %} + {% if n.calendar %} +

      + calendar_today + +

      + {% endif %} +
    • + {% endfor %} +
    + + +{% endblock %} \ No newline at end of file diff --git a/templates/search.html b/templates/search.html index c332983..df1aaea 100644 --- a/templates/search.html +++ b/templates/search.html @@ -27,6 +27,8 @@ {% elif q %}

    {{ T('search-no-results') }} {{ q }}

    +{% else %} +

    Please note that search queries do not search for page text.

    {% endif %} {% endblock %} diff --git a/templates/view.html b/templates/view.html index 696ebc1..b66fb00 100644 --- a/templates/view.html +++ b/templates/view.html @@ -7,6 +7,10 @@ {% block content %}

    {{ p.title }}

    + + {% if p.calendar %} +

    calendar_today{{ p.calendar.strftime('%B %-d, %Y') }} + {% endif %}

    {{ T('last-changed') }} {{ rev.human_pub_date() }} · From 5f4370f9539a1e55d1fffeab3894b3076b9ee9f5 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Sun, 12 Mar 2023 10:03:01 +0100 Subject: [PATCH 21/51] =?UTF-8?q?Extensions=20don=E2=80=99t=20work=20anymo?= =?UTF-8?q?re=20on=20Python=203.10=20(Markdown=203.4.1),=20adding=20a=20wa?= =?UTF-8?q?y=20to=20disable=20them.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 ++ app.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 17b4aa3..78205c2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## 0.7 ++ XXX Custom Markdown extensions are broken with Markdown 3.4.1. While we find a workaround to it, + use the config `[markdown]disable_custom_extensions` set to 1. + Schema changes: + Removed `PagePolicy` and `PagePolicyKey` tables altogether. They were never useful. + Added `calendar` field to `Page`. diff --git a/app.py b/app.py index 9460d11..7d41b12 100644 --- a/app.py +++ b/app.py @@ -95,7 +95,7 @@ def _makelist(l): #### MARKDOWN EXTENSIONS #### class StrikethroughExtension(markdown.extensions.Extension): - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md, md_globals=None): postprocessor = StrikethroughPostprocessor(md) md.postprocessors.add('strikethrough', postprocessor, '>raw_html') @@ -114,7 +114,7 @@ BlockQuoteProcessor.RE = re.compile(r'(^|\n)[ ]{0,3}>(?!!)[ ]?(.*)') ### XXX it currently only detects spoilers that are not at the beginning of the line. To be fixed. class SpoilerExtension(markdown.extensions.Extension): - def extendMarkdown(self, md, md_globals): + def extendMarkdown(self, md, md_globals=None): md.inlinePatterns.register(markdown.inlinepatterns.SimpleTagInlineProcessor(r'()>!(.*?)!<', 'span class="spoiler"'), 'spoiler', 14) @@ -390,8 +390,11 @@ def md(text, expand_magic=False, toc=True, math=True): if expand_magic: # DEPRECATED seeking for a better solution. warnings.warn('Magic words are no more supported.', DeprecationWarning) - extensions = ['tables', 'footnotes', 'fenced_code', 'sane_lists', StrikethroughExtension(), SpoilerExtension()] + extensions = ['tables', 'footnotes', 'fenced_code', 'sane_lists'] extension_configs = {} + if not _getconf('markdown', 'disable_custom_extensions'): + extensions.append(StrikethroughExtension()) + extensions.append(SpoilerExtension()) if toc: extensions.append('toc') if math and markdown_katex and ('$`' in text or '```math' in text): From 7f050afb8bfde30e75d2f647cff20f2b3df737f7 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Thu, 16 Mar 2023 14:08:40 +0100 Subject: [PATCH 22/51] fixed markdown extensions --- CHANGELOG.md | 3 +-- app.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 78205c2..d0fc778 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,8 +2,6 @@ ## 0.7 -+ XXX Custom Markdown extensions are broken with Markdown 3.4.1. While we find a workaround to it, - use the config `[markdown]disable_custom_extensions` set to 1. + Schema changes: + Removed `PagePolicy` and `PagePolicyKey` tables altogether. They were never useful. + Added `calendar` field to `Page`. @@ -16,6 +14,7 @@ + Now a page can be dated in the calendar. + Now you can export and import pages in a JSON format. Importing can be done by admin users only. + Improved page history view, and added user contributions page. ++ Updated Markdown extensions to work under latest version. + Like it or not, now gzip library is required. + Added CSS variables in the site style. diff --git a/app.py b/app.py index 7d41b12..cb89d2f 100644 --- a/app.py +++ b/app.py @@ -97,7 +97,7 @@ def _makelist(l): class StrikethroughExtension(markdown.extensions.Extension): def extendMarkdown(self, md, md_globals=None): postprocessor = StrikethroughPostprocessor(md) - md.postprocessors.add('strikethrough', postprocessor, '>raw_html') + md.postprocessors.register(postprocessor, 'strikethrough', 0) class StrikethroughPostprocessor(markdown.postprocessors.Postprocessor): pattern = re.compile(r"~~(((?!~~).)+)~~", re.DOTALL) From 2887d94a8c4ecfc819647b4cdbb4d372171369d3 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Thu, 16 Mar 2023 14:17:50 +0100 Subject: [PATCH 23/51] rename .html templates to .jinja2 for more consistency --- CHANGELOG.md | 1 + app.py | 66 +++++++++---------- .../{backlinks.html => backlinks.jinja2} | 0 .../{badrequest.html => badrequest.jinja2} | 0 templates/{base.html => base.jinja2} | 0 templates/{calendar.html => calendar.jinja2} | 0 templates/circles/{add.html => add.jinja2} | 0 templates/circles/{csv.html => csv.jinja2} | 0 templates/circles/{list.html => list.jinja2} | 0 .../circles/{stats.html => stats.jinja2} | 0 .../contactnova/{list.html => list.jinja2} | 0 .../contactnova/{new.html => new.jinja2} | 0 .../{single.html => single.jinja2} | 0 ...ontributions.html => contributions.jinja2} | 0 templates/{easter.html => easter.jinja2} | 0 templates/{edit.html => edit.jinja2} | 0 .../{exportpages.html => exportpages.jinja2} | 0 .../{forbidden.html => forbidden.jinja2} | 0 templates/{history.html => history.jinja2} | 0 templates/{home.html => home.jinja2} | 0 .../{importpages.html => importpages.jinja2} | 0 .../includes/{nl_item.html => nl_item.jinja2} | 0 ...rerror.html => internalservererror.jinja2} | 0 .../{leaderboard.html => leaderboard.jinja2} | 0 .../{listrecent.html => listrecent.jinja2} | 0 templates/{listtag.html => listtag.jinja2} | 0 templates/{login.html => login.jinja2} | 0 templates/{month.html => month.jinja2} | 0 templates/{notfound.html => notfound.jinja2} | 0 templates/{register.html => register.jinja2} | 0 templates/{search.html => search.jinja2} | 0 templates/{stats.html => stats.jinja2} | 0 templates/{upload.html => upload.jinja2} | 0 .../{uploadinfo.html => uploadinfo.jinja2} | 0 templates/{view.html => view.jinja2} | 0 templates/{viewold.html => viewold.jinja2} | 0 36 files changed, 34 insertions(+), 33 deletions(-) rename templates/{backlinks.html => backlinks.jinja2} (100%) rename templates/{badrequest.html => badrequest.jinja2} (100%) rename templates/{base.html => base.jinja2} (100%) rename templates/{calendar.html => calendar.jinja2} (100%) rename templates/circles/{add.html => add.jinja2} (100%) rename templates/circles/{csv.html => csv.jinja2} (100%) rename templates/circles/{list.html => list.jinja2} (100%) rename templates/circles/{stats.html => stats.jinja2} (100%) rename templates/contactnova/{list.html => list.jinja2} (100%) rename templates/contactnova/{new.html => new.jinja2} (100%) rename templates/contactnova/{single.html => single.jinja2} (100%) rename templates/{contributions.html => contributions.jinja2} (100%) rename templates/{easter.html => easter.jinja2} (100%) rename templates/{edit.html => edit.jinja2} (100%) rename templates/{exportpages.html => exportpages.jinja2} (100%) rename templates/{forbidden.html => forbidden.jinja2} (100%) rename templates/{history.html => history.jinja2} (100%) rename templates/{home.html => home.jinja2} (100%) rename templates/{importpages.html => importpages.jinja2} (100%) rename templates/includes/{nl_item.html => nl_item.jinja2} (100%) rename templates/{internalservererror.html => internalservererror.jinja2} (100%) rename templates/{leaderboard.html => leaderboard.jinja2} (100%) rename templates/{listrecent.html => listrecent.jinja2} (100%) rename templates/{listtag.html => listtag.jinja2} (100%) rename templates/{login.html => login.jinja2} (100%) rename templates/{month.html => month.jinja2} (100%) rename templates/{notfound.html => notfound.jinja2} (100%) rename templates/{register.html => register.jinja2} (100%) rename templates/{search.html => search.jinja2} (100%) rename templates/{stats.html => stats.jinja2} (100%) rename templates/{upload.html => upload.jinja2} (100%) rename templates/{uploadinfo.html => uploadinfo.jinja2} (100%) rename templates/{view.html => view.jinja2} (100%) rename templates/{viewold.html => viewold.jinja2} (100%) diff --git a/CHANGELOG.md b/CHANGELOG.md index d0fc778..af38890 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ + Updated Markdown extensions to work under latest version. + Like it or not, now gzip library is required. + Added CSS variables in the site style. ++ Templates are now with `.jinja2` extension. ## 0.6 diff --git a/app.py b/app.py index cb89d2f..26375bb 100644 --- a/app.py +++ b/app.py @@ -75,7 +75,7 @@ else: markdown_katex = None try: if _getconf('appearance', 'math') != 'off': - import markdown_katex + import markdown_katex #pragma: no cover except ImportError: pass @@ -497,7 +497,7 @@ def linebreaks(text): @app.route('/') def homepage(): page_limit = _getconf("appearance","items_per_page",20,cast=int) - return render_template('home.html', new_notes=Page.select() + return render_template('home.jinja2', new_notes=Page.select() .order_by(Page.touched.desc()).limit(page_limit)) @app.route('/robots.txt') @@ -512,19 +512,19 @@ def favicon(): @app.errorhandler(404) def error_404(body): - return render_template('notfound.html'), 404 + return render_template('notfound.jinja2'), 404 @app.errorhandler(403) def error_403(body): - return render_template('forbidden.html'), 403 + return render_template('forbidden.jinja2'), 403 @app.errorhandler(400) def error_400(body): - return render_template('badrequest.html'), 400 + return render_template('badrequest.jinja2'), 400 @app.errorhandler(500) def error_500(body): - return render_template('internalservererror.html'), 500 + return render_template('internalservererror.jinja2'), 500 # Middle point during page editing. def savepoint(form, is_preview=False, pageobj=None): @@ -539,7 +539,7 @@ def savepoint(form, is_preview=False, pageobj=None): page_id = pageobj.id if pageobj else None ) return render_template( - 'edit.html', + 'edit.jinja2', pl_url=form['url'], pl_title=form['title'], pl_text=form['text'], @@ -714,7 +714,7 @@ def view_unnamed(id): return redirect(p.get_url()) else: flash('The URL of this page is a reserved URL. Please change it.') - return render_template('view.html', p=p, rev=p.latest) + return render_template('view.jinja2', p=p, rev=p.latest) @app.route('/embed//') def embed_view(id): @@ -730,7 +730,7 @@ def embed_view(id): @app.route('/p/most_recent//') def view_most_recent(page=1): general_query = Page.select().order_by(Page.touched.desc()) - return render_template('listrecent.html', notes=general_query.paginate(page), + return render_template('listrecent.jinja2', notes=general_query.paginate(page), page_n=page, total_count=general_query.count(), min=min) @app.route('/p/random/') @@ -758,7 +758,7 @@ def page_leaderboard(): pages.append((p, score, p.back_links.count(), p.forward_links.count(), p.latest.length)) pages.sort(key = lambda x: (x[1], x[2], x[4], x[3]), reverse = True) - return render_template('leaderboard.html', pages=pages), headers + return render_template('leaderboard.jinja2', pages=pages), headers @app.route('//') def view_named(name): @@ -766,7 +766,7 @@ def view_named(name): p = Page.get(Page.url == name) except Page.DoesNotExist: abort(404) - return render_template('view.html', p=p, rev=p.latest) + return render_template('view.jinja2', p=p, rev=p.latest) @app.route('/init-config/tables/') def init_config_tables(): @@ -780,7 +780,7 @@ def history(id): p = Page[id] except Page.DoesNotExist: abort(404) - return render_template('history.html', p=p, history=p.revisions.order_by(PageRevision.pub_date.desc())) + return render_template('history.jinja2', p=p, history=p.revisions.order_by(PageRevision.pub_date.desc())) @app.route('/u//') def contributions(username): @@ -788,11 +788,11 @@ def contributions(username): user = User.get(User.username == username) except User.DoesNotExist: abort(404) - return render_template('contributions.html', u=user, contributions=user.contributions.order_by(PageRevision.pub_date.desc())) + return render_template('contributions.jinja2', u=user, contributions=user.contributions.order_by(PageRevision.pub_date.desc())) @app.route('/calendar/') def calendar_view(): - return render_template('calendar.html') + return render_template('calendar.jinja2') @app.route('/calendar//') def calendar_month(y, m): @@ -801,7 +801,7 @@ def calendar_month(y, m): (Page.calendar < datetime.date(y+1 if m==12 else y, 1 if m==12 else m+1, 1)) ).order_by(Page.calendar) - return render_template('month.html', d=datetime.date(y, m, 1), notes=notes) + return render_template('month.jinja2', d=datetime.date(y, m, 1), notes=notes) @app.route('/history/revision//') def view_old(revisionid): @@ -810,7 +810,7 @@ def view_old(revisionid): except PageRevision.DoesNotExist: abort(404) p = rev.page - return render_template('viewold.html', p=p, rev=rev) + return render_template('viewold.jinja2', p=p, rev=rev) @app.route('/backlinks//') def backlinks(id): @@ -818,7 +818,7 @@ def backlinks(id): p = Page[id] except Page.DoesNotExist: abort(404) - return render_template('backlinks.html', p=p, backlinks=Page.select().join(PageLink, on=PageLink.to_page).where(PageLink.from_page == p)) + return render_template('backlinks.jinja2', p=p, backlinks=Page.select().join(PageLink, on=PageLink.to_page).where(PageLink.from_page == p)) @app.route('/search/', methods=['GET', 'POST']) def search(): @@ -830,27 +830,27 @@ def search(): query |= Page.select().join(PageTag, on=PageTag.page ).where(PageTag.name ** ('%' + q + '%')) query = query.order_by(Page.touched.desc()) - return render_template('search.html', q=q, pl_include_tags=include_tags, + return render_template('search.jinja2', q=q, pl_include_tags=include_tags, results=query.paginate(1)) - return render_template('search.html', pl_include_tags=True) + return render_template('search.jinja2', pl_include_tags=True) @app.route('/tags//') @app.route('/tags///') def listtag(tag, page=1): general_query = Page.select().join(PageTag, on=PageTag.page).where(PageTag.name == tag).order_by(Page.touched.desc()) page_query = general_query.paginate(page) - return render_template('listtag.html', tagname=tag, tagged_notes=page_query, + return render_template('listtag.jinja2', tagname=tag, tagged_notes=page_query, page_n=page, total_count=general_query.count(), min=min) # symbolic route as of v0.5 @app.route('/upload/', methods=['GET']) def upload(): - return render_template('upload.html') + return render_template('upload.jinja2') @app.route('/stats/') def stats(): - return render_template('stats.html', + return render_template('stats.jinja2', notes_count=Page.select().count(), notes_with_url=Page.select().where(Page.url != None).count(), revision_count=PageRevision.select().count(), @@ -876,7 +876,7 @@ def accounts_login(): user = User.get(User.username == username) if not check_password_hash(user.password, request.form['password']): flash('Invalid username or password.') - return render_template('login.html') + return render_template('login.jinja2') except User.DoesNotExist: flash('Invalid username or password.') else: @@ -887,7 +887,7 @@ def accounts_login(): else: login_user(user) return redirect(request.args.get('next', '/')) - return render_template('login.html') + return render_template('login.jinja2') @app.route('/accounts/register/', methods=['GET','POST']) def accounts_register(): @@ -896,10 +896,10 @@ def accounts_register(): password = request.form['password'] if not is_username(username): flash('Invalid username: usernames can contain only letters, numbers, underscores and hyphens.') - return render_template('register.html') + return render_template('register.jinja2') if request.form['password'] != request.form['confirm_password']: flash('Passwords do not match.') - return render_template('register.html') + return render_template('register.jinja2') if not request.form['legal']: flash('You must accept Terms in order to register.') try: @@ -915,7 +915,7 @@ def accounts_register(): return redirect(request.args.get('next', '/')) except IntegrityError: flash('Username taken') - return render_template('register.html') + return render_template('register.jinja2') @app.route('/accounts/logout/') def accounts_logout(): @@ -963,10 +963,10 @@ def easter_y(y=None): if y: if y > 2499: flash('Years above 2500 A.D. are currently not supported.') - return render_template('easter.html') - return render_template('easter.html', y=y, easter_dates=stash_easter(y)) + return render_template('easter.jinja2') + return render_template('easter.jinja2', y=y, easter_dates=stash_easter(y)) else: - return render_template('easter.html') + return render_template('easter.jinja2') ## import / export ## @@ -1075,7 +1075,7 @@ def exportpages(): q_list.append(Page.select().where(Page.title == item)) if not q_list: flash('Failed to export pages: The list is empty!') - return render_template('exportpages.html') + return render_template('exportpages.jinja2') query = q_list.pop(0) while q_list: query |= q_list.pop(0) @@ -1083,7 +1083,7 @@ def exportpages(): e.add_page_list(query, include_history='history' in request.form) return e.export(), {'Content-Type': 'application/json', 'Content-Disposition': 'attachment; ' + 'filename=export-{}.json'.format(datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))} - return render_template('exportpages.html') + return render_template('exportpages.jinja2') @app.route('/manage/import/', methods=['GET', 'POST']) @login_required @@ -1098,7 +1098,7 @@ def importpages(): flash('Imported successfully {} pages and {} revisions'.format(*res)) else: flash('Pages can be imported by Administrators only!') - return render_template('importpages.html') + return render_template('importpages.jinja2') #### EXTENSIONS #### diff --git a/templates/backlinks.html b/templates/backlinks.jinja2 similarity index 100% rename from templates/backlinks.html rename to templates/backlinks.jinja2 diff --git a/templates/badrequest.html b/templates/badrequest.jinja2 similarity index 100% rename from templates/badrequest.html rename to templates/badrequest.jinja2 diff --git a/templates/base.html b/templates/base.jinja2 similarity index 100% rename from templates/base.html rename to templates/base.jinja2 diff --git a/templates/calendar.html b/templates/calendar.jinja2 similarity index 100% rename from templates/calendar.html rename to templates/calendar.jinja2 diff --git a/templates/circles/add.html b/templates/circles/add.jinja2 similarity index 100% rename from templates/circles/add.html rename to templates/circles/add.jinja2 diff --git a/templates/circles/csv.html b/templates/circles/csv.jinja2 similarity index 100% rename from templates/circles/csv.html rename to templates/circles/csv.jinja2 diff --git a/templates/circles/list.html b/templates/circles/list.jinja2 similarity index 100% rename from templates/circles/list.html rename to templates/circles/list.jinja2 diff --git a/templates/circles/stats.html b/templates/circles/stats.jinja2 similarity index 100% rename from templates/circles/stats.html rename to templates/circles/stats.jinja2 diff --git a/templates/contactnova/list.html b/templates/contactnova/list.jinja2 similarity index 100% rename from templates/contactnova/list.html rename to templates/contactnova/list.jinja2 diff --git a/templates/contactnova/new.html b/templates/contactnova/new.jinja2 similarity index 100% rename from templates/contactnova/new.html rename to templates/contactnova/new.jinja2 diff --git a/templates/contactnova/single.html b/templates/contactnova/single.jinja2 similarity index 100% rename from templates/contactnova/single.html rename to templates/contactnova/single.jinja2 diff --git a/templates/contributions.html b/templates/contributions.jinja2 similarity index 100% rename from templates/contributions.html rename to templates/contributions.jinja2 diff --git a/templates/easter.html b/templates/easter.jinja2 similarity index 100% rename from templates/easter.html rename to templates/easter.jinja2 diff --git a/templates/edit.html b/templates/edit.jinja2 similarity index 100% rename from templates/edit.html rename to templates/edit.jinja2 diff --git a/templates/exportpages.html b/templates/exportpages.jinja2 similarity index 100% rename from templates/exportpages.html rename to templates/exportpages.jinja2 diff --git a/templates/forbidden.html b/templates/forbidden.jinja2 similarity index 100% rename from templates/forbidden.html rename to templates/forbidden.jinja2 diff --git a/templates/history.html b/templates/history.jinja2 similarity index 100% rename from templates/history.html rename to templates/history.jinja2 diff --git a/templates/home.html b/templates/home.jinja2 similarity index 100% rename from templates/home.html rename to templates/home.jinja2 diff --git a/templates/importpages.html b/templates/importpages.jinja2 similarity index 100% rename from templates/importpages.html rename to templates/importpages.jinja2 diff --git a/templates/includes/nl_item.html b/templates/includes/nl_item.jinja2 similarity index 100% rename from templates/includes/nl_item.html rename to templates/includes/nl_item.jinja2 diff --git a/templates/internalservererror.html b/templates/internalservererror.jinja2 similarity index 100% rename from templates/internalservererror.html rename to templates/internalservererror.jinja2 diff --git a/templates/leaderboard.html b/templates/leaderboard.jinja2 similarity index 100% rename from templates/leaderboard.html rename to templates/leaderboard.jinja2 diff --git a/templates/listrecent.html b/templates/listrecent.jinja2 similarity index 100% rename from templates/listrecent.html rename to templates/listrecent.jinja2 diff --git a/templates/listtag.html b/templates/listtag.jinja2 similarity index 100% rename from templates/listtag.html rename to templates/listtag.jinja2 diff --git a/templates/login.html b/templates/login.jinja2 similarity index 100% rename from templates/login.html rename to templates/login.jinja2 diff --git a/templates/month.html b/templates/month.jinja2 similarity index 100% rename from templates/month.html rename to templates/month.jinja2 diff --git a/templates/notfound.html b/templates/notfound.jinja2 similarity index 100% rename from templates/notfound.html rename to templates/notfound.jinja2 diff --git a/templates/register.html b/templates/register.jinja2 similarity index 100% rename from templates/register.html rename to templates/register.jinja2 diff --git a/templates/search.html b/templates/search.jinja2 similarity index 100% rename from templates/search.html rename to templates/search.jinja2 diff --git a/templates/stats.html b/templates/stats.jinja2 similarity index 100% rename from templates/stats.html rename to templates/stats.jinja2 diff --git a/templates/upload.html b/templates/upload.jinja2 similarity index 100% rename from templates/upload.html rename to templates/upload.jinja2 diff --git a/templates/uploadinfo.html b/templates/uploadinfo.jinja2 similarity index 100% rename from templates/uploadinfo.html rename to templates/uploadinfo.jinja2 diff --git a/templates/view.html b/templates/view.jinja2 similarity index 100% rename from templates/view.html rename to templates/view.jinja2 diff --git a/templates/viewold.html b/templates/viewold.jinja2 similarity index 100% rename from templates/viewold.html rename to templates/viewold.jinja2 From 722ceb4724ad7f25c27400e8e048ac71d3894376 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Thu, 16 Mar 2023 14:24:29 +0100 Subject: [PATCH 24/51] rename .html to .jinja2, part 2 --- templates/backlinks.jinja2 | 2 +- templates/badrequest.jinja2 | 2 +- templates/calendar.jinja2 | 2 +- templates/circles/add.jinja2 | 2 +- templates/circles/csv.jinja2 | 2 +- templates/circles/list.jinja2 | 2 +- templates/circles/stats.jinja2 | 2 +- templates/contactnova/list.jinja2 | 2 +- templates/contactnova/new.jinja2 | 2 +- templates/contactnova/single.jinja2 | 2 +- templates/contributions.jinja2 | 2 +- templates/easter.jinja2 | 2 +- templates/edit.jinja2 | 2 +- templates/exportpages.jinja2 | 2 +- templates/forbidden.jinja2 | 2 +- templates/history.jinja2 | 2 +- templates/home.jinja2 | 2 +- templates/importpages.jinja2 | 2 +- templates/internalservererror.jinja2 | 2 +- templates/leaderboard.jinja2 | 2 +- templates/listrecent.jinja2 | 2 +- templates/listtag.jinja2 | 2 +- templates/login.jinja2 | 2 +- templates/month.jinja2 | 2 +- templates/notfound.jinja2 | 2 +- templates/register.jinja2 | 2 +- templates/search.jinja2 | 4 ++-- templates/stats.jinja2 | 2 +- templates/upload.jinja2 | 2 +- templates/uploadinfo.jinja2 | 2 +- templates/view.jinja2 | 2 +- templates/viewold.jinja2 | 2 +- 32 files changed, 33 insertions(+), 33 deletions(-) diff --git a/templates/backlinks.jinja2 b/templates/backlinks.jinja2 index 9cf9f7c..aa6bbff 100644 --- a/templates/backlinks.jinja2 +++ b/templates/backlinks.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Pages linking to “{{ p.title }}” - {{ app_name }}{% endblock %} diff --git a/templates/badrequest.jinja2 b/templates/badrequest.jinja2 index df7c777..ba04376 100644 --- a/templates/badrequest.jinja2 +++ b/templates/badrequest.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Bad Request - {{ app_name }}{% endblock %} diff --git a/templates/calendar.jinja2 b/templates/calendar.jinja2 index f3e5c23..d4272f7 100644 --- a/templates/calendar.jinja2 +++ b/templates/calendar.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Calendar – {{ app_name }}{% endblock %} diff --git a/templates/circles/add.jinja2 b/templates/circles/add.jinja2 index eb2a937..90e76cc 100644 --- a/templates/circles/add.jinja2 +++ b/templates/circles/add.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Circles – {{ app_name }}{% endblock %} diff --git a/templates/circles/csv.jinja2 b/templates/circles/csv.jinja2 index e50404c..b1b3e49 100644 --- a/templates/circles/csv.jinja2 +++ b/templates/circles/csv.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Circles – {{ app_name }}{% endblock %} diff --git a/templates/circles/list.jinja2 b/templates/circles/list.jinja2 index 8b03b4a..4f9884e 100644 --- a/templates/circles/list.jinja2 +++ b/templates/circles/list.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Circles – {{ app_name }}{% endblock %} diff --git a/templates/circles/stats.jinja2 b/templates/circles/stats.jinja2 index f1617b0..4fdff58 100644 --- a/templates/circles/stats.jinja2 +++ b/templates/circles/stats.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Circles – {{ app_name }}{% endblock %} diff --git a/templates/contactnova/list.jinja2 b/templates/contactnova/list.jinja2 index 276370b..5289f64 100644 --- a/templates/contactnova/list.jinja2 +++ b/templates/contactnova/list.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}List of contacts – {{ app_name }}{% endblock %} diff --git a/templates/contactnova/new.jinja2 b/templates/contactnova/new.jinja2 index 89e17c8..5608dc1 100644 --- a/templates/contactnova/new.jinja2 +++ b/templates/contactnova/new.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Create new contact – {{ app_name }}{% endblock %} diff --git a/templates/contactnova/single.jinja2 b/templates/contactnova/single.jinja2 index 3e77462..ff7a1df 100644 --- a/templates/contactnova/single.jinja2 +++ b/templates/contactnova/single.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Contact {{ p.code }} – {{ app_name }}{% endblock %} diff --git a/templates/contributions.jinja2 b/templates/contributions.jinja2 index f94509f..69cc4db 100644 --- a/templates/contributions.jinja2 +++ b/templates/contributions.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Contributions of {{ u.username }} - {{ app_name }}{% endblock %} diff --git a/templates/easter.jinja2 b/templates/easter.jinja2 index 4a8ebc8..dd81ef8 100644 --- a/templates/easter.jinja2 +++ b/templates/easter.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}{{ T('easter-date-calc') }} - {{ app_name }}{% endblock %} diff --git a/templates/edit.jinja2 b/templates/edit.jinja2 index bceabbc..45374c9 100644 --- a/templates/edit.jinja2 +++ b/templates/edit.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %} diff --git a/templates/exportpages.jinja2 b/templates/exportpages.jinja2 index 574c644..136a739 100644 --- a/templates/exportpages.jinja2 +++ b/templates/exportpages.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Export pages - {{ app_name }}{% endblock %} diff --git a/templates/forbidden.jinja2 b/templates/forbidden.jinja2 index 8ac8560..83b57b4 100644 --- a/templates/forbidden.jinja2 +++ b/templates/forbidden.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Access Denied - {{ app_name }}{% endblock %} diff --git a/templates/history.jinja2 b/templates/history.jinja2 index 8760ec4..3f38d0c 100644 --- a/templates/history.jinja2 +++ b/templates/history.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Page history - {{ app_name }}{% endblock %} diff --git a/templates/home.jinja2 b/templates/home.jinja2 index f776700..2c02af0 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}{{ T('homepage') }} - {{ app_name }}{% endblock %} diff --git a/templates/importpages.jinja2 b/templates/importpages.jinja2 index 67b3a64..e0f54c7 100644 --- a/templates/importpages.jinja2 +++ b/templates/importpages.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Import pages - {{ app_name }}{% endblock %} diff --git a/templates/internalservererror.jinja2 b/templates/internalservererror.jinja2 index 53ae91f..050f4c2 100644 --- a/templates/internalservererror.jinja2 +++ b/templates/internalservererror.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Internal Server Error - {{ app_name }}{% endblock %} diff --git a/templates/leaderboard.jinja2 b/templates/leaderboard.jinja2 index 3cc88d2..b8966c7 100644 --- a/templates/leaderboard.jinja2 +++ b/templates/leaderboard.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Best pages - {{ app_name }}{% endblock %} diff --git a/templates/listrecent.jinja2 b/templates/listrecent.jinja2 index 01bb1ff..deebe66 100644 --- a/templates/listrecent.jinja2 +++ b/templates/listrecent.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block content %}

    Notes by date

    diff --git a/templates/listtag.jinja2 b/templates/listtag.jinja2 index 68ef780..4249bfe 100644 --- a/templates/listtag.jinja2 +++ b/templates/listtag.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Notes tagged #{{ tagname }} - {{ app_name }}{% endblock %} diff --git a/templates/login.jinja2 b/templates/login.jinja2 index 384a652..40c7c0d 100644 --- a/templates/login.jinja2 +++ b/templates/login.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}{{ T('login') }} – {{ app_name }}{% endblock %} diff --git a/templates/month.jinja2 b/templates/month.jinja2 index 9211ade..55ba03b 100644 --- a/templates/month.jinja2 +++ b/templates/month.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}{{ d.strftime("%B %Y") }} – {{ app_name }}{% endblock %} diff --git a/templates/notfound.jinja2 b/templates/notfound.jinja2 index 0bd7f79..5b1f7b8 100644 --- a/templates/notfound.jinja2 +++ b/templates/notfound.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Not found - {{ app_name }}{% endblock %} diff --git a/templates/register.jinja2 b/templates/register.jinja2 index cf6012f..f255136 100644 --- a/templates/register.jinja2 +++ b/templates/register.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}{{ T('sign-up') }} – {{ app_name }}{% endblock %} diff --git a/templates/search.jinja2 b/templates/search.jinja2 index df1aaea..01a7304 100644 --- a/templates/search.jinja2 +++ b/templates/search.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}{% if q %}Search results for "{{ q }}"{% else %}Search{% endif %} - {{ app_name }}{% endblock %} @@ -22,7 +22,7 @@
      {% for n in results %} -
    • {% include "includes/nl_item.html" %}
    • +
    • {% include "includes/nl_item.jinja2" %}
    • {% endfor %}
    {% elif q %} diff --git a/templates/stats.jinja2 b/templates/stats.jinja2 index 4e660ca..0c0f2b0 100644 --- a/templates/stats.jinja2 +++ b/templates/stats.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Statistics - {{ app_name }}{% endblock %} diff --git a/templates/upload.jinja2 b/templates/upload.jinja2 index e3f2266..6c044b5 100644 --- a/templates/upload.jinja2 +++ b/templates/upload.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block content %}

    Upload new file

    diff --git a/templates/uploadinfo.jinja2 b/templates/uploadinfo.jinja2 index 6eec1fa..1be8c30 100644 --- a/templates/uploadinfo.jinja2 +++ b/templates/uploadinfo.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}Info on file "{{ upl.name }}" - {{ app_name }}{% endblock %} diff --git a/templates/view.jinja2 b/templates/view.jinja2 index b66fb00..76fbb8e 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -1,4 +1,4 @@ -{% extends "base.html" %} +{% extends "base.jinja2" %} {% block title %}{{ p.title }} - {{ app_name }}{% endblock %} diff --git a/templates/viewold.jinja2 b/templates/viewold.jinja2 index 085ba4a..0ea4139 100644 --- a/templates/viewold.jinja2 +++ b/templates/viewold.jinja2 @@ -1,4 +1,4 @@ -{% extends "view.html" %} +{% extends "view.jinja2" %} {% block meta %} From a8b24f2765ea80f412c0322f421c84af95499669 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Thu, 16 Mar 2023 14:52:55 +0100 Subject: [PATCH 25/51] minor fixes + update version number --- README.md | 5 ----- app.py | 7 +++---- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 7fabd29..b8e5e23 100644 --- a/README.md +++ b/README.md @@ -46,11 +46,6 @@ directory = /path/to/database/ + You can now access Salvi in your browser at port 5000. -## Caveats - -+ All pages created are, as of now, viewable and editable by anyone, with no - trace of users and/or passwords. - ## License [MIT License](./LICENSE). diff --git a/app.py b/app.py index 26375bb..edc53fd 100644 --- a/app.py +++ b/app.py @@ -29,7 +29,7 @@ from configparser import ConfigParser import i18n import gzip -__version__ = '0.7-dev' +__version__ = '0.7.0' #### CONSTANTS #### @@ -381,7 +381,7 @@ class PageLink(BaseModel): def init_db(): database.create_tables([ - Page, PageText, PageRevision, PageTag, PageProperty, PageLink + User, Page, PageText, PageRevision, PageTag, PageProperty, PageLink ]) #### WIKI SYNTAX #### @@ -454,7 +454,6 @@ login_manager = LoginManager(app) login_manager.login_view = 'accounts_login' - #### ROUTES #### @app.before_request @@ -487,13 +486,13 @@ def _inject_variables(): def _inject_user(userid): return User[userid] - @app.template_filter() def linebreaks(text): text = html.escape(text) text = text.replace("\n\n", '

    ').replace('\n', '
    ') return Markup(text) + @app.route('/') def homepage(): page_limit = _getconf("appearance","items_per_page",20,cast=int) From 9f9525ecd1db02aac95e8a63be823c808d58787c Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Fri, 17 Mar 2023 11:40:34 +0100 Subject: [PATCH 26/51] Improved calendar view --- CHANGELOG.md | 4 ++++ README.md | 1 + app.py | 8 +++++-- migrations/0_7to0_8.py | 0 templates/calendar.jinja2 | 47 +++++++++++++++++++-------------------- 5 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 migrations/0_7to0_8.py diff --git a/CHANGELOG.md b/CHANGELOG.md index af38890..77ebe84 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # What’s New +## 0.7.1 + ++ Improved calendar view. Now `/calendar` shows a list of years and months. + ## 0.7 + Schema changes: diff --git a/README.md b/README.md index b8e5e23..a3173fa 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,7 @@ suitable as a community or team knowledge base. + Stored in SQLite/MySQL databases + Material Icons + Light/dark theme ++ Calendar + Works fine even with JavaScript disabled. ## Requirements diff --git a/app.py b/app.py index edc53fd..b3bdb4b 100644 --- a/app.py +++ b/app.py @@ -29,7 +29,7 @@ from configparser import ConfigParser import i18n import gzip -__version__ = '0.7.0' +__version__ = '0.7.1' #### CONSTANTS #### @@ -791,7 +791,11 @@ def contributions(username): @app.route('/calendar/') def calendar_view(): - return render_template('calendar.jinja2') + now = datetime.datetime.now() + return render_template('calendar.jinja2', now=now, + from_year=int(request.args.get('from_year', now.year - 12)), + till_year=int(request.args.get('till_year', now.year + 5)) + ) @app.route('/calendar//') def calendar_month(y, m): diff --git a/migrations/0_7to0_8.py b/migrations/0_7to0_8.py new file mode 100644 index 0000000..e69de29 diff --git a/templates/calendar.jinja2 b/templates/calendar.jinja2 index d4272f7..8edcc2a 100644 --- a/templates/calendar.jinja2 +++ b/templates/calendar.jinja2 @@ -5,29 +5,28 @@ {% block content %}

    Calendar

    -
    -
    - Calendar view +
      + {% for year in range(till_year, from_year-1, -1) %} +
    • + {{ year }} {% if year == now.year %}(current){% endif %}: + +
    • + {% endfor %} +
    -
    - -
      - {% set view_choices = ["month"] %} - {% for vch in view_choices %} -
    • - {% if vch == viewas %} - {{ T(vch) }} - {% else %} - {{ T(vch) }} - {% endif %} -
    • - {% endfor %} -
    -
    - -
    - -
    -
    -
    +

    + Show more years: +

    +

    {% endblock %} \ No newline at end of file From c46b07a3b293c49e1bf3bfff930299f687723562 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Fri, 17 Mar 2023 23:26:05 +0100 Subject: [PATCH 27/51] version advance, schema changes, style changes, terms and privacy policy, added installer script --- CHANGELOG.md | 13 +++ README.md | 5 ++ app.py | 163 +++++++++++++++++++++++++++++++----- app_init.py | 5 ++ migrations/0_7to0_8.py | 23 +++++ static/style.css | 11 ++- templates/base.jinja2 | 32 ++++--- templates/edit.jinja2 | 4 + templates/history.jinja2 | 4 + templates/home.jinja2 | 8 +- templates/listrecent.jinja2 | 8 +- templates/listtag.jinja2 | 20 +++-- templates/privacy.jinja2 | 46 ++++++++++ templates/register.jinja2 | 2 +- templates/terms.jinja2 | 145 ++++++++++++++++++++++++++++++++ templates/view.jinja2 | 8 ++ 16 files changed, 446 insertions(+), 51 deletions(-) create mode 100644 app_init.py create mode 100644 templates/privacy.jinja2 create mode 100644 templates/terms.jinja2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 77ebe84..63b5360 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # What’s New +## 0.8 + ++ Schema changes: + + New tables `UserGroup`, `UserGroupMembership` and `PagePermission`. + + Added flag `is_cw` to `Page`. + + Added `restrictions` field to `User`. ++ Pages now can have a Content Warning. It prevents them to show up in previews, and adds a + caution message when viewing them. ++ Added Terms and Privacy Policy. ++ Changed user page URLs (contributions page) from `/u/user` to `/@user`. ++ Style changes: added a top bar with the site title. It replaces the floating menu on the top right. ++ Added a built-in installer (`app_init.py`). You still need to manually create `site.conf`. + ## 0.7.1 + Improved calendar view. Now `/calendar` shows a list of years and months. diff --git a/README.md b/README.md index a3173fa..80617eb 100644 --- a/README.md +++ b/README.md @@ -43,9 +43,14 @@ name = Salvi directory = /path/to/database/ ``` ++ Run `python3 -m app_init` to initialize the database and create the administrator user. + Run `flask run`. + You can now access Salvi in your browser at port 5000. +## Caveats + ++ The whole application is untested. ++ If you forget the password, there is currently no way to reset it. ## License diff --git a/app.py b/app.py index b3bdb4b..94ee51f 100644 --- a/app.py +++ b/app.py @@ -28,8 +28,9 @@ from urllib.parse import quote from configparser import ConfigParser import i18n import gzip +from getpass import getpass -__version__ = '0.7.1' +__version__ = '0.8-dev' #### CONSTANTS #### @@ -65,10 +66,8 @@ if _cfp.read([CONFIG_FILE]): v = fallback return v else: - def _getconf(k1, k2, fallback=None, cast=None): - if fallback is None: - fallback = DEFAULT_CONF.get((k1, k2)) - return fallback + print('Uh oh, site.conf not found.') + exit(-1) #### OPTIONAL IMPORTS #### @@ -148,6 +147,8 @@ class User(BaseModel): karma = IntegerField(default=1) privileges = BitField() is_admin = privileges.flag(1) + restrictions = BitField() + is_disabled = restrictions.flag(1) # helpers for flask_login @property @@ -155,11 +156,42 @@ class User(BaseModel): return False @property def is_active(self): - return True + return not self.is_disabled @property def is_authenticated(self): return True +class UserGroup(BaseModel): + name = CharField(32, unique=True) + permissions = BitField() + # Can read pages. + can_read = permissions.flag(1) + # Can edit all non-locked pages. + can_edit = permissions.flag(2) + # Can create and edit their own pages. + can_create = permissions.flag(4) + # Can set page URL. + can_set_url = permissions.flag(8) + # Can change page tags. + can_set_tags = permissions.flag(16) + + @classmethod + def get_default_group(cls): + try: + return cls[1] + except cls.DoesNotExist: + return cls.get(cls.name == 'default') + +class UserGroupMembership(BaseModel): + user = ForeignKeyField(User, backref='group_memberships') + group = ForeignKeyField(UserGroup, backref='user_memberships') + since = DateTimeField(default=datetime.datetime.now) + + class Meta: + indexes = ( + (('user', 'group'), True), + ) + class Page(BaseModel): url = CharField(64, unique=True, null=True) @@ -172,6 +204,7 @@ class Page(BaseModel): is_sync = flags.flag(2) is_math_enabled = flags.flag(4) is_locked = flags.flag(8) + is_cw = flags.flag(16) @property def latest(self): if self.revisions: @@ -179,6 +212,8 @@ class Page(BaseModel): def get_url(self): return '/' + self.url + '/' if self.url else '/p/{}/'.format(self.id) def short_desc(self): + if self.is_cw: + return '(Content Warning)' full_text = self.latest.text text = remove_tags(full_text, convert = not self.is_math_enabled and not _getconf('appearance', 'simple_remove_tags', False)) return text[:200] + ('\u2026' if len(text) > 200 else '') @@ -377,13 +412,76 @@ class PageLink(BaseModel): def refresh_all_links(cls): for p in Page.select(): cls.parse_links(p, p.latest.text) - + +class PagePermission(BaseModel): + page = ForeignKeyField(Page, backref='permission_overrides') + group = ForeignKeyField(UserGroup, backref='page_permissions') + permissions = BitField() + # Can read pages. + can_read = permissions.flag(1) + # Can edit all non-locked pages. + can_edit = permissions.flag(2) + # Can create and edit their own pages. + can_create = permissions.flag(4) + # Can set page URL. + can_set_url = permissions.flag(8) + # Can change page tags. + can_set_tags = permissions.flag(16) + + class Meta: + indexes = ( + (('page', 'group'), True), + ) def init_db(): database.create_tables([ - User, Page, PageText, PageRevision, PageTag, PageProperty, PageLink + User, UserGroup, UserGroupMembership, + Page, PageText, PageRevision, PageTag, PageProperty, PageLink, + PagePermission ]) +def init_db_and_create_first_user(): + try: + User[1] + UserGroup[1] + except Exception: + pass + else: + print('Looks like this site is already set up!') + return + username = input('Username: ') + password = getpass('Password: ') + confirm_password = getpass('Confirm password: ') + email = input('Email: (optional)') or None + if not is_username(username): + print('Invalid username: usernames can contain only letters, numbers, underscores and hyphens.') + return + if password != confirm_password: + print('Passwords do not match.') + return + default_permissions = 31 # all permissions + if not input('Agree to the Terms of Use?')[0].lower() == 'y': + print('You must accept Terms in order to register.') + return + with database.atomic(): + init_db() + ua = User.create( + username = username, + email = email, + password = generate_password_hash(password), + join_date = datetime.datetime.now(), + is_admin = True + ) + ug = UserGroup.create( + name = 'default', + permissions = int(default_permissions) + ) + UserGroupMembership.create( + user = ua, + group = ug + ) + print('Installed successfully!') + #### WIKI SYNTAX #### def md(text, expand_magic=False, toc=True, math=True): @@ -456,8 +554,7 @@ login_manager.login_view = 'accounts_login' #### ROUTES #### -@app.before_request -def _before_request(): +def _get_lang(): if request.args.get('uselang') is not None: lang = request.args['uselang'] else: @@ -468,12 +565,17 @@ def _before_request(): break else: lang = 'en' - g.lang = lang + return lang + +@app.before_request +def _before_request(): + g.lang = _get_lang() @app.context_processor def _inject_variables(): + return { - 'T': partial(get_string, g.lang), + 'T': partial(get_string, _get_lang()), 'app_name': _getconf('site', 'title'), 'strong': lambda x:Markup('{0}').format(x), 'app_version': __version__, @@ -492,6 +594,7 @@ def linebreaks(text): text = text.replace("\n\n", '

    ').replace('\n', '
    ') return Markup(text) +app.template_filter(name='markdown')(md) @app.route('/') def homepage(): @@ -523,6 +626,10 @@ def error_400(body): @app.errorhandler(500) def error_500(body): + try: + User[1] + except OperationalError: + g.need_install = True return render_template('internalservererror.jinja2'), 500 # Middle point during page editing. @@ -550,7 +657,8 @@ def savepoint(form, is_preview=False, pageobj=None): preview=preview, pl_js_info=pl_js_info, pl_calendar=('usecalendar' in form) and form['calendar'], - pl_readonly=not pageobj.can_edit(current_user) if pageobj else False + pl_readonly=not pageobj.can_edit(current_user) if pageobj else False, + pl_cw=('cw' in form) and form['cw'] ) @app.route('/create/', methods=['GET', 'POST']) @@ -581,7 +689,8 @@ def create(): owner_id=current_user.id, calendar=datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None, is_math_enabled='enablemath' in request.form, - is_locked = 'lockpage' in request.form + is_locked = 'lockpage' in request.form, + is_cw = 'cw' in request.form ) p.change_tags(p_tags) except IntegrityError as e: @@ -636,6 +745,7 @@ def edit(id): p.touched = datetime.datetime.now() p.is_math_enabled = 'enablemath' in request.form p.is_locked = 'lockpage' in request.form + p.is_cw = 'cw' in request.form p.calendar = datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None p.save() p.change_tags(p_tags) @@ -767,12 +877,6 @@ def view_named(name): abort(404) return render_template('view.jinja2', p=p, rev=p.latest) -@app.route('/init-config/tables/') -def init_config_tables(): - init_db() - flash('Tables successfully created.') - return redirect('/') - @app.route('/history//') def history(id): try: @@ -782,6 +886,10 @@ def history(id): return render_template('history.jinja2', p=p, history=p.revisions.order_by(PageRevision.pub_date.desc())) @app.route('/u//') +def contributions_legacy_url(username): + return redirect(f'/@{username}/') + +@app.route('/@/') def contributions(username): try: user = User.get(User.username == username) @@ -913,6 +1021,10 @@ def accounts_register(): password = generate_password_hash(password), join_date = datetime.datetime.now() ) + UserGroupMembership.create( + user = u, + group = UserGroup.get_default_group() + ) login_user(u) return redirect(request.args.get('next', '/')) @@ -1058,7 +1170,6 @@ class Importer(object): continue return no_pages, no_revs - @app.route('/manage/export/', methods=['GET', 'POST']) def exportpages(): if request.method == 'POST': @@ -1103,6 +1214,16 @@ def importpages(): flash('Pages can be imported by Administrators only!') return render_template('importpages.jinja2') +## terms / privacy ## + +@app.route('/terms/') +def terms(): + return render_template('terms.jinja2') + +@app.route('/privacy/') +def privacy(): + return render_template('privacy.jinja2') + #### EXTENSIONS #### active_extensions = [] diff --git a/app_init.py b/app_init.py new file mode 100644 index 0000000..e872b6c --- /dev/null +++ b/app_init.py @@ -0,0 +1,5 @@ +from app import init_db_and_create_first_user + + +if __name__ == '__main__': + init_db_and_create_first_user() \ No newline at end of file diff --git a/migrations/0_7to0_8.py b/migrations/0_7to0_8.py index e69de29..92a56cb 100644 --- a/migrations/0_7to0_8.py +++ b/migrations/0_7to0_8.py @@ -0,0 +1,23 @@ +from playhouse.migrate import migrate, SqliteMigrator, MySQLMigrator +from peewee import MySQLDatabase, SqliteDatabase, \ + IntegerField, DateTimeField, ForeignKeyField, DeferredForeignKey, BitField +from app import database, UserGroup, UserGroupMembership, PagePermission + + +if type(database) == MySQLDatabase: + migrator = MySQLMigrator(database) +elif type(database) == SqliteDatabase: + migrator = SqliteMigrator(database) +else: + print("Unsupported database") + exit() + +with database.atomic(): + database.create_tables([UserGroup, UserGroupMembership, PagePermission]) + migrate( + migrator.add_column('user', 'restrictions', BitField()) + ) + UserGroup.create( + name='default', + permissions=31 + ) \ No newline at end of file diff --git a/static/style.css b/static/style.css index 49e1a57..ee5b0d7 100644 --- a/static/style.css +++ b/static/style.css @@ -22,8 +22,15 @@ /* basic styles */ * {box-sizing: border-box;} -body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-main)} +body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-main);margin:0;position:relative} .content{margin: 3em 1.3em} +.footer{text-align:center;} + +/* header styles */ +.header{padding:1em;display:flex;flex-direction:row;justify-content:space-between;position:sticky;top:0;background-color:var(--bg-alt); z-index: 99} +.header .header-blank {flex: 1; margin: 0 .5em} +.header ul{list-style:none;padding:0;margin:0;font-size:0.9em;border-left:var(--border) 1px solid} +.header ul > li{display:inline-block;padding-right:1em} /* content styles */ #firstHeading {font-family:sans-serif; text-align: center;font-size:3em; font-weight: normal} @@ -105,8 +112,6 @@ ul.inline > li:last-child::after {content: ""} /* floating elements */ -.top-menu{list-style:none;padding:0;margin:0;font-size:0.9em;position:absolute;right:0;top:.5em;text-transform:lowercase} -.top-menu li{display:inline-block;padding-right:1em} .toc{float:right} @media (max-width:639px){ .toc{display:none} diff --git a/templates/base.jinja2 b/templates/base.jinja2 index 6192923..d66af71 100644 --- a/templates/base.jinja2 +++ b/templates/base.jinja2 @@ -27,23 +27,33 @@ {% endif %} {% block json_info %}{% endblock %} - +

    +
    + {{ app_name }} +   + + +
    {% for msg in get_flashed_messages() %}
    {{ msg }}
    {% endfor %} {% block content %}{% endblock %}
    - - + {% block scripts %}{% endblock %} diff --git a/templates/edit.jinja2 b/templates/edit.jinja2 index 45374c9..b574022 100644 --- a/templates/edit.jinja2 +++ b/templates/edit.jinja2 @@ -82,6 +82,10 @@
    +
    + + +
    {% endif %} diff --git a/templates/history.jinja2 b/templates/history.jinja2 index 3f38d0c..ac47106 100644 --- a/templates/history.jinja2 +++ b/templates/history.jinja2 @@ -22,9 +22,13 @@ {% endif %} by + {% if rev.user_id %} {{ rev.user.username }} + {% else %} + <Unknown User> + {% endif %} {% endfor %} diff --git a/templates/home.jinja2 b/templates/home.jinja2 index 2c02af0..fd0f3ad 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -18,11 +18,13 @@ {{ n.title }}

    {{ n.short_desc() }}

    {% if n.tags %} -

    {{ T('tags') }}: - {% for tag in n.tags %} +

    + tag + {{ T('tags') }}: + {% for tag in n.tags %} {% set tn = tag.name %} #{{ tn }} - {% endfor %} + {% endfor %}

    {% endif %} {% if n.calendar %} diff --git a/templates/listrecent.jinja2 b/templates/listrecent.jinja2 index deebe66..fa6ae03 100644 --- a/templates/listrecent.jinja2 +++ b/templates/listrecent.jinja2 @@ -14,11 +14,13 @@ {{ n.title }}

    {{ n.short_desc() }}

    {% if n.tags %} -

    {{ T('tags') }}: - {% for tag in n.tags %} +

    + tag + {{ T('tags') }}: + {% for tag in n.tags %} {% set tn = tag.name %} #{{ tn }} - {% endfor %} + {% endfor %}

    {% endif %} {% if n.calendar %} diff --git a/templates/listtag.jinja2 b/templates/listtag.jinja2 index 4249bfe..c7c07aa 100644 --- a/templates/listtag.jinja2 +++ b/templates/listtag.jinja2 @@ -16,21 +16,23 @@
  • {{ n.title }}

    {{ n.short_desc() }}

    -

    Tags: - {% for tag in n.tags %} +

    + tag + {{ T('tags') }}: + {% for tag in n.tags %} {% set tn = tag.name %} {% if tn == tagname %} #{{ tn }} {% else %} #{{ tn }} {% endif %} - {% endfor %} - {% if n.calendar %} -

    - calendar_today - -

    - {% endif %} + {% endfor %} + {% if n.calendar %} +

    + calendar_today + +

    + {% endif %}

  • {% endfor %} diff --git a/templates/privacy.jinja2 b/templates/privacy.jinja2 new file mode 100644 index 0000000..a631f98 --- /dev/null +++ b/templates/privacy.jinja2 @@ -0,0 +1,46 @@ +{% extends "base.jinja2" %} + +{% block title %}Privacy Policy — {{ app_name }}{% endblock %} + + +{% block content %} + +

    Privacy Policy

    + +
    +{% filter markdown %} +# Privacy + +## Cookies + +{{ app_name }} uses one cryptographically signed cookie to maintain secure authenticated sessions. This cookie is randomized with each new session. + +This cookie is used for the following purposes: + +* Authentication +* Abuse mitigation +* Per-device settings like light/dark theme selection. + +This cookie is mandatory and cannot be opted out. Manual alteration or removal of this cookie will result in deauthentication and reversion to default per-device settings. + +## Your information + +We collect basic information in order to operate the platform, and to mitigate abuse. This includes: + +* Your email address, if you choose to provide it. +* The IP address from which content is submitted. +* Whether or not you have indicated that you are 18 years of age. +* Other saved personal preferances, which you can edit from your account settings. + +We use - and then promptly forget - the following information about you from third parties: + +* Verification from our captcha provider that you aren't a bot, during account registration + +{{ app_name }} will not release your information to third parties, except: + +* As required by Italian law +* At our discretion, {{ app_name }} may share your information with Italian law enforcement in the event of an emergency, if we have good cause to believe that doing so would avert or mitigate the emergency. +{% endfilter %} + +
    +{% endblock %} diff --git a/templates/register.jinja2 b/templates/register.jinja2 index f255136..5b2f9d2 100644 --- a/templates/register.jinja2 +++ b/templates/register.jinja2 @@ -25,7 +25,7 @@
    - +
    diff --git a/templates/terms.jinja2 b/templates/terms.jinja2 new file mode 100644 index 0000000..e02b8a6 --- /dev/null +++ b/templates/terms.jinja2 @@ -0,0 +1,145 @@ +{% extends "base.jinja2" %} + +{% block title %}Terms of Service — {{ app_name }}{% endblock %} + +{% block content %} +

    Terms of Service

    + +
    +{% filter markdown %} +## Scope and Definitions + +These terms of service ("Terms") are between {{ app_name }} +("{{ request.headers['Host'] }}") and You, regarding Your use of the social platform +{{ app_name }}, and any other services or test servers which may exist +now or in the future (collectively "{{ app_name }}"). + +Content on this page displayed in blockquotes is provided for convenience +and readability only. Blockquote text is not part of the terms of service. + +## Conduct and content + +You agree to follow the Content Policy [here](/rules/). +The Content Policy is incorporated into these Terms by this reference. + +## Age + +You warrant that You are at least 13 years old. +You warrant that You are at least 18 years old, +if You indicate as such in Your user settings. + + +## Account Access + +Your {{ app_name }} account is nontransferable. You will not buy, sell, gift, loan, or otherwise grant a third party access to Your {{ app_name }} account. You will not provide a third party with Your {{ app_name }} password or two-factor authentication codes. + +In the event that Your {{ app_name }} account is accessed by a third party anyways, You remain accountable for all actions taken by Your account. + +## Liability + +{{ app_name }} shall not be liable for Your damages arising from any of the following: + +* Use of the {{ app_name }} platform +* Technical failure of {{ app_name }} +* Administrative actions performed by {{ app_name }}, including both automatic and manual actions +* Unauthorized access of a third party to Your {{ app_name }} account +* Loss or breach of {{ app_name }} data +* Force majeur + +You accept full liability for all content You upload to, or embed within, {{ app_name }}, and indemnify {{ app_name }} from all such liability. + +> You are responsible for everything You do, and everything that happens with, with Your {{ app_name }} account. Don't use {{ app_name }} to store important data or sensitive data. + +## Intellectual Property + +You grant {{ app_name }} a permanent, worldwide, and irrevocable license to store, modify, copy, and distribute all content that You submit to or upload to {{ app_name }}, including any creative works. Additionally, You grant {{ app_name }} a permanent, worldwide, and irrevocable license to sublicense these same rights to {{ app_name }}'s service providers. Additionally, You warrant that You are authorized to grant {{ app_name }} these rights. + +You retain all other intellectual property rights, including ownership. + +> We need to be able to store, copy, and distribute Your content, because that's just how websites like {{ app_name }} work. We also need to be able to modify it for security and formatting purposes. + +## Acceptable Use + +You will not use {{ app_name }} as a means to support any other website or service without written permission by {{ app_name }}. + +You will not attempt to gain access to a {{ app_name }} account that does not belong to You. + +You will not use {{ app_name }} to engage in child sexual abuse activities, as defined in the [CSAM Policy](/help/csam). The CSAM Policy is incorporated into these terms by this reference. + +## Multiple Accounts + +You are permitted to create and use multiple {{ app_name }} accounts. + +You will not use multiple accounts to circumvent per-user limits. + +Additionally, these terms apply to all of Your accounts, which may exist now or in the future. {{ app_name }} reserves the right to take administrative action against all of Your accounts in the event of a terms of service violation on any of Your accounts. + +## Your Privacy + +{{ app_name }} will never intentionally share Your personal information with third parties, except: + +* As required by United States law. +* We will share Your data with our own service providers that are necessary for {{ app_name }} operations. +* At our discretion, {{ app_name }} may share Your information with United States law enforcement in the event of an emergency, if we have good cause to believe that doing so would avert or mitigate the emergency. + +{{ app_name }} will make all reasonable efforts to ensure that user information is kept confidential. + +## Legal Forms + +{{ app_name }} grants You the ability to use Your {{ app_name }} account to send us certain types of legal forms through our help portal. These include, but are not limited to: + +* DMCA takedown request +* DMCA takedown counter-request +* Subpoenas and court orders + +You acknowledge that these forms are provided for the sake of convenience and speed, and that {{ app_name }} does not waive any of its rights regarding legal process, including the right to object for lack of proper service. + +You agree to refrain from making abusive, frivolous, or otherwise improper submissions to these forms. This includes, but is not limited to: + +* Nonsensical or incomplete requests +* Duplicate submissions +* DMCA takedown requests not made in good faith +* DMCA takedown requests for content protected by Fair Use +* DMCA takedown requests for content that you are not authorized to claim. +* Legal demands not based in fact and/or not backed by appropriate law. +* Legal documents from courts or law enforcement that lack jurisdiction inside the United States +* Legal documents that are not in English and are not accompanied by an official English translation. + +## Enforcement + +{{ app_name }} retains sole discretion in all determinations regarding content policy and terms of service violations. + +{{ app_name }} reserves the right to remove content that we deem to violate content policy or the terms of service, and to deactivate accounts responsible for such violations. + +## Severability + +If one clause of these Terms or the Content Policy is determined by a court to be unenforceable, the remainder of the Terms and Content Policy shall remain in force. + +## No Implied Waiver + +A failure by {{ app_name }} in one or more instances to insist upon Your strict adherence to these terms or the Content Policy, shall not be construed as a waiver of any continuing or subsequent violations of these terms or the Content Policy. + +## Completeness + +These Terms, together with the other policies incorporated into them by reference, contain all the terms and conditions agreed upon by You and {{ app_name }} regarding Your use of the {{ app_name }} service. No other agreement, oral or otherwise, will be deemed to exist or to bind either of the parties to this Agreement. + +## Governing Law + +These terms of service are governed by, and shall be interpreted in accordance with, the laws of Italy. You consent to the sole jurisdiction of Italy for all disputes between You and {{ app_name }}, and You consent to the sole application of Italian and E.U. law for all such disputes. + +## Updates + +{{ app_name }} may periodically update these terms of service and/or the Content Policy. When this happens, {{ app_name }} will make reasonable efforts to notify You of such changes. + +Whenever {{ app_name }} updates these terms of service or the Content Policy, Your continued use of the {{ app_name }} platform constitutes Your agreement to the updated terms of service. + +## Translations + +If there is any inconsistency between these terms and any translation into other languages, the English language version takes precedence. + + + +{% endfilter %} + +
    +{% endblock %} \ No newline at end of file diff --git a/templates/view.jinja2 b/templates/view.jinja2 index 76fbb8e..14f3f81 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -19,6 +19,14 @@ {% block history_nav %}{% endblock %} + {% if p.is_cw %} +
    + Content Warning - + this page may contain shocking or unexpected content, spoilers, or similar. + Proceed with caution. +
    + {% endif %} +
    {{ rev.html(math = request.args.get('math') not in ['0', 'false', 'no', 'off'])|safe }}
    From 4536f7fbd96e46c04d6bfc52fd7200977de5fa95 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Mon, 20 Mar 2023 10:40:39 +0100 Subject: [PATCH 28/51] print style fixes --- static/style.css | 1 + 1 file changed, 1 insertion(+) diff --git a/static/style.css b/static/style.css index ee5b0d7..e139c73 100644 --- a/static/style.css +++ b/static/style.css @@ -119,6 +119,7 @@ ul.inline > li:last-child::after {content: ""} .backontop{position:fixed;bottom:0;right:0} @media print{ .backontop {display:none} + .header{position:static} } #__top{position:absolute;top:0;left:0} From 191e2351375596934810d403e9f6f3a282dd3323 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 22 Mar 2023 09:49:38 +0100 Subject: [PATCH 29/51] added /manage/ and /manage/accounts/ views, added TOC to template --- CHANGELOG.md | 3 ++ app.py | 62 +++++++++++++++++++++++++++------ static/style.css | 9 ++--- templates/administration.jinja2 | 23 ++++++++++++ templates/base.jinja2 | 1 + templates/contributions.jinja2 | 17 +++++++-- templates/manageaccounts.jinja2 | 52 +++++++++++++++++++++++++++ templates/notfound.jinja2 | 4 +-- templates/view.jinja2 | 12 +++++-- 9 files changed, 162 insertions(+), 21 deletions(-) create mode 100644 templates/administration.jinja2 create mode 100644 templates/manageaccounts.jinja2 diff --git a/CHANGELOG.md b/CHANGELOG.md index 63b5360..a030905 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,9 @@ caution message when viewing them. + Added Terms and Privacy Policy. + Changed user page URLs (contributions page) from `/u/user` to `/@user`. ++ `/manage/` is now a list of all managing options, including export/import and the brand new + `/manage/accounts`. ++ TOC is now shown in pages where screen width is greater than 960 pixels. + Style changes: added a top bar with the site title. It replaces the floating menu on the top right. + Added a built-in installer (`app_init.py`). You still need to manually create `site.conf`. diff --git a/app.py b/app.py index 94ee51f..95f0ad4 100644 --- a/app.py +++ b/app.py @@ -161,6 +161,12 @@ class User(BaseModel): def is_authenticated(self): return True + def groups(self): + return ( + UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group) + .where(UserGroupMembership.user == self) + ) + class UserGroup(BaseModel): name = CharField(32, unique=True) permissions = BitField() @@ -295,6 +301,8 @@ class PageRevision(BaseModel): return self.textref.get_content() def html(self, *, math=True): return md(self.text, math=self.page.is_math_enabled and math) + def html_and_toc(self, *, math=True): + return md_and_toc(self.text, math=self.page.is_math_enabled and math) def human_pub_date(self): delta = datetime.datetime.now() - self.pub_date T = partial(get_string, g.lang) @@ -484,7 +492,7 @@ def init_db_and_create_first_user(): #### WIKI SYNTAX #### -def md(text, expand_magic=False, toc=True, math=True): +def md_and_toc(text, expand_magic=False, toc=True, math=True): if expand_magic: # DEPRECATED seeking for a better solution. warnings.warn('Magic words are no more supported.', DeprecationWarning) @@ -502,9 +510,16 @@ def md(text, expand_magic=False, toc=True, math=True): 'insert_fonts_css': not _getconf('site', 'katex_url') } try: - return markdown.Markdown(extensions=extensions, extension_configs=extension_configs).convert(text) + converter = markdown.Markdown(extensions=extensions, extension_configs=extension_configs) + if toc: + return converter.convert(text), converter.toc + else: + return converter.convert(text), '' except Exception as e: - return '

    There was an error during rendering: {e.__class__.__name__}: {e}

    '.format(e=e) + return '

    There was an error during rendering: {e.__class__.__name__}: {e}

    '.format(e=e), '' + +def md(text, expand_magic=False, toc=True, math=True): + return md_and_toc(text, expand_magic=expand_magic, toc=toc, math=math)[0] def remove_tags(text, convert=True, headings=True): if headings: @@ -538,7 +553,7 @@ def is_url_available(url): forbidden_urls = [ 'about', 'accounts', 'ajax', 'backlinks', 'calendar', 'circles', 'create', - 'easter', 'edit', 'embed', 'help', 'history', 'init-config', 'kt', + 'easter', 'edit', 'embed', 'group', 'help', 'history', 'init-config', 'kt', 'manage', 'media', 'p', 'privacy', 'protect', 'search', 'static', 'stats', 'tags', 'terms', 'u', 'upload', 'upload-info' ] @@ -783,11 +798,7 @@ def edit(id): @app.route("/__sync_start") def __sync_start(): - if _getconf("sync", "master", "this") == "this": - abort(403) - from app_sync import main - main() - flash("Successfully synced messages.") + flash("Sync is unavailable. Please import and export pages manually.") return redirect("/") @app.route('/_jsoninfo/', methods=['GET', 'POST']) @@ -895,7 +906,15 @@ def contributions(username): user = User.get(User.username == username) except User.DoesNotExist: abort(404) - return render_template('contributions.jinja2', u=user, contributions=user.contributions.order_by(PageRevision.pub_date.desc())) + contributions = user.contributions.order_by(PageRevision.pub_date.desc()) + page = int(request.args.get('page', 1)) + return render_template('contributions.jinja2', + u=user, + contributions=contributions.paginate(page), + page_n=page, + total_count=contributions.count(), + min=min + ) @app.route('/calendar/') def calendar_view(): @@ -1083,6 +1102,12 @@ def easter_y(y=None): else: return render_template('easter.jinja2') +## administration ## + +@app.route('/manage/') +def manage_main(): + return render_template('administration.jinja2') + ## import / export ## class Exporter(object): @@ -1214,6 +1239,23 @@ def importpages(): flash('Pages can be imported by Administrators only!') return render_template('importpages.jinja2') +@app.route('/manage/accounts/', methods=['GET', 'POST']) +@login_required +def manage_accounts(): + users = User.select().order_by(User.join_date.desc()) + page = int(request.args.get('page', 1)) + if request.method == 'POST': + if current_user.is_admin: + pass + else: + flash('Operation not permitted!') + return render_template('manageaccounts.jinja2', + users=users.paginate(page), + page_n=page, + total_count=users.count(), + min=min + ) + ## terms / privacy ## @app.route('/terms/') diff --git a/static/style.css b/static/style.css index e139c73..e30a246 100644 --- a/static/style.css +++ b/static/style.css @@ -23,7 +23,7 @@ /* basic styles */ * {box-sizing: border-box;} body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-main);margin:0;position:relative} -.content{margin: 3em 1.3em} +.content{margin: 3em 1.3em; position: relative} .footer{text-align:center;} /* header styles */ @@ -112,9 +112,10 @@ ul.inline > li:last-child::after {content: ""} /* floating elements */ -.toc{float:right} -@media (max-width:639px){ - .toc{display:none} +nav.toc{display:none} +@media only screen and (min-width:960px){ + nav.toc{display:block;position:absolute; top: 0; right: 0; width: 320px;} + .inner-content {margin-right: 320px} } .backontop{position:fixed;bottom:0;right:0} @media print{ diff --git a/templates/administration.jinja2 b/templates/administration.jinja2 new file mode 100644 index 0000000..39ce634 --- /dev/null +++ b/templates/administration.jinja2 @@ -0,0 +1,23 @@ +{% extends "base.jinja2" %} + +{% block title %}Administrative tools — {{ app_name }}{% endblock %} + +{% block content %} +

    Administrative tools

    + +{% if current_user and current_user.is_admin %} + +{% else %} +

    Administrative tools can be accessed by administrator users only.

    +{% endif %} +{% endblock %} \ No newline at end of file diff --git a/templates/base.jinja2 b/templates/base.jinja2 index d66af71..288bd0a 100644 --- a/templates/base.jinja2 +++ b/templates/base.jinja2 @@ -52,6 +52,7 @@
    {{ msg }}
    {% endfor %} {% block content %}{% endblock %} + {% block toc %}{% endblock %}
    diff --git a/templates/contributions.jinja2 b/templates/contributions.jinja2 index 69cc4db..7ddbf6f 100644 --- a/templates/contributions.jinja2 +++ b/templates/contributions.jinja2 @@ -7,10 +7,17 @@ {% endblock %} {% block content %} -

    Contributions of {{ u.username }} +

    Contributions of {{ u.username }}

    + +

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    {% endblock %} diff --git a/templates/manageaccounts.jinja2 b/templates/manageaccounts.jinja2 new file mode 100644 index 0000000..0f42c0e --- /dev/null +++ b/templates/manageaccounts.jinja2 @@ -0,0 +1,52 @@ +{% extends "base.jinja2" %} + +{% block title %}Manage accounts - {{ app_name }}{% endblock %} + +{% block content %} +

    Manage accounts

    + +{% if current_user.is_admin %} +

    + Here is the list of users registered on {{ app_name }}, in reverse chronological order. + Beware: you are managing sensitive informations. +

    + +

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    + +
    + + +
      + {% if page_n > 1 %} +
    • « Previous page
    • + {% endif %} + + {% for u in users %} +
    • + + {{ u.username }} + {% if u == current_user %}(you){% endif %} + - + Groups: +
        + {% for ug in u.groups() %} +
      • {{ ug.name }}
      • + {% endfor %} +
      + - + Registered on: + {{ u.join_date }} +
    • + {% endfor %} + + {% if page_n < total_count // 20 %} +
    • Next page »
    • + {% endif %} +
    + + +
    +{% else %} +

    Managing accounts can be done by users with Admin permissions only.

    +{% endif %} +{% endblock %} \ No newline at end of file diff --git a/templates/notfound.jinja2 b/templates/notfound.jinja2 index 5b1f7b8..eb4e06b 100644 --- a/templates/notfound.jinja2 +++ b/templates/notfound.jinja2 @@ -1,9 +1,9 @@ {% extends "base.jinja2" %} -{% block title %}Not found - {{ app_name }}{% endblock %} +{% block title %}{{ T('not-found') }} - {{ app_name }}{% endblock %} {% block content %}

    {{ T('not-found') }}

    -

    {{ T('not-found-text-1')}} {{ request.path }} {{ T('not-found-text-2' )}}.

    +

    {{ T('not-found-text-1') }} {{ request.path }} {{ T('not-found-text-2') }}.

    {% endblock %} diff --git a/templates/view.jinja2 b/templates/view.jinja2 index 14f3f81..8205317 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -4,12 +4,14 @@ {% block json_info %}{% endblock %} +{% set html_and_toc = rev.html_and_toc(math = request.args.get('math') not in ['0', 'false', 'no', 'off']) %} + {% block content %}

    {{ p.title }}

    {% if p.calendar %} -

    calendar_today{{ p.calendar.strftime('%B %-d, %Y') }} +

    calendar_today{{ p.calendar.strftime('%B %-d, %Y') }}

    {% endif %}
    @@ -28,7 +30,7 @@ {% endif %}
    - {{ rev.html(math = request.args.get('math') not in ['0', 'false', 'no', 'off'])|safe }} + {{ html_and_toc[0]|safe }}
    {% if p.tags %} @@ -54,6 +56,12 @@ {{ T('owner') }}: {{ p.owner.username }} {% endblock %} +{% block toc %} + +{% endblock %} + {% block scripts %} {% endblock %} From 63a2b60f1dfc6cfef78f6d217c305a1ba42a9d01 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Thu, 6 Apr 2023 22:10:55 +0200 Subject: [PATCH 30/51] Add .article-header and .article-actions classes to view.html --- app.py | 2 +- i18n/salvi.en.json | 5 ++++- i18n/salvi.it.json | 5 ++++- static/style.css | 1 + templates/edit.jinja2 | 4 ++-- templates/view.jinja2 | 26 +++++++++++++++++--------- 6 files changed, 29 insertions(+), 14 deletions(-) diff --git a/app.py b/app.py index 95f0ad4..0a22c55 100644 --- a/app.py +++ b/app.py @@ -258,7 +258,7 @@ class Page(BaseModel): return True def is_owned_by(self, user): return user.id == self.owner.id - + class PageText(BaseModel): content = BlobField() diff --git a/i18n/salvi.en.json b/i18n/salvi.en.json index b49cb7a..42a17cf 100644 --- a/i18n/salvi.en.json +++ b/i18n/salvi.en.json @@ -13,6 +13,7 @@ "last-changed": "Last changed", "page-id": "Page ID", "action-edit": "Edit", + "action-view-source": "View source", "action-history": "History", "tags": "Tags", "old-revision-notice": "Showing an old revision of the page as of", @@ -58,6 +59,8 @@ "not-logged-in": "Not logged in", "owner": "Owner", "page-created": "Page created", - "write-a-comment": "Write a comment…" + "write-a-comment": "Write a comment…", + "input-tags": "Tags (comma separated)", + "no-tags": "No tags" } } \ No newline at end of file diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index a01ce65..c0daee9 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -13,6 +13,7 @@ "last-changed": "Ultima modifica", "page-id": "ID pagina", "action-edit": "Modifica", + "action-view-source": "Visualizza sorgente", "action-history": "Cronologia", "tags": "Etichette", "old-revision-notice": "\u00c8 mostrata una revisione vecchia della pagina, risalente al", @@ -48,6 +49,8 @@ "revision-count-per-page": "Media di revisioni per pagina", "remember-me-for": "Ricordami per", "owner": "Proprietario", - "write-a-comment": "Scrivi un commento…" + "write-a-comment": "Scrivi un commento…", + "input-tags": "Etichette (separate da virgola)", + "no-tags": "Nessuna etichetta" } } \ No newline at end of file diff --git a/static/style.css b/static/style.css index e30a246..6174cff 100644 --- a/static/style.css +++ b/static/style.css @@ -33,6 +33,7 @@ body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-mai .header ul > li{display:inline-block;padding-right:1em} /* content styles */ +.article-header {text-align: center;} #firstHeading {font-family:sans-serif; text-align: center;font-size:3em; font-weight: normal} .inner-content{font-family:serif; margin: 1.7em auto; max-width: 1280px; line-height: 1.9; color: var(--fg-main)} .inner-content em,.inner-content strong{color: var(--fg-sharp)} diff --git a/templates/edit.jinja2 b/templates/edit.jinja2 index b574022..3ccbc37 100644 --- a/templates/edit.jinja2 +++ b/templates/edit.jinja2 @@ -57,8 +57,8 @@
    - - + +
    {% if not pl_readonly %}
    diff --git a/templates/view.jinja2 b/templates/view.jinja2 index 8205317..139a63b 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -8,16 +8,24 @@ {% block content %}
    -

    {{ p.title }}

    +
    +

    {{ p.title }}

    - {% if p.calendar %} -

    calendar_today{{ p.calendar.strftime('%B %-d, %Y') }}

    - {% endif %} - -
    - {{ T('last-changed') }} {{ rev.human_pub_date() }} · - {{ T('jump-to-actions') }} -
    + {% if p.calendar %} +

    calendar_today{{ p.calendar.strftime('%B %-d, %Y') }}

    + {% endif %} + + +
    {% block history_nav %}{% endblock %} From 0801e841ad3bf178de0818ef53f74e3075fdedae Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Thu, 6 Apr 2023 23:02:07 +0200 Subject: [PATCH 31/51] Improve calendar and nl-list --- CHANGELOG.md | 2 + app.py | 7 +++- i18n/salvi.en.json | 5 ++- i18n/salvi.it.json | 14 ++++++- static/style.css | 6 +++ templates/calendar.jinja2 | 46 +++++++++++----------- templates/home.jinja2 | 66 ++++++++++++++++--------------- templates/listrecent.jinja2 | 64 +++++++++++++++--------------- templates/listtag.jinja2 | 77 ++++++++++++++++++++----------------- templates/month.jinja2 | 61 ++++++++++++++++++----------- templates/stats.jinja2 | 1 + templates/view.jinja2 | 7 +++- 12 files changed, 210 insertions(+), 146 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a030905..9855143 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,8 @@ `/manage/accounts`. + TOC is now shown in pages where screen width is greater than 960 pixels. + Style changes: added a top bar with the site title. It replaces the floating menu on the top right. ++ Now logged-in users have an “Edit” button below the first heading. All users can access page history + by clicking the last modified time. + Added a built-in installer (`app_init.py`). You still need to manually create `site.conf`. ## 0.7.1 diff --git a/app.py b/app.py index 0a22c55..36e198d 100644 --- a/app.py +++ b/app.py @@ -930,8 +930,10 @@ def calendar_month(y, m): (datetime.date(y, m, 1) <= Page.calendar) & (Page.calendar < datetime.date(y+1 if m==12 else y, 1 if m==12 else m+1, 1)) ).order_by(Page.calendar) + page = int(request.args.get('page', 1)) - return render_template('month.jinja2', d=datetime.date(y, m, 1), notes=notes) + return render_template('month.jinja2', d=datetime.date(y, m, 1), notes=notes.paginate(page), + page_n=page, total_count=notes.count(), min=min) @app.route('/history/revision//') def view_old(revisionid): @@ -984,7 +986,8 @@ def stats(): notes_count=Page.select().count(), notes_with_url=Page.select().where(Page.url != None).count(), revision_count=PageRevision.select().count(), - users_count = User.select().count() + users_count = User.select().count(), + groups_count = UserGroup.select().count() ) ## account management ## diff --git a/i18n/salvi.en.json b/i18n/salvi.en.json index 42a17cf..91bb088 100644 --- a/i18n/salvi.en.json +++ b/i18n/salvi.en.json @@ -61,6 +61,9 @@ "page-created": "Page created", "write-a-comment": "Write a comment…", "input-tags": "Tags (comma separated)", - "no-tags": "No tags" + "no-tags": "No tags", + "notes-month-empty": "None found :(", + "calendar": "Calendar", + "groups-count": "User group count" } } \ No newline at end of file diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index c0daee9..2dd878a 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -48,9 +48,21 @@ "revision-count": "Numero di revisioni", "revision-count-per-page": "Media di revisioni per pagina", "remember-me-for": "Ricordami per", + "confirm-password": "Conferma password", + "email": "E-mail", + "optional": "opzionale", + "have-read-terms": "Ho letto i {0} e la {1}", + "terms-of-service": "Termini di Servizio", + "privacy-policy": "Politica sulla riservatezza", + "already-have-account": "Hai già un account?", + "logged-in-as": "Autenticato come", + "not-logged-in": "Non autenticato", "owner": "Proprietario", "write-a-comment": "Scrivi un commento…", "input-tags": "Etichette (separate da virgola)", - "no-tags": "Nessuna etichetta" + "no-tags": "Nessuna etichetta", + "notes-month-empty": "Non c\u2019\u00e8 nulla :(", + "calendar": "Calendario", + "groups-count": "Numero di gruppi utente" } } \ No newline at end of file diff --git a/static/style.css b/static/style.css index 6174cff..c2daa20 100644 --- a/static/style.css +++ b/static/style.css @@ -35,6 +35,7 @@ body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-mai /* content styles */ .article-header {text-align: center;} #firstHeading {font-family:sans-serif; text-align: center;font-size:3em; font-weight: normal} +@media (min-width:800px) {.homepage #firstHeading {font-size: 4.5em}} .inner-content{font-family:serif; margin: 1.7em auto; max-width: 1280px; line-height: 1.9; color: var(--fg-main)} .inner-content em,.inner-content strong{color: var(--fg-sharp)} .inner-content h1{color: var(--fg-error)} @@ -64,6 +65,7 @@ body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-mai .nl-new{margin:6px 0 12px 0;display:flex;justify-content:start; float: right} .nl-new > a{margin-right:12px} .nl-prev,.nl-next{text-align:center} +.nl-placeholder {font-style: italic; text-align: center;} input{border:0;border-bottom:3px solid var(--border);font:inherit;color:var(--fg-main);background-color:transparent} input:focus{color:var(--fg-sharp);border-bottom-color:var(--border-sharp)} input.error{border-bottom-color:var(--btn-error)} @@ -161,6 +163,10 @@ a:hover{color:var(--fg-link-hover)} .nl-list {display: grid; grid-template-rows: auto; grid-template-columns: 1fr 1fr; column-gap: 1.5em} .nl-list > .nl-prev, .nl-list > .nl-next {grid-column-end: span 2} } +@media (min-width:1200px){ + .nl-list {grid-template-columns: 1fr 1fr 1fr} + .nl-list > .nl-prev, .nl-list > .nl-next {grid-column-end: span 3} +} /* dark theme */ diff --git a/templates/calendar.jinja2 b/templates/calendar.jinja2 index 8edcc2a..2d092f8 100644 --- a/templates/calendar.jinja2 +++ b/templates/calendar.jinja2 @@ -3,30 +3,32 @@ {% block title %}Calendar – {{ app_name }}{% endblock %} {% block content %} -

    Calendar

    +
    +

    {{ T('calendar') }}

    -
      - {% for year in range(till_year, from_year-1, -1) %} -
    • - {{ year }} {% if year == now.year %}(current){% endif %}: - -
    • - {% endfor %} -
    - -

    - Show more years: -

    {% endblock %} \ No newline at end of file diff --git a/templates/home.jinja2 b/templates/home.jinja2 index fd0f3ad..971648f 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -3,39 +3,43 @@ {% block title %}{{ T('homepage') }} - {{ app_name }}{% endblock %} {% block content %} -

    {{ T('welcome').format(app_name) }}

    +
    +

    {{ T('welcome').format(app_name) }}

    - + -

    {{ T('latest-notes') }}

    +

    {{ T('latest-notes') }}

    -
    -
      - {% for n in new_notes %} -
    • - {{ n.title }} -

      {{ n.short_desc() }}

      - {% if n.tags %} -

      - tag - {{ T('tags') }}: - {% for tag in n.tags %} - {% set tn = tag.name %} - #{{ tn }} - {% endfor %} -

      - {% endif %} - {% if n.calendar %} -

      - calendar_today - -

      - {% endif %} -
    • - {% endfor %} -
    • {{ T('show-all') }}
    • -
    +
    + +
    {% endblock %} diff --git a/templates/listrecent.jinja2 b/templates/listrecent.jinja2 index fa6ae03..2634cfe 100644 --- a/templates/listrecent.jinja2 +++ b/templates/listrecent.jinja2 @@ -1,38 +1,42 @@ {% extends "base.jinja2" %} {% block content %} -

    Notes by date

    +
    +

    Notes by date

    -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    +

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    -
      - {% if page_n > 1 %} -
    • « Previous page
    • - {% endif %} - {% for n in notes %} -
    • - {{ n.title }} -

      {{ n.short_desc() }}

      - {% if n.tags %} -

      - tag - {{ T('tags') }}: - {% for tag in n.tags %} - {% set tn = tag.name %} - #{{ tn }} - {% endfor %} -

      +
        + {% if page_n > 1 %} +
      • « Previous page
      • {% endif %} - {% if n.calendar %} -

        - calendar_today - -

        + {% for n in notes %} +
      • + {{ n.title }} +

        {{ n.short_desc() }}

        + {% if n.tags %} +

        + tag + {{ T('tags') }}: + {% for tag in n.tags %} + {% set tn = tag.name %} + #{{ tn }} + {% endfor %} +

        + {% endif %} + {% if n.calendar %} +

        + calendar_today + + + +

        + {% endif %} +
      • + {% endfor %} + {% if page_n <= total_count // 20 %} +
      • Next page »
      • {% endif %} - - {% endfor %} - {% if page_n <= total_count // 20 %} -
      • Next page »
      • - {% endif %} -
      +
    +
    {% endblock %} diff --git a/templates/listtag.jinja2 b/templates/listtag.jinja2 index c7c07aa..46d6ec3 100644 --- a/templates/listtag.jinja2 +++ b/templates/listtag.jinja2 @@ -3,46 +3,51 @@ {% block title %}Notes tagged #{{ tagname }} - {{ app_name }}{% endblock %} {% block content %} -

    {{ T('notes-tagged') }} #{{ tagname }}

    +
    +

    #{{ tagname }}

    +
    {{ T('notes-tagged') }}
    -{% if total_count > 0 %} -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    + {% if total_count > 0 %} +

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    -
      - {% if page_n > 1 %} -
    • « Previous page
    • - {% endif %} - {% for n in tagged_notes %} -
    • - {{ n.title }} -

      {{ n.short_desc() }}

      -

      - tag - {{ T('tags') }}: - {% for tag in n.tags %} - {% set tn = tag.name %} - {% if tn == tagname %} - #{{ tn }} - {% else %} - #{{ tn }} - {% endif %} - {% endfor %} - {% if n.calendar %} -

      - calendar_today - +

        + {% if page_n > 1 %} +
      • « Previous page
      • + {% endif %} + {% for n in tagged_notes %} +
      • + {{ n.title }} +

        {{ n.short_desc() }}

        +

        + tag + {{ T('tags') }}: + {% for tag in n.tags %} + {% set tn = tag.name %} + {% if tn == tagname %} + #{{ tn }} + {% else %} + #{{ tn }} + {% endif %} + {% endfor %} + {% if n.calendar %} +

        + calendar_today + + + +

        + {% endif %}

        - {% endif %} -

        -
      • - {% endfor %} - {% if page_n < total_count // 20 %} -
      • Next page »
      • + + {% endfor %} + {% if page_n < total_count // 20 %} +
      • Next page »
      • + {% endif %} +
      + {% else %} +

      {{ T('notes-tagged-empty') }}

      {% endif %} -
    -{% else %} -

    {{ T('notes-tagged-empty') }}

    -{% endif %} +
    {% endblock %} diff --git a/templates/month.jinja2 b/templates/month.jinja2 index 55ba03b..3e9cd31 100644 --- a/templates/month.jinja2 +++ b/templates/month.jinja2 @@ -3,30 +3,47 @@ {% block title %}{{ d.strftime("%B %Y") }} – {{ app_name }}{% endblock %} {% block content %} -

    {{ d.strftime("%B %Y") }}

    +
    +

    {{ d.strftime("%B %Y") }}

    -
      - {% for n in notes %} -
    • - {{ n.title }} -

      {{ n.short_desc() }}

      - {% if n.tags %} -

      {{ T('tags') }}: - {% for tag in n.tags %} - {% set tn = tag.name %} - #{{ tn }} + {% if total_count > 0 %} +

      Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

      + +
        + {% for n in notes %} +
      • + {{ n.title }} +

        {{ n.short_desc() }}

        + {% if n.tags %} +

        {{ T('tags') }}: + {% for tag in n.tags %} + {% set tn = tag.name %} + #{{ tn }} + {% endfor %} +

        + {% endif %} + {% if n.calendar %} +

        + calendar_today + {% if n.calendar.year == d.year and n.calendar.month == d.month %} + + + + {% else %} + + + + {% endif %} +

        + {% endif %} +
      • {% endfor %} -

        - {% endif %} - {% if n.calendar %} -

        - calendar_today - -

        - {% endif %} - - {% endfor %} -
      +
    + {% else %} +

    {{ T('notes-month-empty') }}

    + {% endif %} +

    {{ T('back-to') }} {{ T('calendar') }}

    +
    {% endblock %} \ No newline at end of file diff --git a/templates/stats.jinja2 b/templates/stats.jinja2 index 0c0f2b0..1f0812a 100644 --- a/templates/stats.jinja2 +++ b/templates/stats.jinja2 @@ -11,5 +11,6 @@
  • {{ T("revision-count") }}: {{ revision_count }}
  • {{ T("revision-count-per-page") }}: {{ (revision_count / notes_count)|round(2) }}
  • {{ T('users-count') }}: {{ users_count }}
  • +
  • {{ T('groups-count') }}: {{ groups_count }}
  • {% endblock %} diff --git a/templates/view.jinja2 b/templates/view.jinja2 index 139a63b..0b82b77 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -12,7 +12,12 @@

    {{ p.title }}

    {% if p.calendar %} -

    calendar_today{{ p.calendar.strftime('%B %-d, %Y') }}

    +

    + calendar_today + + + +

    {% endif %}
      From a24386c45e22aa13f577b5eec78e854136116b35 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Tue, 16 May 2023 22:38:21 +0200 Subject: [PATCH 32/51] Add Page.get_perms() + more material icons --- app.py | 42 +++++++++++++++++++++++++++++---- templates/base.jinja2 | 4 ++-- templates/contributions.jinja2 | 2 +- templates/home.jinja2 | 4 +++- templates/manageaccounts.jinja2 | 4 ++-- 5 files changed, 46 insertions(+), 10 deletions(-) diff --git a/app.py b/app.py index 36e198d..e358abf 100644 --- a/app.py +++ b/app.py @@ -161,12 +161,23 @@ class User(BaseModel): def is_authenticated(self): return True + @property def groups(self): return ( UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group) .where(UserGroupMembership.user == self) ) + +# page perms (used as bitmasks) +PERM_READ = 1 +PERM_EDIT = 2 +PERM_CREATE = 4 +PERM_SET_URL = 8 +PERM_SET_TAGS = 16 +PERM_ALL = 31 +PERM_LOCK = ~(PERM_EDIT | PERM_CREATE | PERM_SET_URL | PERM_SET_TAGS) + class UserGroup(BaseModel): name = CharField(32, unique=True) permissions = BitField() @@ -250,15 +261,38 @@ class Page(BaseModel): def prop(self): return PagePropertyDict(self) def is_editable(self): - return not self.is_locked + return self.can_edit(current_user) def can_edit(self, user): - if self.is_locked: - return user.id == self.owner.id - return True + perm = self.get_perms(user) + return perm & PERM_EDIT or (self.owner == user and perm & PERM_CREATE) def is_owned_by(self, user): return user.id == self.owner.id + def get_perms(self, user=None): + if user is None: + user = current_user + + if user.is_anonymous: + return UserGroup.get_default_group().permissions & PERM_LOCK + + if user.is_admin: + return PERM_ALL + + perm = 0 + # default groups + for gr in user.groups: + perm |= gr.permissions + + # page overrides + for ov in self.permission_overrides: + if ov.group in user.groups: + perm |= ov.permissions + + if self.is_locked and self.owner.id != user.id: + perm &= PERM_LOCK + return perm + class PageText(BaseModel): content = BlobField() diff --git a/templates/base.jinja2 b/templates/base.jinja2 index 288bd0a..34a2b4f 100644 --- a/templates/base.jinja2 +++ b/templates/base.jinja2 @@ -58,9 +58,9 @@ diff --git a/templates/contributions.jinja2 b/templates/contributions.jinja2 index 7ddbf6f..8b37de7 100644 --- a/templates/contributions.jinja2 +++ b/templates/contributions.jinja2 @@ -13,7 +13,7 @@
        {% if page_n > 1 %} -
      • « Previous page
      • +
      • « Previous page
      • {% endif %} diff --git a/templates/home.jinja2 b/templates/home.jinja2 index 971648f..ab01ddc 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -7,7 +7,9 @@

        {{ T('welcome').format(app_name) }}

        {{ T('latest-notes') }}

        diff --git a/templates/manageaccounts.jinja2 b/templates/manageaccounts.jinja2 index 0f42c0e..defd67f 100644 --- a/templates/manageaccounts.jinja2 +++ b/templates/manageaccounts.jinja2 @@ -18,7 +18,7 @@
          {% if page_n > 1 %} -
        • « Previous page
        • +
        • « Previous page
        • {% endif %} {% for u in users %} @@ -29,7 +29,7 @@ - Groups:
            - {% for ug in u.groups() %} + {% for ug in u.groups %}
          • {{ ug.name }}
          • {% endfor %}
          From ba3ed04b44898ef632b8dbda5dfa5fc77db678d9 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 17 May 2023 20:52:33 +0200 Subject: [PATCH 33/51] Improved calendar month view --- app.py | 14 +++++++++++++- static/style.css | 1 + templates/home.jinja2 | 3 ++- templates/month.jinja2 | 14 ++++++++++++++ 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index e358abf..2464df6 100644 --- a/app.py +++ b/app.py @@ -950,6 +950,18 @@ def contributions(username): min=min ) +def _advance_calendar(date, offset=0): + if offset == -2: + return datetime.date(date.year - 1, date.month, 1) + elif offset == -1: + return datetime.date(date.year, date.month - 1, 1) if date.month > 1 else datetime.date(date.year - 1, 12, 1) + elif offset == 1: + return datetime.date(date.year, date.month + 1, 1) if date.month < 12 else datetime.date(date.year + 1, 1, 1) + elif offset == 2: + return datetime.date(date.year + 1, date.month, 1) + else: + return date + @app.route('/calendar/') def calendar_view(): now = datetime.datetime.now() @@ -967,7 +979,7 @@ def calendar_month(y, m): page = int(request.args.get('page', 1)) return render_template('month.jinja2', d=datetime.date(y, m, 1), notes=notes.paginate(page), - page_n=page, total_count=notes.count(), min=min) + page_n=page, total_count=notes.count(), min=min, advance_calendar=_advance_calendar) @app.route('/history/revision//') def view_old(revisionid): diff --git a/static/style.css b/static/style.css index c2daa20..b5df299 100644 --- a/static/style.css +++ b/static/style.css @@ -28,6 +28,7 @@ body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-mai /* header styles */ .header{padding:1em;display:flex;flex-direction:row;justify-content:space-between;position:sticky;top:0;background-color:var(--bg-alt); z-index: 99} +.header-app-name{font-size: 1.5em; font-weight: bold} .header .header-blank {flex: 1; margin: 0 .5em} .header ul{list-style:none;padding:0;margin:0;font-size:0.9em;border-left:var(--border) 1px solid} .header ul > li{display:inline-block;padding-right:1em} diff --git a/templates/home.jinja2 b/templates/home.jinja2 index ab01ddc..90dc091 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -40,8 +40,9 @@ {% endif %} {% endfor %} -
        • {{ T('show-all') }}
        • +
        • {{ T('show-all') }}
        +
    {% endblock %} diff --git a/templates/month.jinja2 b/templates/month.jinja2 index 3e9cd31..6f1b369 100644 --- a/templates/month.jinja2 +++ b/templates/month.jinja2 @@ -43,6 +43,20 @@

    {{ T('notes-month-empty') }}

    {% endif %} + {% set past_year = advance_calendar(d, -2) %} + {% set past_month = advance_calendar(d, -1) %} + {% set next_month = advance_calendar(d, 1) %} + {% set next_year = advance_calendar(d, 2) %} + +

    {{ T('back-to') }} {{ T('calendar') }}

    From d53677ea669b98b6df4113db5babdefa6ff0aa3a Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Thu, 18 May 2023 08:47:06 +0200 Subject: [PATCH 34/51] Style changes --- static/style.css | 33 ++++---- templates/administration.jinja2 | 32 ++++---- templates/backlinks.jinja2 | 25 +++--- templates/badrequest.jinja2 | 4 +- templates/calendar.jinja2 | 44 ++++++----- templates/contributions.jinja2 | 54 ++++++------- templates/easter.jinja2 | 38 ++++----- templates/edit.jinja2 | 112 ++++++++++++++------------- templates/exportpages.jinja2 | 30 +++---- templates/forbidden.jinja2 | 4 +- templates/history.jinja2 | 44 ++++++----- templates/importpages.jinja2 | 44 ++++++----- templates/internalservererror.jinja2 | 6 +- templates/leaderboard.jinja2 | 54 ++++++------- templates/listrecent.jinja2 | 2 +- templates/login.jinja2 | 48 ++++++------ templates/manageaccounts.jinja2 | 78 +++++++++---------- templates/notfound.jinja2 | 4 +- templates/privacy.jinja2 | 2 +- templates/register.jinja2 | 9 ++- templates/search.jinja2 | 48 ++++++------ templates/stats.jinja2 | 18 +++-- templates/terms.jinja2 | 2 +- templates/uploadinfo.jinja2 | 23 ------ templates/view.jinja2 | 2 +- 25 files changed, 389 insertions(+), 371 deletions(-) delete mode 100644 templates/uploadinfo.jinja2 diff --git a/static/style.css b/static/style.css index b5df299..f6ba380 100644 --- a/static/style.css +++ b/static/style.css @@ -37,22 +37,23 @@ body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-mai .article-header {text-align: center;} #firstHeading {font-family:sans-serif; text-align: center;font-size:3em; font-weight: normal} @media (min-width:800px) {.homepage #firstHeading {font-size: 4.5em}} -.inner-content{font-family:serif; margin: 1.7em auto; max-width: 1280px; line-height: 1.9; color: var(--fg-main)} -.inner-content em,.inner-content strong{color: var(--fg-sharp)} -.inner-content h1{color: var(--fg-error)} -.inner-content table {font-family: sans-serif} -.inner-content h2, .inner-content h3, .inner-content h4, .inner-content h5, .inner-content h6{font-family:sans-serif; color: var(--fg-sharp); font-weight: normal} -.inner-content h2{border-bottom: 1px solid var(--border)} -.inner-content h3{margin:0.8em 0} -.inner-content h4{margin:0.6em 0} -.inner-content h5{margin:0.5em 0} -.inner-content h6{margin:0.4em 0} -.inner-content p{text-indent: 1.9em; margin: 0} -.inner-content li{margin: .3em 0} -.inner-content blockquote{color:var(--fg-alt); border-left: 4px solid var(--bg-alt);margin-left:0;padding-left:12px} -.inner-content table{border:var(--bg-alt) 1px solid;border-collapse:collapse;line-height: 1.5;overflow-x:auto} -.inner-content table > * > tr > th, .inner-content table > tr > th {background-color:var(--bg-alt);border:var(--border) 1px solid;padding:2px} -.inner-content table > * > tr > td, .inner-content table > tr > td {border:var(--border) 1px solid;padding:2px} +.inner-content, .article-content {margin: 1.7em auto; max-width: 1280px;} +.article-content{font-family:serif; margin: 1.7em auto; max-width: 1280px; line-height: 1.9; color: var(--fg-main)} +.article-content em,.article-content strong{color: var(--fg-sharp)} +.article-content h1{color: var(--fg-error)} +.article-content table {font-family: sans-serif} +.article-content h2, .article-content h3, .article-content h4, .article-content h5, .article-content h6{font-family:sans-serif; color: var(--fg-sharp); font-weight: normal} +.article-content h2{border-bottom: 1px solid var(--border)} +.article-content h3{margin:0.8em 0} +.article-content h4{margin:0.6em 0} +.article-content h5{margin:0.5em 0} +.article-content h6{margin:0.4em 0} +.article-content p{text-indent: 1.9em; margin: 0} +.article-content li{margin: .3em 0} +.article-content blockquote{color:var(--fg-alt); border-left: 4px solid var(--bg-alt);margin-left:0;padding-left:12px} +.article-content table{border:var(--bg-alt) 1px solid;border-collapse:collapse;line-height: 1.5;overflow-x:auto} +.article-content table > * > tr > th, .article-content table > tr > th {background-color:var(--bg-alt);border:var(--border) 1px solid;padding:2px} +.article-content table > * > tr > td, .article-content table > tr > td {border:var(--border) 1px solid;padding:2px} /* spoiler styles */ .spoiler {color: var(--fg-sharp); background-color: var(--fg-sharp)} diff --git a/templates/administration.jinja2 b/templates/administration.jinja2 index 39ce634..535e175 100644 --- a/templates/administration.jinja2 +++ b/templates/administration.jinja2 @@ -5,19 +5,21 @@ {% block content %}

    Administrative tools

    -{% if current_user and current_user.is_admin %} - -{% else %} -

    Administrative tools can be accessed by administrator users only.

    -{% endif %} +
    + {% if current_user and current_user.is_admin %} + + {% else %} +

    Administrative tools can be accessed by administrator users only.

    + {% endif %} +
    {% endblock %} \ No newline at end of file diff --git a/templates/backlinks.jinja2 b/templates/backlinks.jinja2 index aa6bbff..e22ab81 100644 --- a/templates/backlinks.jinja2 +++ b/templates/backlinks.jinja2 @@ -10,17 +10,20 @@

    {{ p.title }}

    {{ T('backlinks') }}
    -{% if backlinks %} - -{% else %} -

    {{ T("backlinks-empty") }}

    -{% endif %} +
    + {% if backlinks %} + + {% else %} +

    {{ T("backlinks-empty") }}

    + {% endif %} +
    +

    {{ T("back-to") }} {{ p.title }}.

    {% endblock %} diff --git a/templates/badrequest.jinja2 b/templates/badrequest.jinja2 index ba04376..3759000 100644 --- a/templates/badrequest.jinja2 +++ b/templates/badrequest.jinja2 @@ -5,5 +5,7 @@ {% block content %}

    Bad request

    -

    You sent a request the server can’t understand. If you entered the URL manually please check your spelling and try again.

    +
    +

    You sent a request the server can’t understand. If you entered the URL manually please check your spelling and try again.

    +
    {% endblock %} diff --git a/templates/calendar.jinja2 b/templates/calendar.jinja2 index 2d092f8..b7ccf5e 100644 --- a/templates/calendar.jinja2 +++ b/templates/calendar.jinja2 @@ -6,29 +6,31 @@

    {{ T('calendar') }}

    -
      - {% for year in range(till_year, from_year-1, -1) %} -
    • - {{ year }} {% if year == now.year %}(current){% endif %}: - -
    • - {% endfor %} -
    - -

    - Show more years: -

    {% endblock %} \ No newline at end of file diff --git a/templates/contributions.jinja2 b/templates/contributions.jinja2 index 8b37de7..ec13a94 100644 --- a/templates/contributions.jinja2 +++ b/templates/contributions.jinja2 @@ -9,33 +9,35 @@ {% block content %}

    Contributions of {{ u.username }}

    -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    +
    +

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    - +
    {% endblock %} diff --git a/templates/easter.jinja2 b/templates/easter.jinja2 index dd81ef8..54a4568 100644 --- a/templates/easter.jinja2 +++ b/templates/easter.jinja2 @@ -5,25 +5,27 @@ {% block content %}

    {{ T('easter-date-calc') }}

    -
    -
    - - - -
    -
    +
    +
    +
    + + + +
    +
    -{% if easter_dates %} -
    -

    {{ T('easter') }}: {{ easter_dates['easter'].strftime('%B %-d, %Y') }}

    - -

    {{ T('other-dates') }}

    -
      -
    • Mercoledì delle Ceneri: {{ easter_dates['ceneri'].strftime('%B %-d, %Y') }}
    • -
    • Ascensione: {{ easter_dates['ascensione'].strftime('%B %-d, %Y') }}
    • -
    • Pentecoste: {{ easter_dates['pentecoste'].strftime('%B %-d, %Y') }}
    • -
    • Prima Domenica d'Avvento: {{ easter_dates['avvento1'].strftime('%B %-d, %Y') }}
    • -
    + {% if easter_dates %} +
    +

    {{ T('easter') }}: {{ easter_dates['easter'].strftime('%B %-d, %Y') }}

    + +

    {{ T('other-dates') }}

    +
      +
    • Mercoledì delle Ceneri: {{ easter_dates['ceneri'].strftime('%B %-d, %Y') }}
    • +
    • Ascensione: {{ easter_dates['ascensione'].strftime('%B %-d, %Y') }}
    • +
    • Pentecoste: {{ easter_dates['pentecoste'].strftime('%B %-d, %Y') }}
    • +
    • Prima Domenica d'Avvento: {{ easter_dates['avvento1'].strftime('%B %-d, %Y') }}
    • +
    +
    {% endif %} {% endblock %} diff --git a/templates/edit.jinja2 b/templates/edit.jinja2 index 3ccbc37..63f0ae9 100644 --- a/templates/edit.jinja2 +++ b/templates/edit.jinja2 @@ -25,69 +25,71 @@ Remember this is only a preview. Your changes were not saved yet! Jump to editing area
    -
    {{ preview|safe }}
    +
    {{ preview|safe }}
    {% endif %} -
    - -
    - - -
    -
    - -
    -
    - {% if not pl_readonly %} -
    -

    This editor is using Markdown for text formatting (e.g. bold, italic, headers and tables). More info on Markdown.

    - {% if math_version and pl_enablemath %} -

    Math with KaTeX is enabled. KaTeX guide

    - {% endif %} +
    + + +
    + +
    -
    - {% else %} -
    -

    This page was locked by the owner, and is therefore not editable. You can still copy the source text.

    +
    + +
    +
    + {% if not pl_readonly %} +
    +

    This editor is using Markdown for text formatting (e.g. bold, italic, headers and tables). More info on Markdown.

    + {% if math_version and pl_enablemath %} +

    Math with KaTeX is enabled. KaTeX guide

    + {% endif %} +
    +
    + {% else %} +
    +

    This page was locked by the owner, and is therefore not editable. You can still copy the source text.

    +
    + {% endif %} + +
    +
    + + +
    + {% if not pl_readonly %} +
    + + + +
    +

    Advanced options

    +
    + + +
    + {% if pl_owner_is_current_user %} +
    + +
    {% endif %} - -
    -
    - - -
    - {% if not pl_readonly %} -
    - - - -
    -

    Advanced options

    -
    - - -
    - {% if pl_owner_is_current_user %} -
    - - -
    - {% endif %} -
    - - - -
    -
    - - -
    - {% endif %} - +
    + + + +
    +
    + + +
    + {% endif %} + +
    {% endblock %} diff --git a/templates/exportpages.jinja2 b/templates/exportpages.jinja2 index 136a739..ffb1df1 100644 --- a/templates/exportpages.jinja2 +++ b/templates/exportpages.jinja2 @@ -5,20 +5,22 @@ {% block content %}

    Export pages

    -

    You can export how many pages you want, that will be downloaded in JSON format and can be imported in another {{ app_name }} instance.

    +
    +

    You can export how many pages you want, that will be downloaded in JSON format and can be imported in another {{ app_name }} instance.

    -

    In order to add page to export list, please enter exact title, /url, #tag or +id. Entering a tag will add all pages with that tag to list. Each page or tag is separated by a newline.

    +

    In order to add page to export list, please enter exact title, /url, #tag or +id. Entering a tag will add all pages with that tag to list. Each page or tag is separated by a newline.

    -
    - -
    - -
    -
    - -
    -
    - -
    -
    +
    + +
    + +
    +
    + +
    +
    + +
    +
    +
    {% endblock %} diff --git a/templates/forbidden.jinja2 b/templates/forbidden.jinja2 index 83b57b4..ab97d1d 100644 --- a/templates/forbidden.jinja2 +++ b/templates/forbidden.jinja2 @@ -5,5 +5,7 @@ {% block content %}

    Forbidden

    -

    You don’t have permission to access this resource.

    +
    +

    You don’t have permission to access this resource.

    +
    {% endblock %} diff --git a/templates/history.jinja2 b/templates/history.jinja2 index ac47106..c671e1f 100644 --- a/templates/history.jinja2 +++ b/templates/history.jinja2 @@ -10,28 +10,30 @@

    {{ p.title }}

    Page history
    - +

    {{ T("back-to") }} {{ p.title }}.

    {% endblock %} diff --git a/templates/importpages.jinja2 b/templates/importpages.jinja2 index e0f54c7..0008985 100644 --- a/templates/importpages.jinja2 +++ b/templates/importpages.jinja2 @@ -5,27 +5,31 @@ {% block content %}

    Import pages

    -{% if current_user.is_admin %} -

    - You can import files produced by the exporter tool, in JSON format. - Importing pages can be done by users with Admin permissions only. -

    +
    + {% if current_user.is_admin %} + +

    + You can import files produced by the exporter tool, in JSON format. + Importing pages can be done by users with Admin permissions only. +

    -
    - -
    - + + +
    + +
    +
    + + +
    +
    + +
    +
    -
    - - -
    -
    - -
    - -{% else %} -

    Importing pages can be done by users with Admin permissions only.

    -{% endif %} + {% else %} +

    Importing pages can be done by users with Admin permissions only.

    + {% endif %} +
    {% endblock %} \ No newline at end of file diff --git a/templates/internalservererror.jinja2 b/templates/internalservererror.jinja2 index 050f4c2..8c24e60 100644 --- a/templates/internalservererror.jinja2 +++ b/templates/internalservererror.jinja2 @@ -1,9 +1,11 @@ {% extends "base.jinja2" %} -{% block title %}Internal Server Error - {{ app_name }}{% endblock %} +{% block title %}% _ % - {{ app_name }}{% endblock %} {% block content %}

    Internal Server Error

    -

    We’re sorry, an unexpected error occurred. Try refreshing the page.

    +
    +

    We’re sorry, an unexpected error occurred. Try refreshing the page.

    +
    {% endblock %} diff --git a/templates/leaderboard.jinja2 b/templates/leaderboard.jinja2 index b8966c7..38aaa37 100644 --- a/templates/leaderboard.jinja2 +++ b/templates/leaderboard.jinja2 @@ -9,30 +9,32 @@ {% block content %}

    Best pages

    - - - - - - - - - - - - - {% set counters = namespace(row = 0) %} - {% for p, score, bklinks, fwlinks, length in pages %} - - {% set counters.row = counters.row + 1 %} - - - - - - - - {% endfor %} - -
    #Page NameScoreLenBLFL
    {{ counters.row }}{{ p.title }} (#{{ p.id }}){{ score }}{{ length }}{{ bklinks }}{{ fwlinks }}
    +
    + + + + + + + + + + + + + {% set counters = namespace(row = 0) %} + {% for p, score, bklinks, fwlinks, length in pages %} + + {% set counters.row = counters.row + 1 %} + + + + + + + + {% endfor %} + +
    #Page NameScoreLenBLFL
    {{ counters.row }}{{ p.title }} (#{{ p.id }}){{ score }}{{ length }}{{ bklinks }}{{ fwlinks }}
    +
    {% endblock %} diff --git a/templates/listrecent.jinja2 b/templates/listrecent.jinja2 index 2634cfe..5df3b13 100644 --- a/templates/listrecent.jinja2 +++ b/templates/listrecent.jinja2 @@ -2,7 +2,7 @@ {% block content %}
    -

    Notes by date

    +

    Notes by date

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    diff --git a/templates/login.jinja2 b/templates/login.jinja2 index 40c7c0d..706aca7 100644 --- a/templates/login.jinja2 +++ b/templates/login.jinja2 @@ -5,29 +5,31 @@ {% block content %}

    {{ T('login') }}

    -
    - -
    - - -
    -
    - - -
    -
    - - -
    -
    - -
    -
    +
    +
    + +
    + + +
    +
    + + +
    +
    + + +
    +
    + +
    +
    +

    {{ T('no-account-sign-up') }} {{ T("sign-up") }}

    {% endblock %} \ No newline at end of file diff --git a/templates/manageaccounts.jinja2 b/templates/manageaccounts.jinja2 index defd67f..7689dc8 100644 --- a/templates/manageaccounts.jinja2 +++ b/templates/manageaccounts.jinja2 @@ -5,48 +5,48 @@ {% block content %}

    Manage accounts

    +
    {% if current_user.is_admin %} -

    - Here is the list of users registered on {{ app_name }}, in reverse chronological order. - Beware: you are managing sensitive informations. -

    +

    + Here is the list of users registered on {{ app_name }}, in reverse chronological order. + Beware: you are managing sensitive informations. +

    -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    +

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    -
    - - -
      - {% if page_n > 1 %} -
    • « Previous page
    • - {% endif %} + + - {% for u in users %} -
    • - - {{ u.username }} - {% if u == current_user %}(you){% endif %} - - - Groups: -
        - {% for ug in u.groups %} -
      • {{ ug.name }}
      • - {% endfor %} -
      - - - Registered on: - {{ u.join_date }} -
    • - {% endfor %} +
        + {% if page_n > 1 %} +
      • « Previous page
      • + {% endif %} + + {% for u in users %} +
      • + + {{ u.username }} + {% if u == current_user %}(you){% endif %} + - + Groups: +
          + {% for ug in u.groups %} +
        • {{ ug.name }}
        • + {% endfor %} +
        + - + Registered on: + {{ u.join_date }} +
      • + {% endfor %} - {% if page_n < total_count // 20 %} -
      • Next page »
      • - {% endif %} -
      - - - -{% else %} -

      Managing accounts can be done by users with Admin permissions only.

      -{% endif %} + {% if page_n < total_count // 20 %} +
    • Next page »
    • + {% endif %} +
    + + {% else %} +

    Managing accounts can be done by users with Admin permissions only.

    + {% endif %} +
    {% endblock %} \ No newline at end of file diff --git a/templates/notfound.jinja2 b/templates/notfound.jinja2 index eb4e06b..e79d893 100644 --- a/templates/notfound.jinja2 +++ b/templates/notfound.jinja2 @@ -5,5 +5,7 @@ {% block content %}

    {{ T('not-found') }}

    -

    {{ T('not-found-text-1') }} {{ request.path }} {{ T('not-found-text-2') }}.

    +
    +

    {{ T('not-found-text-1') }} {{ request.path }} {{ T('not-found-text-2') }}.

    +
    {% endblock %} diff --git a/templates/privacy.jinja2 b/templates/privacy.jinja2 index a631f98..c7cf33a 100644 --- a/templates/privacy.jinja2 +++ b/templates/privacy.jinja2 @@ -7,7 +7,7 @@

    Privacy Policy

    -
    +
    {% filter markdown %} # Privacy diff --git a/templates/register.jinja2 b/templates/register.jinja2 index 5b2f9d2..821b406 100644 --- a/templates/register.jinja2 +++ b/templates/register.jinja2 @@ -5,7 +5,8 @@ {% block content %}

    {{ T('sign-up') }}

    -
    +
    +
    @@ -30,7 +31,9 @@
    - + + +

    {{ T('already-have-account') }} {{ T("login") }}

    +
    -

    {{ T('already-have-account') }} {{ T("login") }}

    {% endblock %} \ No newline at end of file diff --git a/templates/search.jinja2 b/templates/search.jinja2 index 01a7304..ddbf029 100644 --- a/templates/search.jinja2 +++ b/templates/search.jinja2 @@ -5,30 +5,32 @@ {% block content %}

    Search

    -
    - -
    - - -
    -
    - - -
    -
    +
    +
    + +
    + + +
    +
    + + +
    +
    -{% if results %} -

    Search results for {{ q }}

    + {% if results %} +

    Search results for {{ q }}

    -
      - {% for n in results %} -
    • {% include "includes/nl_item.jinja2" %}
    • - {% endfor %} -
    -{% elif q %} -

    {{ T('search-no-results') }} {{ q }}

    -{% else %} -

    Please note that search queries do not search for page text.

    -{% endif %} +
      + {% for n in results %} +
    • {% include "includes/nl_item.jinja2" %}
    • + {% endfor %} +
    + {% elif q %} +

    {{ T('search-no-results') }} {{ q }}

    + {% else %} +

    Please note that search queries do not search for page text.

    + {% endif %} +
    {% endblock %} diff --git a/templates/stats.jinja2 b/templates/stats.jinja2 index 1f0812a..88511af 100644 --- a/templates/stats.jinja2 +++ b/templates/stats.jinja2 @@ -5,12 +5,14 @@ {% block content %}

    Statistics

    -
      -
    • {{ T("notes-count") }}: {{ notes_count }}
    • -
    • {{ T("notes-count-with-url") }}: {{ notes_with_url }}
    • -
    • {{ T("revision-count") }}: {{ revision_count }}
    • -
    • {{ T("revision-count-per-page") }}: {{ (revision_count / notes_count)|round(2) }}
    • -
    • {{ T('users-count') }}: {{ users_count }}
    • -
    • {{ T('groups-count') }}: {{ groups_count }}
    • -
    +
    +
      +
    • {{ T("notes-count") }}: {{ notes_count }}
    • +
    • {{ T("notes-count-with-url") }}: {{ notes_with_url }}
    • +
    • {{ T("revision-count") }}: {{ revision_count }}
    • +
    • {{ T("revision-count-per-page") }}: {{ (revision_count / notes_count)|round(2) }}
    • +
    • {{ T('users-count') }}: {{ users_count }}
    • +
    • {{ T('groups-count') }}: {{ groups_count }}
    • +
    +
    {% endblock %} diff --git a/templates/terms.jinja2 b/templates/terms.jinja2 index e02b8a6..69c9360 100644 --- a/templates/terms.jinja2 +++ b/templates/terms.jinja2 @@ -5,7 +5,7 @@ {% block content %}

    Terms of Service

    -
    +
    {% filter markdown %} ## Scope and Definitions diff --git a/templates/uploadinfo.jinja2 b/templates/uploadinfo.jinja2 deleted file mode 100644 index 1be8c30..0000000 --- a/templates/uploadinfo.jinja2 +++ /dev/null @@ -1,23 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Info on file "{{ upl.name }}" - {{ app_name }}{% endblock %} - -{% block content %} -

    Info on file "{{ upl.name }}"

    - -
    - {{ upl.name }} -
    - -

    You can include this file in other pages with {{ '{{' }}media:{{ upl.id }}{{ '}}' }}.

    - -

    File info

    - -

    Type: {{ type_list[upl.filetype] }}

    - -

    Upload ID: {{ upl.id }}

    - -

    Uploaded on: {{ upl.upload_date.strftime('%B %-d, %Y %H:%M:%S') }}

    - -

    Size: {{ upl.filesize }} bytes

    -{% endblock %} diff --git a/templates/view.jinja2 b/templates/view.jinja2 index 0b82b77..ae0efcb 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -42,7 +42,7 @@
    {% endif %} -
    +
    {{ html_and_toc[0]|safe }}
    From a30d1c6fe0670e24cdfc56981a8805ace25b0e61 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Tue, 20 Jun 2023 19:37:38 +0200 Subject: [PATCH 35/51] Fix import/export calendar --- app.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app.py b/app.py index 2464df6..ccb7bce 100644 --- a/app.py +++ b/app.py @@ -1167,7 +1167,7 @@ class Exporter(object): pobj['title'] = p.title pobj['url'] = p.url pobj['tags'] = [tag.name for tag in p.tags] - pobj['calendar'] = p.calendar + pobj['calendar'] = p.calendar.isoformat("T") if p.calendar else None pobj['flags'] = p.flags if include_users: pobj['owner'] = p.owner_id @@ -1217,7 +1217,7 @@ class Importer(object): p = Page.create( url = purl if self.overwrite_urls else None, title = pobj['title'], - calendar = pobj.get('calendar'), + calendar = datetime.datetime.fromisoformat(pobj["calendar"]) if 'calendar' in pobj else None, owner = self.owner.id, flags = pobj.get('flags'), touched = datetime.datetime.now() From ea0ccc0d0d0fe606d03f2a656681154c5bf15b74 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 21 Jun 2023 00:13:30 +0200 Subject: [PATCH 36/51] Introduce /rules + minor style changes --- .gitignore | 5 +++ CHANGELOG.md | 2 +- app.py | 52 +++++++++++++------------- static/style.css | 8 +++- templates/calendar.jinja2 | 2 +- templates/rules.jinja2 | 78 +++++++++++++++++++++++++++++++++++++++ 6 files changed, 117 insertions(+), 30 deletions(-) create mode 100644 templates/rules.jinja2 diff --git a/.gitignore b/.gitignore index c8156ed..0bd0625 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,10 @@ database/ site.conf site-*.conf run_8180.py +.env +alembic.ini +venv/ +venv-*/ # automatically generated garbage **/__pycache__/ @@ -13,3 +17,4 @@ run_8180.py **/.\#* **/\#*\# ig_api_settings/ +node_modules/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 9855143..8e4067b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ + Added `restrictions` field to `User`. + Pages now can have a Content Warning. It prevents them to show up in previews, and adds a caution message when viewing them. -+ Added Terms and Privacy Policy. ++ Added Terms, Privacy Policy and Rules. + Changed user page URLs (contributions page) from `/u/user` to `/@user`. + `/manage/` is now a list of all managing options, including export/import and the brand new `/manage/accounts`. diff --git a/app.py b/app.py index ccb7bce..ad84f2e 100644 --- a/app.py +++ b/app.py @@ -90,6 +90,17 @@ def _makelist(l): else: return [] +def render_paginated_template(template_name, query_name, **kwargs): + query = kwargs.pop(query_name) + page = int(request.args.get('page', 1)) + kwargs[query_name] = query.paginate(page) + return render_template( + template_name, + page_n = page, + total_count = query.count(), + min=min, + **kwargs + ) #### MARKDOWN EXTENSIONS #### @@ -588,8 +599,8 @@ def is_url_available(url): forbidden_urls = [ 'about', 'accounts', 'ajax', 'backlinks', 'calendar', 'circles', 'create', 'easter', 'edit', 'embed', 'group', 'help', 'history', 'init-config', 'kt', - 'manage', 'media', 'p', 'privacy', 'protect', 'search', 'static', 'stats', - 'tags', 'terms', 'u', 'upload', 'upload-info' + 'manage', 'media', 'p', 'privacy', 'protect', 'rules', 'search', 'static', + 'stats', 'tags', 'terms', 'u', 'upload', 'upload-info' ] app = Flask(__name__) @@ -881,11 +892,9 @@ def embed_view(id): html.escape(p.title), rev.html()) @app.route('/p/most_recent/') -@app.route('/p/most_recent//') -def view_most_recent(page=1): +def view_most_recent(): general_query = Page.select().order_by(Page.touched.desc()) - return render_template('listrecent.jinja2', notes=general_query.paginate(page), - page_n=page, total_count=general_query.count(), min=min) + return render_paginated_template('listrecent.jinja2', 'notes', notes=general_query) @app.route('/p/random/') def view_random(): @@ -941,13 +950,10 @@ def contributions(username): except User.DoesNotExist: abort(404) contributions = user.contributions.order_by(PageRevision.pub_date.desc()) - page = int(request.args.get('page', 1)) - return render_template('contributions.jinja2', + return render_template('contributions.jinja2', + "contributions", u=user, - contributions=contributions.paginate(page), - page_n=page, - total_count=contributions.count(), - min=min + contributions=contributions, ) def _advance_calendar(date, offset=0): @@ -976,10 +982,8 @@ def calendar_month(y, m): (datetime.date(y, m, 1) <= Page.calendar) & (Page.calendar < datetime.date(y+1 if m==12 else y, 1 if m==12 else m+1, 1)) ).order_by(Page.calendar) - page = int(request.args.get('page', 1)) - return render_template('month.jinja2', d=datetime.date(y, m, 1), notes=notes.paginate(page), - page_n=page, total_count=notes.count(), min=min, advance_calendar=_advance_calendar) + return render_paginated_template('month.jinja2', "notes", d=datetime.date(y, m, 1), notes=notes, advance_calendar=_advance_calendar) @app.route('/history/revision//') def view_old(revisionid): @@ -1013,12 +1017,9 @@ def search(): return render_template('search.jinja2', pl_include_tags=True) @app.route('/tags//') -@app.route('/tags///') -def listtag(tag, page=1): +def listtag(tag): general_query = Page.select().join(PageTag, on=PageTag.page).where(PageTag.name == tag).order_by(Page.touched.desc()) - page_query = general_query.paginate(page) - return render_template('listtag.jinja2', tagname=tag, tagged_notes=page_query, - page_n=page, total_count=general_query.count(), min=min) + return render_paginated_template('listtag.jinja2', "tagged_notes", tagname=tag, tagged_notes=general_query) # symbolic route as of v0.5 @@ -1298,12 +1299,7 @@ def manage_accounts(): pass else: flash('Operation not permitted!') - return render_template('manageaccounts.jinja2', - users=users.paginate(page), - page_n=page, - total_count=users.count(), - min=min - ) + return render_paginated_template('manageaccounts.jinja2', 'users', users=users) ## terms / privacy ## @@ -1315,6 +1311,10 @@ def terms(): def privacy(): return render_template('privacy.jinja2') +@app.route('/rules/') +def rules(): + return render_template('rules.jinja2') + #### EXTENSIONS #### active_extensions = [] diff --git a/static/style.css b/static/style.css index f6ba380..0562303 100644 --- a/static/style.css +++ b/static/style.css @@ -68,6 +68,7 @@ body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-mai .nl-new > a{margin-right:12px} .nl-prev,.nl-next{text-align:center} .nl-placeholder {font-style: italic; text-align: center;} +.nl-pagination {text-align: center; color: var(--fg-alt)} input{border:0;border-bottom:3px solid var(--border);font:inherit;color:var(--fg-main);background-color:transparent} input:focus{color:var(--fg-sharp);border-bottom-color:var(--border-sharp)} input.error{border-bottom-color:var(--btn-error)} @@ -119,8 +120,11 @@ ul.inline > li:last-child::after {content: ""} /* floating elements */ nav.toc{display:none} @media only screen and (min-width:960px){ - nav.toc{display:block;position:absolute; top: 0; right: 0; width: 320px;} - .inner-content {margin-right: 320px} + nav.toc{display:block;position:absolute; top: 0; right: 0; width: 240px;} + .inner-content {margin-right: 240px;} +} +@media only screen and (min-width:1200px){ + .inner-content {margin-left: 240px} } .backontop{position:fixed;bottom:0;right:0} @media print{ diff --git a/templates/calendar.jinja2 b/templates/calendar.jinja2 index b7ccf5e..85c3afa 100644 --- a/templates/calendar.jinja2 +++ b/templates/calendar.jinja2 @@ -10,7 +10,7 @@
      {% for year in range(till_year, from_year-1, -1) %}
    • - {{ year }} {% if year == now.year %}(current){% endif %}: + {% if year == now.year %}{% endif %}{{ year }}{% if year == now.year %}{% endif %}:
        {% for month in range(1, 13) %}
      • {{ year }}.{{ month }}
      • diff --git a/templates/rules.jinja2 b/templates/rules.jinja2 new file mode 100644 index 0000000..4ba30af --- /dev/null +++ b/templates/rules.jinja2 @@ -0,0 +1,78 @@ +{% extends "base.jinja2" %} + +{% block title %}Content Policy — {{ app_name }}{% endblock %} + +{% block content %} +

        Content Policy

        + +
        +{% filter markdown %} +These are the Rules of {{ app_name }}. + +{{ app_name }} is a Free Speech environment. However, in order to ensure +the safety of our users as well as the longevity of the platform, there are a +few things you are not permitted to do with the platform. + +By using {{ app_name }}, you agree to follow these rules. +Violations may lead to the suspension or terminaton of your {{ app_name }} account. + +## 1. Intellectual Property + +You may not upload to, embed within, or link out from {{ app_name }}: + +1. Copyrighted material that you are not authorized to distribute +2. Anything not legal to publish within, or export from, intentionally + +## 2. Digital Safety + +You may not upload to, embed within, or link out from {{ app_name }}: + +1. IP or token grabbers +2. Viruses or exploits +3. URL shorteners +4. Anything with the intent of breaking {{ app_name }} + +## 3. User Safety + +You may not use {{ app_name }} to do any of the following: + +1. Threaten, intimidate, or harass other users +2. Impersonate other users, real life people, or {{ app_name }} staff +3. Solicit, collect, or publish personally identifiable information (PII), be it yours or the one + of another individual +4. Spam (the definition of “spam” is at {{ app_name }}’s own discretion) + +## 4. Sexual Content + +You may not upload to, embed within, or link out from {{ app_name }}: + +1. Sexual or sexually suggestive material not marked "NSFW" +2. Sexual or sexually suggestive material involving individuals under the age of 18, including fictitious content. Solicitation of such material is also prohibited. +3. Sexual or sexually suggestive material involving individuals who did not consent to its creation and distribution (commonly called "revenge pornography" or "involuntary pornography"). Solicitation of such material is also prohibited. + +## 5. IRL Safety + +You may not use {{ app_name }} to do any of the following: + +1. Incite, plan, or execute unlawful or violent activity +2. Engage in fraud + +## 6. Evil + +While it would be nice to be able to entertain all viewpoints, certain ideologies are ontologically evil. We have a zero tolerance policy on advocacy, propaganda, recruitment, and any other forms of promotion of evil. + +Evil ideologies prohibited from {{ app_name }} include, but are not limited to: + +* Ethnic, racial, or sex-based supremecism +* Pedophile acceptance/normalization +* Terrorism + +Discussion of these topics as they relate to current events or other subject matter is permitted; advocacy or promotion of them is not. + +## 7. Additional Rules + +Additional rules may be put in place by the {{ app_name }} administrator. + +{% endfilter %} +
        +{% endblock %} \ No newline at end of file From 09bbbd74a479d9b51239df67611e01d6829eb8d7 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 12 Jul 2023 11:58:19 +0200 Subject: [PATCH 37/51] Improve SEO + add perms helper --- CHANGELOG.md | 3 ++- app.py | 39 ++++++++++++++++++++++++++++++++++++++- templates/view.jinja2 | 5 +++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e4067b..8b068eb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,11 +8,12 @@ + Added `restrictions` field to `User`. + Pages now can have a Content Warning. It prevents them to show up in previews, and adds a caution message when viewing them. ++ SEO improvement: added `keywords` and `description` meta tags to viewing pages. + Added Terms, Privacy Policy and Rules. + Changed user page URLs (contributions page) from `/u/user` to `/@user`. + `/manage/` is now a list of all managing options, including export/import and the brand new `/manage/accounts`. -+ TOC is now shown in pages where screen width is greater than 960 pixels. ++ TOC is now shown in pages when screen width is greater than 960 pixels. + Style changes: added a top bar with the site title. It replaces the floating menu on the top right. + Now logged-in users have an “Edit” button below the first heading. All users can access page history by clicking the last modified time. diff --git a/app.py b/app.py index ad84f2e..05e236f 100644 --- a/app.py +++ b/app.py @@ -178,6 +178,17 @@ class User(BaseModel): UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group) .where(UserGroupMembership.user == self) ) + + def get_perms(self): + if self.is_admin: + return PERM_ALL + + perm = 0 + + for gr in self.groups: + perm |= gr.permissions + + return perm # page perms (used as bitmasks) @@ -241,7 +252,7 @@ class Page(BaseModel): return '/' + self.url + '/' if self.url else '/p/{}/'.format(self.id) def short_desc(self): if self.is_cw: - return '(Content Warning)' + return '(Content Warning: we are not allowed to show a description.)' full_text = self.latest.text text = remove_tags(full_text, convert = not self.is_math_enabled and not _getconf('appearance', 'simple_remove_tags', False)) return text[:200] + ('\u2026' if len(text) > 200 else '') @@ -303,6 +314,17 @@ class Page(BaseModel): if self.is_locked and self.owner.id != user.id: perm &= PERM_LOCK return perm + + def seo_keywords(self): + kw = [] + for tag in self.tags: + kw.append(tag.name.replace("-", " ")) + for bkl in self.back_links: + try: + kw.append(bkl.from_page.title.replace(",", "")) + except Exception: + pass + return ", ".join(kw) class PageText(BaseModel): @@ -535,6 +557,18 @@ def init_db_and_create_first_user(): ) print('Installed successfully!') +#### PERMS HELPERS #### + +def has_perms(user, flags, page=None): + if page: + perm = page.get_perms(user) + else: + perm = user.get_perms() + + if perm & flags: + return True + return False + #### WIKI SYNTAX #### def md_and_toc(text, expand_magic=False, toc=True, math=True): @@ -724,6 +758,9 @@ def savepoint(form, is_preview=False, pageobj=None): @app.route('/create/', methods=['GET', 'POST']) @login_required def create(): + if not has_perms(current_user, PERM_CREATE): + flash("You are not allowed to create pages.") + abort(403) if request.method == 'POST': if request.form.get('preview'): return savepoint(request.form, is_preview=True) diff --git a/templates/view.jinja2 b/templates/view.jinja2 index ae0efcb..c2ec808 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -2,6 +2,11 @@ {% block title %}{{ p.title }} - {{ app_name }}{% endblock %} +{% block meta %} + + +{% endblock %} + {% block json_info %}{% endblock %} {% set html_and_toc = rev.html_and_toc(math = request.args.get('math') not in ['0', 'false', 'no', 'off']) %} From 1bbf7eebfe029e08d7fe68d2d679bee2105032e3 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 12 Jul 2023 15:43:16 +0200 Subject: [PATCH 38/51] Implement user disabling --- .gitignore | 2 ++ CHANGELOG.md | 1 + app.py | 32 ++++++++++++++++++++++++++++---- templates/manageaccounts.jinja2 | 14 +++++++++++++- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 0bd0625..bdac4f8 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,8 @@ run_8180.py alembic.ini venv/ venv-*/ +.venv +env # automatically generated garbage **/__pycache__/ diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b068eb..4565eda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,7 @@ + Changed user page URLs (contributions page) from `/u/user` to `/@user`. + `/manage/` is now a list of all managing options, including export/import and the brand new `/manage/accounts`. ++ Users can now be disabled (and re-enabled) by administrator. + TOC is now shown in pages when screen width is greater than 960 pixels. + Style changes: added a top bar with the site title. It replaces the floating menu on the top right. + Now logged-in users have an “Edit” button below the first heading. All users can access page history diff --git a/app.py b/app.py index 05e236f..ef2f36d 100644 --- a/app.py +++ b/app.py @@ -534,7 +534,7 @@ def init_db_and_create_first_user(): if password != confirm_password: print('Passwords do not match.') return - default_permissions = 31 # all permissions + default_permissions = PERM_ALL # all permissions if not input('Agree to the Terms of Use?')[0].lower() == 'y': print('You must accept Terms in order to register.') return @@ -680,7 +680,9 @@ def _inject_variables(): @login_manager.user_loader def _inject_user(userid): - return User[userid] + u = User[userid] + if not u.is_disabled: + return u @app.template_filter() def linebreaks(text): @@ -987,7 +989,7 @@ def contributions(username): except User.DoesNotExist: abort(404) contributions = user.contributions.order_by(PageRevision.pub_date.desc()) - return render_template('contributions.jinja2', + return render_paginated_template('contributions.jinja2', "contributions", u=user, contributions=contributions, @@ -1097,6 +1099,10 @@ def accounts_login(): except User.DoesNotExist: flash('Invalid username or password.') else: + if user.is_disabled: + flash("Your account is disabled.") + return render_template("login.jinja2") + remember_for = int(request.form['remember']) if remember_for > 0: login_user(user, remember=True, @@ -1119,6 +1125,7 @@ def accounts_register(): return render_template('register.jinja2') if not request.form['legal']: flash('You must accept Terms in order to register.') + return render_template('register.jinja2') try: with database.atomic(): u = User.create( @@ -1333,7 +1340,24 @@ def manage_accounts(): page = int(request.args.get('page', 1)) if request.method == 'POST': if current_user.is_admin: - pass + action = request.form.get("action") + userids = [] + if action == "disable": + for key in request.form.keys(): + if key.startswith("u") and key[1:].isdigit(): + userids.append(int(key[1:])) + uu = 0 + for uid in userids: + try: + u = User[uid] + except User.DoesNotExist: + continue + u.is_disabled = not u.is_disabled + u.save() + uu += 1 + flash(f"Successfully disabled {uu} users!") + else: + flash("Unknown action") else: flash('Operation not permitted!') return render_paginated_template('manageaccounts.jinja2', 'users', users=users) diff --git a/templates/manageaccounts.jinja2 b/templates/manageaccounts.jinja2 index 7689dc8..8cce7c9 100644 --- a/templates/manageaccounts.jinja2 +++ b/templates/manageaccounts.jinja2 @@ -24,9 +24,12 @@ {% for u in users %}
      • - + + {% if u.is_disabled %}{% endif %} {{ u.username }} + {% if u.is_disabled %}{% endif %} {% if u == current_user %}(you){% endif %} + {% if u.is_disabled %}(disabled){% endif %} - Groups: + +
        + + + +
        {% else %}

        Managing accounts can be done by users with Admin permissions only.

        From ded90d1ac4a5a8fcb0d2495998bbc01602c42027 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 12 Jul 2023 15:48:41 +0200 Subject: [PATCH 39/51] Version advance --- app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app.py b/app.py index ef2f36d..4d89c54 100644 --- a/app.py +++ b/app.py @@ -30,7 +30,7 @@ import i18n import gzip from getpass import getpass -__version__ = '0.8-dev' +__version__ = '0.8' #### CONSTANTS #### From 910e01b691a470b5ed9e255d8afd1881d29b2f75 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Wed, 12 Jul 2023 16:28:59 +0200 Subject: [PATCH 40/51] version advance + some refactoring --- CHANGELOG.md | 9 ++++++ README.md | 15 ++++++--- app.py | 75 +++++++++++++++++-------------------------- templates/base.jinja2 | 13 -------- templates/edit.jinja2 | 7 ---- templates/view.jinja2 | 2 +- 6 files changed, 49 insertions(+), 72 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4565eda..5ff070e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,14 @@ # What’s New +## 0.9 + ++ Removed `markdown_katex` dependency, and therefore support for math. + It is bloat; moreover, it ships executables with it, negatively impacting the lightweightness of the app. ++ Added support for `.env` (dotenv) file. ++ Now a database URL is required. For example, `[database]directory = /path/to/data/` becomes + `[database]url = sqlite:////path/to/data/data.sqlite` (site.conf) or + `DATABASE_URL=sqlite:////path/to/data/data.sqlite` (.env). + ## 0.8 + Schema changes: diff --git a/README.md b/README.md index 80617eb..b9d20e8 100644 --- a/README.md +++ b/README.md @@ -24,16 +24,13 @@ suitable as a community or team knowledge base. + **Peewee** ORM. + **Markdown** for page rendering. + **Python-I18n**. +* **Python-Dotenv**. + The database drivers needed for the type of database. -### Optional requirements - -* **Markdown-KaTeX** if you want to display math inside pages. - ## Usage + Clone this repository: `git clone https://github.com/sakuragasaki46/salvi` -+ Edit site.conf with the needed parameters. An example site.conf: ++ Edit site.conf (old way) or .env (new way) with the needed parameters. An example site.conf with SQLite: ``` [site] @@ -41,6 +38,13 @@ name = Salvi [database] directory = /path/to/database/ +``` + + An example .env with MySQL: + +``` +APP_NAME=Salvi +DATABASE_URL=mysql://root:root@localhost/salvi ``` + Run `python3 -m app_init` to initialize the database and create the administrator user. @@ -51,6 +55,7 @@ directory = /path/to/database/ + The whole application is untested. + If you forget the password, there is currently no way to reset it. ++ This app comes with no content. It means, you have to write it yourself. ## License diff --git a/app.py b/app.py index 4d89c54..eec7bd5 100644 --- a/app.py +++ b/app.py @@ -21,7 +21,7 @@ from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.routing import BaseConverter from peewee import * from playhouse.db_url import connect as dbconnect -import calendar, datetime, hashlib, html, importlib, json, markdown, os, random, \ +import datetime, hashlib, html, importlib, json, markdown, os, random, \ re, sys, warnings from functools import lru_cache, partial from urllib.parse import quote @@ -29,8 +29,9 @@ from configparser import ConfigParser import i18n import gzip from getpass import getpass +import dotenv -__version__ = '0.8' +__version__ = '0.9-dev' #### CONSTANTS #### @@ -45,11 +46,17 @@ PING_RE = r'(? 200 else '') def change_tags(self, new_tags): old_tags = set(x.name for x in self.tags) @@ -366,10 +365,10 @@ class PageRevision(BaseModel): @property def text(self): return self.textref.get_content() - def html(self, *, math=True): - return md(self.text, math=self.page.is_math_enabled and math) - def html_and_toc(self, *, math=True): - return md_and_toc(self.text, math=self.page.is_math_enabled and math) + def html(self): + return md(self.text) + def html_and_toc(self): + return md_and_toc(self.text) def human_pub_date(self): delta = datetime.datetime.now() - self.pub_date T = partial(get_string, g.lang) @@ -571,10 +570,7 @@ def has_perms(user, flags, page=None): #### WIKI SYNTAX #### -def md_and_toc(text, expand_magic=False, toc=True, math=True): - if expand_magic: - # DEPRECATED seeking for a better solution. - warnings.warn('Magic words are no more supported.', DeprecationWarning) +def md_and_toc(text, toc=True): extensions = ['tables', 'footnotes', 'fenced_code', 'sane_lists'] extension_configs = {} if not _getconf('markdown', 'disable_custom_extensions'): @@ -582,12 +578,6 @@ def md_and_toc(text, expand_magic=False, toc=True, math=True): extensions.append(SpoilerExtension()) if toc: extensions.append('toc') - if math and markdown_katex and ('$`' in text or '```math' in text): - extensions.append('markdown_katex') - extension_configs['markdown_katex'] = { - 'no_inline_svg': True, - 'insert_fonts_css': not _getconf('site', 'katex_url') - } try: converter = markdown.Markdown(extensions=extensions, extension_configs=extension_configs) if toc: @@ -597,14 +587,14 @@ def md_and_toc(text, expand_magic=False, toc=True, math=True): except Exception as e: return '

        There was an error during rendering: {e.__class__.__name__}: {e}

        '.format(e=e), '' -def md(text, expand_magic=False, toc=True, math=True): - return md_and_toc(text, expand_magic=expand_magic, toc=toc, math=math)[0] +def md(text, toc=True): + return md_and_toc(text, toc=toc)[0] def remove_tags(text, convert=True, headings=True): if headings: text = re.sub(r'\#[^\n]*', '', text) if convert: - text = md(text, toc=False, math=False) + text = md(text, toc=False) return re.sub(r'<.*?>', '', text) def is_username(s): @@ -670,12 +660,10 @@ def _inject_variables(): return { 'T': partial(get_string, _get_lang()), - 'app_name': _getconf('site', 'title'), + 'app_name': os.getenv("APP_NAME") or _getconf('site', 'title'), 'strong': lambda x:Markup('{0}').format(x), 'app_version': __version__, - 'math_version': markdown_katex.__version__ if markdown_katex else None, - 'material_icons_url': _getconf('site', 'material_icons_url'), - 'katex_url': _getconf('site', 'katex_url') + 'material_icons_url': _getconf('site', 'material_icons_url') } @login_manager.user_loader @@ -694,7 +682,7 @@ app.template_filter(name='markdown')(md) @app.route('/') def homepage(): - page_limit = _getconf("appearance","items_per_page",20,cast=int) + page_limit = _getconf("appearance", "items_per_page", 20, cast=int) return render_template('home.jinja2', new_notes=Page.select() .order_by(Page.touched.desc()).limit(page_limit)) @@ -731,7 +719,7 @@ def error_500(body): # Middle point during page editing. def savepoint(form, is_preview=False, pageobj=None): if is_preview: - preview = md(form['text'], math='enablemath' in form) + preview = md(form['text']) else: preview = None pl_js_info = dict() @@ -747,7 +735,6 @@ def savepoint(form, is_preview=False, pageobj=None): pl_text=form['text'], pl_tags=form['tags'], pl_comment=form['comment'], - pl_enablemath='enablemath' in form, pl_is_locked='lockpage' in form, pl_owner_is_current_user=pageobj.is_owned_by(current_user) if pageobj else True, preview=preview, @@ -787,7 +774,6 @@ def create(): touched=datetime.datetime.now(), owner_id=current_user.id, calendar=datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None, - is_math_enabled='enablemath' in request.form, is_locked = 'lockpage' in request.form, is_cw = 'cw' in request.form ) @@ -842,7 +828,6 @@ def edit(id): p.url = p_url p.title = request.form['title'] p.touched = datetime.datetime.now() - p.is_math_enabled = 'enablemath' in request.form p.is_locked = 'lockpage' in request.form p.is_cw = 'cw' in request.form p.calendar = datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None @@ -867,8 +852,6 @@ def edit(id): "tags": ','.join(x.name for x in p.tags), "comment": "" } - if p.is_math_enabled: - form["enablemath"] = "1" if p.is_locked: form["lockpage"] = "1" if p.calendar: diff --git a/templates/base.jinja2 b/templates/base.jinja2 index 34a2b4f..512e33b 100644 --- a/templates/base.jinja2 +++ b/templates/base.jinja2 @@ -12,19 +12,6 @@ {% else %} {% endif %} - {% if math_version and katex_url %} - - - {% endif %} {% block json_info %}{% endblock %} diff --git a/templates/edit.jinja2 b/templates/edit.jinja2 index 63f0ae9..09cd0bb 100644 --- a/templates/edit.jinja2 +++ b/templates/edit.jinja2 @@ -45,9 +45,6 @@ {% if not pl_readonly %}

        This editor is using Markdown for text formatting (e.g. bold, italic, headers and tables). More info on Markdown.

        - {% if math_version and pl_enablemath %} -

        Math with KaTeX is enabled. KaTeX guide

        - {% endif %}
        {% else %} @@ -68,10 +65,6 @@

    Advanced options

    -
    - - -
    {% if pl_owner_is_current_user %}
    diff --git a/templates/view.jinja2 b/templates/view.jinja2 index c2ec808..389c64f 100644 --- a/templates/view.jinja2 +++ b/templates/view.jinja2 @@ -9,7 +9,7 @@ {% block json_info %}{% endblock %} -{% set html_and_toc = rev.html_and_toc(math = request.args.get('math') not in ['0', 'false', 'no', 'off']) %} +{% set html_and_toc = rev.html_and_toc() %} {% block content %}
    From acf918f6567232b25f9fbfb8b4cd7b6224e7bc1c Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Tue, 12 Dec 2023 10:10:20 +0100 Subject: [PATCH 41/51] Introduce flask_arrest, fix dotenv loading (Apache), and new template macro "nl.jinja2" --- .gitignore | 4 +-- README.md | 2 +- app.py | 22 ++++++++-------- requirements.txt | 4 ++- templates/base.jinja2 | 5 +++- templates/home.jinja2 | 30 +++------------------- templates/listrecent.jinja2 | 36 ++------------------------ templates/listtag.jinja2 | 38 ++-------------------------- templates/macros/nl.jinja2 | 50 +++++++++++++++++++++++++++++++++++++ templates/search.jinja2 | 7 ++---- 10 files changed, 80 insertions(+), 118 deletions(-) create mode 100644 templates/macros/nl.jinja2 diff --git a/.gitignore b/.gitignore index bdac4f8..9c35e69 100644 --- a/.gitignore +++ b/.gitignore @@ -7,8 +7,8 @@ site-*.conf run_8180.py .env alembic.ini -venv/ -venv-*/ +venv +venv-* .venv env diff --git a/README.md b/README.md index b9d20e8..941565a 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ suitable as a community or team knowledge base. ## Requirements + **Python** 3.6+. -+ **Flask** web framework (and Flask-Login / Flask-WTF extensions). ++ **Flask** web framework (and Flask-Login / Flask-WTF / Flask-Arrest extensions). + **Peewee** ORM. + **Markdown** for page rendering. + **Python-I18n**. diff --git a/app.py b/app.py index eec7bd5..ac847bb 100644 --- a/app.py +++ b/app.py @@ -17,6 +17,7 @@ from flask import ( render_template, send_from_directory) from flask_login import LoginManager, login_user, logout_user, current_user, login_required from flask_wtf import CSRFProtect +#from flask_arrest import RestBlueprint, serialize_response from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.routing import BaseConverter from peewee import * @@ -46,9 +47,9 @@ PING_RE = r'(?{0}').format(x), 'app_version': __version__, - 'material_icons_url': _getconf('site', 'material_icons_url') + 'material_icons_url': _getconf('site', 'material_icons_url'), + 'min': min } @login_manager.user_loader diff --git a/requirements.txt b/requirements.txt index 18bfb00..b498231 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,4 +3,6 @@ peewee markdown flask-login flask-wtf -python-i18n \ No newline at end of file +python-i18n +flask-arrest +python-dotenv diff --git a/templates/base.jinja2 b/templates/base.jinja2 index 512e33b..18890f4 100644 --- a/templates/base.jinja2 +++ b/templates/base.jinja2 @@ -13,8 +13,11 @@ {% endif %} {% block json_info %}{% endblock %} + {% block ldjson %}{% endblock %} - +
    {{ app_name }} diff --git a/templates/home.jinja2 b/templates/home.jinja2 index 90dc091..44abd83 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -15,33 +15,9 @@

    {{ T('latest-notes') }}


    - + + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(new_notes) }}
    diff --git a/templates/listrecent.jinja2 b/templates/listrecent.jinja2 index 5df3b13..3f19545 100644 --- a/templates/listrecent.jinja2 +++ b/templates/listrecent.jinja2 @@ -4,39 +4,7 @@

    Notes by date

    -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    - - + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(notes, page_n=page_n, total_count=total_count) }}
    {% endblock %} diff --git a/templates/listtag.jinja2 b/templates/listtag.jinja2 index 46d6ec3..c33b6ca 100644 --- a/templates/listtag.jinja2 +++ b/templates/listtag.jinja2 @@ -8,42 +8,8 @@
    {{ T('notes-tagged') }}
    {% if total_count > 0 %} -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    - -
      - {% if page_n > 1 %} -
    • « Previous page
    • - {% endif %} - {% for n in tagged_notes %} -
    • - {{ n.title }} -

      {{ n.short_desc() }}

      -

      - tag - {{ T('tags') }}: - {% for tag in n.tags %} - {% set tn = tag.name %} - {% if tn == tagname %} - #{{ tn }} - {% else %} - #{{ tn }} - {% endif %} - {% endfor %} - {% if n.calendar %} -

      - calendar_today - - - -

      - {% endif %} -

      -
    • - {% endfor %} - {% if page_n < total_count // 20 %} -
    • Next page »
    • - {% endif %} -
    + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(l, page_n=page_n, total_count=total_count, hl_tags=(tagname,)) }} {% else %}

    {{ T('notes-tagged-empty') }}

    {% endif %} diff --git a/templates/macros/nl.jinja2 b/templates/macros/nl.jinja2 new file mode 100644 index 0000000..c34f963 --- /dev/null +++ b/templates/macros/nl.jinja2 @@ -0,0 +1,50 @@ + + +{% macro nl_list(l, page_n=None, total_count=None, hl_tags=(), other_url='p/most_recent') %} +{% if page_n and total_count %} +

    + Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} + of {{ total_count }} total.

    +{% endif %} + +
      + {% if page_n and page_n > 1 %} +
    • « Previous page
    • + {% endif %} + {% for n in l %} +
    • + {{ n.title }} +

      {{ n.short_desc() }}

      + {% if n.tags %} +

      + tag + {{ T('tags') }}: + {% for tag in n.tags %} + {% set tn = tag.name %} + {% if tn in hl_tags %} + #{{ tn }} + {% else %} + #{{ tn }} + {% endif %} + {% endfor %} +

      + {% endif %} + {% if n.calendar %} +

      + calendar_today + + + +

      + {% endif %} +
    • + {% endfor %} + + {% if page_n is none %} +
    • {{ T('show-all') }}
    • + {% elif page_n <= total_count // 20 %} +
    • Next page »
    • + {% endif %} +
    +{% endmacro %} + diff --git a/templates/search.jinja2 b/templates/search.jinja2 index ddbf029..b883186 100644 --- a/templates/search.jinja2 +++ b/templates/search.jinja2 @@ -21,11 +21,8 @@ {% if results %}

    Search results for {{ q }}

    -
      - {% for n in results %} -
    • {% include "includes/nl_item.jinja2" %}
    • - {% endfor %} -
    + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(l, other_url=None) }} {% elif q %}

    {{ T('search-no-results') }} {{ q }}

    {% else %} From 14e69ac29187d71ff3202cca178795ca52086a04 Mon Sep 17 00:00:00 2001 From: Mattia Succurro Date: Tue, 12 Dec 2023 19:29:43 +0100 Subject: [PATCH 42/51] CSS + HTML change --- app.py | 13 ++++- i18n/salvi.fr.json | 5 +- static/style.css | 1 + templates/administration.jinja2 | 38 ++++++------ templates/backlinks.jinja2 | 34 ++++++----- templates/badrequest.jinja2 | 10 ++-- templates/calendar.jinja2 | 4 +- templates/contributions.jinja2 | 61 +++++++++---------- templates/forbidden.jinja2 | 10 ++-- templates/history.jinja2 | 54 ++++++++--------- templates/home.jinja2 | 28 ++++----- templates/includes/nl_item.jinja2 | 5 ++ templates/internalservererror.jinja2 | 10 ++-- templates/listrecent.jinja2 | 10 ++-- templates/listtag.jinja2 | 18 +++--- templates/macros/nl.jinja2 | 62 ++++++++++++-------- templates/month.jinja2 | 87 ++++++++++++---------------- templates/notfound.jinja2 | 10 ++-- templates/search.jinja2 | 48 +++++++-------- templates/upload.jinja2 | 32 +++++----- 20 files changed, 292 insertions(+), 248 deletions(-) diff --git a/app.py b/app.py index ac847bb..2bdec0d 100644 --- a/app.py +++ b/app.py @@ -13,8 +13,9 @@ Application is kept compact, with all its core in a single file. #### IMPORTS #### from flask import ( - Flask, Markup, abort, flash, g, jsonify, make_response, redirect, request, + Flask, abort, flash, g, jsonify, make_response, redirect, request, render_template, send_from_directory) +from markupsafe import Markup from flask_login import LoginManager, login_user, logout_user, current_user, login_required from flask_wtf import CSRFProtect #from flask_arrest import RestBlueprint, serialize_response @@ -1005,7 +1006,15 @@ def calendar_month(y, m): (Page.calendar < datetime.date(y+1 if m==12 else y, 1 if m==12 else m+1, 1)) ).order_by(Page.calendar) - return render_paginated_template('month.jinja2', "notes", d=datetime.date(y, m, 1), notes=notes, advance_calendar=_advance_calendar) + #toc_q = Page.select(fn.Month(Page.calendar).alias('month'), fn.Count(Page.id).alias('n_notes')).where( + # (datetime.date(y, 1, 1) <= Page.calendar) & + # (Page.calendar < datetime.date(y+1, 1, 1)) + #).group_by() + toc = {} + #for i in toc_q: + # toc[i.month] = i.n_notes + + return render_paginated_template('month.jinja2', "notes", d=datetime.date(y, m, 1), notes=notes, advance_calendar=_advance_calendar, toc=toc) @app.route('/history/revision//') def view_old(revisionid): diff --git a/i18n/salvi.fr.json b/i18n/salvi.fr.json index 439b6a4..9a63a86 100644 --- a/i18n/salvi.fr.json +++ b/i18n/salvi.fr.json @@ -5,6 +5,9 @@ "latest-notes": "Dernières notes", "latest-uploads": "Derniers téléchargements", "new-note": "Créer un note", - "upload-file": "Télécharger une image" + "upload-file": "Télécharger une image", + "easter-date-calc": "Calculer la date de Pâques", + "easter": "Pâques", + "other-dates": "Autres dates" } } \ No newline at end of file diff --git a/static/style.css b/static/style.css index 0562303..6734259 100644 --- a/static/style.css +++ b/static/style.css @@ -69,6 +69,7 @@ body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-mai .nl-prev,.nl-next{text-align:center} .nl-placeholder {font-style: italic; text-align: center;} .nl-pagination {text-align: center; color: var(--fg-alt)} +.nl-item {padding: 12px; border: 1px solid gray; border-radius: 12px; box-shadow: 2px 2px 2px gray; background-color: var(--bg-alt);} input{border:0;border-bottom:3px solid var(--border);font:inherit;color:var(--fg-main);background-color:transparent} input:focus{color:var(--fg-sharp);border-bottom-color:var(--border-sharp)} input.error{border-bottom-color:var(--btn-error)} diff --git a/templates/administration.jinja2 b/templates/administration.jinja2 index 535e175..2e86d73 100644 --- a/templates/administration.jinja2 +++ b/templates/administration.jinja2 @@ -3,23 +3,25 @@ {% block title %}Administrative tools — {{ app_name }}{% endblock %} {% block content %} -

    Administrative tools

    +
    +

    Administrative tools

    -
    - {% if current_user and current_user.is_admin %} - - {% else %} -

    Administrative tools can be accessed by administrator users only.

    - {% endif %} -
    +
    + {% if current_user and current_user.is_admin %} + + {% else %} +

    Administrative tools can be accessed by administrator users only.

    + {% endif %} +
    +
    {% endblock %} \ No newline at end of file diff --git a/templates/backlinks.jinja2 b/templates/backlinks.jinja2 index e22ab81..12189d5 100644 --- a/templates/backlinks.jinja2 +++ b/templates/backlinks.jinja2 @@ -7,24 +7,26 @@ {% endblock %} {% block content %} -

    {{ p.title }}

    -
    {{ T('backlinks') }}
    +
    +

    {{ p.title }}

    +
    {{ T('backlinks') }}
    -
    - {% if backlinks %} - - {% else %} -

    {{ T("backlinks-empty") }}

    - {% endif %} -
    +
    + {% if backlinks %} + + {% else %} +

    {{ T("backlinks-empty") }}

    + {% endif %} +
    -

    {{ T("back-to") }} {{ p.title }}.

    +

    {{ T("back-to") }} {{ p.title }}.

    +
    {% endblock %} diff --git a/templates/badrequest.jinja2 b/templates/badrequest.jinja2 index 3759000..380edba 100644 --- a/templates/badrequest.jinja2 +++ b/templates/badrequest.jinja2 @@ -3,9 +3,11 @@ {% block title %}Bad Request - {{ app_name }}{% endblock %} {% block content %} -

    Bad request

    +
    +

    Bad request

    -
    -

    You sent a request the server can’t understand. If you entered the URL manually please check your spelling and try again.

    -
    +
    +

    You sent a request the server can’t understand. If you entered the URL manually please check your spelling and try again.

    +
    +
    {% endblock %} diff --git a/templates/calendar.jinja2 b/templates/calendar.jinja2 index 85c3afa..85c44a5 100644 --- a/templates/calendar.jinja2 +++ b/templates/calendar.jinja2 @@ -3,7 +3,7 @@ {% block title %}Calendar – {{ app_name }}{% endblock %} {% block content %} -
    +

    {{ T('calendar') }}

    @@ -32,5 +32,5 @@

    -
    + {% endblock %} \ No newline at end of file diff --git a/templates/contributions.jinja2 b/templates/contributions.jinja2 index ec13a94..59986a9 100644 --- a/templates/contributions.jinja2 +++ b/templates/contributions.jinja2 @@ -7,37 +7,40 @@ {% endblock %} {% block content %} -

    Contributions of {{ u.username }}

    +
    +

    {{ u.username }}

    +

    Contributions

    -
    -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    +
    +

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    -
    + {% if page_n < total_count // 20 %} +
  • Next page »
  • + {% endif %} + +
    +
    {% endblock %} diff --git a/templates/forbidden.jinja2 b/templates/forbidden.jinja2 index ab97d1d..2f039df 100644 --- a/templates/forbidden.jinja2 +++ b/templates/forbidden.jinja2 @@ -3,9 +3,11 @@ {% block title %}Access Denied - {{ app_name }}{% endblock %} {% block content %} -

    Forbidden

    +
    +

    Forbidden

    -
    -

    You don’t have permission to access this resource.

    -
    +
    +

    You don’t have permission to access this resource.

    +
    +
    {% endblock %} diff --git a/templates/history.jinja2 b/templates/history.jinja2 index c671e1f..e7e9914 100644 --- a/templates/history.jinja2 +++ b/templates/history.jinja2 @@ -7,33 +7,35 @@ {% endblock %} {% block content %} -

    {{ p.title }}

    -
    Page history
    +
    +

    {{ p.title }}

    +
    Page history
    - -

    {{ T("back-to") }} {{ p.title }}.

    +

    {{ T("back-to") }} {{ p.title }}.

    +
    {% endblock %} diff --git a/templates/home.jinja2 b/templates/home.jinja2 index 44abd83..e0d69c0 100644 --- a/templates/home.jinja2 +++ b/templates/home.jinja2 @@ -3,22 +3,24 @@ {% block title %}{{ T('homepage') }} - {{ app_name }}{% endblock %} {% block content %} -
    +

    {{ T('welcome').format(app_name) }}

    -
    - - - +
    + + +

    {{ T('latest-notes') }}

    + +
    + + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(new_notes) }}
    -

    {{ T('latest-notes') }}

    - -
    - - {% from "macros/nl.jinja2" import nl_list with context %} - {{ nl_list(new_notes) }} - -
    + {% endblock %} diff --git a/templates/includes/nl_item.jinja2 b/templates/includes/nl_item.jinja2 index c898901..f372285 100644 --- a/templates/includes/nl_item.jinja2 +++ b/templates/includes/nl_item.jinja2 @@ -1,3 +1,8 @@ +{# DEPRECATED #} +{# Use "macros/nl.jinja2" instead. #} + + +
    DEPRECATED

    {{ n.title }}

    diff --git a/templates/internalservererror.jinja2 b/templates/internalservererror.jinja2 index 8c24e60..01d6143 100644 --- a/templates/internalservererror.jinja2 +++ b/templates/internalservererror.jinja2 @@ -3,9 +3,11 @@ {% block title %}% _ % - {{ app_name }}{% endblock %} {% block content %} -

    Internal Server Error

    +
    +

    Internal Server Error

    -
    -

    We’re sorry, an unexpected error occurred. Try refreshing the page.

    -
    +
    +

    We’re sorry, an unexpected error occurred. Try refreshing the page.

    +
    +
    {% endblock %} diff --git a/templates/listrecent.jinja2 b/templates/listrecent.jinja2 index 3f19545..1da0196 100644 --- a/templates/listrecent.jinja2 +++ b/templates/listrecent.jinja2 @@ -1,10 +1,12 @@ {% extends "base.jinja2" %} {% block content %} -
    +

    Notes by date

    - {% from "macros/nl.jinja2" import nl_list with context %} - {{ nl_list(notes, page_n=page_n, total_count=total_count) }} -
    +
    + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(notes, page_n=page_n, total_count=total_count) }} +
    + {% endblock %} diff --git a/templates/listtag.jinja2 b/templates/listtag.jinja2 index c33b6ca..2f6b6c5 100644 --- a/templates/listtag.jinja2 +++ b/templates/listtag.jinja2 @@ -3,17 +3,19 @@ {% block title %}Notes tagged #{{ tagname }} - {{ app_name }}{% endblock %} {% block content %} -
    +

    #{{ tagname }}

    {{ T('notes-tagged') }}
    - {% if total_count > 0 %} - {% from "macros/nl.jinja2" import nl_list with context %} - {{ nl_list(l, page_n=page_n, total_count=total_count, hl_tags=(tagname,)) }} - {% else %} -

    {{ T('notes-tagged-empty') }}

    - {% endif %} +
    + {% if total_count > 0 %} + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(tagged_notes, page_n=page_n, total_count=total_count, hl_tags=(tagname,), other_url='tags/' + tagname) }} + {% else %} +

    {{ T('notes-tagged-empty') }}

    + {% endif %} +
    -
    + {% endblock %} diff --git a/templates/macros/nl.jinja2 b/templates/macros/nl.jinja2 index c34f963..5956fc8 100644 --- a/templates/macros/nl.jinja2 +++ b/templates/macros/nl.jinja2 @@ -1,6 +1,10 @@ +{# + Recommendations: Always import this macro with context, + otherwise it fails. It depends on a couple context-defined functions. +#} -{% macro nl_list(l, page_n=None, total_count=None, hl_tags=(), other_url='p/most_recent') %} +{% macro nl_list(l, page_n=None, total_count=None, hl_tags=(), hl_calendar=None, other_url='p/most_recent') %} {% if page_n and total_count %}

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} @@ -13,36 +17,44 @@ {% endif %} {% for n in l %}

  • - {{ n.title }} -

    {{ n.short_desc() }}

    - {% if n.tags %} -

    - tag - {{ T('tags') }}: - {% for tag in n.tags %} - {% set tn = tag.name %} - {% if tn in hl_tags %} - #{{ tn }} - {% else %} - #{{ tn }} +

    + {{ n.title }} +

    {{ n.short_desc() }}

    + {% if n.tags %} +

    + tag + {{ T('tags') }}: + {% for tag in n.tags %} + {% set tn = tag.name %} + {% if tn in hl_tags %} + #{{ tn }} + {% else %} + #{{ tn }} + {% endif %} + {% endfor %} +

    {% endif %} - {% endfor %} -

    - {% endif %} - {% if n.calendar %} -

    - calendar_today - - - -

    - {% endif %} + {% if n.calendar %} +

    + calendar_today + {% if hl_calendar and hl_calendar.y == n.calendar.y and hl_calendar.m == n.calendar.m %} + + + + {% else %} + + + + {% endif %} +

    + {% endif %} +
  • {% endfor %} {% if page_n is none %}
  • {{ T('show-all') }}
  • - {% elif page_n <= total_count // 20 %} + {% elif page_n <= (total_count - 1) // 20 %}
  • Next page »
  • {% endif %} diff --git a/templates/month.jinja2 b/templates/month.jinja2 index 6f1b369..3b28a70 100644 --- a/templates/month.jinja2 +++ b/templates/month.jinja2 @@ -3,61 +3,48 @@ {% block title %}{{ d.strftime("%B %Y") }} – {{ app_name }}{% endblock %} {% block content %} -
    +

    {{ d.strftime("%B %Y") }}

    - {% if total_count > 0 %} -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    +
    + {% if total_count > 0 %} + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(notes, total_count=total_count, page_n=page_n, hl_calendar=d) }} + + {% else %} +

    {{ T('notes-month-empty') }}

    + {% endif %} -
      - {% for n in notes %} -
    • - {{ n.title }} -

      {{ n.short_desc() }}

      - {% if n.tags %} -

      {{ T('tags') }}: - {% for tag in n.tags %} - {% set tn = tag.name %} - #{{ tn }} - {% endfor %} -

      - {% endif %} - {% if n.calendar %} -

      - calendar_today - {% if n.calendar.year == d.year and n.calendar.month == d.month %} - - - - {% else %} - - - - {% endif %} -

      - {% endif %} -
    • - {% endfor %} -
    - {% else %} -

    {{ T('notes-month-empty') }}

    - {% endif %} + {% set past_year = advance_calendar(d, -2) %} + {% set past_month = advance_calendar(d, -1) %} + {% set next_month = advance_calendar(d, 1) %} + {% set next_year = advance_calendar(d, 2) %} + - {% set past_year = advance_calendar(d, -2) %} - {% set past_month = advance_calendar(d, -1) %} - {% set next_month = advance_calendar(d, 1) %} - {% set next_year = advance_calendar(d, 2) %} - +
    +{% endblock %} -

    {{ T('back-to') }} {{ T('calendar') }}

    -
    +{% block toc %} + {% endblock %} \ No newline at end of file diff --git a/templates/notfound.jinja2 b/templates/notfound.jinja2 index e79d893..93c5af7 100644 --- a/templates/notfound.jinja2 +++ b/templates/notfound.jinja2 @@ -3,9 +3,11 @@ {% block title %}{{ T('not-found') }} - {{ app_name }}{% endblock %} {% block content %} -

    {{ T('not-found') }}

    +
    +

    {{ T('not-found') }}

    -
    -

    {{ T('not-found-text-1') }} {{ request.path }} {{ T('not-found-text-2') }}.

    -
    +
    +

    {{ T('not-found-text-1') }} {{ request.path }} {{ T('not-found-text-2') }}.

    +
    +
    {% endblock %} diff --git a/templates/search.jinja2 b/templates/search.jinja2 index b883186..29059d2 100644 --- a/templates/search.jinja2 +++ b/templates/search.jinja2 @@ -3,31 +3,33 @@ {% block title %}{% if q %}Search results for "{{ q }}"{% else %}Search{% endif %} - {{ app_name }}{% endblock %} {% block content %} -

    Search

    +
    +

    Search

    -
    -
    - -
    - - -
    -
    - - -
    -
    +
    +
    + +
    + + +
    +
    + + +
    +
    - {% if results %} -

    Search results for {{ q }}

    + {% if results %} +

    Search results for {{ q }}

    - {% from "macros/nl.jinja2" import nl_list with context %} - {{ nl_list(l, other_url=None) }} - {% elif q %} -

    {{ T('search-no-results') }} {{ q }}

    - {% else %} -

    Please note that search queries do not search for page text.

    - {% endif %} -
    + {% from "macros/nl.jinja2" import nl_list with context %} + {{ nl_list(results, other_url=None) }} + {% elif q %} +

    {{ T('search-no-results') }} {{ q }}

    + {% else %} +

    Please note that search queries do not search for page text.

    + {% endif %} +
    +
    {% endblock %} diff --git a/templates/upload.jinja2 b/templates/upload.jinja2 index 6c044b5..12bf89b 100644 --- a/templates/upload.jinja2 +++ b/templates/upload.jinja2 @@ -1,21 +1,23 @@ {% extends "base.jinja2" %} {% block content %} -

    Upload new file

    +
    +

    Upload new file

    -

    Uploads are no more supported. Please use this syntax instead for external images: ![alt text](https://www.example.com/path/to/image.jpg).

    +

    Uploads are no more supported. Please use this syntax instead for external images: ![alt text](https://www.example.com/path/to/image.jpg).

    -
    -
    - - -
    -
    - - -
    -
    - -
    -
    +
    +
    + + +
    +
    + + +
    +
    + +
    +
    +
    {% endblock %} From 93e6b9a0d3a3abe539f504186a11601bb07a924b Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Fri, 5 Sep 2025 21:42:18 +0200 Subject: [PATCH 43/51] the big change --- .gitignore | 47 +- CHANGELOG.md | 13 + LICENSE | 68 +- LICENSE.0_9 | 20 + README.md | 41 +- app.py | 1389 ----------------- app_init.py | 5 - app_sync.py | 175 --- extensions/circles.py | 241 --- extensions/contactnova.py | 214 --- i18n/salvi.en.json | 110 +- i18n/salvi.it.json | 11 +- migrations/0_6to0_7.py | 20 - migrations/0_7to0_8.py | 23 - pyproject.toml | 31 + requirements.txt | 16 +- robots.txt | 2 + salvi/__init__.py | 191 +++ salvi/colors.py | 77 + salvi/constants.py | 15 + salvi/exceptions.py | 9 + salvi/i18n.py | 34 + salvi/models.py | 657 ++++++++ salvi/renderer.py | 40 + salvi/routes/__init__.py | 44 + salvi/routes/accounts.py | 95 ++ salvi/routes/admin.py | 93 ++ salvi/routes/calendar.py | 47 + salvi/routes/edit.py | 203 +++ salvi/routes/embed.py | 22 + salvi/routes/home.py | 55 + salvi/routes/legal.py | 16 + salvi/routes/search.py | 22 + salvi/routes/stats.py | 75 + salvi/routes/view.py | 50 + {static => salvi/static}/content.js | 0 {static => salvi/static}/edit.js | 0 {static => salvi/static}/style.css | 238 +-- salvi/templates/400.html | 15 + salvi/templates/403.html | 14 + salvi/templates/404.html | 14 + .../templates/500.html | 7 +- .../templates/administration.html | 5 +- .../templates/backlinks.html | 12 +- salvi/templates/base.html | 83 + .../templates/calendar.html | 7 +- .../templates/contributions.html | 22 +- .../templates/easter.html | 5 +- .../edit.jinja2 => salvi/templates/edit.html | 15 +- .../templates/exportpages.html | 6 +- .../templates/history.html | 23 +- .../home.jinja2 => salvi/templates/home.html | 13 +- .../templates/importpages.html | 5 +- .../templates/kt_list.html | 2 +- .../new.jinja2 => salvi/templates/kt_new.html | 2 +- .../templates/kt_single.html | 2 +- salvi/templates/listrecent.html | 15 + .../templates/listtag.html | 9 +- salvi/templates/login.html | 32 + .../templates/macros/icon.html | 0 .../templates/macros/nl.html | 14 +- salvi/templates/macros/title.html | 17 + .../templates/manageaccounts.html | 17 +- .../templates/month.html | 11 +- .../templates/privacy.html | 7 +- salvi/templates/register.html | 41 + .../templates/rules.html | 7 +- .../templates/search.html | 13 +- salvi/templates/stats.html | 23 + .../templates/terms.html | 8 +- .../view.jinja2 => salvi/templates/view.html | 11 +- .../templates/viewold.html | 8 +- salvi/transfer.py | 104 ++ salvi/utils.py | 35 + strings.csv | 29 - templates/badrequest.jinja2 | 13 - templates/base.jinja2 | 62 - templates/circles/add.jinja2 | 58 - templates/circles/csv.jinja2 | 21 - templates/circles/list.jinja2 | 65 - templates/circles/stats.jinja2 | 30 - templates/forbidden.jinja2 | 13 - templates/includes/nl_item.jinja2 | 25 - templates/leaderboard.jinja2 | 40 - templates/listrecent.jinja2 | 12 - templates/login.jinja2 | 35 - templates/notfound.jinja2 | 13 - templates/register.jinja2 | 39 - templates/stats.jinja2 | 18 - templates/upload.jinja2 | 23 - 90 files changed, 2629 insertions(+), 2900 deletions(-) create mode 100644 LICENSE.0_9 delete mode 100644 app.py delete mode 100644 app_init.py delete mode 100644 app_sync.py delete mode 100644 extensions/circles.py delete mode 100644 extensions/contactnova.py delete mode 100644 migrations/0_6to0_7.py delete mode 100644 migrations/0_7to0_8.py create mode 100644 pyproject.toml create mode 100644 salvi/__init__.py create mode 100644 salvi/colors.py create mode 100644 salvi/constants.py create mode 100644 salvi/exceptions.py create mode 100644 salvi/i18n.py create mode 100644 salvi/models.py create mode 100644 salvi/renderer.py create mode 100644 salvi/routes/__init__.py create mode 100644 salvi/routes/accounts.py create mode 100644 salvi/routes/admin.py create mode 100644 salvi/routes/calendar.py create mode 100644 salvi/routes/edit.py create mode 100644 salvi/routes/embed.py create mode 100644 salvi/routes/home.py create mode 100644 salvi/routes/legal.py create mode 100644 salvi/routes/search.py create mode 100644 salvi/routes/stats.py create mode 100644 salvi/routes/view.py rename {static => salvi/static}/content.js (100%) rename {static => salvi/static}/edit.js (100%) rename {static => salvi/static}/style.css (57%) create mode 100644 salvi/templates/400.html create mode 100644 salvi/templates/403.html create mode 100644 salvi/templates/404.html rename templates/internalservererror.jinja2 => salvi/templates/500.html (50%) rename templates/administration.jinja2 => salvi/templates/administration.html (75%) rename templates/backlinks.jinja2 => salvi/templates/backlinks.html (75%) create mode 100644 salvi/templates/base.html rename templates/calendar.jinja2 => salvi/templates/calendar.html (83%) rename templates/contributions.jinja2 => salvi/templates/contributions.html (50%) rename templates/easter.jinja2 => salvi/templates/easter.html (86%) rename templates/edit.jinja2 => salvi/templates/edit.html (94%) rename templates/exportpages.jinja2 => salvi/templates/exportpages.html (84%) rename templates/history.jinja2 => salvi/templates/history.html (62%) rename templates/home.jinja2 => salvi/templates/home.html (59%) rename templates/importpages.jinja2 => salvi/templates/importpages.html (83%) rename templates/contactnova/list.jinja2 => salvi/templates/kt_list.html (98%) rename templates/contactnova/new.jinja2 => salvi/templates/kt_new.html (98%) rename templates/contactnova/single.jinja2 => salvi/templates/kt_single.html (95%) create mode 100644 salvi/templates/listrecent.html rename templates/listtag.jinja2 => salvi/templates/listtag.html (68%) create mode 100644 salvi/templates/login.html rename extensions/__init__.py => salvi/templates/macros/icon.html (100%) rename templates/macros/nl.jinja2 => salvi/templates/macros/nl.html (88%) create mode 100644 salvi/templates/macros/title.html rename templates/manageaccounts.jinja2 => salvi/templates/manageaccounts.html (68%) rename templates/month.jinja2 => salvi/templates/month.html (82%) rename templates/privacy.jinja2 => salvi/templates/privacy.html (91%) create mode 100644 salvi/templates/register.html rename templates/rules.jinja2 => salvi/templates/rules.html (91%) rename templates/search.jinja2 => salvi/templates/search.html (67%) create mode 100644 salvi/templates/stats.html rename templates/terms.jinja2 => salvi/templates/terms.html (97%) rename templates/view.jinja2 => salvi/templates/view.html (91%) rename templates/viewold.jinja2 => salvi/templates/viewold.html (65%) create mode 100644 salvi/transfer.py create mode 100644 salvi/utils.py delete mode 100644 strings.csv delete mode 100644 templates/badrequest.jinja2 delete mode 100644 templates/base.jinja2 delete mode 100644 templates/circles/add.jinja2 delete mode 100644 templates/circles/csv.jinja2 delete mode 100644 templates/circles/list.jinja2 delete mode 100644 templates/circles/stats.jinja2 delete mode 100644 templates/forbidden.jinja2 delete mode 100644 templates/includes/nl_item.jinja2 delete mode 100644 templates/leaderboard.jinja2 delete mode 100644 templates/listrecent.jinja2 delete mode 100644 templates/login.jinja2 delete mode 100644 templates/notfound.jinja2 delete mode 100644 templates/register.jinja2 delete mode 100644 templates/stats.jinja2 delete mode 100644 templates/upload.jinja2 diff --git a/.gitignore b/.gitignore index 9c35e69..62f74a0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,22 +1,29 @@ -# application content -media/ -**.sqlite -database/ -site.conf -site-*.conf -run_8180.py -.env -alembic.ini -venv -venv-* -.venv -env - -# automatically generated garbage -**/__pycache__/ +node_modules/ +__pycache__/ **.pyc **~ -**/.\#* -**/\#*\# -ig_api_settings/ -node_modules/ +.*.swp +\#*\# +.\#* +alembic.ini +.env +.env.* +.venv +env +venv +venv-*/ +config/ +conf/ +config.json +data/ +site-*.conf +site.conf +.err +.vscode +/run.sh +/target +dist/ +build/ +**.egg-info +ROADMAP.md + diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ff070e..0b40585 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # What’s New +## 1.0.0 + ++ **BREAKING CHANGES AHEAD**! ++ **SECURITY ADVISORY**: versions from `0.7` up to `0.9` are **VULNERABLE to XSS** due to `.jinja2` not getting autoescaped. ++ No more a monolith: `app.py` got split into several files into `salvi` package ++ Switched from peewee to **SQLAlchemy**; Schema as of 1.0 is the same as 0.9 ++ Added dependency on [libsuou](https://github.com/yusurko/suou) ++ `site.conf` deprecated, but still supported for the time being ++ Switched to `pyproject.toml`. `requirements.txt` has been sunset. ++ Switched to the Apache License; the old license text is moved to `LICENSE.0_9` ++ Added color themes! This is a breaking (but trivial) aesthetic change. Default theme is 'Miku' (aquamarine green). ++ Extensions have been removed. They never had a clear, usable, public API in the first place. + ## 0.9 + Removed `markdown_katex` dependency, and therefore support for math. diff --git a/LICENSE b/LICENSE index a2d4b30..df8a219 100644 --- a/LICENSE +++ b/LICENSE @@ -1,19 +1,55 @@ -Copyright (C) 2020-2021 Sakuragasaki46 -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Copyright (c) 2020-2025 Sakuragasaki46 -The above copyright notice and this permission notice shall be included -in all copies or substantial portions of the Software. +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. + +"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + + You must give any other recipients of the Work or Derivative Works a copy of this License; and + You must cause any modified files to carry prominent notices stating that You changed the files; and + You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and + If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. + +You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS diff --git a/LICENSE.0_9 b/LICENSE.0_9 new file mode 100644 index 0000000..a513535 --- /dev/null +++ b/LICENSE.0_9 @@ -0,0 +1,20 @@ + +Copyright (C) 2020-2021 Sakuragasaki46 + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included +in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 941565a..56a8ff2 100644 --- a/README.md +++ b/README.md @@ -17,46 +17,47 @@ suitable as a community or team knowledge base. + Calendar + Works fine even with JavaScript disabled. -## Requirements +## Requirements & Dependencies -+ **Python** 3.6+. ++ **Python** 3.10+. + **Flask** web framework (and Flask-Login / Flask-WTF / Flask-Arrest extensions). -+ **Peewee** ORM. ++ **SQLAlchemy** ORM. + **Markdown** for page rendering. -+ **Python-I18n**. ++ **[SUOU](https://github.com/yusurko/suou)**. (since 1.0) * **Python-Dotenv**. + The database drivers needed for the type of database. +To install the dependencies, run (virtualenv advised): +> `pip install -e .` + ## Usage -+ Clone this repository: `git clone https://github.com/sakuragasaki46/salvi` -+ Edit site.conf (old way) or .env (new way) with the needed parameters. An example site.conf with SQLite: - -``` -[site] -name = Salvi - -[database] -directory = /path/to/database/ -``` - - An example .env with MySQL: ++ Clone this repository: `git clone https://github.com/yusurko/salvi` ++ Edit `.env` with the needed parameters. An example `.env` with MySQL: ``` APP_NAME=Salvi DATABASE_URL=mysql://root:root@localhost/salvi ``` -+ Run `python3 -m app_init` to initialize the database and create the administrator user. ++ Run `python3 -m app_init` to initialize the database and create the administrator user. (The installer is unstable) + Run `flask run`. + You can now access Salvi in your browser at port 5000. ## Caveats -+ The whole application is untested. -+ If you forget the password, there is currently no way to reset it. ++ The whole application is free of unit tests. Ergo untested. No coverage. ++ If you forget the password, there is currently no way to reset it. E-mail is not supported as of 1.0. + This app comes with no content. It means, you have to write it yourself. ## License -[MIT License](./LICENSE). +Since 1.0, Salvi is licensed under the [Apache License, Version 2.0](LICENSE), a non-copyleft free and open source license. + +Salvi`<=0.9` is under **[MIT license](LICENSE.0_9)**, available at `LICENSE.0_9`. + +This is a hobby project, made available “AS IS”, with __no warranty__ express or implied. + +I (sakuragasaki46) may NOT be held accountable for Your use of my code. + +> It's pointless to file a lawsuit because you feel damaged, and it's only going to turn against you. What a waste of money you could have spent on a vacation or charity, or invested in stocks. diff --git a/app.py b/app.py deleted file mode 100644 index 2bdec0d..0000000 --- a/app.py +++ /dev/null @@ -1,1389 +0,0 @@ -# (C) 2020-2021 Sakuragasaki46. -# See LICENSE for copying info. - -''' -A simple wiki-like note webapp. - -Pages are stored in SQLite/MySQL databases. -Markdown is used for text formatting. - -Application is kept compact, with all its core in a single file. -''' - -#### IMPORTS #### - -from flask import ( - Flask, abort, flash, g, jsonify, make_response, redirect, request, - render_template, send_from_directory) -from markupsafe import Markup -from flask_login import LoginManager, login_user, logout_user, current_user, login_required -from flask_wtf import CSRFProtect -#from flask_arrest import RestBlueprint, serialize_response -from werkzeug.security import generate_password_hash, check_password_hash -from werkzeug.routing import BaseConverter -from peewee import * -from playhouse.db_url import connect as dbconnect -import datetime, hashlib, html, importlib, json, markdown, os, random, \ - re, sys, warnings -from functools import lru_cache, partial -from urllib.parse import quote -from configparser import ConfigParser -import i18n -import gzip -from getpass import getpass -import dotenv - -__version__ = '0.9-dev' - -#### CONSTANTS #### - -APP_BASE_DIR = os.path.dirname(__file__) - -FK = ForeignKeyField - -SLUG_RE = r'[a-z0-9]+(?:-[a-z0-9]+)*' -ILINK_RE = r'\]\(/(p/\d+|' + SLUG_RE + ')/?\)' -USERNAME_RE = r'[a-z0-9_-]{3,30}' -PING_RE = r'(?' + match.group(1) + '' - -# XXX ugly monkeypatch to make spoilers prevail over blockquotes -from markdown.blockprocessors import BlockQuoteProcessor -BlockQuoteProcessor.RE = re.compile(r'(^|\n)[ ]{0,3}>(?!!)[ ]?(.*)') - -### XXX it currently only detects spoilers that are not at the beginning of the line. To be fixed. -class SpoilerExtension(markdown.extensions.Extension): - def extendMarkdown(self, md, md_globals=None): - md.inlinePatterns.register(markdown.inlinepatterns.SimpleTagInlineProcessor(r'()>!(.*?)!<', 'span class="spoiler"'), 'spoiler', 14) - - -#class PingExtension(markdown.extensions.Extension): -# def extendMarkdown(self, md): -# pass - -#### DATABASE SCHEMA #### - -database_url = os.getenv("DATABASE_URL") or _getconf('database', 'url') -if database_url: - database = dbconnect(database_url) -else: - print("Database URL required.") - exit(-1) - -class BaseModel(Model): - class Meta: - database = database - -class User(BaseModel): - username = CharField(32, unique=True) - email = CharField(256, null=True) - password = CharField() - join_date = DateTimeField(default=datetime.datetime.now) - karma = IntegerField(default=1) - privileges = BitField() - is_admin = privileges.flag(1) - restrictions = BitField() - is_disabled = restrictions.flag(1) - - # helpers for flask_login - @property - def is_anonymous(self): - return False - @property - def is_active(self): - return not self.is_disabled - @property - def is_authenticated(self): - return True - - @property - def groups(self): - return ( - UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group) - .where(UserGroupMembership.user == self) - ) - - def get_perms(self): - if self.is_admin: - return PERM_ALL - - perm = 0 - - for gr in self.groups: - perm |= gr.permissions - - return perm - - -# page perms (used as bitmasks) -PERM_READ = 1 -PERM_EDIT = 2 -PERM_CREATE = 4 -PERM_SET_URL = 8 -PERM_SET_TAGS = 16 -PERM_ALL = 31 -PERM_LOCK = ~(PERM_EDIT | PERM_CREATE | PERM_SET_URL | PERM_SET_TAGS) - -class UserGroup(BaseModel): - name = CharField(32, unique=True) - permissions = BitField() - # Can read pages. - can_read = permissions.flag(1) - # Can edit all non-locked pages. - can_edit = permissions.flag(2) - # Can create and edit their own pages. - can_create = permissions.flag(4) - # Can set page URL. - can_set_url = permissions.flag(8) - # Can change page tags. - can_set_tags = permissions.flag(16) - - @classmethod - def get_default_group(cls): - try: - return cls[1] - except cls.DoesNotExist: - return cls.get(cls.name == 'default') - -class UserGroupMembership(BaseModel): - user = ForeignKeyField(User, backref='group_memberships') - group = ForeignKeyField(UserGroup, backref='user_memberships') - since = DateTimeField(default=datetime.datetime.now) - - class Meta: - indexes = ( - (('user', 'group'), True), - ) - - -class Page(BaseModel): - url = CharField(64, unique=True, null=True) - title = CharField(256, index=True) - touched = DateTimeField(index=True) - calendar = DateTimeField(index=True, null=True) - owner = ForeignKeyField(User, null=True) - flags = BitField() - is_redirect = flags.flag(1) - is_sync = flags.flag(2) - is_math_enabled = flags.flag(4) # legacy, math is no more supported - is_locked = flags.flag(8) - is_cw = flags.flag(16) - @property - def latest(self): - if self.revisions: - return self.revisions.order_by(PageRevision.pub_date.desc())[0] - def get_url(self): - return '/' + self.url + '/' if self.url else '/p/{}/'.format(self.id) - def short_desc(self): - if self.is_cw: - return '(Content Warning: we are not allowed to show a description.)' - full_text = self.latest.text - text = remove_tags(full_text, convert = not _getconf('appearance', 'simple_remove_tags', False)) - return text[:200] + ('\u2026' if len(text) > 200 else '') - def change_tags(self, new_tags): - old_tags = set(x.name for x in self.tags) - new_tags = set(new_tags) - PageTag.delete().where((PageTag.page == self) & - (PageTag.name << (old_tags - new_tags))).execute() - for tag in (new_tags - old_tags): - PageTag.create(page=self, name=tag) - def js_info(self): - latest = self.latest - return dict( - id=self.id, - url=self.url, - title=self.title, - is_redirect=self.is_redirect, - touched=self.touched.timestamp(), - is_editable=self.is_editable(), - latest=dict( - id=latest.id if latest else None, - length=latest.length, - pub_date=latest.pub_date.timestamp() if latest and latest.pub_date else None - ), - tags=[x.name for x in self.tags] - ) - @property - def prop(self): - return PagePropertyDict(self) - def is_editable(self): - return self.can_edit(current_user) - - def can_edit(self, user): - perm = self.get_perms(user) - return perm & PERM_EDIT or (self.owner == user and perm & PERM_CREATE) - def is_owned_by(self, user): - return user.id == self.owner.id - - def get_perms(self, user=None): - if user is None: - user = current_user - - if user.is_anonymous: - return UserGroup.get_default_group().permissions & PERM_LOCK - - if user.is_admin: - return PERM_ALL - - perm = 0 - # default groups - for gr in user.groups: - perm |= gr.permissions - - # page overrides - for ov in self.permission_overrides: - if ov.group in user.groups: - perm |= ov.permissions - - if self.is_locked and self.owner.id != user.id: - perm &= PERM_LOCK - return perm - - def seo_keywords(self): - kw = [] - for tag in self.tags: - kw.append(tag.name.replace("-", " ")) - for bkl in self.back_links: - try: - kw.append(bkl.from_page.title.replace(",", "")) - except Exception: - pass - return ", ".join(kw) - - #def ldjson(self): - # return { - # "@context": "https://www.w3.org/ns/activitystreams", - # - # } - - -class PageText(BaseModel): - content = BlobField() - flags = BitField() - is_utf8 = flags.flag(1) - is_gzipped = flags.flag(2) - def get_content(self): - c = self.content - if self.is_gzipped: - c = gzip.decompress(c) - if self.is_utf8: - return c.decode('utf-8') - else: - return c.decode('latin-1') - @classmethod - def create_content(cls, text, *, treshold=600, search_dup=True): - c = text.encode('utf-8') - use_gzip = len(c) > treshold - if use_gzip and gzip: - c = gzip.compress(c) - if search_dup: - item = cls.get_or_none((cls.content == c) & (cls.is_gzipped == use_gzip)) - if item: - return item - return cls.create( - content=c, - is_utf8=True, - is_gzipped=use_gzip - ) - -class PageRevision(BaseModel): - page = FK(Page, backref='revisions', index=True) - user = ForeignKeyField(User, backref='contributions', null=True) - comment = CharField(1024, default='') - textref = FK(PageText) - pub_date = DateTimeField(index=True) - length = IntegerField() - @property - def text(self): - return self.textref.get_content() - def html(self): - return md(self.text) - def html_and_toc(self): - return md_and_toc(self.text) - def human_pub_date(self): - delta = datetime.datetime.now() - self.pub_date - T = partial(get_string, g.lang) - if delta < datetime.timedelta(seconds=60): - return T('just-now') - elif delta < datetime.timedelta(seconds=3600): - return T('n-minutes-ago').format(delta.seconds // 60) - - elif delta < datetime.timedelta(days=1): - return T('n-hours-ago').format(delta.seconds // 3600) - elif delta < datetime.timedelta(days=15): - return T('n-days-ago').format(delta.days) - else: - return self.pub_date.strftime('%B %-d, %Y') - -class PageTag(BaseModel): - page = FK(Page, backref='tags', index=True) - name = CharField(64, index=True) - class Meta: - indexes = ( - (('page', 'name'), True), - ) - def popularity(self): - return PageTag.select().where(PageTag.name == self.name).count() - -class PageProperty(BaseModel): - page = ForeignKeyField(Page, backref='page_meta', index=True) - key = CharField(64) - value = CharField(8000) - class Meta: - indexes = ( - (('page', 'key'), True), - ) - -# currently experimental -class PagePropertyDict(object): - def __init__(self, page): - self._page = page - def items(self): - for kv in self._page.page_meta: - yield kv.key, kv.value - def __len__(self): - return self._page.page_meta.count() - def keys(self): - for kv in self._page.page_meta: - yield kv.key - __iter__ = keys - def __getitem__(self, key): - try: - return self._page.page_meta.get(PageProperty.key == key).value - except PageProperty.DoesNotExist: - raise KeyError(key) - def get(self, key, default=None): - try: - return self._page.page_meta.get(PageProperty.key == key).value - except PageProperty.DoesNotExist: - return default - def setdefault(self, key, default): - try: - return self._page.page_meta.get(PageProperty.key == key).value - except PageProperty.DoesNotExist: - self[key] = default - return default - def __setitem__(self, key, value): - if key in self: - pp = self._page.page_meta.get(PageProperty.key == key) - pp.value = value - pp.save() - else: - PageProperty.create(page=self._page, key=key, value=value) - def __delitem__(self, key): - PageProperty.delete().where((PageProperty.page == self._page) & - (PageProperty.key == key)).execute() - def __contains__(self, key): - return PageProperty.select().where((PageProperty.page == self._page) & - (PageProperty.key == key)).exists() - - -# Link table for caching purposes. -class PageLink(BaseModel): - from_page = FK(Page, backref='forward_links') - to_page = FK(Page, backref='back_links') - - class Meta: - indexes = ( - (('from_page', 'to_page'), True), - ) - - @classmethod - def parse_links(cls, from_page, text, erase=True): - with database.atomic(): - old_links = list(cls.select().where(cls.from_page == from_page)) - for mo in re.finditer(ILINK_RE, text): - try: - pageurl = mo.group(1) - if pageurl.startswith('p/'): - to_page = Page[int(pageurl[2:])] - else: - to_page = Page.get(Page.url == pageurl) - - linkobj, created = PageLink.get_or_create( - from_page = from_page, - to_page = to_page) - if linkobj in old_links: - old_links.remove(linkobj) - except Exception: - continue - if erase: - for linkobj in old_links: - linkobj.delete_instance() - - # The actual ULTIMATE method to refresh all links - # To be called from a maintenance script only! - @classmethod - def refresh_all_links(cls): - for p in Page.select(): - cls.parse_links(p, p.latest.text) - -class PagePermission(BaseModel): - page = ForeignKeyField(Page, backref='permission_overrides') - group = ForeignKeyField(UserGroup, backref='page_permissions') - permissions = BitField() - # Can read pages. - can_read = permissions.flag(1) - # Can edit all non-locked pages. - can_edit = permissions.flag(2) - # Can create and edit their own pages. - can_create = permissions.flag(4) - # Can set page URL. - can_set_url = permissions.flag(8) - # Can change page tags. - can_set_tags = permissions.flag(16) - - class Meta: - indexes = ( - (('page', 'group'), True), - ) - -def init_db(): - database.create_tables([ - User, UserGroup, UserGroupMembership, - Page, PageText, PageRevision, PageTag, PageProperty, PageLink, - PagePermission - ]) - -def init_db_and_create_first_user(): - try: - User[1] - UserGroup[1] - except Exception: - pass - else: - print('Looks like this site is already set up!') - return - username = input('Username: ') - password = getpass('Password: ') - confirm_password = getpass('Confirm password: ') - email = input('Email: (optional)') or None - if not is_username(username): - print('Invalid username: usernames can contain only letters, numbers, underscores and hyphens.') - return - if password != confirm_password: - print('Passwords do not match.') - return - default_permissions = PERM_ALL # all permissions - if not input('Agree to the Terms of Use?')[0].lower() == 'y': - print('You must accept Terms in order to register.') - return - with database.atomic(): - init_db() - ua = User.create( - username = username, - email = email, - password = generate_password_hash(password), - join_date = datetime.datetime.now(), - is_admin = True - ) - ug = UserGroup.create( - name = 'default', - permissions = int(default_permissions) - ) - UserGroupMembership.create( - user = ua, - group = ug - ) - print('Installed successfully!') - -#### PERMS HELPERS #### - -def has_perms(user, flags, page=None): - if page: - perm = page.get_perms(user) - else: - perm = user.get_perms() - - if perm & flags: - return True - return False - -#### WIKI SYNTAX #### - -def md_and_toc(text, toc=True): - extensions = ['tables', 'footnotes', 'fenced_code', 'sane_lists'] - extension_configs = {} - if not _getconf('markdown', 'disable_custom_extensions'): - extensions.append(StrikethroughExtension()) - extensions.append(SpoilerExtension()) - if toc: - extensions.append('toc') - try: - converter = markdown.Markdown(extensions=extensions, extension_configs=extension_configs) - if toc: - return converter.convert(text), converter.toc - else: - return converter.convert(text), '' - except Exception as e: - return '

    There was an error during rendering: {e.__class__.__name__}: {e}

    '.format(e=e), '' - -def md(text, toc=True): - return md_and_toc(text, toc=toc)[0] - -def remove_tags(text, convert=True, headings=True): - if headings: - text = re.sub(r'\#[^\n]*', '', text) - if convert: - text = md(text, toc=False) - return re.sub(r'<.*?>', '', text) - -def is_username(s): - return re.match('^' + USERNAME_RE + '$', s) - -#### I18N #### - -i18n.load_path.append(os.path.join(APP_BASE_DIR, 'i18n')) -i18n.set('file_format', 'json') - -def get_string(loc, s): - i18n.set('locale', loc) - return i18n.t('salvi.' + s) - -#### APPLICATION CONFIG #### - -class SlugConverter(BaseConverter): - regex = SLUG_RE - -def is_valid_url(url): - return re.fullmatch(SLUG_RE, url) - -def is_url_available(url): - return url not in forbidden_urls and not Page.select().where(Page.url == url).exists() - -forbidden_urls = [ - 'about', 'accounts', 'ajax', 'backlinks', 'calendar', 'circles', 'create', - 'easter', 'edit', 'embed', 'group', 'help', 'history', 'init-config', 'kt', - 'manage', 'media', 'p', 'privacy', 'protect', 'rules', 'search', 'static', - 'stats', 'tags', 'terms', 'u', 'upload', 'upload-info' -] - -app = Flask(__name__) -app.secret_key = b'\xf3\xa9?\xbee$L\xabA\xd3\r\xa2\x08\xf6\x00%0b\xa9\xfe\x11\x04\xa6\xd8=\xd3\xa2\x00\xb3\xd5;9' -app.url_map.converters['slug'] = SlugConverter - -csrf = CSRFProtect(app) -login_manager = LoginManager(app) - -login_manager.login_view = 'accounts_login' - -#### ROUTES #### - -def _get_lang(): - if request.args.get('uselang') is not None: - lang = request.args['uselang'] - else: - for l in request.headers.get('accept-language', 'it,en').split(','): - if ';' in l: - l, _ = l.split(';') - lang = l - break - else: - lang = 'en' - return lang - -@app.before_request -def _before_request(): - g.lang = _get_lang() - -@app.context_processor -def _inject_variables(): - return { - 'T': partial(get_string, _get_lang()), - 'app_name': os.getenv("APP_NAME") or _getconf('site', 'title'), - 'strong': lambda x:Markup('{0}').format(x), - 'app_version': __version__, - 'material_icons_url': _getconf('site', 'material_icons_url'), - 'min': min - } - -@login_manager.user_loader -def _inject_user(userid): - u = User[userid] - if not u.is_disabled: - return u - -@app.template_filter() -def linebreaks(text): - text = html.escape(text) - text = text.replace("\n\n", '

    ').replace('\n', '
    ') - return Markup(text) - -app.template_filter(name='markdown')(md) - -@app.route('/') -def homepage(): - page_limit = _getconf("appearance", "items_per_page", 20, cast=int) - return render_template('home.jinja2', new_notes=Page.select() - .order_by(Page.touched.desc()).limit(page_limit)) - -@app.route('/robots.txt') -def robots(): - return send_from_directory(APP_BASE_DIR, 'robots.txt') - -@app.route('/favicon.ico') -def favicon(): - return send_from_directory(APP_BASE_DIR, 'favicon.ico') - -## error handlers ## - -@app.errorhandler(404) -def error_404(body): - return render_template('notfound.jinja2'), 404 - -@app.errorhandler(403) -def error_403(body): - return render_template('forbidden.jinja2'), 403 - -@app.errorhandler(400) -def error_400(body): - return render_template('badrequest.jinja2'), 400 - -@app.errorhandler(500) -def error_500(body): - try: - User[1] - except OperationalError: - g.need_install = True - return render_template('internalservererror.jinja2'), 500 - -# Middle point during page editing. -def savepoint(form, is_preview=False, pageobj=None): - if is_preview: - preview = md(form['text']) - else: - preview = None - pl_js_info = dict() - pl_js_info['editing'] = dict( - original_text = None, # TODO - preview_text = form['text'], - page_id = pageobj.id if pageobj else None - ) - return render_template( - 'edit.jinja2', - pl_url=form['url'], - pl_title=form['title'], - pl_text=form['text'], - pl_tags=form['tags'], - pl_comment=form['comment'], - pl_is_locked='lockpage' in form, - pl_owner_is_current_user=pageobj.is_owned_by(current_user) if pageobj else True, - preview=preview, - pl_js_info=pl_js_info, - pl_calendar=('usecalendar' in form) and form['calendar'], - pl_readonly=not pageobj.can_edit(current_user) if pageobj else False, - pl_cw=('cw' in form) and form['cw'] - ) - -@app.route('/create/', methods=['GET', 'POST']) -@login_required -def create(): - if not has_perms(current_user, PERM_CREATE): - flash("You are not allowed to create pages.") - abort(403) - if request.method == 'POST': - if request.form.get('preview'): - return savepoint(request.form, is_preview=True) - p_url = request.form['url'] or None - if p_url: - if not is_valid_url(p_url): - flash('Invalid URL. Valid URLs contain only letters, numbers and hyphens.') - return savepoint(request.form) - elif not is_url_available(p_url): - flash('This URL is not available.') - return savepoint(request.form) - p_tags = [x.strip().lower().replace(' ', '-').replace('_', '-').lstrip('#') - for x in request.form.get('tags', '').split(',') if x] - if any(not re.fullmatch(SLUG_RE, x) for x in p_tags): - flash('Invalid tags text. Tags contain only letters, numbers and hyphens, and are separated by comma.') - return savepoint(request.form) - try: - p = Page.create( - url=p_url, - title=request.form['title'], - is_redirect=False, - touched=datetime.datetime.now(), - owner_id=current_user.id, - calendar=datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None, - is_locked = 'lockpage' in request.form, - is_cw = 'cw' in request.form - ) - p.change_tags(p_tags) - except IntegrityError as e: - flash('An error occurred while saving this revision: {e}'.format(e=e)) - return savepoint(request.form) - pr = PageRevision.create( - page=p, - user_id=p.owner.id, - comment='', - textref=PageText.create_content(request.form['text']), - pub_date=datetime.datetime.now(), - length=len(request.form['text']) - ) - PageLink.parse_links(p, request.form['text']) - return redirect(p.get_url()) - return savepoint({ - "url": request.args.get("url"), - "title": "", - "text": "", - "tags": "", - "comment": get_string(g.lang, "page-created") - }) - -@app.route('/edit//', methods=['GET', 'POST']) -@login_required -def edit(id): - p = Page[id] - if request.method == 'POST': - # check if page is locked first! - if not p.can_edit(current_user): - flash('You are trying to edit a locked page!') - abort(403) - - if request.form.get('preview'): - return savepoint(request.form, is_preview=True, pageobj=p) - p_url = request.form['url'] or None - if p_url: - if not is_valid_url(p_url): - flash('Invalid URL. Valid URLs contain only letters, numbers and hyphens.') - return savepoint(request.form, pageobj=p) - elif not is_url_available(p_url) and p_url != p.url: - flash('This URL is not available.') - return savepoint(request.form, pageobj=p) - p_tags = [x.strip().lower().replace(' ', '-').replace('_', '-').lstrip('#') - for x in request.form.get('tags', '').split(',')] - p_tags = [x for x in p_tags if x] - if any(not re.fullmatch(SLUG_RE, x) for x in p_tags): - flash('Invalid tags text. Tags contain only letters, numbers and hyphens, and are separated by comma.') - return savepoint(request.form, pageobj=p) - p.url = p_url - p.title = request.form['title'] - p.touched = datetime.datetime.now() - p.is_locked = 'lockpage' in request.form - p.is_cw = 'cw' in request.form - p.calendar = datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None - p.save() - p.change_tags(p_tags) - if request.form['text'] != p.latest.text: - pr = PageRevision.create( - page=p, - user_id=current_user.id, - comment=request.form["comment"], - textref=PageText.create_content(request.form['text']), - pub_date=datetime.datetime.now(), - length=len(request.form['text']) - ) - PageLink.parse_links(p, request.form['text']) - return redirect(p.get_url()) - - form = { - "url": p.url, - "title": p.title, - "text": p.latest.text, - "tags": ','.join(x.name for x in p.tags), - "comment": "" - } - if p.is_locked: - form["lockpage"] = "1" - if p.calendar: - form["usecalendar"] = "1" - try: - form["calendar"] = p.calendar.isoformat().split("T")[0] - except Exception: - form["calendar"] = p.calendar - - return savepoint(form, pageobj=p) - -@app.route("/__sync_start") -def __sync_start(): - flash("Sync is unavailable. Please import and export pages manually.") - return redirect("/") - -@app.route('/_jsoninfo/', methods=['GET', 'POST']) -def page_jsoninfo(id): - try: - p = Page[id] - except Page.DoesNotExist: - return jsonify({'status':'fail'}), 404 - j = p.js_info() - j["status"] = "ok" - if request.method == "POST": - j["text"] = p.latest.text - return jsonify(j) - -@app.route("/_jsoninfo/changed/") -def jsoninfo_changed(ts): - tse = str(datetime.datetime.fromtimestamp(ts).isoformat(" ")) - ps = Page.select().where(Page.touched >= tse) - return jsonify({ - "ids": [i.id for i in ps], - "status": "ok" - }) - - -@app.route('/p//') -def view_unnamed(id): - try: - p = Page[id] - except Page.DoesNotExist: - abort(404) - if p.url: - if p.url not in forbidden_urls: - return redirect(p.get_url()) - else: - flash('The URL of this page is a reserved URL. Please change it.') - return render_template('view.jinja2', p=p, rev=p.latest) - -@app.route('/embed//') -def embed_view(id): - try: - p = Page[id] - except Page.DoesNotExist: - return "", 404 - rev = p.latest - return "

    {0}

    {1}
    ".format( - html.escape(p.title), rev.html()) - -@app.route('/p/most_recent/') -def view_most_recent(): - general_query = Page.select().order_by(Page.touched.desc()) - return render_paginated_template('listrecent.jinja2', 'notes', notes=general_query) - -@app.route('/p/random/') -def view_random(): - page = None - if Page.select().count() < 2: - flash('Too few pages in this site.') - abort(404) - while not page: - try: - page = Page[random.randint(1, Page.select().count())] - except Page.DoesNotExist: - continue - return redirect(page.get_url()) - -@app.route('/p/leaderboard/') -def page_leaderboard(): - headers = { - 'Cache-Control': 'max-age=180, stale-while-revalidate=1800' - } - - pages = [] - for p in Page.select(): - score = (p.latest.length >> 10) + p.forward_links.count() + p.back_links.count() - pages.append((p, score, p.back_links.count(), p.forward_links.count(), p.latest.length)) - pages.sort(key = lambda x: (x[1], x[2], x[4], x[3]), reverse = True) - - return render_template('leaderboard.jinja2', pages=pages), headers - -@app.route('//') -def view_named(name): - try: - p = Page.get(Page.url == name) - except Page.DoesNotExist: - abort(404) - return render_template('view.jinja2', p=p, rev=p.latest) - -@app.route('/history//') -def history(id): - try: - p = Page[id] - except Page.DoesNotExist: - abort(404) - return render_template('history.jinja2', p=p, history=p.revisions.order_by(PageRevision.pub_date.desc())) - -@app.route('/u//') -def contributions_legacy_url(username): - return redirect(f'/@{username}/') - -@app.route('/@/') -def contributions(username): - try: - user = User.get(User.username == username) - except User.DoesNotExist: - abort(404) - contributions = user.contributions.order_by(PageRevision.pub_date.desc()) - return render_paginated_template('contributions.jinja2', - "contributions", - u=user, - contributions=contributions, - ) - -def _advance_calendar(date, offset=0): - if offset == -2: - return datetime.date(date.year - 1, date.month, 1) - elif offset == -1: - return datetime.date(date.year, date.month - 1, 1) if date.month > 1 else datetime.date(date.year - 1, 12, 1) - elif offset == 1: - return datetime.date(date.year, date.month + 1, 1) if date.month < 12 else datetime.date(date.year + 1, 1, 1) - elif offset == 2: - return datetime.date(date.year + 1, date.month, 1) - else: - return date - -@app.route('/calendar/') -def calendar_view(): - now = datetime.datetime.now() - return render_template('calendar.jinja2', now=now, - from_year=int(request.args.get('from_year', now.year - 12)), - till_year=int(request.args.get('till_year', now.year + 5)) - ) - -@app.route('/calendar//') -def calendar_month(y, m): - notes = Page.select().where( - (datetime.date(y, m, 1) <= Page.calendar) & - (Page.calendar < datetime.date(y+1 if m==12 else y, 1 if m==12 else m+1, 1)) - ).order_by(Page.calendar) - - #toc_q = Page.select(fn.Month(Page.calendar).alias('month'), fn.Count(Page.id).alias('n_notes')).where( - # (datetime.date(y, 1, 1) <= Page.calendar) & - # (Page.calendar < datetime.date(y+1, 1, 1)) - #).group_by() - toc = {} - #for i in toc_q: - # toc[i.month] = i.n_notes - - return render_paginated_template('month.jinja2', "notes", d=datetime.date(y, m, 1), notes=notes, advance_calendar=_advance_calendar, toc=toc) - -@app.route('/history/revision//') -def view_old(revisionid): - try: - rev = PageRevision[revisionid] - except PageRevision.DoesNotExist: - abort(404) - p = rev.page - return render_template('viewold.jinja2', p=p, rev=rev) - -@app.route('/backlinks//') -def backlinks(id): - try: - p = Page[id] - except Page.DoesNotExist: - abort(404) - return render_template('backlinks.jinja2', p=p, backlinks=Page.select().join(PageLink, on=PageLink.to_page).where(PageLink.from_page == p)) - -@app.route('/search/', methods=['GET', 'POST']) -def search(): - if request.method == 'POST': - q = request.form['q'] - include_tags = bool(request.form.get('include-tags')) - query = Page.select().where(Page.title ** ('%' + q + '%')) - if include_tags: - query |= Page.select().join(PageTag, on=PageTag.page - ).where(PageTag.name ** ('%' + q + '%')) - query = query.order_by(Page.touched.desc()) - return render_template('search.jinja2', q=q, pl_include_tags=include_tags, - results=query.paginate(1)) - return render_template('search.jinja2', pl_include_tags=True) - -@app.route('/tags//') -def listtag(tag): - general_query = Page.select().join(PageTag, on=PageTag.page).where(PageTag.name == tag).order_by(Page.touched.desc()) - return render_paginated_template('listtag.jinja2', "tagged_notes", tagname=tag, tagged_notes=general_query) - - -# symbolic route as of v0.5 -@app.route('/upload/', methods=['GET']) -def upload(): - return render_template('upload.jinja2') - -@app.route('/stats/') -def stats(): - return render_template('stats.jinja2', - notes_count=Page.select().count(), - notes_with_url=Page.select().where(Page.url != None).count(), - revision_count=PageRevision.select().count(), - users_count = User.select().count(), - groups_count = UserGroup.select().count() - ) - -## account management ## - -@app.route('/accounts/theme-switch') -def theme_switch(): - cook = request.cookies.get('dark') - resp = make_response(redirect(request.args.get('next', '/'))) - resp.set_cookie('dark', '0' if cook == '1' else '1', max_age=31556952, path='/') - return resp - -@app.route('/accounts/login/', methods=['GET','POST']) -def accounts_login(): - if current_user.is_authenticated: - return redirect(request.args.get('next', '/')) - if request.method == 'POST': - try: - username = request.form['username'] - user = User.get(User.username == username) - if not check_password_hash(user.password, request.form['password']): - flash('Invalid username or password.') - return render_template('login.jinja2') - except User.DoesNotExist: - flash('Invalid username or password.') - else: - if user.is_disabled: - flash("Your account is disabled.") - return render_template("login.jinja2") - - remember_for = int(request.form['remember']) - if remember_for > 0: - login_user(user, remember=True, - duration=datetime.timedelta(days=remember_for)) - else: - login_user(user) - return redirect(request.args.get('next', '/')) - return render_template('login.jinja2') - -@app.route('/accounts/register/', methods=['GET','POST']) -def accounts_register(): - if request.method == 'POST': - username = request.form['username'] - password = request.form['password'] - if not is_username(username): - flash('Invalid username: usernames can contain only letters, numbers, underscores and hyphens.') - return render_template('register.jinja2') - if request.form['password'] != request.form['confirm_password']: - flash('Passwords do not match.') - return render_template('register.jinja2') - if not request.form['legal']: - flash('You must accept Terms in order to register.') - return render_template('register.jinja2') - try: - with database.atomic(): - u = User.create( - username = username, - email = request.form.get('email'), - password = generate_password_hash(password), - join_date = datetime.datetime.now() - ) - UserGroupMembership.create( - user = u, - group = UserGroup.get_default_group() - ) - - login_user(u) - return redirect(request.args.get('next', '/')) - except IntegrityError: - flash('Username taken') - return render_template('register.jinja2') - -@app.route('/accounts/logout/') -def accounts_logout(): - logout_user() - return redirect(request.args.get('next', '/')) - -## easter egg (lol) ## - -MNeaster = { - 15: (22, 2), 16: (22, 2), 17: (23, 3), 18: (23, 4), 19: (24, 5), 20: (24, 5), - 21: (24, 6), 22: (25, 0), 23: (26, 1), 24: (25, 1)} - -def calculate_easter(y): - a, b, c = y % 19, y % 4, y % 7 - M, N = (15, 6) if y < 1583 else MNeaster[y // 100] - d = (19 * a + M) % 30 - e = (2 * b + 4 * c + 6 * d + N) % 7 - if d + e < 10: - return datetime.date(y, 3, d + e + 22) - else: - day = d + e - 9 - if day == 26: - day = 19 - elif day == 25 and d == 28 and e == 6 and a > 10: - day = 18 - return datetime.date(y, 4, day) - -def stash_easter(y): - easter = calculate_easter(y) - natale = datetime.date(y, 12, 25) - avvento1 = natale - datetime.timedelta(days=22 + natale.weekday()) - return dict( - easter = easter, - ceneri = easter - datetime.timedelta(days=47), - ascensione = easter + datetime.timedelta(days=42), - pentecoste = easter + datetime.timedelta(days=49), - avvento1 = avvento1 - ) - -@app.route('/easter/') -@app.route('/easter//') -def easter_y(y=None): - if 'y' in request.args: - return redirect('/easter/' + request.args['y'] + '/') - if y: - if y > 2499: - flash('Years above 2500 A.D. are currently not supported.') - return render_template('easter.jinja2') - return render_template('easter.jinja2', y=y, easter_dates=stash_easter(y)) - else: - return render_template('easter.jinja2') - -## administration ## - -@app.route('/manage/') -def manage_main(): - return render_template('administration.jinja2') - -## import / export ## - -class Exporter(object): - def __init__(self): - self.root = {'pages': [], 'users': {}} - def add_page(self, p, include_history=True, include_users=False): - pobj = {} - pobj['title'] = p.title - pobj['url'] = p.url - pobj['tags'] = [tag.name for tag in p.tags] - pobj['calendar'] = p.calendar.isoformat("T") if p.calendar else None - pobj['flags'] = p.flags - if include_users: - pobj['owner'] = p.owner_id - hist = [] - for rev in (p.revisions if include_history else [p.latest]): - revobj = {} - revobj['text'] = rev.text - revobj['timestamp'] = rev.pub_date.timestamp() - if include_users: - revobj['user'] = rev.user_id - if rev.user_id not in self.root['users']: - self.root['users'][rev.user_id] = rev.user_info() - else: - revobj['user'] = None - revobj['comment'] = rev.comment - revobj['length'] = rev.length - hist.append(revobj) - pobj['history'] = hist - self.root['pages'].append(pobj) - def add_page_list(self, pl, include_history=True, include_users=False): - for p in pl: - self.add_page(p, include_history=include_history, include_users=include_users) - def export(self): - return json.dumps(self.root) - -class Importer(object): - def __init__(self, dump, *, overwrite_urls = True): - self.root = json.loads(dump) - self.owner = None - self.overwrite_urls = overwrite_urls - def claim(self, owner): - self.owner = owner - def execute(self): - no_pages = 0 - no_revs = 0 - for pobj in self.root['pages']: - purl = pobj.get("url") - try: - if purl: - try: - p2 = Page.get(Page.url == purl) - p2.url = None - p2.save() - except Page.DoesNotExist: - pass - - p = Page.create( - url = purl if self.overwrite_urls else None, - title = pobj['title'], - calendar = datetime.datetime.fromisoformat(pobj["calendar"]) if 'calendar' in pobj else None, - owner = self.owner.id, - flags = pobj.get('flags'), - touched = datetime.datetime.now() - ) - p.change_tags(pobj.get('tags')) - no_pages += 1 - - for revobj in pobj['history']: - textref = PageText.create_content( - revobj['text'] - ) - - rev = PageRevision.create( - page = p, - user_id = self.owner.id, - textref = textref, - comment = revobj.get('comment'), - pub_date = datetime.datetime.fromtimestamp(revobj['timestamp']), - length = revobj['length'] - ) - no_revs += 1 - except Exception as e: - sys.excepthook(*sys.exc_info()) - continue - return no_pages, no_revs - -@app.route('/manage/export/', methods=['GET', 'POST']) -def exportpages(): - if request.method == 'POST': - raw_list = request.form['export-list'] - q_list = [] - for item in raw_list.split('\n'): - item = item.strip() - if len(item) < 2: - continue - if item.startswith('+'): - q_list.append(Page.select().where(Page.id == item[1:])) - elif item.startswith('#'): - q_list.append(Page.select().join(PageTag, on=PageTag.page).where(PageTag.name == item[1:])) - elif item.startswith('/'): - q_list.append(Page.select().where(Page.url == item[1:].rstrip('/'))) - else: - q_list.append(Page.select().where(Page.title == item)) - if not q_list: - flash('Failed to export pages: The list is empty!') - return render_template('exportpages.jinja2') - query = q_list.pop(0) - while q_list: - query |= q_list.pop(0) - e = Exporter() - e.add_page_list(query, include_history='history' in request.form) - return e.export(), {'Content-Type': 'application/json', 'Content-Disposition': 'attachment; ' + - 'filename=export-{}.json'.format(datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))} - return render_template('exportpages.jinja2') - -@app.route('/manage/import/', methods=['GET', 'POST']) -@login_required -def importpages(): - if request.method == 'POST': - if current_user.is_admin: - f = request.files['import'] - overwrite_urls = request.form.get('ovwurls') - im = Importer(f.read(), overwrite_urls=overwrite_urls) - im.claim(current_user) - res = im.execute() - flash('Imported successfully {} pages and {} revisions'.format(*res)) - else: - flash('Pages can be imported by Administrators only!') - return render_template('importpages.jinja2') - -@app.route('/manage/accounts/', methods=['GET', 'POST']) -@login_required -def manage_accounts(): - users = User.select().order_by(User.join_date.desc()) - page = int(request.args.get('page', 1)) - if request.method == 'POST': - if current_user.is_admin: - action = request.form.get("action") - userids = [] - if action == "disable": - for key in request.form.keys(): - if key.startswith("u") and key[1:].isdigit(): - userids.append(int(key[1:])) - uu = 0 - for uid in userids: - try: - u = User[uid] - except User.DoesNotExist: - continue - u.is_disabled = not u.is_disabled - u.save() - uu += 1 - flash(f"Successfully disabled {uu} users!") - else: - flash("Unknown action") - else: - flash('Operation not permitted!') - return render_paginated_template('manageaccounts.jinja2', 'users', users=users) - -## terms / privacy ## - -@app.route('/terms/') -def terms(): - return render_template('terms.jinja2') - -@app.route('/privacy/') -def privacy(): - return render_template('privacy.jinja2') - -@app.route('/rules/') -def rules(): - return render_template('rules.jinja2') - -#### EXTENSIONS #### - -active_extensions = [] - -_i = 1 -_extension_name = _getconf('extensions', 'ext.{}'.format(_i)) -while _extension_name: - active_extensions.append(_extension_name) - _i += 1 - _extension_name = _getconf('extensions', 'ext.{}'.format(_i)) - -for ext in active_extensions: - try: - bp = importlib.import_module('extensions.' + ext).bp - app.register_blueprint(bp) - except Exception: - sys.stderr.write('Extension not loaded: ' + ext + '\n') - sys.excepthook(*sys.exc_info()) - diff --git a/app_init.py b/app_init.py deleted file mode 100644 index e872b6c..0000000 --- a/app_init.py +++ /dev/null @@ -1,5 +0,0 @@ -from app import init_db_and_create_first_user - - -if __name__ == '__main__': - init_db_and_create_first_user() \ No newline at end of file diff --git a/app_sync.py b/app_sync.py deleted file mode 100644 index 1c55434..0000000 --- a/app_sync.py +++ /dev/null @@ -1,175 +0,0 @@ -""" -Helper module for sync. - -Remember to set sync:master variable in site.conf! - -(c) 2021 Sakuragasaki46. -""" - -import datetime, time -import requests -import sys, os -from configparser import ConfigParser -from app import Page, PageRevision, PageText -from peewee import IntegrityError -from functools import lru_cache - -## CONSTANTS ## - -APP_BASE_DIR = os.path.dirname(__file__) - -UPLOAD_DIR = APP_BASE_DIR + '/media' -DATABASE_DIR = APP_BASE_DIR + "/database" - -#### GENERAL CONFIG #### - -DEFAULT_CONF = { - ('site', 'title'): 'Salvi', - ('config', 'media_dir'): APP_BASE_DIR + '/media', - ('config', 'database_dir'): APP_BASE_DIR + "/database", -} - -_cfp = ConfigParser() -if _cfp.read([APP_BASE_DIR + '/site.conf']): - @lru_cache(maxsize=50) - def _getconf(k1, k2, fallback=None): - if fallback is None: - fallback = DEFAULT_CONF.get((k1, k2)) - v = _cfp.get(k1, k2, fallback=fallback) - return v -else: - def _getconf(k1, k2, fallback=None): - if fallback is None: - fallback = DEFAULT_CONF.get((k1, k2)) - return fallback - -#### misc. helpers #### - -def _makelist(l): - if isinstance(l, (str, bytes, bytearray)): - return [l] - elif hasattr(l, '__iter__'): - return list(l) - elif l: - return [l] - else: - return [] - -#### REQUESTS #### - -def fetch_updated_ids(baseurl): - try: - with open(_getconf("config", "database_dir") + "/latest_sync") as f: - last_sync = float(f.read().rstrip("\n")) - except (OSError, ValueError): - last_sync = 946681200.0 # Jan 1, 2000 - r = requests.get(baseurl + "/_jsoninfo/changed/{ts}".format(ts=last_sync)) - if r.status_code >= 400: - raise RuntimeError("sync unavailable") - return r.json()["ids"] - -def update_contacts(baseurl): - from extensions.contactnova import Contact - try: - with open(_getconf("config", "database_dir") + "/latest_sync") as f: - last_sync = float(f.read().rstrip("\n")) - except (OSError, ValueError): - last_sync = 946681200.0 # Jan 1, 2000 - r = requests.get(baseurl + "/kt/_jsoninfo/{ts}".format(ts=last_sync)) - if r.status_code >= 400: - raise RuntimeError("sync unavailable") - # update contacts - updated = 0 - for pinfo in r.json()['data']: - p = Contact.get_or_none(Contact.code == pinfo['code']) - if p is None: - p = Contact.create( - code = pinfo['code'], - display_name = pinfo['display_name'], - issues = pinfo['issues'], - status = pinfo['status'], - description = pinfo['description'], - due = datetime.date.fromtimestamp(pinfo['due']) - ) - else: - p.display_name = pinfo['display_name'] - p.issues = pinfo['issues'] - p.status = pinfo['status'] - p.description = pinfo['description'] - p.due = datetime.date.fromtimestamp(pinfo['due']) - p.touched = datetime.datetime.now() - p.save() - updated += 1 - print('\x1b[32m{0} contacts updated :)\x1b[0m'.format(updated)) - - -def update_page(p, pageinfo): - p.touched = datetime.datetime.fromtimestamp(pageinfo["touched"]) - p.url = pageinfo["url"] - p.title = pageinfo["title"] - p.save() - p.change_tags(pageinfo["tags"]) - assert len(pageinfo["text"]) == pageinfo["latest"]["length"] - pr = PageRevision.create( - page=p, - user_id=0, - comment='', - textref=PageText.create_content(pageinfo['text']), - pub_date=datetime.datetime.fromtimestamp(pageinfo["latest"]["pub_date"]), - length=pageinfo["latest"]["length"] - ) - -#### MAIN #### - -def main(): - baseurl = _getconf("sync", "master", "this") - if baseurl == "this": - print("unsyncable: master", file=sys.stderr) - return - if not baseurl.startswith(("http:", "https:")): - print("unsyncable: invalid url", repr(baseurl), file=sys.stderr) - return - passed, failed = 0, 0 - for i in fetch_updated_ids(baseurl): - pageinfo_r = requests.post(baseurl + "/_jsoninfo/{i}".format(i=i)) - if pageinfo_r.status_code >= 400: - print("\x1b[31mSkipping {i}: HTTP {s}\x1b[0m".format(i=i, s=pageinfo_r.status_code)) - failed += 1 - continue - pageinfo = pageinfo_r.json() - try: - p = Page[i] - except Page.DoesNotExist: - try: - p = Page.create( - id=i, - url=pageinfo['url'], - title=pageinfo['title'], - is_redirect=pageinfo['is_redirect'], - touched=datetime.datetime.fromtimestamp(pageinfo["touched"]), - is_sync = True - ) - update_page(p, pageinfo) - except IntegrityError: - print("\x1b[31mSkipping {i}: Integrity error\x1b[0m".format(i=i)) - failed += 1 - continue - else: - if pageinfo["touched"] > p.touched.timestamp(): - update_page(p, pageinfo) - passed += 1 - try: - if _getconf("sync", "contacts", None) is not None: - update_contacts(baseurl) - except Exception as e: - print("\x1b[33mContacts not updated - {e} :(\x1b[0m".format(e=e)) - with open(DATABASE_DIR + "/last_sync", "w") as fw: - fw.write(str(time.time())) - if passed > 0 and failed == 0: - print("\x1b[32mSuccessfully updated {p} pages :)\x1b[0m".format(p=passed)) - else: - print("\x1b[33m{p} pages successfully updated, {f} errors.\x1b[0m".format(p=passed, f=failed)) - - -if __name__ == "__main__": - main() diff --git a/extensions/circles.py b/extensions/circles.py deleted file mode 100644 index b3b813e..0000000 --- a/extensions/circles.py +++ /dev/null @@ -1,241 +0,0 @@ -# (C) 2020-2021 Sakuragasaki46. -# See LICENSE for copying info. - -''' -Circles (people) extension for Salvi. -''' - -from peewee import * -import datetime -from app import _getconf -from flask import Blueprint, request, redirect, render_template -from werkzeug.routing import BaseConverter -import csv -import io -import itertools - -#### HELPERS #### - -def _getmbt(s): - s = s.upper() - try: - return 16 + ( - dict(I=0, E=8)[s[0]] | - dict(N=0, S=4)[s[1]] | - dict(T=0, F=2)[s[2]] | - dict(J=0, P=1)[s[3]] - ) - except Exception: - return 2 - - -def _putmbt(s): - if s & 16 == 0: - return "1x38B" - return ''.join(( - 'IE'[(s & 8) >> 3], - 'NS'[(s & 4) >> 2], - 'TF'[(s & 2) >> 1], - 'JP'[s & 1] - )) - - - -#### DATABASE SCHEMA #### - -database = SqliteDatabase(_getconf("config", "database_dir") + '/circles.sqlite') - -class BaseModel(Model): - class Meta: - database = database - -class MbTypeField(Field): - field_type = 'integer' - - def db_value(self, value): - if isinstance(value, int): - return value - return _getmbt(value) - def python_value(self, value): - return _putmbt(value) - -ST_ORANGE = 0 -ST_YELLOW = 1 -ST_GREEN = 2 -ST_RED = -1 - -class Person(BaseModel): - code = IntegerField(primary_key=True) - display_name = CharField(256) - first_name = CharField(128, null=True) - last_name = CharField(128, null=True) - circle = IntegerField(default=7, index=True) - status = IntegerField(default=ST_ORANGE, index=True) - type = MbTypeField(default=0, index=True) - area = IntegerField(default=0, index=True) - touched = DateTimeField(default=datetime.datetime.now) - class Meta: - indexes = ( - (('last_name', 'first_name'), False), - ) - -def init_db(): - database.create_tables([Person]) - -def add_from_csv(s): - f = io.StringIO() - f.write(s) - f.seek(0) - rd = csv.reader(f) - for line in rd: - code = line[0] - if not code.isdigit(): - continue - names = line[1:4] - while len(names) < 3: - names.append('') - if not names[2]: - names[2] = names[0] + " " + names[1] - type_ = line[4] if len(line) > 4 else 0 - try: - p = Person[code] - except Person.DoesNotExist: - p = Person.create( - code = code, - display_name = names[2], - first_name = names[0], - last_name = names[1], - type = type_ - ) - else: - p.touched = datetime.datetime.now() - p.first_name, p.last_name, p.display_name = names - p.type = type_ or p.type - p.save() - -#### ROUTING #### - -class MbTypeConverter(BaseConverter): - regex = '[IE][NS][TF][JP]|[ie][ns][tf][jp]' - def to_python(self, value): - return value.upper() - def to_url(self, value): - return value.lower() - -class StatusColorConverter(BaseConverter): - regex = 'red|yellow|green|orange' - def to_python(self, value): - if value == 'red': - return -1 - return ['orange', 'yellow', 'green'].index(value) - def to_url(self, value): - return ['orange', 'yellow', 'green', ..., 'red'][value] - -def _register_converters(state): - state.app.url_map.converters['mbtype'] = MbTypeConverter - state.app.url_map.converters['statuscolor'] = StatusColorConverter - -bp = Blueprint('circles', __name__, - url_prefix='/circles') -bp.record_once(_register_converters) - -@bp.route('/init-config') -def _init_config(): - init_db() - return redirect('/circles') - -@bp.route('/new', methods=['GET', 'POST']) -def add_new(): - if request.method == 'POST': - p = Person.create( - code = request.form["code"], - display_name = request.form["display_name"], - first_name = request.form["first_name"], - last_name = request.form["last_name"], - type = request.form["type"], - status = int(request.form.get('status', 0)) - ) - return redirect("/circles") - return render_template("circles/add.html") - -@bp.route('/edit/', methods=['GET', 'POST']) -def edit_detail(id): - p = Person[id] - if request.method == 'POST': - p.touched = datetime.datetime.now() - p.first_name = request.form['first_name'] - p.last_name = request.form['last_name'] - p.display_name = request.form['display_name'] - p.status = int(request.form.get('status', 0)) - p.type = request.form["type"] - p.area = request.form['area'] - p.save() - return redirect(request.form['returnto']) - return render_template("circles/add.html", pl=p, returnto=request.headers.get('Referer', '/circles')) - -@bp.route('/csv', methods=['GET', 'POST']) -def add_csv(): - if request.method == 'POST' and request.form.get('consent') == 'y': - add_from_csv(request.form['text']) - return redirect('/circles') - return render_template("circles/csv.html") - -# Helper for pagination. -def paginate_list(cat, q): - pageno = int(request.args.get('page', 1)) - return render_template( - "circles/list.html", - cat=cat, - count=q.count(), - people=q.offset(50 * (pageno - 1)).limit(50), - pageno=pageno - ) - -@bp.route('/') -def homepage(): - q = Person.select().order_by(Person.touched.desc()) - return paginate_list('all', q) - - -@bp.route('/') -def typelist(typ): - q = Person.select().where(Person.type == typ).order_by(Person.touched.desc()) - return paginate_list(typ, q) - -@bp.route('/') -def statuslist(typ): - q = Person.select().where(Person.status == typ).order_by(Person.touched.desc()) - return paginate_list(['Orange', 'Yellow', 'Green', ..., 'Red'][typ], q) - -@bp.route('/area-') -def arealist(a): - q = Person.select().where(Person.area == a).order_by(Person.status.desc(), Person.touched.desc()) - return paginate_list('Area {}'.format(a), q) - -@bp.route('/no-area') -def noarealist(): - q = Person.select().where(Person.area == 0).order_by(Person.touched.desc()) - return paginate_list('Unassigned area', q) - -@bp.route("/stats") -def stats(): - bq = Person.select() - return render_template( - "circles/stats.html", - count=bq.count(), - typed_count={ - ''.join(x) : bq.where(Person.type == ''.join(x)).count() - for x in itertools.product('IE', 'NS', 'TF', 'JP') - }, - status_count={ - 'Red': bq.where(Person.status == -1).count(), - 'Orange': bq.where(Person.status == 0).count(), - 'Yellow': bq.where(Person.status == 1).count(), - 'Green': bq.where(Person.status == 2).count() - }, - area_count={ - k: bq.where(Person.area == k).count() - for k in range(1, 13) - }, - no_area_count=bq.where(Person.area == None).count() - ) diff --git a/extensions/contactnova.py b/extensions/contactnova.py deleted file mode 100644 index 579a421..0000000 --- a/extensions/contactnova.py +++ /dev/null @@ -1,214 +0,0 @@ -# (c) 2021 Sakuragasaki46 -# See LICENSE for copying info. - -''' -Contact Nova extension for Salvi. -''' - -from peewee import * -import datetime -from app import _getconf -from flask import Blueprint, request, redirect, render_template, jsonify, abort -from werkzeug.routing import BaseConverter -import csv -import io -import re -import itertools - -#### HELPERS #### - -class RegExpField(CharField): - def __init__(self, regex, max_length=255, *args, **kw): - super().__init__(max_length, *args, **kw) - self.regex = regex - def db_value(self, value): - value = value.strip() - # XXX %: bug fix for LIKE, but it’s not something regular! - if not '%' in value and not re.fullmatch(self.regex, value): - raise IntegrityError("regexp mismatch: {!r}".format(self.regex)) - return value - -def now7days(): - return datetime.datetime.now() + datetime.timedelta(days=7) - -#### DATABASE SCHEMA #### - -database = SqliteDatabase(_getconf("config", "database_dir") + '/contactnova.sqlite') - -class BaseModel(Model): - class Meta: - database = database - -ST_UNKNOWN = 0 -ST_OK = 1 -ST_ISSUES = 2 - -class Contact(BaseModel): - code = RegExpField(r'[A-Z]\.\d+', 30, unique=True) - display_name = RegExpField('[A-Za-z0-9_. ]+', 50, index=True) - status = IntegerField(default=ST_UNKNOWN, index=True) - issues = CharField(500, default='') - description = CharField(5000, default='') - due = DateField(index=True, default=now7days) - touched = DateTimeField(index=True, default=datetime.datetime.now) - def status_str(self): - return { - 1: "task_alt", - 2: "error_outline", - }.get(self.status, "") - def __repr__(self): - return '<{0.__class__.__name__}: {0.code}>'.format(self) - @classmethod - def next_code(cls, letter): - try: - last_code = Contact.select().where(Contact.code ** (letter + '%')).order_by(Contact.id.desc()).first().code.split('.') - except (Contact.DoesNotExist, AttributeError): - last_code = letter, '0' - code = letter + '.' + str(int(last_code[1]) + 1) - return code - -def init_db(): - database.create_tables([Contact]) - -def create_cli(): - code = Contact.next_code(input('Code letter:')) - - return Contact.create( - code=code, - display_name=input('Display name:'), - due=datetime.datetime.fromisoformat(input('Due date (ISO):')), - description=input('Description (optional):') - ) - -# Helper for command line. -class _CCClass: - def __init__(self, cb=lambda x:x): - self.callback = cb - def __neg__(self): - return create_cli() - def __getattr__(self, value): - code = value[0] + '.' + value[1:] - try: - return self.callback(Contact.get(Contact.code == code)) - except Contact.DoesNotExist: - raise AttributeError(value) from None - def ok(self): - def cb(a): - a.status = 1 - a.save() - return a - return self.__class__(cb) -CC = _CCClass() -del _CCClass - -#### ROUTE HELPERS #### - -class ContactCodeConverter(BaseConverter): - regex = r'[A-Z]\.\d+' - -class ContactMockConverter(BaseConverter): - regex = r'[A-Z]' - -def _register_converters(state): - state.app.url_map.converters['contactcode'] = ContactCodeConverter - state.app.url_map.converters['singleletter'] = ContactMockConverter - -# Helper for pagination. -def paginate_list(cat, q): - pageno = int(request.args.get('page', 1)) - return render_template( - "contactnova/list.html", - cat=cat, - count=q.count(), - people=q.offset(50 * (pageno - 1)).limit(50), - pageno=pageno - ) - -bp = Blueprint('contactnova', __name__, - url_prefix='/kt') -bp.record_once(_register_converters) - -@bp.route('/init-config') -def _init_config(): - init_db() - return redirect('/kt') - -@bp.route('/') -def homepage(): - q = Contact.select().where(Contact.due > datetime.datetime.now()).order_by(Contact.due.desc()) - return paginate_list('All', q) - -@bp.route('/expired') -def expired(): - q = Contact.select().where(Contact.due <= datetime.datetime.now()).order_by(Contact.due) - return paginate_list('Expired', q) - -@bp.route('/ok') -def sanecontacts(): - q = Contact.select().where((Contact.due > datetime.datetime.now()) & - (Contact.status == ST_OK)).order_by(Contact.due) - return paginate_list('Sane', q) - -@bp.route('/') -def singleletter(l): - q = Contact.select().where(Contact.code ** (l + '%')).order_by(Contact.id) - return paginate_list('Series {}'.format(l), q) - -@bp.route('/') -def singlecontact(code): - try: - p = Contact.get(Contact.code == code) - except IntegrityError: - abort(404) - return render_template('contactnova/single.html', p=p) - -@bp.route('/_newcode/') -def newcode(l): - return Contact.next_code(l), {"Content-Type": "text/plain"} - -@bp.route('/new', methods=['GET', 'POST']) -def newcontact(): - if request.method == 'POST': - Contact.create( - code = Contact.next_code(request.form['letter']), - display_name = request.form['display_name'], - status = request.form['status'], - issues = request.form['issues'], - description = request.form['description'], - due = datetime.date.fromisoformat(request.form['due']) - ) - return redirect(request.form.get('returnto','/kt/'+request.form['letter'])) - return render_template('contactnova/new.html', pl_date = now7days()) - -@bp.route('/edit/', methods=['GET', 'POST']) -def editcontact(code): - pl = Contact.get(Contact.code == code) - if request.method == 'POST': - pl.display_name = request.form['display_name'] - pl.issues = request.form['issues'] - pl.status = request.form['status'] - pl.description = request.form['description'] - pl.due = datetime.date.fromisoformat(request.form['due']) - pl.touched = datetime.datetime.now() - pl.save() - return redirect(request.form.get('returnto','/kt/'+pl.code[0])) - return render_template('contactnova/new.html', pl = pl) - -@bp.route('/_jsoninfo/') -def contact_jsoninfo(ts): - tse = str(datetime.datetime.fromtimestamp(ts).isoformat(" ")) - ps = Contact.select().where(Contact.touched >= tse) - return jsonify({ - "ids": [i.code for i in ps], - "data": [ - { - 'code': i.code, - 'display_name': i.display_name, - 'due': datetime.datetime.fromisoformat(i.due.isoformat()).timestamp(), - 'issues': i.issues, - 'description': i.description, - 'status': i.status - } for i in ps - ], - "status": "ok" - }) diff --git a/i18n/salvi.en.json b/i18n/salvi.en.json index 91bb088..2376fe5 100644 --- a/i18n/salvi.en.json +++ b/i18n/salvi.en.json @@ -1,69 +1,75 @@ { "en": { - "welcome": "Welcome to {0}!", - "homepage": "Homepage", - "latest-notes": "Latest notes", - "latest-uploads": "Latest uploads", - "new-note": "New note", - "upload-file": "Upload file", - "easter-date-calc": "Easter date calculation", - "easter": "Easter", - "other-dates": "Other dates", - "jump-to-actions": "Jump to actions", - "last-changed": "Last changed", - "page-id": "Page ID", + "access-denied": "Access Denied", + "access-denied-text": "You have no permission to access this resource", "action-edit": "Edit", + "action-history": "History", "action-view-source": "View source", - "action-history": "History", - "tags": "Tags", - "old-revision-notice": "Showing an old revision of the page as of", - "notes-tagged": "Notes tagged", - "include-tags": "Include tags", - "notes-tagged-empty": "None found :(", - "search-no-results": "No results for", - "random-page": "Random page", - "search": "Search", - "year": "Year", - "month": "Month", - "calculate": "Calculate", - "show-all": "Show all", - "just-now": "just now", - "n-minutes-ago": "{0} minutes ago", - "n-hours-ago": "{0} hours ago", - "n-days-ago": "{0} days ago", + "already-have-account": "Already have an account?", + "back-to": "Back to", "backlinks": "Backlinks", "backlinks-empty": "No other pages linking here. Is this page orphan?", - "back-to": "Back to", + "calculate": "Calculate", + "calendar": "Calendar", + "confirm-password": "Confirm password", + "easter": "Easter", + "easter-date-calc": "Easter date calculation", + "email": "E-mail", + "groups-count": "User group count", + "have-read-terms": "I have read {0} and {1}", + "homepage": "Homepage", + "include-tags": "Include tags", + "input-tags": "Tags (comma separated)", + "jump-to-actions": "Jump to actions", + "just-now": "just now", + "last-changed": "Last changed", + "latest-notes": "Most recent pages", + "latest-uploads": "Latest uploads", + "logged-in-as": "Logged in as", "login": "Log in", - "username": "Username", - "password": "Password", + "month": "Month", + "n-days-ago": "{0} days ago", + "n-hours-ago": "{0} hours ago", + "n-minutes-ago": "{0} minutes ago", + "new-note": "New page", "no-account-sign-up": "Don’t have an account?", - "sign-up": "Sign up", + "no-tags": "No tags", "not-found": "Not found", + "not-found-text": "The page at {0} does not exist", "not-found-text-1": "The page at", "not-found-text-2": "does not exist.", - "users-count": "Number of users", - "notes-count": "Number of notes", - "notes-count-with-url": "Number of pages with URL set", - "revision-count": "Number of revisions", - "revision-count-per-page": "Average revisions per page", - "remember-me-for": "Remember me for", - "confirm-password": "Confirm password", - "email": "E-mail", - "optional": "optional", - "have-read-terms": "I have read {0} and {1}", - "terms-of-service": "Terms of Service", - "privacy-policy": "Privacy Policy", - "already-have-account": "Already have an account?", - "logged-in-as": "Logged in as", "not-logged-in": "Not logged in", + "note-history": "Page history", + "notes-count": "Number of pages", + "notes-count-with-url": "Number of pages with URL set", + "notes-month-empty": "None found :(", + "notes-tagged": "Pages tagged", + "notes-tagged-empty": "None found :(", + "old-revision-notice": "Showing an old revision of the page as of", + "optional": "optional", + "other-dates": "Other dates", "owner": "Owner", "page-created": "Page created", + "page-id": "Page ID", + "password": "Password", + "privacy-policy": "Privacy Policy", + "random-page": "Random page", + "remember-me": "Remember me", + "remember-me-for": "Remember me for", + "revision-count": "Number of revisions", + "revision-count-per-page": "Average revisions per page", + "search": "Search", + "search-results": "Search results for", + "search-no-results": "No results for", + "show-all": "Show all", + "sign-up": "Sign up", + "tags": "Tags", + "terms-of-service": "Terms of Service", + "upload-file": "Upload file", + "username": "Username", + "users-count": "Number of users", + "welcome": "Welcome to {0}!", "write-a-comment": "Write a comment…", - "input-tags": "Tags (comma separated)", - "no-tags": "No tags", - "notes-month-empty": "None found :(", - "calendar": "Calendar", - "groups-count": "User group count" + "year": "Year" } } \ No newline at end of file diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index 2dd878a..dd4d4ca 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -2,7 +2,7 @@ "it": { "welcome": "Benvenuti in {0}!", "homepage": "Pagina iniziale", - "latest-notes": "Note pi\u00f9 recenti", + "latest-notes": "Pagine pi\u00f9 recenti", "latest-uploads": "Caricamenti pi\u00f9 recenti", "new-note": "Crea nota", "upload-file": "Carica immagine", @@ -17,7 +17,7 @@ "action-history": "Cronologia", "tags": "Etichette", "old-revision-notice": "\u00c8 mostrata una revisione vecchia della pagina, risalente al", - "notes-tagged": "Note con etichetta", + "notes-tagged": "Pagine con etichetta", "include-tags": "Includi etichette", "notes-tagged-empty": "Non c\u2019\u00e8 nulla :(", "search-no-results": "Nessun risultato per", @@ -42,9 +42,12 @@ "not-found": "Non trovato", "not-found-text-1": "La pagina con url", "not-found-text-2": "non esiste", + "not-found-text": "La pagina con url {0} non esiste", + "access-denied": "Accesso negato", + "access-denied-text": "Non hai il permesso per accedere a questa risorsa", "users-count": "Numero di utenti", - "notes-count": "Numero di note", - "notes-count-with-url": "Numero di note con URL impostato", + "notes-count": "Numero di pagine", + "notes-count-with-url": "Numero di pagine con URL impostato", "revision-count": "Numero di revisioni", "revision-count-per-page": "Media di revisioni per pagina", "remember-me-for": "Ricordami per", diff --git a/migrations/0_6to0_7.py b/migrations/0_6to0_7.py deleted file mode 100644 index 5d11341..0000000 --- a/migrations/0_6to0_7.py +++ /dev/null @@ -1,20 +0,0 @@ -from playhouse.migrate import migrate, SqliteMigrator, MySQLMigrator -from peewee import MySQLDatabase, SqliteDatabase, \ - IntegerField, DateTimeField, ForeignKeyField, DeferredForeignKey -from app import database, User - -if type(database) == MySQLDatabase: - migrator = MySQLMigrator(database) -elif type(database) == SqliteDatabase: - migrator = SqliteMigrator(database) -else: - print("Unsupported database") - exit() - -with database.atomic(): - database.create_tables([User]) - migrate( - migrator.add_column('page', 'calendar', DateTimeField(index=True, null=True)), - migrator.add_column('page', 'owner', DeferredForeignKey('User', null=True)) - ) - diff --git a/migrations/0_7to0_8.py b/migrations/0_7to0_8.py deleted file mode 100644 index 92a56cb..0000000 --- a/migrations/0_7to0_8.py +++ /dev/null @@ -1,23 +0,0 @@ -from playhouse.migrate import migrate, SqliteMigrator, MySQLMigrator -from peewee import MySQLDatabase, SqliteDatabase, \ - IntegerField, DateTimeField, ForeignKeyField, DeferredForeignKey, BitField -from app import database, UserGroup, UserGroupMembership, PagePermission - - -if type(database) == MySQLDatabase: - migrator = MySQLMigrator(database) -elif type(database) == SqliteDatabase: - migrator = SqliteMigrator(database) -else: - print("Unsupported database") - exit() - -with database.atomic(): - database.create_tables([UserGroup, UserGroupMembership, PagePermission]) - migrate( - migrator.add_column('user', 'restrictions', BitField()) - ) - UserGroup.create( - name='default', - permissions=31 - ) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..6f11f74 --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,31 @@ +[project] +name = "sakuragasaki46_salvi" +authors = [ + { name = "Sakuragasaki46" } +] +dynamic = ["version"] +dependencies = [ + "Flask>=3.0", + "SQLAlchemy>=2.0", + "Flask-SQLAlchemy", + "Flask-RestX", + "Alembic", + "markdown", + "Flask-Login", + "Flask-WTF", + "python-dotenv>=1.0.0", + "pymysql", + "sakuragasaki46-suou>=0.5.0" +] +requires-python = ">=3.10" +classifiers = [ + "Private :: X" +] + + +[tool.setuptools] +packages = ["salvi"] + +[tool.setuptools.dynamic] +version = { attr = "salvi.__version__" } + diff --git a/requirements.txt b/requirements.txt index b498231..9fe0aec 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,8 +1,12 @@ -flask -peewee +Flask>=3.0 +SQLAlchemy>=2.0 +Flask-SQLAlchemy +Alembic markdown -flask-login -flask-wtf -python-i18n -flask-arrest +Flask-Login +Flask-WTF python-dotenv +python-i18n +pymysql + +## DEPRECATED use pyproject.toml instead \ No newline at end of file diff --git a/robots.txt b/robots.txt index d8b9aa9..14deb7c 100644 --- a/robots.txt +++ b/robots.txt @@ -2,3 +2,5 @@ User-Agent: * Noindex: /edit/ Noindex: /history/ Disallow: /accounts/ +Noindex: /admin/ + diff --git a/salvi/__init__.py b/salvi/__init__.py new file mode 100644 index 0000000..54d50d4 --- /dev/null +++ b/salvi/__init__.py @@ -0,0 +1,191 @@ +# (C) 2020-2023 Sakuragasaki46. +# See LICENSE for copying info. + +''' +A simple wiki-like note webapp. + +Pages are stored in SQLite/MySQL databases. +Markdown is used for text formatting. +''' + +__version__ = '1.0.0-dev35' + +from flask import ( + Flask, abort, flash, g, jsonify, make_response, redirect, + request, render_template, send_from_directory +) +from markupsafe import Markup +from flask_login import LoginManager, login_user, logout_user, current_user, login_required +from flask_wtf import CSRFProtect +from flask_sqlalchemy import SQLAlchemy +from sqlalchemy import select +from suou.configparse import ConfigOptions, ConfigParserConfigSource, ConfigValue +from suou.flask import add_context_from_config +from werkzeug.security import generate_password_hash, check_password_hash +from werkzeug.routing import BaseConverter +import datetime, hashlib, html, importlib, json, markdown, os, random, \ + re, sys, warnings +from functools import lru_cache, partial +from urllib.parse import quote +import gzip +from getpass import getpass +from configparser import ConfigParser +import dotenv + +APP_BASE_DIR = os.path.dirname(os.path.dirname(__file__)) + +dotenv.load_dotenv(os.path.join(APP_BASE_DIR, '.env')) + +## CONFIG ## + +class SiteConfig(ConfigOptions): + app_name = ConfigValue(default='Salvi', legacy_src='site.title', public=True) + domain_name = ConfigValue(public=True) + database_url = ConfigValue(legacy_src='database.url', required=True) + secret_key = ConfigValue(required=True) + default_group = ConfigValue(prefix='salvi_', cast=int, default=1) + material_icons_url = ConfigValue(default='https://fonts.googleapis.com/icon?family=Material+Icons', public=True) + default_items_per_page = ConfigValue(prefix='salvi_', legacy_src='appearance.items_per_page', default=20, cast=int) + ... + +app_config = SiteConfig() +# add site.conf (deprecated) +legacy_cfp = ConfigParser() +legacy_conf_file = os.getenv('SALVI_CONF', os.path.join(APP_BASE_DIR, 'site.conf')) +if os.path.exists(legacy_conf_file): + legacy_cfp.read_file([legacy_conf_file]) +app_config.add_source('legacy', ConfigParserConfigSource(legacy_cfp)) + +## end CONFIG + +## DON'T ADD LOCAL IMPORTS ABOVE THIS LINE! +from .i18n import get_string +from .constants import SLUG_RE +from .colors import theme_classes, theme_style_tag +from .models import User, db +from .renderer import md_and_toc + +app = Flask(__name__) +app.secret_key = app_config.secret_key +app.config['SQLALCHEMY_DATABASE_URI'] = app_config.database_url + +db.init_app(app) + +## TODO add an installer? And do it online or offline? +## The installer must contain the creation of the admin account +## and the default group. + +class SlugConverter(BaseConverter): + regex = SLUG_RE + +app.url_map.converters['slug'] = SlugConverter + +csrf = CSRFProtect(app) + +login_manager = LoginManager(app) +login_manager.login_view = 'accounts.login' + +### ROUTES ### + +def _get_lang(): + if request.args.get('uselang') is not None: + lang = request.args['uselang'] + else: + for l in request.headers.get('accept-language', 'it,en').split(','): + if ';' in l: + l, _ = l.split(';') + lang = l + break + else: + lang = 'en' + return lang + +def _get_theme(): + c = request.cookies.get('dark', '0') + try: + return int(c) + except Exception: + return 0 + +@app.before_request +def _before_request(): + g.lang = _get_lang() + +add_context_from_config(app, app_config) + +@app.context_processor +def _inject_variables(): + return { + 'T': partial(get_string, _get_lang()), + 'strong': lambda x: Markup('{0}').format(x), + 'app_name': app_config.app_name, + 'domain_name': app_config.domain_name, + 'material_icons_url': app_config.material_icons_url, + 'app_version': __version__, + 'min': min, + 'max': max, + 'icon': lambda x: Markup('{0}').format(x), + 'theme_style_tag': theme_style_tag, + 'theme_classes': theme_classes, + 'color_theme': _get_theme() # TEMP + } + +@login_manager.user_loader +def _inject_user(userid): + u: User | None = db.session.execute(select(User).where(User.id == int(userid))).scalar() + if u is None: + return None + if not u.is_disabled: + return u + +## filters here + +@app.template_filter() +def linebreaks(text): + text = html.escape(text) + text = text.replace("\n\n", '

    ').replace('\n', '
    ') + return Markup(text) + +app.template_filter(name='markdown')((lambda x: md_and_toc(x)[0])) + +@app.route('/robots.txt') +def robots(): + return send_from_directory(APP_BASE_DIR, 'robots.txt') + +@app.route('/favicon.ico') +def favicon(): + return send_from_directory(APP_BASE_DIR, 'favicon.ico') + +@app.errorhandler(400) +def error_400(body): + return render_template('400.html'), 400 + +@app.errorhandler(403) +def error_403(body): + return render_template('403.html'), 403 + +@app.errorhandler(404) +def error_404(body): + return render_template('404.html'), 404 + +@app.errorhandler(500) +def error_500(body): + g.no_user = True + return render_template('500.html'), 500 + +@app.route('/theme-switch') +def theme_switch(): + try: + cook = int(request.cookies.get('dark')) + except Exception: + cook = 0 + resp = make_response(redirect(request.args.get('next', '/'))) + resp.set_cookie('dark', str(cook + (256 if cook < 256 else -256)), max_age=31556952, path='/') + return resp + +from .routes import blueprints + +for bp in blueprints: + app.register_blueprint(bp) + + diff --git a/salvi/colors.py b/salvi/colors.py new file mode 100644 index 0000000..9d2a6e3 --- /dev/null +++ b/salvi/colors.py @@ -0,0 +1,77 @@ +from collections import namedtuple +from types import ModuleType +from typing import Iterable + +from markupsafe import Markup + +ColorTheme = namedtuple('ColorTheme', 'code name fg_sharp fg_medium fg_dark fg_light') + +## actual color codes are set in CSS + +color_themes = [ + ColorTheme(0, 'Default', 0xff7300, 0, 0, 0), + ColorTheme(1, 'Rei', 0xff7300, 0, 0, 0), + ColorTheme(2, 'Ai', 0xf837cb, 0, 0, 0), + ColorTheme(3, 'Aqua', 0x38b8ff, 0, 0, 0), + ColorTheme(4, 'Neru', 0xffe338, 0, 0, 0), + ColorTheme(5, 'Gumi', 0x78f038, 0, 0, 0), + ColorTheme(6, 'Emu', 0xff9aae, 0, 0, 0), + ColorTheme(7, 'Spacegray', 0x606080, 0, 0, 0), + ColorTheme(8, 'Haku', 0xaeaac0, 0, 0, 0), + ColorTheme(9, 'Miku', 0x3ae0b8, 0, 0, 0), + ColorTheme(10, 'Defoko', 0x8828ea, 0, 0, 0), + ColorTheme(11, 'Kaito', 0x1871d8, 0, 0, 0), + ColorTheme(12, 'Meiko', 0x885a18, 0, 0, 0), + ColorTheme(13, 'Leek', 0x38a856, 0, 0, 0), + ColorTheme(14, 'Teto', 0xff3018, 0, 0, 0), + ColorTheme(15, 'Ruby', 0xff1668, 0, 0, 0) +] + +def split_color(color: int) -> tuple[int, int, int]: + return ((color >> 16) % 256, (color >> 8) % 256, color % 256) + + +def join_color(it: tuple[int, int, int]) -> int: + return (it[0] << 16) | (it[1] << 8) | it[2] + +def make_color_theme(it: ColorTheme) -> ColorTheme: + fs = split_color(it.fg_sharp) + f_dark = it.fg_dark or join_color((int(fs[0] * .75), int(fs[1] * .75), int(fs[2] * .75))) + f_light = it.fg_light or join_color(( + 255 - int((255 - fs[0]) * .75), + 255 - int((255 - fs[1]) * .75), + 255 - int((255 - fs[2]) * .75) + )) + f_medium = it.fg_medium or join_color([(i + j) // 2 for i, j in zip(split_color(f_light), split_color(f_dark))]) + return ColorTheme(it.code, it.name, it.fg_sharp, f_medium, f_dark, f_light) + +color_themes = [make_color_theme(x) for x in color_themes] + +def theme_classes(color_code: int): + cl = [] + sch, th = divmod(color_code, 256) + if sch == 1: + cl.append('color-scheme-light') + if sch == 2: + cl.append('color-scheme-dark') + if 1 <= th <= 15: + cl.append(f'color-theme-{th}') + + return ' '.join(cl) + +def t_rgba(color: int, opacity: float = 1): + return f'rgba({(color >> 16) % 256}, {(color >> 8) % 256}, {color % 256}, {opacity})' + +def t_hex(color: int): + return f'#{color:06x}' + +def theme_style_tag(*, defcode = 9): + rules1 = [] + rules2 = [] + for it in color_themes: + rules1.append(f'--c{it.code}-accent-sharp: {t_hex(it.fg_sharp)}; --c{it.code}-accent-medium: {t_hex(it.fg_medium)}; --c{it.code}-accent-dark: {t_hex(it.fg_dark)}; --c{it.code}-accent-light: {t_hex(it.fg_light)}; --c{it.code}-accent-bg: {t_rgba(it.fg_sharp, .25)};') + rules2.append(f'.color-theme-{it.code} {{ --accent-sharp: var(--c{it.code}-accent-sharp); --accent-medium: var(--c{it.code}-accent-medium); --accent-dark: var(--c{it.code}-accent-dark); --accent-light: var(--c{it.code}-accent-light); --accent-bg: var(--c{it.code}-accent-bg); }}') + return Markup('') + diff --git a/salvi/constants.py b/salvi/constants.py new file mode 100644 index 0000000..a5b425b --- /dev/null +++ b/salvi/constants.py @@ -0,0 +1,15 @@ +# (C) 2020-2023 Sakuragasaki46. +# See LICENSE for copying info. + +SLUG_RE = r'[a-z0-9]+(?:-[a-z0-9]+)*' +ILINK_RE = r'\]\(/(p/\d+|' + SLUG_RE + ')/?\)' +USERNAME_RE = r'[a-z0-9_-]{3,30}' +PING_RE = r'(? str: + return i18n.lang(loc).t(s) + +def human_elapsed_time(then: datetime.datetime, lang = None) -> str: + if lang is None: ## XXX make it explicit? + try: + lang = g.lang + except AttributeError: + lang = None ## TODO replace with default lang (?) + delta = datetime.datetime.now() - then + T = partial(get_string, g.lang) + if delta < datetime.timedelta(seconds=60): + return T('just-now') + elif delta < datetime.timedelta(seconds=3600): + return T('n-minutes-ago').format(delta.seconds // 60) + + elif delta < datetime.timedelta(days=1): + return T('n-hours-ago').format(delta.seconds // 3600) + elif delta < datetime.timedelta(days=15): + return T('n-days-ago').format(delta.days) + else: + return then.strftime('%B %-d, %Y') + diff --git a/salvi/models.py b/salvi/models.py new file mode 100644 index 0000000..67d51aa --- /dev/null +++ b/salvi/models.py @@ -0,0 +1,657 @@ +# (C) 2020-2023 Sakuragasaki46. +# See LICENSE for copying info. + + +from __future__ import annotations + +from functools import partial, wraps +from getpass import getpass +import os +import re +import sys +from typing import Any, Callable, Iterable, List, Mapping +import warnings + +# sqlalchemy imports +from flask import abort, flash +from flask_login import AnonymousUserMixin, current_user, login_required +from flask_sqlalchemy import SQLAlchemy +from markupsafe import Markup +from sqlalchemy.orm import Relationship, declarative_base, deferred, relationship +from sqlalchemy import Column, Integer, String, DateTime, BigInteger, ForeignKey, UniqueConstraint, create_engine, Index, BLOB as Blob, delete, func, insert, or_, select, update +import datetime +import os +import gzip + +from suou.functools import deprecated, deprecated_alias, not_implemented +from werkzeug.security import generate_password_hash + +from . import app_config +from .constants import FORBIDDEN_URLS, ILINK_RE, SLUG_RE, USERNAME_RE +from .renderer import md_and_toc, remove_tags +from .i18n import human_elapsed_time + +current_user: User + +## TODO consider moving it to suou. +## TODO consider sqlalchemy.ext.hybrid.hybrid_property works +from sqlalchemy.ext.hybrid import Comparator + +class _BitComparator(Comparator): + _column: Column + _flag: int + def __init__(self, col, flag): + self._column = col + self._flag = flag + def _bulk_update_tuples(self, value): + return [ (self._column, self._upd_exp(value)) ] + def operate(self, op, other, **kwargs): + return op(self._sel_exp(), self._flag if other else 0, **kwargs) + def __clause_element__(self): + return self._column + def __str__(self): + return self._column + def _sel_exp(self): + return self._column.op('&')(self._flag) + def _upd_exp(self, value): + return self._column.op('|')(self._flag) if value else self._column.op('&')(~self._flag) + +class BitSelector: + _column: Column + _flag: int + _name: str + def __init__(self, column, flag: int): + if bin(flag := int(flag))[2:].rstrip('0') != '1': + warnings.warn('using non-powers of 2 as flags may cause errors or undefined behavior', FutureWarning) + self._column = column + self._flag = flag + def __set_name__(self, name, owner=None): + self._name = name + def __get__(self, obj, objtype=None): + if obj: + return getattr(obj, self._column.name) & self._flag > 0 + else: + return _BitComparator(self._column, self._flag) + def __set__(self, obj, val): + if obj: + orig = getattr(obj, self._column.name) + if val: + orig |= self._flag + else: + orig &= ~(self._flag) + setattr(obj, self._column.name, orig) + else: + raise NotImplementedError + + +# Helper for interactive session management +from suou.sqlalchemy import create_session, declarative_base + +CSI = create_session_interactively = partial(create_session, app_config.database_url) + +Base = declarative_base(app_config.domain_name, app_config.secret_key) + +db = SQLAlchemy(model_class=Base) + +class User(db.Model): + __tablename__ = 'user' + + id = Column(Integer, primary_key=True) + username = Column(String(32), unique=True) + email = Column(String(256), nullable=True) + password = Column(String(255)) + # will change to server_default in 1.1 + join_date = Column(DateTime, default=datetime.datetime.now) + karma = Column(Integer, default=1) + privileges = Column(BigInteger, default=0) + is_admin = BitSelector(privileges, 1) + restrictions = Column(BigInteger, default=0) + is_disabled = BitSelector(restrictions, 1) + + owned_pages: Relationship[List[Page]] = relationship('Page', back_populates='owner') + group_memberships: Relationship[UserGroupMembership] = relationship('UserGroupMembership', back_populates='user') + contributions = relationship("PageRevision", back_populates='user') + + # helpers for flask_login + @property + def is_anonymous(self) -> False: + return False + @property + def is_active(self) -> bool: + return not self.is_disabled + @property + def is_authenticated(self) -> True: + return True + + def groups(self) -> Iterable[UserGroup]: + return db.session.execute( + select(UserGroup).join(UserGroupMembership, UserGroupMembership.group_id == UserGroup.id) + .where(UserGroupMembership.user_id == self.id) + ).scalars() + + def get_perms(self) -> int: + if self.is_admin: + return PERM_ALL + + perm = 0 + + for gr in self.groups(): + perm |= gr.permissions + + return perm + + def has_perms_on_page(self, flags: int, page: Page | None = None): + if page: + perm = page.get_perms(user) + else: + perm = self.get_perms() + + if perm & flags: + return True + return False + + has_perms = has_perms_on_page + + def owns(self, other: Page) -> bool: + return other.owner == self + + def get_id(self) -> str: + return str(self.id) + +# page perms (used as bitmasks) +PERM_READ = 1 +PERM_EDIT = 2 +PERM_CREATE = 4 +PERM_SET_URL = 8 +PERM_SET_TAGS = 16 +PERM_ALL = 31 +PERM_LOCK = PERM_READ + +class UserGroup(db.Model): + __tablename__ = 'usergroup' + + id = Column(Integer, primary_key=True) + name = Column(String(32), unique=True) + permissions = Column(BigInteger, default=0) + can_read = BitSelector(permissions, PERM_READ) + can_edit = BitSelector(permissions, PERM_EDIT) + can_create = BitSelector(permissions, PERM_CREATE) + can_set_url = BitSelector(permissions, PERM_SET_URL) + can_set_tags = BitSelector(permissions, PERM_SET_TAGS) + + user_memberships: Relationship[UserGroupMembership] = relationship('UserGroupMembership', back_populates='group') + page_permissions: Relationship[PagePermission] = relationship('PagePermission', back_populates = 'group') + + @classmethod + def get_default(cls) -> UserGroup: + return db.session.execute( + select(cls).where(or_( + cls.id == app_config.default_group, + cls.name == 'default' + )) + ).scalar() + + @classmethod + @deprecated('shortened to get_default()') + def get_default_group(cls): + return cls.get_default() + +class UserGroupMembership(db.Model): + __tablename__ = 'usergroupmembership' + __table_args__ = ( + UniqueConstraint('user_id', 'group_id'), + ) + + id = Column(Integer, primary_key = True) + user_id = Column(Integer, ForeignKey('user.id')) + group_id = Column(Integer, ForeignKey('usergroup.id')) + since = Column(DateTime, default=datetime.datetime.now) + + user: Relationship[User] = relationship("User", back_populates='group_memberships') + group: Relationship[UserGroup] = relationship("UserGroup", back_populates='user_memberships') + + +class Page(db.Model): + __tablename__ = 'page' + + id = Column(Integer, primary_key=True) + url = Column(String(64), unique=True, nullable=True) + title = Column(String(256), index=True) + touched = Column(DateTime, index=True) + calendar = Column(DateTime, index=True, nullable=True) + owner_id = Column(Integer, ForeignKey('user.id'), nullable=True) + flags = Column(BigInteger, default=0) + is_redirect = BitSelector(flags, 1) + is_sync = BitSelector(flags, 2) + is_math_enabled = BitSelector(flags, 4) + is_locked = BitSelector(flags, 8) + is_cw = BitSelector(flags, 16) + + revisions: Relationship[List[PageRevision]] = relationship("PageRevision", back_populates = 'page') + owner: Relationship[User] = relationship('User', back_populates = 'owned_pages') + tags: Relationship[List[PageTag]] = relationship('PageTag', back_populates = 'page') + properties: Relationship[List[PageProperty]] = relationship('PageProperty', back_populates = 'page') + forward_links: Relationship[List[PageLink]] = relationship('PageLink', primaryjoin=lambda: PageLink.from_page_id == Page.id, back_populates = 'from_page') + back_links: Relationship[List[PageLink]] = relationship('PageLink', primaryjoin=lambda: PageLink.to_page_id == Page.id, back_populates = 'to_page') + permission_overrides: Relationship[List[PagePermission]] = relationship('PagePermission', back_populates = 'page') + + def latest(self) -> PageRevision: + return db.session.execute( + db.select(PageRevision).where(PageRevision.page_id == self.id).order_by(PageRevision.pub_date.desc()).limit(1) + ).scalar() + + def get_url(self): + return f'/{self.url}/' if self.url else f'/p/{self.id}/' + + @classmethod + def by_url(cls, url: str): + return db.session.execute(db.select(Page).where(Page.url == url)).scalar() + + def short_desc(self) -> str: + if self.is_cw: + return '(Content Warning: we are not allowed to show a description.)' + full_text = self.latest().text() + text = remove_tags(full_text, convert=False) + return text[:200] + ('\u2026' if len(text) > 200 else '') + + def get_perms(self, user: User | AnonymousUserMixin) -> int: + if user.is_anonymous: + return UserGroup.get_default().permissions & PERM_LOCK + if user.is_admin: + return PERM_ALL + perm = 0 + + user_groups = user.groups() + for gr in user_groups: + perm |= gr.permissions + + for ov in self.permission_overrides: + ov: PagePermission + if ov.group in user_groups: + perm |= ov.permissions + + if self.is_locked and self.owner_id != user.id: + perm &= PERM_LOCK + return perm + + def tag_names(self) -> list[str]: + return [x.name for x in self.tags] + + def change_tags(self, taglist: Iterable[str]): + old_tags = set(self.tag_names()) + new_tags = set(taglist) + db.session.execute(delete(PageTag).where(PageTag.page == self, PageTag.name.in_(list(old_tags - new_tags)))) + for t in (new_tags - old_tags): + db.session.execute(insert(PageTag).values(page=self, name=t)) + # commit handled by caller! + + def js_info(self): + latest = self.latest() + return dict( + id = self.id, + url = self.url, + title = self.title, + is_redirect = self.is_redirect, + touched = self.touched.isoformat('T'), + is_editable = self.is_editable_by(current_user), + latest = latest.js_info(), + tags = [x.name for x in self.tags] + ) + + @not_implemented + def ldjson(): + ... + + @deprecated('meta name="keywords" is nowadays ignored by search engines') + def seo_keywords(self): + kw = [] + for tag in self.tags: + kw.append(tag.name.replace('-', '')) + for bkl in self.back_links: + try: + kw.append(bkl.from_page.title.replace('-', ' ')) + except Exception: + pass + return ', '.join(kw) + + def is_owned_by(self, user: User, /) -> bool: + return self.owner_id == user.id + + def is_editable_by(self, /, user: User | AnonymousUserMixin) -> bool: + if not isinstance(user, User): + return False + perm = self.get_perms(user) + return bool(perm & PERM_EDIT or (user.owns(self) & PERM_CREATE)) + + can_edit = deprecated_alias(is_editable_by) + + @deprecated('use explicit .is_editable_by(current_user) instead') + def is_editable(self) -> bool: + return self.is_editable_by(current_user) + + + def current_text(self) -> str: + ## XXX is it needed? It's just better to .latest().text()? + return self.latest().text() + + @deprecated('use get_prop() and set_prop() instead') + def prop(self) -> _PagePropertyDict: + return _PagePropertyDict(self) + + def get_prop(self, key: str, /, default = None): + kv = db.session.execute( + select(PageProperty).where(PageProperty.page == self, PageProperty.key == key) + ).scalar() + return kv.value if kv else default + + def set_prop(self, key: str, value, /, commit: bool = False): + if self.has_prop(key): + db.session.execute(update(PageProperty).where(PageProperty.page_id == self.id, PageProperty.key == key) + .values(value=value)) + else: + db.session.execute(insert(PageProperty).values( + page=self, + key=key, + value=value + )) + + if commit: + db.session.commit() + + def has_prop(self, key: str, /) -> bool: + return db.session.execute( + select(PageProperty).where(PageProperty.page == self, PageProperty.key == key) + ).scalar() != None + + +class PageText(db.Model): + __tablename__ = 'pagetext' + + id = Column(Integer, primary_key=True) + content = Column(Blob) + flags = Column(BigInteger, default=0) + is_utf8 = BitSelector(flags, 1) + is_gzipped = BitSelector(flags, 2) + def get_content(self) -> str: + c = self.content + if self.is_gzipped: + c = gzip.decompress(c) + if self.is_utf8: + return c.decode('utf-8') + else: + return c.decode('latin-1') + @classmethod + def create_content(cls, text: str, *, treshold=600, search_dup = True) -> PageText: + c: bytes = text.encode('utf-8') + use_gzip = len(c) > treshold + if use_gzip and gzip: + c = gzip.compress(c) + if search_dup: + item = db.session.execute(select(cls).where(cls.content == c, cls.is_gzipped == use_gzip)).scalar() + if item: + return item + return db.session.execute(insert(cls).values( + content = c, + is_utf8 = True, + is_gzipped = use_gzip + ).returning(cls)).scalar() + + + +class PageRevision(db.Model): + __tablename__ = 'pagerevision' + + id = Column(Integer, primary_key=True) + page_id = Column(Integer, ForeignKey('page.id')) + user_id = Column(Integer, ForeignKey('user.id'), nullable=True) + comment = Column(String(1024), default='') + textref_id = Column(Integer, ForeignKey('pagetext.id')) + pub_date = Column(DateTime, index=True) + length = Column(Integer) + + page = relationship("Page", back_populates='revisions') + user = relationship("User", back_populates='contributions') + textref: Relationship[PageText] = relationship("PageText") + + + def text(self) -> str: + return self.textref.get_content() + + def html_and_toc(self) -> tuple[Markup, Any | None]: + return md_and_toc(self.text()) + + def js_info(self) -> dict: + return dict( + id = self.id, + length = self.length, + pub_date = self.pub_date.isoformat('T') + ) + + @deprecated('use .html_and_toc() instead') + def html(self) -> Markup: + return self.html_and_toc()[0] + + @deprecated('use utils.human_elapsed_time() instead') + def human_pub_date(self): + return human_elapsed_time(self.pub_date) + + +class PageTag(db.Model): + __tablename__ = 'pagetag' + __table_args__ = ( + UniqueConstraint('page_id', 'name'), + ) + + id = Column(Integer, primary_key=True) + page_id = Column(Integer, ForeignKey('page.id')) + name: Column[str] = Column(String(64), index=True) + + page = relationship('Page', back_populates='tags') + + def popularity(self) -> int: + return db.session.execute( + select(func.count('*')).select_from(PageTag).where(PageTag.name == self.name) + ).scalar() + + +class PageProperty(db.Model): + __tablename__ = 'pageproperty' + __table_args__ = ( + UniqueConstraint('page_id', 'key'), + ) + + id = Column(Integer, primary_key = True) + page_id = Column(Integer, ForeignKey('page.id')) + key = Column(String(64)) + value = Column(String(8000)) + + page = relationship('Page', back_populates = 'properties') + + +# XXX is it *really* worth it to implement PagePropertyDict? +class _PagePropertyDict(Mapping): + __slots__ = ('_page', ) + def __init__(self, page: Page, /) -> tuple[str, Any]: + self._page = page + def items(self, /): + for kv in self._page.properties: + yield (kv.key, kv.value) + def keys(self, /): + for kv in self._page.properties: + yield kv.key + def values(self, /): + for kv in self._page.properties: + yield kv.value + __iter__ = keys + def __len__(self, /): + return len(self._page.properties) + def __contains__(self, key, /): + return self._page.has_prop(key) + def __getitem__(self, key, /): + if not key in self: + raise KeyError(key) + return self._page.get_prop(key) + def __setitem__(self, key, value, /): + return self._page.set_prop(key, value) + def __delitem__(self, key, /): + db.session.execute(delete(PageProperty).where( + PageProperty.page_id == self._page.id, + PageProperty.key == key + )) + def get(self, key, /, default = None): + return self._page.get_prop(key, default) + def setdefault(self, key, /, default): + if key not in self: + return db.session.execute(insert(PageProperty).values( + page_id = self._page.id, + key = key, + value = default + ).returning(PageProperty.value)).scalar() + else: + return self._page.get_prop(key) + +class PageLink(db.Model): + __tablename__ = 'pagelink' + __table_args__ = ( + UniqueConstraint('from_page_id', 'to_page_id'), + ) + + id = Column(Integer, primary_key = True) + from_page_id = Column(Integer, ForeignKey('page.id')) + to_page_id = Column(Integer, ForeignKey('page.id')) + + from_page: Relationship[Page] = relationship('Page', foreign_keys=[from_page_id], back_populates='forward_links') + to_page: Relationship[Page] = relationship('Page', foreign_keys=[to_page_id], back_populates='back_links') + + @classmethod + def parse_links(cls, from_page, text: str, erase=True): + with db.session.begin(nested=True): + old_links = set(db.session.execute(select(PageLink.id).where(cls.from_page == from_page)).scalars()) + for mo in re.finditer(ILINK_RE, text): + try: + pageurl = mo.group(1) + if pageurl.startswith('p/'): + to_page_cond = Page.id == int(pageurl[2:]) + else: + to_page_cond = Page.url == pageurl + to_page = db.session.execute(select(Page).where(to_page_cond)).scalar() + if to_page is None: + continue + + linkobj = db.session.execute(select(PageLink).where(PageLink.from_page == from_page, PageLink.to_page == to_page)).scalar() + if linkobj is None: + linkobj = db.session.execute(insert(PageLink).values( + from_page=from_page, to_page=to_page + ).returning(PageLink)).scalar() + if linkobj.id in old_links: + old_links.remove(linkobj.id) + except Exception: + continue + if erase: + db.session.execute(delete(PageLink).where(PageLink.id.in_(old_links))) + db.session.commit() + + + # The actual ULTIMATE method to refresh all links + # To be called from a maintenance script only! + @classmethod + def refresh_all_links(cls): + for p in db.session.execute(select(Page)).scalars(): + cls.parse_links(p, p.current_text()) + +class PagePermission(db.Model): + __tablename__ = 'pagepermission' + __table_args__ = ( + UniqueConstraint('page_id', 'group_id'), + ) + + id = Column(Integer, primary_key = True) + page_id = Column(Integer, ForeignKey('page.id')) + group_id = Column(Integer, ForeignKey('usergroup.id')) + permissions = Column(BigInteger, default=0) + can_read = BitSelector(permissions, PERM_READ) + can_edit = BitSelector(permissions, PERM_EDIT) + can_create = BitSelector(permissions, PERM_CREATE) + can_set_url = BitSelector(permissions, PERM_SET_URL) + can_set_tags = BitSelector(permissions, PERM_SET_TAGS) + + page = relationship('Page', back_populates = 'permission_overrides') + group = relationship('UserGroup', back_populates = 'page_permissions') + +# END MODELS + +def is_username(username: str): + return bool(re.fullmatch(USERNAME_RE, username)) + +## TODO move page_url to separate table in 1.1 (?) +def is_valid_url(url): + return re.fullmatch(SLUG_RE, url) + +def is_url_available(url): + return url not in FORBIDDEN_URLS and Page.by_url(url) == None + + +def perms_required(perms: int, *, message: str | None = None): + ## XXX experimental, does not work at all times! + def decorator(func: Callable): + @wraps(func) + @login_required + def wrapper(*a, **ka): + if not current_user.has_perms_on_page(perms): + if message: + flash(message) + abort(403) + return func(*a, **ka) + return wrapper + return decorator + + + +def create_first_user(): + """ + Rudimentary installer. Untested: use at your own risk. + """ + first_user = db.session.execute(select(User).limit(1)).scalar() + if first_user is not None: + print('Superuser already exists!') + return first_user + + username = input('Username: ') + password = getpass('Password: ') + confirm_password = getpass('Confirm password: ') + email = input('Email: (optional)') or None + if not is_username(username): + print('Invalid username: usernames can contain only letters, numbers, underscores and hyphens.') + return + if password != confirm_password: + print('Passwords do not match.') + return + default_permissions = PERM_ALL # all permissions + if not input('Agree to the Terms of Use?')[0].lower() == 'y': + print('You must accept Terms in order to register.') + return + try: + db.metadata.create_all() + ua = db.session.execute(insert(User).values( + username = username, + email = email, + password = generate_password_hash(password), + join_date = datetime.datetime.now(), + is_admin = True + ).returning(User)).scalar() + ug = db.session.execute(insert(UserGroup).values( + name = 'default', + permissions = int(default_permissions) + ).returning(UserGroup)).scalar() + db.session.execute(insert(UserGroupMembership).values( + user = ua, + group = ug + )) + db.session.commit() + print('Installed successfully!') + return ua + except Exception: + sys.excepthook(*sys.exc_info()) + db.session.rollback() + + diff --git a/salvi/renderer.py b/salvi/renderer.py new file mode 100644 index 0000000..b286048 --- /dev/null +++ b/salvi/renderer.py @@ -0,0 +1,40 @@ +# (C) 2020-2025 Sakuragasaki46. +# See LICENSE for copying info. + +from typing import Any +import markdown +import re + +from markupsafe import Markup +from suou import deprecated +from suou.markdown import StrikethroughExtension, SpoilerExtension + +SpoilerExtension.patch_blockquote_processor() + +def error_p(text: str) -> Markup: + return Markup(f'

    {0}

    ').format(text) + +def md_and_toc(text) -> tuple[Markup, Any | None]: + extensions = ['tables', 'footnotes', 'fenced_code', 'sane_lists', 'toc', + StrikethroughExtension(), SpoilerExtension()] + extension_configs = {} + + try: + converter: markdown.Markdown = markdown.Markdown(extensions=extensions, extension_configs=extension_configs) + markup = Markup(converter.convert(text)) + toc = converter.toc + return markup, toc + except Exception as e: + return error_p('Error during rendering: {0}: {1}') + +@deprecated('inefficient, needs to call markdown engine just to discard any tags') +def remove_tags(text, convert=True, headings=True): + if headings: + text = re.sub(r'\#[^\n]*', '', text) + if convert: + text = md_and_toc(text)[0] + return re.sub(r'<.*?>', '', text) + + + + diff --git a/salvi/routes/__init__.py b/salvi/routes/__init__.py new file mode 100644 index 0000000..ead421e --- /dev/null +++ b/salvi/routes/__init__.py @@ -0,0 +1,44 @@ + + + +from flask import render_template, request +from suou.functools import deprecated +from ..models import db + +blueprints = [] + +@deprecated('use db.paginate()') +def render_paginated_template(template_name, query_name, **kwargs): + query = kwargs.pop(query_name) + page = int(request.args.get('page', 1)) + kwargs[query_name] = pag_query = db.paginate(query) + return render_template( + template_name, + page_n = page, + total_count = pag_query.total, + **kwargs + ) + +from .accounts import bp +blueprints.append(bp) +from .admin import bp +blueprints.append(bp) +from .calendar import bp +blueprints.append(bp) +from .edit import bp +blueprints.append(bp) +from .embed import bp +blueprints.append(bp) +from .home import bp +blueprints.append(bp) +from .legal import bp +blueprints.append(bp) +from .search import bp +blueprints.append(bp) +from .stats import bp +blueprints.append(bp) +from .view import bp +blueprints.append(bp) + + + diff --git a/salvi/routes/accounts.py b/salvi/routes/accounts.py new file mode 100644 index 0000000..4847418 --- /dev/null +++ b/salvi/routes/accounts.py @@ -0,0 +1,95 @@ + +import datetime +from flask import Blueprint, abort, flash, redirect, render_template, request +from flask_login import current_user, login_user, logout_user +from sqlalchemy import insert, select +from sqlalchemy.exc import IntegrityError +from werkzeug.security import check_password_hash, generate_password_hash + +from ..models import PageRevision, User, UserGroup, UserGroupMembership, db, is_username + +bp = Blueprint('accounts', __name__) + +current_user: User + + +@bp.route('/u//') +def contributions_legacy_url(username): + return redirect(f'/@{username}/') + +@bp.route('/@/') +def contributions(username): + user = db.session.execute(select(User).where(User.username == username)).scalar() + if user is None: + abort(404) + + contributions = db.paginate(select(PageRevision).where(PageRevision.user == user).order_by(PageRevision.pub_date.desc())) + return render_template('contributions.html', + u=user, + contributions=contributions, + ) + + +@bp.route('/accounts/login/', methods=['GET','POST']) +def login(): + if current_user.is_authenticated: + return redirect(request.args.get('next', '/')) + if request.method == 'POST': + username = request.form['username'] + user = db.session.execute(select(User).where(User.username == username)).scalar() + if user is None or not check_password_hash(user.password, request.form['password']): + flash('Invalid username or password.') + return render_template('login.html') + + if user.is_disabled: + flash("Your account is disabled.") + return render_template("login.html") + + remember_for = 'remember' in request.form + if remember_for: + login_user(user, remember=True, + duration=datetime.timedelta(days=30)) + else: + login_user(user) + return redirect(request.args.get('next', '/')) + return render_template('login.html') + + +@bp.route('/accounts/register/', methods=['GET','POST']) +def register(): + if request.method == 'POST': + username = request.form['username'] + password = request.form['password'] + if not is_username(username): + flash('Invalid username: usernames can contain only letters, numbers, underscores and hyphens.') + return render_template('register.html') + if request.form['password'] != request.form['confirm_password']: + flash('Passwords do not match.') + return render_template('register.html') + if not request.form['legal']: + flash('You must accept Terms in order to register.') + return render_template('register.html') + try: + with db.session.begin(): + u = db.session.execute(insert(User).values( + username = username, + email = request.form.get('email'), + password = generate_password_hash(password), + join_date = datetime.datetime.now() + ).returning(User)).scalar() + db.session.execute(insert(UserGroupMembership).values( + user = u, + group = UserGroup.get_default() + )) + db.session.commit() + + login_user(u) + return redirect(request.args.get('next', '/')) + except IntegrityError: + flash('Username taken') + return render_template('register.html') + +@bp.route('/accounts/logout/') +def accounts_logout(): + logout_user() + return redirect(request.args.get('next', '/')) diff --git a/salvi/routes/admin.py b/salvi/routes/admin.py new file mode 100644 index 0000000..38e8735 --- /dev/null +++ b/salvi/routes/admin.py @@ -0,0 +1,93 @@ + +import datetime +from flask import Blueprint, flash, render_template, request +from flask_login import current_user, login_required +from sqlalchemy import Select, select + +from salvi.transfer import Exporter, Importer + +from ..models import Page, PageTag, User, db + +current_user : User + + +bp = Blueprint('admin', __name__) + + +@bp.route('/manage/') +def manage_main(): + return render_template('administration.html') + + +@bp.route('/manage/export/', methods=['GET', 'POST']) +def exportpages(): + if request.method == 'POST': + raw_list = request.form['export-list'] + q_list: list[Select] = [] + for item in raw_list.split('\n'): + item = item.strip() + if len(item) < 2: + continue + if item.startswith('+'): + q_list.append(select(Page).where(Page.id == item[1:])) + elif item.startswith('#'): + q_list.append(select(Page).join(PageTag, PageTag.page_id == Page.id).where(PageTag.name == item[1:])) + elif item.startswith('/'): + q_list.append(select(Page).where(Page.url == item[1:].rstrip('/'))) + else: + q_list.append(select(Page).where(Page.title == item)) + if not q_list: + flash('Failed to export pages: The list is empty!') + return render_template('exportpages.jinja2') + q_union = q_list.pop(0) + while q_list: + q_union = q_union.union(q_list.pop(0)) + e = Exporter() + e.add_page_list(db.session.execute(q_union).scalars(), include_history='history' in request.form) + return e.export(), {'Content-Type': 'application/json', 'Content-Disposition': 'attachment; ' + + 'filename=export-{}.json'.format(datetime.datetime.now().strftime('%Y%m%d-%H%M%S'))} + return render_template('exportpages.html') + +@bp.route('/manage/import/', methods=['GET', 'POST']) +@login_required +def importpages(): + if not current_user.is_admin: + flash('Pages can be imported by Administrators only!') + return render_template('403.html'), 403 + if request.method == 'POST': + f = request.files['import'] + overwrite_urls = request.form.get('ovwurls') + im = Importer(f.read(), overwrite_urls=overwrite_urls) + im.claim(current_user) + res = im.execute() + flash(f'Imported successfully {res[0]} pages and {res[1]} revisions') + + return render_template('importpages.html') + +@bp.route('/manage/accounts/', methods=['GET', 'POST']) +@login_required +def manage_accounts(): + users_q = select(User).order_by(User.join_date.desc()) + if request.method == 'POST': + if current_user.is_admin: + action = request.form.get("action") + userids = [] + if action == "disable": + for key in request.form.keys(): + if key.startswith("u") and key[1:].isdigit(): + userids.append(int(key[1:])) + uu = 0 + for uid in userids: + u = db.session.execute(select(User).where(User.id == uid)).scalar() + if u is None: + continue + u.is_disabled = not u.is_disabled + db.session.add(u) + uu += 1 + db.session.commit() + flash(f"Successfully disabled {uu} users!") + else: + flash("Unknown action") + else: + flash('Operation not permitted!') + return render_template('manageaccounts.html', users=db.paginate(users_q)) diff --git a/salvi/routes/calendar.py b/salvi/routes/calendar.py new file mode 100644 index 0000000..9bc3081 --- /dev/null +++ b/salvi/routes/calendar.py @@ -0,0 +1,47 @@ + +import datetime +from flask import Blueprint, render_template, request +from sqlalchemy import select + +from ..models import Page, db + +bp = Blueprint('calendar', __name__) + + +def _advance_calendar(date: datetime.date, offset=0): + if offset == -2: + return datetime.date(date.year - 1, date.month, 1) + elif offset == -1: + return datetime.date(date.year, date.month - 1, 1) if date.month > 1 else datetime.date(date.year - 1, 12, 1) + elif offset == 1: + return datetime.date(date.year, date.month + 1, 1) if date.month < 12 else datetime.date(date.year + 1, 1, 1) + elif offset == 2: + return datetime.date(date.year + 1, date.month, 1) + else: + return date + +@bp.route('/calendar/') +def calendar_view(): + now = datetime.datetime.now() + return render_template('calendar.html', now=now, + from_year=int(request.args.get('from_year', now.year - 12)), + till_year=int(request.args.get('till_year', now.year + 5)) + ) + +@bp.route('/calendar//') +def calendar_month(y: int, m: int): + notes = db.paginate(select(Page).where( + (datetime.date(y, m, 1) <= Page.calendar), + (Page.calendar < datetime.date(y+1 if m==12 else y, 1 if m==12 else m+1, 1)) + ).order_by(Page.calendar)).scalars() + + #toc_q = Page.select(fn.Month(Page.calendar).alias('month'), fn.Count(Page.id).alias('n_notes')).where( + # (datetime.date(y, 1, 1) <= Page.calendar) & + # (Page.calendar < datetime.date(y+1, 1, 1)) + #).group_by() + toc = {} + #for i in toc_q: + # toc[i.month] = i.n_notes + + return render_template('month.html', d=datetime.date(y, m, 1), notes=notes, advance_calendar=_advance_calendar, toc=toc) + diff --git a/salvi/routes/edit.py b/salvi/routes/edit.py new file mode 100644 index 0000000..4be78de --- /dev/null +++ b/salvi/routes/edit.py @@ -0,0 +1,203 @@ + + +import datetime +import re +import sys +from typing import Mapping +from flask import Blueprint, abort, flash, g, jsonify, redirect, render_template, request +from flask_login import current_user, login_required +from sqlalchemy import select, insert +from suou import want_isodate + +from salvi.i18n import get_string + +from ..utils import parse_tag_list +from ..renderer import md_and_toc + +from ..models import PERM_CREATE, Page, PageLink, PageRevision, PageText, User, db, is_url_available, is_valid_url, perms_required + +current_user: User + +bp = Blueprint('edit', __name__) + +def savepoint(form: Mapping, is_preview: bool = False, pageobj: Page | None = None): + '''Middle point during page editing.''' + + if is_preview: + preview = md_and_toc(form['text'])[0] + else: + preview = None + pl_js_info = dict() + pl_js_info['editing'] = dict( + original_text = pageobj.current_text() if pageobj else None, + preview_text = form['text'], + page_id = pageobj.id if pageobj else None + ) + return render_template( + 'edit.html', + pl_url=form['url'], + pl_title=form['title'], + pl_text=form['text'], + pl_tags=form['tags'], + pl_comment=form['comment'], + pl_is_locked='lockpage' in form, + pl_owner_is_current_user=current_user.owns(pageobj) if pageobj else True, + preview=preview, + pl_js_info=pl_js_info, + pl_calendar=('usecalendar' in form) and form['calendar'], + pl_readonly=not pageobj.is_editable_by(current_user) if pageobj else False, + pl_cw=('cw' in form) and form['cw'] + ) + +@bp.route('/create/', methods=['GET', 'POST']) +@perms_required(PERM_CREATE, message='You are not authorized to create pages.') +def create(): + if request.method == 'POST': + if request.form.get('preview'): + return savepoint(request.form, is_preview=True) + p_url = request.form['url'] or None + p_title = request.form['title'] + if p_url: + if not is_valid_url(p_url): + flash('Invalid URL. Valid URLs contain only letters, numbers and hyphens.') + return savepoint(request.form) + elif not is_url_available(p_url): + flash('This URL is not available.') + return savepoint(request.form) + + try: + p_tags = parse_tag_list(request.form.get('tags', '')) + except ValueError: + flash('Invalid tags text. Tags contain only letters, numbers and hyphens, and are separated by comma.') + return savepoint(request.form) + + p_calendar = datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None + + try: + p = db.session.execute(insert(Page).values( + url = p_url, + title = p_title, + is_redirect = False, + touched = datetime.datetime.now(), + owner_id = current_user.id, + calendar = p_calendar, + is_locked = 'lockpage' in request.form, + is_cw = 'cw' in request.form + ).returning(Page)).scalar() + + p.change_tags(p_tags) + + pt = PageText.create_content(request.form['text']) + + db.session.execute(insert(PageRevision).values( + page_id = p.id, + user_id = p.owner_id, + comment = request.form.get('comment', ''), + textref_id = pt.id, + pub_date = datetime.datetime.now(), + length = len(request.form['text']) + )) + + PageLink.parse_links(p, request.form['text']) + + db.session.commit() + except Exception as e: + flash(f'An error occurred while saving this revision. Please be patient.') + sys.excepthook(*sys.exc_info()) + return savepoint(request.form) + + return redirect(p.get_url()) + return savepoint(dict(url=request.args.get('url'), title='', text='', tags='', comment=get_string(g.lang, 'page_created'))) + +@bp.route('/edit/', methods=['GET', 'POST']) +@login_required +def edit(id: int): + p = db.session.execute(select(Page).where(Page.id == id)).scalar() + if p is None: + abort(404) + + p_latest = p.latest() + + if request.method == 'POST': + if not p.can_edit(current_user): + flash('You are trying to edit a locked page!') + abort(403) + if request.form.get('preview'): + return savepoint(request.form, is_preview=True, pageobj=p) + p_url = request.form['url'] or None + if p_url: + if not is_valid_url(p_url): + flash('Invalid URL. Valid URLs contain only letters, numbers and hyphens.') + return savepoint(request.form, pageobj=p) + elif not is_url_available(p_url) and p_url != p.url: + flash('This URL is not available.') + return savepoint(request.form, pageobj=p) + p_tags = [x.strip().lower().replace(' ', '-').replace('_', '-').lstrip('#') + for x in request.form.get('tags', '').split(',')] + p_tags = [x for x in p_tags if x] + if any(not re.fullmatch(SLUG_RE, x) for x in p_tags): + flash('Invalid tags text. Tags contain only letters, numbers and hyphens, and are separated by comma.') + return savepoint(request.form, pageobj=p) + p.url = p_url + p.title = request.form['title'] + p.touched = datetime.datetime.now() + p.is_locked = 'lockpage' in request.form + p.is_cw = 'cw' in request.form + p.calendar = datetime.date.fromisoformat(request.form["calendar"]) if 'usecalendar' in request.form else None + p.change_tags(p_tags) + if request.form['text'] != p_latest.text(): + pt = PageText.create_content(request.form['text']) + + db.session.execute(insert(PageRevision).values( + page_id=p.id, + user_id=current_user.id, + comment=request.form["comment"], + textref_id=pt.id, + pub_date=datetime.datetime.now(), + length=len(request.form['text']) + )) + db.session.add(p) + db.session.commit() + return redirect(p.get_url()) + + form = dict( + url=p.url, + title=p.title, + text=p_latest.text(), + tags=','.join(x.name for x in p.tags), + comment='' + ) + if p.is_locked: + form['lockpage'] = '1' + if p.calendar: + form['usecalendar'] = '1' + form['calendar'] = want_isodate(p.calendar).split('T')[0] + return savepoint(form, pageobj=p) + + +@bp.route('/_jsoninfo/', methods=['GET', 'POST']) +@bp.route('/p/.json', methods=['GET']) +def page_jsoninfo(id): + p = db.session.execute(select(Page).where(Page.id == id)).scalar() + if p is None: + return jsonify({'status':'fail'}), 404 + j = p.js_info() + j["status"] = "ok" + if request.method == "POST": + j["text"] = p.latest().text() + return jsonify(j) + +@bp.route("/_jsoninfo/changed/") +def jsoninfo_changed(ts): + tse = str(datetime.datetime.fromtimestamp(ts).isoformat(" ")) + ps = db.session.execute(select(Page).where(Page.touched >= tse)).scalars() + return jsonify({ + "ids": [i.id for i in ps], + "status": "ok" + }) + + + + + + diff --git a/salvi/routes/embed.py b/salvi/routes/embed.py new file mode 100644 index 0000000..7bf32b9 --- /dev/null +++ b/salvi/routes/embed.py @@ -0,0 +1,22 @@ + + + +import html +from sqlalchemy import select +from flask import Blueprint, abort + +from salvi.models import Page, db + +bp = Blueprint('embed', __name__) + +## embed HTML is pending deprecation. Working on API. + +@bp.route('/embed//') +def embed_view(id): + p = db.session.execute(select(Page).where(Page.id == id)).scalar() + if p is None: + return '', 404 + rev = p.latest() + return "

    {0}

    {1}
    ".format( + html.escape(p.title), rev.html()) + diff --git a/salvi/routes/home.py b/salvi/routes/home.py new file mode 100644 index 0000000..764ef0f --- /dev/null +++ b/salvi/routes/home.py @@ -0,0 +1,55 @@ + +from flask import Blueprint, abort, flash, redirect, render_template +from sqlalchemy import select, func + +from ..models import Page, PageTag, db +from .. import app_config + +bp = Blueprint('home', __name__) + +@bp.route('/') +def homepage(): + page_limit = app_config.default_items_per_page + new_notes = db.paginate(select(Page).order_by(Page.touched.desc()).limit(page_limit)) + return render_template('home.html', + new_notes = new_notes) + +@bp.route('/p/most_recent/') +def view_most_recent(): + general_query = db.paginate(select(Page).order_by(Page.touched.desc())) + return render_template('listrecent.html', notes=general_query) + +@bp.route('/p/random/') +def view_random(): + page = None + page_count = db.session.execute(select(func.count()).select_from(Page)).scalar() + if page_count < 4: + flash('Too few pages in this site.') + abort(404) + while not page: + page = db.session.execute(select(Page).order_by(func.random())).scalar() + return redirect(page.get_url()) + +@bp.route('/p/leaderboard/') +def page_leaderboard(): + headers = { + 'Cache-Control': 'max-age=180, stale-while-revalidate=1800' + } + + pages = [] + ## TODO make it a query + pages_q = db.session.execute(select(Page)).scalars() + for p in pages_q: + p_latest = p.latest() + score = (p_latest.length >> 10) + len(p.forward_links) + len(p.back_links) + ## TODO make it a namedtuple + pages.append((p, score, len(p.back_links.count), len(p.forward_links), p_latest.length)) + pages.sort(key = lambda x: (x[1], x[2], x[4], x[3]), reverse = True) + + return render_template('leaderboard.html', pages=pages), headers + +@bp.route('/tags//') +def listtag(tag): + general_query = select(Page).join(PageTag, PageTag.page_id == Page.id).where(PageTag.name == tag).order_by(Page.touched.desc()) + return render_template('listtag.html', tagname=tag, tagged_notes=db.paginate(general_query)) + diff --git a/salvi/routes/legal.py b/salvi/routes/legal.py new file mode 100644 index 0000000..0ebfcab --- /dev/null +++ b/salvi/routes/legal.py @@ -0,0 +1,16 @@ + +from flask import Blueprint, render_template + +bp = Blueprint('legal', __name__) + +@bp.route('/terms/') +def terms(): + return render_template('terms.html') + +@bp.route('/privacy/') +def privacy(): + return render_template('privacy.html') + +@bp.route('/rules/') +def rules(): + return render_template('rules.html') \ No newline at end of file diff --git a/salvi/routes/search.py b/salvi/routes/search.py new file mode 100644 index 0000000..c108749 --- /dev/null +++ b/salvi/routes/search.py @@ -0,0 +1,22 @@ + +from flask import Blueprint, render_template, request +from sqlalchemy import select + +from ..models import Page, PageTag, db + +bp = Blueprint('search', __name__) + +@bp.route('/search/', methods=['GET', 'POST']) +def search(): + if request.method == 'POST': + q = request.form['q'] + include_tags = bool(request.form.get('include-tags')) + query = select(Page).where(Page.title.ilike('%' + q + '%')) + if include_tags: + query = query.union(select(Page).join(PageTag, PageTag.page_id == Page.id + ).where(PageTag.name.ilike('%' + q + '%'))) + query = query.order_by(Page.touched.desc()) + return render_template('search.html', q=q, pl_include_tags=include_tags, + results=db.paginate(query)) + return render_template('search.html', pl_include_tags=True) + diff --git a/salvi/routes/stats.py b/salvi/routes/stats.py new file mode 100644 index 0000000..0ac94f8 --- /dev/null +++ b/salvi/routes/stats.py @@ -0,0 +1,75 @@ + + + +import datetime +from flask import Blueprint, abort, redirect, render_template, request +from sqlalchemy import func, select + +from ..models import Page, PageRevision, User, UserGroup, db + +bp = Blueprint('stats', __name__) + +@bp.route('/backlinks//') +def backlinks(id): + p = db.session.execute(select(Page).where(Page.id == id)).scalar() + if p is None: + abort(404) + return render_template('backlinks.html', p=p, backlinks=p.back_links) + + +@bp.route('/stats/') +def stats(): + return render_template('stats.html', + notes_count = db.session.execute(select(func.count()).select_from(Page)).scalar(), + notes_with_url = db.session.execute(select(func.count()).select_from(Page).where(Page.url != None)).scalar(), + revision_count = db.session.execute(select(func.count()).select_from(PageRevision)).scalar(), + users_count = db.session.execute(select(func.count()).select_from(User)).scalar(), + groups_count = db.session.execute(select(func.count()).select_from(UserGroup)).scalar() + ) + + +## easter egg (lol) ## + +MNeaster = { + 15: (22, 2), 16: (22, 2), 17: (23, 3), 18: (23, 4), 19: (24, 5), 20: (24, 5), + 21: (24, 6), 22: (25, 0), 23: (26, 1), 24: (25, 1)} + +def calculate_easter(y): + a, b, c = y % 19, y % 4, y % 7 + M, N = (15, 6) if y < 1583 else MNeaster[y // 100] + d = (19 * a + M) % 30 + e = (2 * b + 4 * c + 6 * d + N) % 7 + if d + e < 10: + return datetime.date(y, 3, d + e + 22) + else: + day = d + e - 9 + if day == 26: + day = 19 + elif day == 25 and d == 28 and e == 6 and a > 10: + day = 18 + return datetime.date(y, 4, day) + +def stash_easter(y): + easter = calculate_easter(y) + natale = datetime.date(y, 12, 25) + avvento1 = natale - datetime.timedelta(days=22 + natale.weekday()) + return dict( + easter = easter, + ceneri = easter - datetime.timedelta(days=47), + ascensione = easter + datetime.timedelta(days=42), + pentecoste = easter + datetime.timedelta(days=49), + avvento1 = avvento1 + ) + +@bp.route('/easter/') +@bp.route('/easter//') +def easter_y(y=None): + if not y and 'y' in request.args: + return redirect('/easter/' + request.args['y'] + '/') + if y: + if y > 2499: + flash('Years above 2500 A.D. are currently not supported.') + return render_template('easter.jinja2') + return render_template('easter.html', y=y, easter_dates=stash_easter(y)) + else: + return render_template('easter.html') diff --git a/salvi/routes/view.py b/salvi/routes/view.py new file mode 100644 index 0000000..4d540d9 --- /dev/null +++ b/salvi/routes/view.py @@ -0,0 +1,50 @@ + + + + +from flask import Blueprint, abort, redirect, render_template +from sqlalchemy import select + +from salvi.constants import FORBIDDEN_URLS +from salvi.models import Page, PageRevision, db + + +bp = Blueprint('view', __name__) + + + +@bp.route('/p//') +def view_unnamed(id): + p = db.session.execute(select(Page).where(Page.id == id)).scalar() + if p is None: + abort(404) + if p.url and p.url not in FORBIDDEN_URLS: + return redirect(p.get_url()) + return render_template('view.html', p=p, rev=p.latest()) + + +# for some reason, this must be the LAST view defined lol +@bp.route('//') +def view_named(name): + p = db.session.execute(select(Page).where(Page.url == name)).scalar() + if p is None: + abort(404) + return render_template('view.html', p=p, rev=p.latest()) + + +@bp.route('/history//') +def history(id): + p = db.session.execute(select(Page).where(Page.id == id)).scalar() + if p is None: + abort(404) + revisions = db.session.execute(select(PageRevision).where(PageRevision.page_id == p.id).order_by(PageRevision.pub_date.desc())).scalars() + return render_template('history.html', p=p, history=revisions) + +@bp.route('/history/revision//') +def view_old(revisionid: int): + rev = db.session.execute(select(PageRevision).where(PageRevision.id == revisionid)).scalar() + if rev is None: + abort(404) + p = rev.page + return render_template('viewold.html', p=p, rev=rev) + diff --git a/static/content.js b/salvi/static/content.js similarity index 100% rename from static/content.js rename to salvi/static/content.js diff --git a/static/edit.js b/salvi/static/edit.js similarity index 100% rename from static/edit.js rename to salvi/static/edit.js diff --git a/static/style.css b/salvi/static/style.css similarity index 57% rename from static/style.css rename to salvi/static/style.css index 6734259..de22c5f 100644 --- a/static/style.css +++ b/salvi/static/style.css @@ -1,37 +1,119 @@ +/* Style for Salvi */ +/* (c) 2020-2025 Sakuragasaki46 */ +/* TODO rewrite to Sass in 1.1 */ + /* variables */ :root { - --bg-main: #faf5e9; - --fg-main: #1f2528; - --bg-sharp: white; - --fg-sharp: black; - --fg-alt: #363636; - --bg-alt: #f9f9f9; - --bg-flash: #fff2b4; - --border: #ccc; - --border-sharp: #09f; - --border-flash: #ffe660; - --fg-error: #99081f; - --btn-error: #ff1800; - --btn-success: #37b92e; - --fg-link: #239b89; - --fg-link-visited: #2f6a5f; - --fg-link-hover: #0088ff; - --bg-link: aliceblue; + --bg-main-light: #faf5e9; + --fg-main-light: #1f2528; + --bg-sharp-light: white; + --fg-sharp-light: black; + --fg-alt-light: #363636; + --bg-alt-light: #f9f9f9; + --bg-flash-light: #fff2b4; + --border-light: #ccc; + --border-flash-light: #ffe660; + --fg-error-light: #99081f; + --btn-error-light: #ff1800; + --btn-success-light: #37b92e; + --bg-header-light: #2754c0; + + --bg-main-dark: #1f1f1f; + --fg-main-dark: #e5e5e5; + --bg-sharp-dark: black; + --fg-sharp-dark: white; + --fg-alt-dark: #cecece; + --bg-alt-dark: #333; + --bg-flash-dark: #771; + --border-dark: #555; + --border-flash-dark: #fd2; + --fg-error-dark: #ff4860; + --btn-error-dark: #e01400; + --btn-success-dark: #5d3; + --bg-header-dark: #183690; } +/* accent colors are defined by style tags */ + +/*@media (prefers-color-scheme:light) {*/ + :root, .color-scheme-light { + --bg-main: var(--bg-main-light); + --fg-main: var(--fg-main-light); + --bg-sharp: var(--bg-sharp-light); + --fg-sharp: var(--fg-sharp-light); + --fg-alt: var(--fg-alt-light); + --bg-alt: var(--bg-alt-light); + --bg-flash: var(--bg-flash-light); + --border: var(--border-light); + --border-sharp: var(--border-sharp-light); + --border-flash: var(--border-flash-light); + --fg-error: var(--fg-error-light); + --btn-error: var(--btn-error-light); + --btn-success: var(--btn-success-light); + --fg-link: var(--fg-link-light); + --fg-link-visited: var(--fg-link-visited-light); + --fg-link-hover: var(--fg-link-hover-light); + --bg-link: var(--bg-link-light); + --bg-header: var(--bg-header-light); + } +/*}*/ + +@media (prefers-color-scheme:dark) { + :root { + --bg-main: var(--bg-main-dark); + --fg-main: var(--fg-main-dark); + --bg-sharp: var(--bg-sharp-dark); + --fg-sharp: var(--fg-sharp-dark); + --fg-alt: var(--fg-alt-dark); + --bg-alt: var(--bg-alt-dark); + --bg-flash: var(--bg-flash-dark); + --border: var(--border-dark); + --border-sharp: var(--border-sharp-dark); + --border-flash: var(--border-flash-dark); + --fg-error: var(--fg-error-dark); + --btn-error: var(--btn-error-dark); + --btn-success: var(--btn-success-dark); + --fg-link: var(--fg-link-dark); + --fg-link-visited: var(--fg-link-visited-dark); + --fg-link-hover: var(--fg-link-hover-dark); + --bg-link: var(--bg-link-dark); + --bg-header: var(--bg-header-dark); + } +} + +.color-scheme-dark { + --bg-main: var(--bg-main-dark); + --fg-main: var(--fg-main-dark); + --bg-sharp: var(--bg-sharp-dark); + --fg-sharp: var(--fg-sharp-dark); + --fg-alt: var(--fg-alt-dark); + --bg-alt: var(--bg-alt-dark); + --bg-flash: var(--bg-flash-dark); + --border: var(--border-dark); + --border-sharp: var(--border-sharp-dark); + --border-flash: var(--border-flash-dark); + --fg-error: var(--fg-error-dark); + --btn-error: var(--btn-error-dark); + --btn-success: var(--btn-success-dark); + --fg-link: var(--fg-link-dark); + --fg-link-visited: var(--fg-link-visited-dark); + --fg-link-hover: var(--fg-link-hover-dark); + --bg-link: var(--bg-link-dark); + --bg-header: var(--bg-header-dark); +} /* basic styles */ * {box-sizing: border-box;} body{font-family:sans-serif;background-color:var(--bg-main); color: var(--fg-main);margin:0;position:relative} -.content{margin: 3em 1.3em; position: relative} -.footer{text-align:center;} +.site-content{margin: 3em 1.3em; position: relative} +.site-footer{text-align:center;} /* header styles */ -.header{padding:1em;display:flex;flex-direction:row;justify-content:space-between;position:sticky;top:0;background-color:var(--bg-alt); z-index: 99} -.header-app-name{font-size: 1.5em; font-weight: bold} -.header .header-blank {flex: 1; margin: 0 .5em} -.header ul{list-style:none;padding:0;margin:0;font-size:0.9em;border-left:var(--border) 1px solid} -.header ul > li{display:inline-block;padding-right:1em} +.site-header{padding:1em;display:flex;flex-direction:row;justify-content:space-between;position:sticky;top:0;background-color:var(--bg-alt); z-index: 99} +.site-name{font-size: 1.5em; font-weight: bold} +.site-header .site-header-blank {flex: 1; margin: 0 .5em} +.site-header ul{list-style:none;padding:0;margin:0;font-size:0.9em;border-left:var(--border) 1px solid} +.site-header ul > li{display:inline-block;padding-right:1em} /* content styles */ .article-header {text-align: center;} @@ -74,9 +156,9 @@ input{border:0;border-bottom:3px solid var(--border);font:inherit;color:var(--fg input:focus{color:var(--fg-sharp);border-bottom-color:var(--border-sharp)} input.error{border-bottom-color:var(--btn-error)} input[type="submit"],input[type="reset"],input[type="button"],button{font-family:inherit;border-radius:12px;border:1px solid var(--border);display:inline-block} -.submit-primary{color:var(--bg-main);background-color:var(--btn-success);font-family:inherit;border:1px solid var(--btn-success);font-size:1.2em;height:2em;min-width:8em} -.submit-secondary{color:var(--fg-main);background-color:var(--bg-main);font-family:inherit;border:1px solid var(--btn-success);font-size:1.2em;height:2em;min-width:8em} -.submit-quick{color:var(--bg-main);background-color:var(--btn-success);font-family:inherit;border:1px solid var(--btn-success);font-size:inherit;border-radius:6px} +.submit-primary{color:var(--bg-main);background-color:var(--fg-link);font-family:inherit;border:1px solid var(--fg-link);font-size:1.2em;height:2em;min-width:8em} +.submit-secondary{color:var(--fg-main);background-color:var(--bg-main);font-family:inherit;border:1px solid var(--fg-link);font-size:1.2em;height:2em;min-width:8em} +.submit-quick{color:var(--bg-main);background-color:var(--fg-link);font-family:inherit;border:1px solid var(--fg-link);font-size:inherit;border-radius:6px} .flash{background-color:var(--bg-flash);padding:12px;border-radius:4px;border:1px var(--border-flash) solid} .page-tags p{display:inline-block} .page-tags ul{padding:0;margin:0;list-style:none;display:inline-block} @@ -91,32 +173,20 @@ ul.inline {margin:0; padding:0; display: inline} ul.inline > li {display: inline-block;} ul.inline > li::after {content: "·"} ul.inline > li:last-child::after {content: ""} - -/* Circles extension */ -.circles-red{color: #e14} -.circles-orange{color: #f72} -.circles-green{color: #6e4} -.circles-yellow{color: #fc6} -.circles-list{list-style: none} -.circles-list > li{margin: .5em 0;} -.circles-list > li::before{font-size: 24px;transform:translatey(50%);font-weight:bold;margin-right:8px} -.circles-list > li.circles-red::before{color: #e14; content: '❌︎'} -.circles-list > li.circles-orange::before{color: #f72; content: '△'} -.circles-list > li.circles-yellow::before{color: #fc6; content: '◇'} -.circles-list > li.circles-green::before{color: #6e4; content: '○'} -.circles-add-form{display:table} -.circles-add-form > div{display:table-row} -.circles-add-form > div > *{display:table-cell} -.circles-add-form > div > label{text-align:right} -.circles-li-code {font-size: 120%} - -/* ContactNova extension */ -.contactnova-issues {padding-left: 12px; border-left: 4px solid #fc6} -.contactnova-col-code {font-size: 1.25em; font-weight: bold} -.contactnova-status_1 .material-icons {color: #6e4} -.contactnova-status_2 .material-icons {color: #fc6} -.contactnova-list td {vertical-align: middle} - +.wrap_responsive_cells { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); + /* This is better for small screens, once min() is better supported */ + /* grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr)); */ + gap: 1rem; +} +.wrap_responsive_cells_narrow { + display: grid; + grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); + /* This is better for small screens, once min() is better supported */ + /* grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr)); */ + gap: 2px; +} /* floating elements */ nav.toc{display:none} @@ -130,7 +200,7 @@ nav.toc{display:none} .backontop{position:fixed;bottom:0;right:0} @media print{ .backontop {display:none} - .header{position:static} + .site-header{position:static} } #__top{position:absolute;top:0;left:0} @@ -152,18 +222,6 @@ input.title-input{overflow:visible;font-weight:bold;font-size:2em;width:100%;mar a:link{color:var(--fg-link)} a:visited{color:var(--fg-link-visited)} a:hover{color:var(--fg-link-hover)} -.metro-links{padding:12px;color:white;background-color:#333} -.metro-links a{color:white} -.metro-prev{float:left} -.metro-next{float:right} -.metro-links.metro-1{background-color:red} -.metro-links.metro-2{background-color:blue} -.metro-links.metro-3{background-color:green} -.metro-links.metro-4{background-color:orange} -.metro-links.metro-5{background-color:teal} -.metro-links.metro-6{background-color:purple} -.metro-divider{height:1px;background-color:white;clear:both} -.metro-badge{background-color:#333;border-radius:4px;color:white;font-size:80%} /* grids */ @media (min-width:800px) { @@ -175,47 +233,9 @@ a:hover{color:var(--fg-link-hover)} .nl-list > .nl-prev, .nl-list > .nl-next {grid-column-end: span 3} } +/* dark theme extra */ +.color-scheme-dark .text-input{border-bottom-color: #60a} +.color-scheme-dark .over-text-input{background-color: #60a} -/* dark theme */ - -.dark { - --bg-main: #1f1f1f; - --fg-main: #e5e5e5; - --bg-sharp: black; - --fg-sharp: white; - --fg-alt: #cecece; - --bg-alt: #333; - --bg-flash: #771; - --border: #555; - --border-sharp: #4bf; - --border-flash: #fd2; - --fg-error: #ff4860; - --btn-error: #e01400; - --btn-success: #5d3; - --fg-link: #99cadc; - --fg-link-visited: #a2e2de; - --fg-link-hover: #33ffaa; - --bg-link: #555; -} - -.dark .text-input{border-bottom-color: #60a} -.dark .over-text-input{background-color: #60a} -a.dark-theme-toggle-off{display: none} -.dark a.dark-theme-toggle-off{display: inline} -.dark a.dark-theme-toggle-on{display: none} - -/* ?????? */ -.wrap_responsive_cells { - display: grid; - grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); - /* This is better for small screens, once min() is better supported */ - /* grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr)); */ - gap: 1rem; -} -.wrap_responsive_cells_narrow { - display: grid; - grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); - /* This is better for small screens, once min() is better supported */ - /* grid-template-columns: repeat(auto-fill, minmax(min(200px, 100%), 1fr)); */ - gap: 2px; -} +.color-scheme-light .dark-theme-toggle-off { display: none} +.color-scheme-dark .dark-theme-toggle-on { display: none} diff --git a/salvi/templates/400.html b/salvi/templates/400.html new file mode 100644 index 0000000..1e02791 --- /dev/null +++ b/salvi/templates/400.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} + +{% block title %}{{ title_tag(T('Bad Request'), false) }}{% endblock %} + +{% block content %} +
    +

    Bad request

    + +
    +

    You sent a request the server can’t understand.

    +

    If you entered the URL manually please check your spelling and try again.

    +
    +
    +{% endblock %} diff --git a/salvi/templates/403.html b/salvi/templates/403.html new file mode 100644 index 0000000..2ead3b1 --- /dev/null +++ b/salvi/templates/403.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} + +{% block title %}{{ title_tag(T('access-denied'), False) }}{% endblock %} + +{% block content %} +
    +

    {{ T('access-denied') }}

    + +
    +

    {{ T('access-denied-text') }}.

    +
    +
    +{% endblock %} diff --git a/salvi/templates/404.html b/salvi/templates/404.html new file mode 100644 index 0000000..1aa3c14 --- /dev/null +++ b/salvi/templates/404.html @@ -0,0 +1,14 @@ +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} + +{% block title %}{{ title_tag( T('not-found'), False) }}{% endblock %} + +{% block content %} +
    +

    {{ T('not-found') }}

    + +
    +

    {{ T('not-found-text').format(request.path) }}.

    +
    +
    +{% endblock %} diff --git a/templates/internalservererror.jinja2 b/salvi/templates/500.html similarity index 50% rename from templates/internalservererror.jinja2 rename to salvi/templates/500.html index 01d6143..d2ae36d 100644 --- a/templates/internalservererror.jinja2 +++ b/salvi/templates/500.html @@ -1,10 +1,11 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}% _ % - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('% _ %', False) }}{% endblock %} {% block content %}
    -

    Internal Server Error

    +

    % _ %

    We’re sorry, an unexpected error occurred. Try refreshing the page.

    diff --git a/templates/administration.jinja2 b/salvi/templates/administration.html similarity index 75% rename from templates/administration.jinja2 rename to salvi/templates/administration.html index 2e86d73..81f4611 100644 --- a/templates/administration.jinja2 +++ b/salvi/templates/administration.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Administrative tools — {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('Administrative tools') }}{% endblock %} {% block content %}
    diff --git a/templates/backlinks.jinja2 b/salvi/templates/backlinks.html similarity index 75% rename from templates/backlinks.jinja2 rename to salvi/templates/backlinks.html index 12189d5..d93d80c 100644 --- a/templates/backlinks.jinja2 +++ b/salvi/templates/backlinks.html @@ -1,9 +1,8 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Pages linking to “{{ p.title }}” - {{ app_name }}{% endblock %} - -{% block meta %} - +{% block title %} +{{ title_tag('Pages linking to “' + p.title '”', False) }} {% endblock %} {% block content %} @@ -24,8 +23,7 @@

    {{ T("backlinks-empty") }}

    {% endif %}
    - - +

    {{ T("back-to") }} {{ p.title }}.

    {% endblock %} diff --git a/salvi/templates/base.html b/salvi/templates/base.html new file mode 100644 index 0000000..998968e --- /dev/null +++ b/salvi/templates/base.html @@ -0,0 +1,83 @@ + + + + + + + {% block title %} + {{ app_name }} + {% endblock %} + + + {{ theme_style_tag() }} + + + {% block json_info %}{% endblock %} + {% block ldjson %}{% endblock %} + + +
    + +
    + {% for msg in get_flashed_messages() %} +
    {{ icon('info') }} {{ msg }}
    + {% endfor %} + {% block content %}{% endblock %} + {% block toc %}{% endblock %} +
    +
    + + + {% if not g.no_user %} + + {% endif %} + + +
    + + {% block scripts %}{% endblock %} + + diff --git a/templates/calendar.jinja2 b/salvi/templates/calendar.html similarity index 83% rename from templates/calendar.jinja2 rename to salvi/templates/calendar.html index 85c44a5..7b13d90 100644 --- a/templates/calendar.jinja2 +++ b/salvi/templates/calendar.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Calendar – {{ app_name }}{% endblock %} +{% block title %}{{ title_tag(T('calendar')) }}{% endblock %} {% block content %}
    @@ -33,4 +34,4 @@

    -{% endblock %} \ No newline at end of file +{% endblock %} diff --git a/templates/contributions.jinja2 b/salvi/templates/contributions.html similarity index 50% rename from templates/contributions.jinja2 rename to salvi/templates/contributions.html index 59986a9..4b9db84 100644 --- a/templates/contributions.jinja2 +++ b/salvi/templates/contributions.html @@ -1,22 +1,22 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Contributions of {{ u.username }} - {{ app_name }}{% endblock %} - -{% block meta %} - +{% block title %} +{{ title_tag('Contributions of ' + u.username , False) }} {% endblock %} + {% block content %}

    {{ u.username }}

    Contributions

    -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    +

    Showing results {{ contributions.page * 20 - 19 }} to {{ min(contributions.page * 20, contributions.total) }} of {{ total_count }} total.

    -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/templates/easter.jinja2 b/salvi/templates/easter.html similarity index 86% rename from templates/easter.jinja2 rename to salvi/templates/easter.html index 54a4568..4c145a6 100644 --- a/templates/easter.jinja2 +++ b/salvi/templates/easter.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}{{ T('easter-date-calc') }} - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag(T('easter-date-calc')) }}{% endblock %} {% block content %}

    {{ T('easter-date-calc') }}

    diff --git a/templates/edit.jinja2 b/salvi/templates/edit.html similarity index 94% rename from templates/edit.jinja2 rename to salvi/templates/edit.html index 09cd0bb..a030c6f 100644 --- a/templates/edit.jinja2 +++ b/salvi/templates/edit.html @@ -1,20 +1,13 @@ -{% extends "base.jinja2" %} - +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} {% block title %} -{% if pl_title %}Editing “{{ pl_title }}” -{% else %}Edit note{% endif %} - - {{ app_name }} -{% endblock %} - -{% block meta %} - +{{ title_tag("Editing “{{ pl_title }}”" if pl_title else "Edit note", False) }} {% endblock %} {% block json_info %}{% endblock %} {% block content %} - {% if preview %}

    {{ pl_title }}

    @@ -30,7 +23,6 @@
    {% endif %} -
    @@ -83,7 +75,6 @@ {% endif %}
    - {% endblock %} {% block scripts %} diff --git a/templates/exportpages.jinja2 b/salvi/templates/exportpages.html similarity index 84% rename from templates/exportpages.jinja2 rename to salvi/templates/exportpages.html index ffb1df1..c925fb1 100644 --- a/templates/exportpages.jinja2 +++ b/salvi/templates/exportpages.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Export pages - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('Export pages', False) }}{% endblock %} {% block content %}

    Export pages

    @@ -24,3 +25,4 @@
    {% endblock %} + diff --git a/templates/history.jinja2 b/salvi/templates/history.html similarity index 62% rename from templates/history.jinja2 rename to salvi/templates/history.html index e7e9914..13de1e7 100644 --- a/templates/history.jinja2 +++ b/salvi/templates/history.html @@ -1,10 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Page history - {{ app_name }}{% endblock %} - -{% block meta %} - -{% endblock %} +{% block title %}{{ title_tag(T('note-history'), False) }}{% endblock %} {% block content %}
    @@ -19,17 +16,17 @@ #{{ rev.id }} · {{ rev.pub_date.strftime("%B %-d, %Y %H:%M:%S") }} - {% if rev.comment %} - “{{ rev.comment }}” - {% endif %} - + + {% if rev.comment %} + “{{ rev.comment }}” + {% endif %} by {% if rev.user_id %} - + {{ rev.user.username }} {% else %} - <Unknown User> + unknown user {% endif %} {% endfor %} @@ -38,4 +35,4 @@

    {{ T("back-to") }} {{ p.title }}.

    -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/templates/home.jinja2 b/salvi/templates/home.html similarity index 59% rename from templates/home.jinja2 rename to salvi/templates/home.html index e0d69c0..dd7435c 100644 --- a/templates/home.jinja2 +++ b/salvi/templates/home.html @@ -1,6 +1,8 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} +{% from "macros/nl.html" import nl_list with context %} -{% block title %}{{ T('homepage') }} - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag(T('homepage')) }}{% endblock %} {% block content %}
    @@ -15,12 +17,9 @@

    {{ T('latest-notes') }}

    -
    +
    - {% from "macros/nl.jinja2" import nl_list with context %} - {{ nl_list(new_notes) }} + {{ nl_list(new_notes, is_main=True) }} -
    - {% endblock %} diff --git a/templates/importpages.jinja2 b/salvi/templates/importpages.html similarity index 83% rename from templates/importpages.jinja2 rename to salvi/templates/importpages.html index 0008985..c467d67 100644 --- a/templates/importpages.jinja2 +++ b/salvi/templates/importpages.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Import pages - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('Import pages', False) }}{% endblock %} {% block content %}

    Import pages

    diff --git a/templates/contactnova/list.jinja2 b/salvi/templates/kt_list.html similarity index 98% rename from templates/contactnova/list.jinja2 rename to salvi/templates/kt_list.html index 5289f64..276370b 100644 --- a/templates/contactnova/list.jinja2 +++ b/salvi/templates/kt_list.html @@ -1,4 +1,4 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} {% block title %}List of contacts – {{ app_name }}{% endblock %} diff --git a/templates/contactnova/new.jinja2 b/salvi/templates/kt_new.html similarity index 98% rename from templates/contactnova/new.jinja2 rename to salvi/templates/kt_new.html index 5608dc1..89e17c8 100644 --- a/templates/contactnova/new.jinja2 +++ b/salvi/templates/kt_new.html @@ -1,4 +1,4 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} {% block title %}Create new contact – {{ app_name }}{% endblock %} diff --git a/templates/contactnova/single.jinja2 b/salvi/templates/kt_single.html similarity index 95% rename from templates/contactnova/single.jinja2 rename to salvi/templates/kt_single.html index ff7a1df..3e77462 100644 --- a/templates/contactnova/single.jinja2 +++ b/salvi/templates/kt_single.html @@ -1,4 +1,4 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} {% block title %}Contact {{ p.code }} – {{ app_name }}{% endblock %} diff --git a/salvi/templates/listrecent.html b/salvi/templates/listrecent.html new file mode 100644 index 0000000..1a9cb1b --- /dev/null +++ b/salvi/templates/listrecent.html @@ -0,0 +1,15 @@ +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} +{% from "macros/nl.html" import nl_list with context %} + +{% block title %}{{ title_tag('Notes by date') }}{% endblock %} + +{% block content %} +
    +

    Notes by date

    + +
    + {{ nl_list(notes) }} +
    +
    +{% endblock %} diff --git a/templates/listtag.jinja2 b/salvi/templates/listtag.html similarity index 68% rename from templates/listtag.jinja2 rename to salvi/templates/listtag.html index 2f6b6c5..bb0c26a 100644 --- a/templates/listtag.jinja2 +++ b/salvi/templates/listtag.html @@ -1,6 +1,8 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} +{% from "macros/nl.html" import nl_list with context %} -{% block title %}Notes tagged #{{ tagname }} - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('Notes tagged #' + tagname ) }}{% endblock %} {% block content %}
    @@ -9,13 +11,12 @@
    {% if total_count > 0 %} - {% from "macros/nl.jinja2" import nl_list with context %} {{ nl_list(tagged_notes, page_n=page_n, total_count=total_count, hl_tags=(tagname,), other_url='tags/' + tagname) }} {% else %}

    {{ T('notes-tagged-empty') }}

    {% endif %}
    - +
    {% endblock %} diff --git a/salvi/templates/login.html b/salvi/templates/login.html new file mode 100644 index 0000000..29c1b2e --- /dev/null +++ b/salvi/templates/login.html @@ -0,0 +1,32 @@ +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} + +{% block title %}{{ title_tag(T('login'), False) }}{% endblock %} + +{% block content %} +

    {{ T('login') }}

    + +
    +
    + +
    + + +
    +
    + + +
    +
    + + +
    +
    + +
    +
    + +

    {{ T('no-account-sign-up') }} {{ T("sign-up") }}

    +
    + +{% endblock %} \ No newline at end of file diff --git a/extensions/__init__.py b/salvi/templates/macros/icon.html similarity index 100% rename from extensions/__init__.py rename to salvi/templates/macros/icon.html diff --git a/templates/macros/nl.jinja2 b/salvi/templates/macros/nl.html similarity index 88% rename from templates/macros/nl.jinja2 rename to salvi/templates/macros/nl.html index 5956fc8..e448d66 100644 --- a/templates/macros/nl.jinja2 +++ b/salvi/templates/macros/nl.html @@ -1,11 +1,19 @@ + + {# Recommendations: Always import this macro with context, otherwise it fails. It depends on a couple context-defined functions. #} -{% macro nl_list(l, page_n=None, total_count=None, hl_tags=(), hl_calendar=None, other_url='p/most_recent') %} -{% if page_n and total_count %} +{# TODO rewrite to fit! #} +{# TODO rewrite to fit! #} + +{% macro nl_list(l, hl_tags=(), hl_calendar=None, other_url='p/most_recent', is_main = False) %} +{% set page_n = l.page %} +{% set total_count = l.total %} + +{% if not is_main %}

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    @@ -52,7 +60,7 @@ {% endfor %} - {% if page_n is none %} + {% if is_main %}
  • {{ T('show-all') }}
  • {% elif page_n <= (total_count - 1) // 20 %}
  • Next page »
  • diff --git a/salvi/templates/macros/title.html b/salvi/templates/macros/title.html new file mode 100644 index 0000000..95b9b13 --- /dev/null +++ b/salvi/templates/macros/title.html @@ -0,0 +1,17 @@ + + +{% macro title_tag(name, robots=True) %} + +{%- if name -%} +{{ name }} | {{ app_name }} +{%- else -%} +{{ app_name }} +{%- endif -%} + +{% if robots %} + +{% else %} + +{% endif %} + +{% endmacro %} \ No newline at end of file diff --git a/templates/manageaccounts.jinja2 b/salvi/templates/manageaccounts.html similarity index 68% rename from templates/manageaccounts.jinja2 rename to salvi/templates/manageaccounts.html index 8cce7c9..fa59027 100644 --- a/templates/manageaccounts.jinja2 +++ b/salvi/templates/manageaccounts.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Manage accounts - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('Manage accounts', False) }}{% endblock %} {% block content %}

    Manage accounts

    @@ -12,14 +13,14 @@ Beware: you are managing sensitive informations.

    -

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} of {{ total_count }} total.

    +

    Showing results {{ users.page * 20 - 19 }} to {{ min(users.page * 20, users.total) }} of {{ users.total }} total.

      - {% if page_n > 1 %} -
    • « Previous page
    • + {% if users.has_prev %} +
    • « Previous page
    • {% endif %} {% for u in users %} @@ -33,7 +34,7 @@ - Groups:
        - {% for ug in u.groups %} + {% for ug in u.groups() %}
      • {{ ug.name }}
      • {% endfor %}
      @@ -43,8 +44,8 @@ {% endfor %} - {% if page_n < total_count // 20 %} -
    • Next page »
    • + {% if users.has_next %} +
    • Next page »
    • {% endif %}
    diff --git a/templates/month.jinja2 b/salvi/templates/month.html similarity index 82% rename from templates/month.jinja2 rename to salvi/templates/month.html index 3b28a70..b60f234 100644 --- a/templates/month.jinja2 +++ b/salvi/templates/month.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}{{ d.strftime("%B %Y") }} – {{ app_name }}{% endblock %} +{% block title %}{{ title_tag(d.strftime("%B %Y")) }}{% endblock %} {% block content %}
    @@ -39,12 +40,12 @@

    {{ d.year }}

    {% endblock %} \ No newline at end of file diff --git a/templates/privacy.jinja2 b/salvi/templates/privacy.html similarity index 91% rename from templates/privacy.jinja2 rename to salvi/templates/privacy.html index c7cf33a..545fd66 100644 --- a/templates/privacy.jinja2 +++ b/salvi/templates/privacy.html @@ -1,10 +1,9 @@ -{% extends "base.jinja2" %} - -{% block title %}Privacy Policy — {{ app_name }}{% endblock %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} +{% block title %}{{ title_tag('Privacy Policy') }}{% endblock %} {% block content %} -

    Privacy Policy

    diff --git a/salvi/templates/register.html b/salvi/templates/register.html new file mode 100644 index 0000000..c1a1c57 --- /dev/null +++ b/salvi/templates/register.html @@ -0,0 +1,41 @@ +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} + +{% block title %}{{ title_tag(T('sign-up')) }}{% endblock %} + +{% block content %} +
    +

    {{ T('sign-up') }}

    + +
    + + +
    + + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    + + +
    +
    + +
    + + +

    {{ T('already-have-account') }} {{ T("login") }}

    +
    +
    +{% endblock %} \ No newline at end of file diff --git a/templates/rules.jinja2 b/salvi/templates/rules.html similarity index 91% rename from templates/rules.jinja2 rename to salvi/templates/rules.html index 4ba30af..a2aa57d 100644 --- a/templates/rules.jinja2 +++ b/salvi/templates/rules.html @@ -1,12 +1,15 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Content Policy — {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('Content Policy') }}{% endblock %} {% block content %}

    Content Policy

    {% filter markdown %} +__THIS POLICY IS OUTDATED AND SUPERSEDED. SEE __ + These are the Rules of {{ app_name }}. {{ app_name }} is a Free Speech environment. However, in order to ensure diff --git a/templates/search.jinja2 b/salvi/templates/search.html similarity index 67% rename from templates/search.jinja2 rename to salvi/templates/search.html index 29059d2..5ad36b5 100644 --- a/templates/search.jinja2 +++ b/salvi/templates/search.html @@ -1,10 +1,11 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}{% if q %}Search results for "{{ q }}"{% else %}Search{% endif %} - {{ app_name }}{% endblock %} +{% block title %}{{ title_tag(T('search-results', "“" + q + "”") if q else T('search'), False) }}{% endblock %} {% block content %}
    -

    Search

    +

    {{ T('search') }}

    @@ -20,16 +21,16 @@
    {% if results %} -

    Search results for {{ q }}

    +

    {{ T('search-results') }} {{ q }}

    {% from "macros/nl.jinja2" import nl_list with context %} {{ nl_list(results, other_url=None) }} {% elif q %}

    {{ T('search-no-results') }} {{ q }}

    {% else %} -

    Please note that search queries do not search for page text.

    +

    Please note that search queries do not search for page text.

    {% endif %}
    -{% endblock %} +{% endblock %} \ No newline at end of file diff --git a/salvi/templates/stats.html b/salvi/templates/stats.html new file mode 100644 index 0000000..b9859f5 --- /dev/null +++ b/salvi/templates/stats.html @@ -0,0 +1,23 @@ +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} + +{% block title %}{{ title_tag('Statistics') }}{% endblock %} + +{% block content %} +
    +

    Statistics

    + +
    +
      +
    • {{ T("notes-count") }}: {{ notes_count }}
    • +
    • {{ T("notes-count-with-url") }}: {{ notes_with_url }}
    • +
    • {{ T("revision-count") }}: {{ revision_count }}
    • +
    • {{ T("revision-count-per-page") }}: {{ (revision_count / notes_count)|round(2) }}
    • +
    • {{ T('users-count') }}: {{ users_count }}
    • +
    • {{ T('groups-count') }}: {{ groups_count }}
    • +
    +
    +
    +{% block content %} + + diff --git a/templates/terms.jinja2 b/salvi/templates/terms.html similarity index 97% rename from templates/terms.jinja2 rename to salvi/templates/terms.html index 69c9360..b262dc5 100644 --- a/templates/terms.jinja2 +++ b/salvi/templates/terms.html @@ -1,6 +1,7 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}Terms of Service — {{ app_name }}{% endblock %} +{% block title %}{{ title_tag('Terms of Service') }}{% endblock %} {% block content %}

    Terms of Service

    @@ -142,4 +143,5 @@ If there is any inconsistency between these terms and any translation into other {% endfilter %}
    -{% endblock %} \ No newline at end of file +{% endblock %} + diff --git a/templates/view.jinja2 b/salvi/templates/view.html similarity index 91% rename from templates/view.jinja2 rename to salvi/templates/view.html index 389c64f..0467c8f 100644 --- a/templates/view.jinja2 +++ b/salvi/templates/view.html @@ -1,9 +1,10 @@ -{% extends "base.jinja2" %} +{% extends "base.html" %} +{% from "macros/title.html" import title_tag with context %} -{% block title %}{{ p.title }} - {{ app_name }}{% endblock %} - -{% block meta %} +{% block title %} +{{ title_tag(p.title) }} + {% endblock %} @@ -62,10 +63,10 @@
    {% endif %} - {% endblock %} {% block actions %} +{# This block for the actions at the __bottom__. #} {{ T('action-edit') }} - {{ T('action-history') }} - Backlinks - diff --git a/templates/viewold.jinja2 b/salvi/templates/viewold.html similarity index 65% rename from templates/viewold.jinja2 rename to salvi/templates/viewold.html index 0ea4139..d888cfb 100644 --- a/templates/viewold.jinja2 +++ b/salvi/templates/viewold.html @@ -1,4 +1,4 @@ -{% extends "view.jinja2" %} +{% extends "view.html" %} {% block meta %} @@ -6,8 +6,10 @@ {% block history_nav %}
    -

    {{ T('old-revision-notice') }} +

    + {{ T('old-revision-notice') }} - (ID #{{ rev.id }}). Show latest

    + (ID #{{ rev.id }}). Show latest +

    {% endblock %} diff --git a/salvi/transfer.py b/salvi/transfer.py new file mode 100644 index 0000000..fa7428b --- /dev/null +++ b/salvi/transfer.py @@ -0,0 +1,104 @@ +""" +Utilities for import / export +""" + +import datetime +import json +import sys +from typing import Iterable +from sqlalchemy import insert, select +from suou import want_datetime, want_isodate + +from .models import Page, PageRevision, PageText, db + + +class Exporter(object): + root: dict + + def __init__(self): + self.root = {'pages': [], 'users': {}} + def add_page(self, p: Page, include_history=True, include_users=False): + pobj = {} + pobj['title'] = p.title + pobj['url'] = p.url + pobj['tags'] = [tag.name for tag in p.tags] + pobj['calendar'] = want_isodate(p.calendar) if p.calendar else None + pobj['flags'] = p.flags + if include_users: + pobj['owner'] = p.owner_id + hist = [] + for rev in (p.revisions if include_history else [p.latest]): + revobj = {} + revobj['text'] = rev.text + revobj['timestamp'] = rev.pub_date.timestamp() + if include_users: + revobj['user'] = rev.user_id + if rev.user_id not in self.root['users']: + ## PageRevision.user_info() was never implemented + self.root['users'][rev.user_id] = None#rev.user_info() + else: + revobj['user'] = None + revobj['comment'] = rev.comment + revobj['length'] = rev.length + hist.append(revobj) + pobj['history'] = hist + self.root['pages'].append(pobj) + def add_page_list(self, pl: Iterable[Page], include_history=True, include_users=False): + for p in pl: + self.add_page(p, include_history=include_history, include_users=include_users) + def export(self): + return json.dumps(self.root) + + +class Importer(object): + def __init__(self, dump, *, overwrite_urls = True): + self.root = json.loads(dump) + self.owner = None + self.overwrite_urls = overwrite_urls + def claim(self, owner): + self.owner = owner + def execute(self): + no_pages = 0 + no_revs = 0 + for pobj in self.root['pages']: + purl = pobj.get("url") + try: + if purl: + p2 = db.session.execute(select(Page).where(Page.url == purl)).scalar() + if p2: + p2.url = None + db.session.add(p2) + + p = db.session.execute(insert(Page).values( + url = purl if self.overwrite_urls else None, + title = pobj['title'], + calendar = want_datetime(pobj["calendar"]) if 'calendar' in pobj else None, + owner = self.owner.id, + flags = pobj.get('flags'), + touched = datetime.datetime.now() + ).returning(Page)) + p.change_tags(pobj.get('tags')) + no_pages += 1 + + for revobj in pobj['history']: + textref = PageText.create_content( + revobj['text'] + ) + + db.session.execute(insert(PageRevision).values( + page = p, + user_id = self.owner.id, + textref = textref, + comment = revobj.get('comment'), + pub_date = want_datetime(revobj['timestamp']), + length = revobj['length'] + )) + no_revs += 1 + + # one commit per page + db.session.commit() + except Exception as e: + db.session.rollback() + sys.excepthook(*sys.exc_info()) + continue + return no_pages, no_revs diff --git a/salvi/utils.py b/salvi/utils.py new file mode 100644 index 0000000..7c89072 --- /dev/null +++ b/salvi/utils.py @@ -0,0 +1,35 @@ +""" +Miscellaneous utilities. + +--- + +(C) 2020-2025 Sakuragasaki46. +See LICENSE for copying info. +""" + +import re +from suou import deprecated + +from .constants import SLUG_RE + +@deprecated('use suou.makelist() instead') +def makelist(l): + if isinstance(l, (str, bytes, bytearray)): + return [l] + elif hasattr(l, '__iter__'): + return list(l) + elif l: + return [l] + else: + return [] + + + +def parse_tag_list(s: str) -> list[str]: + l = [x.strip().lower().replace(' ', '-').replace('_', '-').lstrip('#') + for x in s.split(',') if x] + if any(not re.fullmatch(SLUG_RE, x) for x in l): + raise ValueError('illegal characters in tags') + return l + + diff --git a/strings.csv b/strings.csv deleted file mode 100644 index af52858..0000000 --- a/strings.csv +++ /dev/null @@ -1,29 +0,0 @@ -welcome,Welcome to {0}!,Benvenuti in {0}! -homepage,Homepage,Pagina iniziale -latest-notes,Latest notes,Note più recenti -latest-uploads,Latest uploads,Caricamenti più recenti -new-note,New note,Crea nota -upload-file,Upload file,Carica immagine -easter-date-calc,Easter date calculation,Calcolo della data di Pasqua -easter,Easter,Pasqua -other-dates,Other dates,Altre date -jump-to-actions,Jump to actions,Salta alle azioni -last-changed,Last changed,Ultima modifica -page-id,Page ID,ID pagina -action-edit,Edit,Modifica -action-history,History,Cronologia -tags,Tags,Etichette -old-revision-notice,Showing an old revision of the page as of,"È mostrata una revisione vecchia della pagina, risalente al" -notes-tagged,Notes tagged,Note con etichetta -include-tags,Include tags,Includi etichette -notes-tagged-empty,None found :(,Non c’è nulla :( -search-no-results,No results for,Nessun risultato per -random-page,Random page,Pagina casuale -search,Search,Cerca -year,Year,Anno -calculate,Calculate,Calcola -show-all,Show all,Mostra tutto -just-now,just now,poco fa -n-minutes-ago,{0} minutes ago,{0} minuti fa -n-hours-ago,{0} hours ago,{0} ore fa -n-days-ago,{0} days ago,{0} giorni fa diff --git a/templates/badrequest.jinja2 b/templates/badrequest.jinja2 deleted file mode 100644 index 380edba..0000000 --- a/templates/badrequest.jinja2 +++ /dev/null @@ -1,13 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Bad Request - {{ app_name }}{% endblock %} - -{% block content %} -
    -

    Bad request

    - -
    -

    You sent a request the server can’t understand. If you entered the URL manually please check your spelling and try again.

    -
    -
    -{% endblock %} diff --git a/templates/base.jinja2 b/templates/base.jinja2 deleted file mode 100644 index 18890f4..0000000 --- a/templates/base.jinja2 +++ /dev/null @@ -1,62 +0,0 @@ - - - - {% block title %}{{ app_name }}{% endblock %} - - - {% block meta %}{% endblock %} - - - {% if material_icons_url %} - - {% else %} - - {% endif %} - {% block json_info %}{% endblock %} - {% block ldjson %}{% endblock %} - - -
    -
    - {{ app_name }} -   - - -
    -
    - {% for msg in get_flashed_messages() %} -
    {{ msg }}
    - {% endfor %} - {% block content %}{% endblock %} - {% block toc %}{% endblock %} -
    -
    - - - - -
    - - {% block scripts %}{% endblock %} - - diff --git a/templates/circles/add.jinja2 b/templates/circles/add.jinja2 deleted file mode 100644 index 90e76cc..0000000 --- a/templates/circles/add.jinja2 +++ /dev/null @@ -1,58 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Circles – {{ app_name }}{% endblock %} - -{% block content %} - -
    - {% if returnto %}{% endif %} -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    - -
    - -{% if not pl %} -

    Looking for mass addition? Try CSV adding form instead.

    -{% endif %} - -{% endblock %} diff --git a/templates/circles/csv.jinja2 b/templates/circles/csv.jinja2 deleted file mode 100644 index b1b3e49..0000000 --- a/templates/circles/csv.jinja2 +++ /dev/null @@ -1,21 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Circles – {{ app_name }}{% endblock %} - -{% block content %} - -
    -

    Enter the contacts you want to bulk add, in comma-separated values (CSV) format, one by line.
    Order matters.

    - - - -
    - - -{% endblock %} diff --git a/templates/circles/list.jinja2 b/templates/circles/list.jinja2 deleted file mode 100644 index 4f9884e..0000000 --- a/templates/circles/list.jinja2 +++ /dev/null @@ -1,65 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Circles – {{ app_name }}{% endblock %} - -{% block content %} -

    Showing: {{ cat }}

    - -
    - Show by: -

    Type: - {% set typ_list = [ - 'INTJ', 'INTP', 'INFJ', 'INFP', 'ENTJ', 'ENTP', - 'ENFJ', 'ENFP', 'ISTJ', 'ISTP', 'ISFJ', 'ISFP', - 'ESTJ', 'ESTP', 'ESFJ', 'ESFP'] %} - {% for t in typ_list %} - {{ t }} · - {% endfor %} -

    -

    Status: - {% set typ_list = ['Green', 'Yellow', 'Orange', 'Red'] %} - {% for t in typ_list %} - {{ t }} · - {% endfor %} -

    -

    Area: - {% for t in range(1, 13) %} - {{ t }} · - {% endfor %} - Unassigned -

    -

    Stats · Add new.

    - - -{% if count > people.count() %} -

    Showing {{ people.count() }} people of {{ count }} total.

    -{% if count > pageno * 50 %} -

    Next page{% if pageno > 1 %} · Prev page{% endif %}

    -{% elif pageno > 1 %} -Prev page

    -{% endif %} -{% else %} -

    {{ count }} people.

    -{% endif %} - -
      - {% for p in people %} -
    • - {{ p.code }} - {{ p.display_name }} – {{ p.type }} – {% if p.area %}Area {{ p.area }}{% else %}No area{% endif %} (edit) -
    • - {% endfor %} -
    - -{% if count > people.count() %} -

    Showing {{ people.count() }} people of {{ count }} total.

    -{% if count > pageno * 50 %} -

    Next page{% if pageno > 1 %} · Prev page{% endif %}

    -{% elif pageno > 1 %} -Prev page

    -{% endif %} -{% else %} -

    {{ count }} people.

    -{% endif %} - -{% endblock %} diff --git a/templates/circles/stats.jinja2 b/templates/circles/stats.jinja2 deleted file mode 100644 index 4fdff58..0000000 --- a/templates/circles/stats.jinja2 +++ /dev/null @@ -1,30 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Circles – {{ app_name }}{% endblock %} - -{% block content %} -

    Stats

    - -
      -
    • All people: {{ count }}
    • -
    • People by type:
    • -
        - {% for k in typed_count.items() %} -
      • {{ k[0] }}: {{ k[1] }}
      • - {% endfor %} -
      -
    • People by status zone:
    • -
        - {% for k in status_count.items() %} -
      • {{ k[0] }}: {{ k[1] }}
      • - {% endfor %} -
      -
    • People by area:
    • -
        - {% for k in area_count.items() %} -
      • Area {{ k[0] }}: {{ k[1] }}
      • - {% endfor %} -
      • No area: {{ no_area_count }}
      • -
      -
    -{% endblock %} diff --git a/templates/forbidden.jinja2 b/templates/forbidden.jinja2 deleted file mode 100644 index 2f039df..0000000 --- a/templates/forbidden.jinja2 +++ /dev/null @@ -1,13 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Access Denied - {{ app_name }}{% endblock %} - -{% block content %} -
    -

    Forbidden

    - -
    -

    You don’t have permission to access this resource.

    -
    -
    -{% endblock %} diff --git a/templates/includes/nl_item.jinja2 b/templates/includes/nl_item.jinja2 deleted file mode 100644 index f372285..0000000 --- a/templates/includes/nl_item.jinja2 +++ /dev/null @@ -1,25 +0,0 @@ -{# DEPRECATED #} -{# Use "macros/nl.jinja2" instead. #} - - -
    DEPRECATED
    -

    - {{ n.title }} -

    -

    {{ n.short_desc() }}

    -

    Tags: - {% for tag in n.tags %} - {% set tn = tag.name %} - {% if hl_tag_name and tn == hl_tag_name %} - #{{ tn }} - {% else %} - #{{ tn }} - {% endif %} - {% endfor %} -

    -{% if n.calendar %} -

    - calendar_today - -

    -{% endif %} diff --git a/templates/leaderboard.jinja2 b/templates/leaderboard.jinja2 deleted file mode 100644 index 38aaa37..0000000 --- a/templates/leaderboard.jinja2 +++ /dev/null @@ -1,40 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Best pages - {{ app_name }}{% endblock %} - -{% block meta %} - -{% endblock %} - -{% block content %} -

    Best pages

    - -
    - - - - - - - - - - - - - {% set counters = namespace(row = 0) %} - {% for p, score, bklinks, fwlinks, length in pages %} - - {% set counters.row = counters.row + 1 %} - - - - - - - - {% endfor %} - -
    #Page NameScoreLenBLFL
    {{ counters.row }}{{ p.title }} (#{{ p.id }}){{ score }}{{ length }}{{ bklinks }}{{ fwlinks }}
    -
    -{% endblock %} diff --git a/templates/listrecent.jinja2 b/templates/listrecent.jinja2 deleted file mode 100644 index 1da0196..0000000 --- a/templates/listrecent.jinja2 +++ /dev/null @@ -1,12 +0,0 @@ -{% extends "base.jinja2" %} - -{% block content %} -
    -

    Notes by date

    - -
    - {% from "macros/nl.jinja2" import nl_list with context %} - {{ nl_list(notes, page_n=page_n, total_count=total_count) }} -
    -
    -{% endblock %} diff --git a/templates/login.jinja2 b/templates/login.jinja2 deleted file mode 100644 index 706aca7..0000000 --- a/templates/login.jinja2 +++ /dev/null @@ -1,35 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}{{ T('login') }} – {{ app_name }}{% endblock %} - -{% block content %} -

    {{ T('login') }}

    - -
    -
    - -
    - - -
    -
    - - -
    -
    - - -
    -
    - -
    -
    -
    - -

    {{ T('no-account-sign-up') }} {{ T("sign-up") }}

    -{% endblock %} \ No newline at end of file diff --git a/templates/notfound.jinja2 b/templates/notfound.jinja2 deleted file mode 100644 index 93c5af7..0000000 --- a/templates/notfound.jinja2 +++ /dev/null @@ -1,13 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}{{ T('not-found') }} - {{ app_name }}{% endblock %} - -{% block content %} -
    -

    {{ T('not-found') }}

    - -
    -

    {{ T('not-found-text-1') }} {{ request.path }} {{ T('not-found-text-2') }}.

    -
    -
    -{% endblock %} diff --git a/templates/register.jinja2 b/templates/register.jinja2 deleted file mode 100644 index 821b406..0000000 --- a/templates/register.jinja2 +++ /dev/null @@ -1,39 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}{{ T('sign-up') }} – {{ app_name }}{% endblock %} - -{% block content %} -

    {{ T('sign-up') }}

    - -
    -
    - -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - - -
    -
    - -
    -
    - -

    {{ T('already-have-account') }} {{ T("login") }}

    -
    - -{% endblock %} \ No newline at end of file diff --git a/templates/stats.jinja2 b/templates/stats.jinja2 deleted file mode 100644 index 88511af..0000000 --- a/templates/stats.jinja2 +++ /dev/null @@ -1,18 +0,0 @@ -{% extends "base.jinja2" %} - -{% block title %}Statistics - {{ app_name }}{% endblock %} - -{% block content %} -

    Statistics

    - -
    -
      -
    • {{ T("notes-count") }}: {{ notes_count }}
    • -
    • {{ T("notes-count-with-url") }}: {{ notes_with_url }}
    • -
    • {{ T("revision-count") }}: {{ revision_count }}
    • -
    • {{ T("revision-count-per-page") }}: {{ (revision_count / notes_count)|round(2) }}
    • -
    • {{ T('users-count') }}: {{ users_count }}
    • -
    • {{ T('groups-count') }}: {{ groups_count }}
    • -
    -
    -{% endblock %} diff --git a/templates/upload.jinja2 b/templates/upload.jinja2 deleted file mode 100644 index 12bf89b..0000000 --- a/templates/upload.jinja2 +++ /dev/null @@ -1,23 +0,0 @@ -{% extends "base.jinja2" %} - -{% block content %} -
    -

    Upload new file

    - -

    Uploads are no more supported. Please use this syntax instead for external images: ![alt text](https://www.example.com/path/to/image.jpg).

    - -
    -
    - - -
    -
    - - -
    -
    - -
    -
    -
    -{% endblock %} From 899bf785226b9c8860f24a63ea9ebae29ebe8ad8 Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Fri, 5 Sep 2025 22:23:27 +0200 Subject: [PATCH 44/51] change string order in i18n --- i18n/salvi.fr.json | 11 +++-- i18n/salvi.it.json | 101 +++++++++++++++++++++++---------------------- i18n/salvi.ru.json | 1 + 3 files changed, 57 insertions(+), 56 deletions(-) diff --git a/i18n/salvi.fr.json b/i18n/salvi.fr.json index 9a63a86..6a95dd0 100644 --- a/i18n/salvi.fr.json +++ b/i18n/salvi.fr.json @@ -1,13 +1,12 @@ { "fr": { - "welcome": "Bienvenue à {0}!", + "easter": "Pâques", + "easter-date-calc": "Calculer la date de Pâques", "homepage": "Page de démarrage", "latest-notes": "Dernières notes", "latest-uploads": "Derniers téléchargements", - "new-note": "Créer un note", - "upload-file": "Télécharger une image", - "easter-date-calc": "Calculer la date de Pâques", - "easter": "Pâques", - "other-dates": "Autres dates" + "new-note": "Créer un page", + "other-dates": "Autres dates", + "welcome": "Bienvenue à {0}!" } } \ No newline at end of file diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index dd4d4ca..711625d 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -1,71 +1,72 @@ { "it": { - "welcome": "Benvenuti in {0}!", - "homepage": "Pagina iniziale", - "latest-notes": "Pagine pi\u00f9 recenti", - "latest-uploads": "Caricamenti pi\u00f9 recenti", - "new-note": "Crea nota", - "upload-file": "Carica immagine", + "access-denied": "Accesso negato", + "access-denied-text": "Non hai il permesso per accedere a questa risorsa", + "action-edit": "Modifica", + "action-history": "Cronologia", + "action-view-source": "Visualizza sorgente", + "already-have-account": "Hai già un account?", + "back-to": "Torna a", + "backlinks": "Collegamenti in entrata", + "backlinks-empty": "Nessuna altra pagina punta qui. Questa pagina è orfana?", + "calculate": "Calcola", + "calendar": "Calendario", + "confirm-password": "Conferma password", "easter-date-calc": "Calcolo della data di Pasqua", "easter": "Pasqua", - "other-dates": "Altre date", - "jump-to-actions": "Salta alle azioni", - "last-changed": "Ultima modifica", - "page-id": "ID pagina", - "action-edit": "Modifica", - "action-view-source": "Visualizza sorgente", - "action-history": "Cronologia", - "tags": "Etichette", - "old-revision-notice": "\u00c8 mostrata una revisione vecchia della pagina, risalente al", - "notes-tagged": "Pagine con etichetta", + "email": "E-mail", + "groups-count": "Numero di gruppi utente", + "have-read-terms": "Ho letto i {0} e la {1}", + "homepage": "Pagina iniziale", "include-tags": "Includi etichette", - "notes-tagged-empty": "Non c\u2019\u00e8 nulla :(", - "search-no-results": "Nessun risultato per", - "random-page": "Pagina casuale", - "search": "Cerca", - "year": "Anno", - "month": "Mese", - "calculate": "Calcola", - "show-all": "Mostra tutto", + "input-tags": "Etichette (separate da virgola)", + "jump-to-actions": "Salta alle azioni", "just-now": "poco fa", + "last-changed": "Ultima modifica", + "latest-notes": "Pagine pi\u00f9 recenti", + "latest-uploads": "Caricamenti pi\u00f9 recenti", + "login": "Entra", + "logged-in-as": "Autenticato come", + "month": "Mese", "n-minutes-ago": "{0} minuti fa", "n-hours-ago": "{0} ore fa", "n-days-ago": "{0} giorni fa", - "backlinks": "Collegamenti in entrata", - "backlinks-empty": "Nessuna altra pagina punta qui. Questa pagina è orfana?", - "back-to": "Torna a", - "login": "Entra", - "username": "Nome utente", - "password": "Password", + "new-note": "Crea nota", "no-account-sign-up": "Non hai un account?", - "sign-up": "Registrati", + "no-tags": "Nessuna etichetta", "not-found": "Non trovato", + "not-found-text": "La pagina con url {0} non esiste", "not-found-text-1": "La pagina con url", "not-found-text-2": "non esiste", - "not-found-text": "La pagina con url {0} non esiste", - "access-denied": "Accesso negato", - "access-denied-text": "Non hai il permesso per accedere a questa risorsa", - "users-count": "Numero di utenti", + "not-logged-in": "Non autenticato", + "note-history": "Cronologia della pagina", "notes-count": "Numero di pagine", "notes-count-with-url": "Numero di pagine con URL impostato", + "notes-month-empty": "Non c\u2019\u00e8 nulla :(", + "notes-tagged": "Pagine con etichetta", + "notes-tagged-empty": "Non c\u2019\u00e8 nulla :(", + "old-revision-notice": "\u00c8 mostrata una revisione vecchia della pagina, risalente al", + "optional": "opzionale", + "other-dates": "Altre date", + "page-created": "Pagina creata", + "owner": "Proprietario", + "page-id": "ID pagina", + "password": "Password", + "privacy-policy": "Politica sulla riservatezza", + "random-page": "Pagina casuale", + "remember-me-for": "Ricordami per", "revision-count": "Numero di revisioni", "revision-count-per-page": "Media di revisioni per pagina", - "remember-me-for": "Ricordami per", - "confirm-password": "Conferma password", - "email": "E-mail", - "optional": "opzionale", - "have-read-terms": "Ho letto i {0} e la {1}", + "search": "Cerca", + "search-no-results": "Nessun risultato per", + "show-all": "Mostra tutto", + "sign-up": "Registrati", + "tags": "Etichette", "terms-of-service": "Termini di Servizio", - "privacy-policy": "Politica sulla riservatezza", - "already-have-account": "Hai già un account?", - "logged-in-as": "Autenticato come", - "not-logged-in": "Non autenticato", - "owner": "Proprietario", + "username": "Nome utente", + "users-count": "Numero di utenti", + "welcome": "Benvenuti in {0}!", "write-a-comment": "Scrivi un commento…", - "input-tags": "Etichette (separate da virgola)", - "no-tags": "Nessuna etichetta", - "notes-month-empty": "Non c\u2019\u00e8 nulla :(", - "calendar": "Calendario", - "groups-count": "Numero di gruppi utente" + "year": "Anno" } } \ No newline at end of file diff --git a/i18n/salvi.ru.json b/i18n/salvi.ru.json index bea96b7..ab209e3 100644 --- a/i18n/salvi.ru.json +++ b/i18n/salvi.ru.json @@ -1,5 +1,6 @@ { "ru":{ + "new-note": "Новая страница", "welcome": "Добро пожаловать в {0}!" } } \ No newline at end of file From f37be35648ff8fe5434a01cf929ce2fb1d6bdf98 Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Fri, 5 Sep 2025 23:56:22 +0200 Subject: [PATCH 45/51] fix bugs in templates --- salvi/templates/base.html | 3 +-- salvi/templates/listtag.html | 4 ++-- salvi/templates/macros/nl.html | 4 ++-- 3 files changed, 5 insertions(+), 6 deletions(-) diff --git a/salvi/templates/base.html b/salvi/templates/base.html index 998968e..9f3deb1 100644 --- a/salvi/templates/base.html +++ b/salvi/templates/base.html @@ -1,8 +1,7 @@ - - + {% block title %} {{ app_name }} diff --git a/salvi/templates/listtag.html b/salvi/templates/listtag.html index bb0c26a..a69a93b 100644 --- a/salvi/templates/listtag.html +++ b/salvi/templates/listtag.html @@ -10,8 +10,8 @@
    {{ T('notes-tagged') }}
    - {% if total_count > 0 %} - {{ nl_list(tagged_notes, page_n=page_n, total_count=total_count, hl_tags=(tagname,), other_url='tags/' + tagname) }} + {% if tagged_notes.total > 0 %} + {{ nl_list(tagged_notes, hl_tags=(tagname,), other_url='tags/' + tagname) }} {% else %}

    {{ T('notes-tagged-empty') }}

    {% endif %} diff --git a/salvi/templates/macros/nl.html b/salvi/templates/macros/nl.html index e448d66..a4355fd 100644 --- a/salvi/templates/macros/nl.html +++ b/salvi/templates/macros/nl.html @@ -16,7 +16,7 @@ {% if not is_main %}

    Showing results {{ page_n * 20 - 19 }} to {{ min(page_n * 20, total_count) }} - of {{ total_count }} total.

    + of {{ l.total }} total.

    {% endif %}
      @@ -62,7 +62,7 @@ {% if is_main %}
    • {{ T('show-all') }}
    • - {% elif page_n <= (total_count - 1) // 20 %} + {% elif page_n <= (l.total - 1) // 20 %}
    • Next page »
    • {% endif %}
    From e1392ce088a6b216f17929bd70eb4fef583cfb6e Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Sun, 7 Sep 2025 22:06:08 +0200 Subject: [PATCH 46/51] fix user group membership not being assigned --- salvi/models.py | 4 ++-- salvi/routes/accounts.py | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/salvi/models.py b/salvi/models.py index 67d51aa..ac26b2c 100644 --- a/salvi/models.py +++ b/salvi/models.py @@ -644,8 +644,8 @@ def create_first_user(): permissions = int(default_permissions) ).returning(UserGroup)).scalar() db.session.execute(insert(UserGroupMembership).values( - user = ua, - group = ug + user_id = ua.id, + group_id = ug.id )) db.session.commit() print('Installed successfully!') diff --git a/salvi/routes/accounts.py b/salvi/routes/accounts.py index 4847418..56c45f5 100644 --- a/salvi/routes/accounts.py +++ b/salvi/routes/accounts.py @@ -78,8 +78,8 @@ def register(): join_date = datetime.datetime.now() ).returning(User)).scalar() db.session.execute(insert(UserGroupMembership).values( - user = u, - group = UserGroup.get_default() + user_id = u.id, + group_id = UserGroup.get_default().id )) db.session.commit() From 3c6d52ed280720c320701f2f355ac140e2e6ca4d Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Thu, 11 Sep 2025 21:13:06 +0200 Subject: [PATCH 47/51] 1.0.0 release + update libsuou version --- pyproject.toml | 2 +- salvi/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6f11f74..ffd0921 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -15,7 +15,7 @@ dependencies = [ "Flask-WTF", "python-dotenv>=1.0.0", "pymysql", - "sakuragasaki46-suou>=0.5.0" + "sakuragasaki46-suou>=0.6.0" ] requires-python = ">=3.10" classifiers = [ diff --git a/salvi/__init__.py b/salvi/__init__.py index 54d50d4..f6ada0d 100644 --- a/salvi/__init__.py +++ b/salvi/__init__.py @@ -8,7 +8,7 @@ Pages are stored in SQLite/MySQL databases. Markdown is used for text formatting. ''' -__version__ = '1.0.0-dev35' +__version__ = '1.0.0' from flask import ( Flask, abort, flash, g, jsonify, make_response, redirect, From b5112c65653a0ca1275eb60cfe09a051840c3edf Mon Sep 17 00:00:00 2001 From: Yusur Princeps Date: Sat, 13 Sep 2025 00:13:15 +0200 Subject: [PATCH 48/51] code cleanup + aesthetic changes --- CHANGELOG.md | 8 +++- i18n/salvi.en.json | 8 ++++ i18n/salvi.it.json | 1 + salvi/__init__.py | 18 +++----- salvi/constants.py | 4 +- salvi/models.py | 72 ++++------------------------- salvi/renderer.py | 2 +- salvi/templates/400.html | 2 +- salvi/templates/base.html | 3 +- salvi/templates/calendar.html | 6 +-- salvi/templates/kt_list.html | 59 ----------------------- salvi/templates/kt_new.html | 72 ----------------------------- salvi/templates/kt_single.html | 24 ---------- salvi/templates/listrecent.html | 4 +- salvi/templates/manageaccounts.html | 6 +-- salvi/templates/search.html | 2 +- 16 files changed, 46 insertions(+), 245 deletions(-) delete mode 100644 salvi/templates/kt_list.html delete mode 100644 salvi/templates/kt_new.html delete mode 100644 salvi/templates/kt_single.html diff --git a/CHANGELOG.md b/CHANGELOG.md index 0b40585..96f6c7a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # What’s New +## 1.1.0 + ++ **Deprecated** several configuration values ~ ++ Removed permanently the remains of extensions. ++ I18n improvements. + ## 1.0.0 + **BREAKING CHANGES AHEAD**! @@ -11,7 +17,7 @@ + Switched to `pyproject.toml`. `requirements.txt` has been sunset. + Switched to the Apache License; the old license text is moved to `LICENSE.0_9` + Added color themes! This is a breaking (but trivial) aesthetic change. Default theme is 'Miku' (aquamarine green). -+ Extensions have been removed. They never had a clear, usable, public API in the first place. ++ Extensions **have been removed**. They never had a clear, usable, public API in the first place. ## 0.9 diff --git a/i18n/salvi.en.json b/i18n/salvi.en.json index 2376fe5..996c8a6 100644 --- a/i18n/salvi.en.json +++ b/i18n/salvi.en.json @@ -9,6 +9,7 @@ "back-to": "Back to", "backlinks": "Backlinks", "backlinks-empty": "No other pages linking here. Is this page orphan?", + "bad-request": "Bad request", "calculate": "Calculate", "calendar": "Calendar", "confirm-password": "Confirm password", @@ -18,6 +19,8 @@ "groups-count": "User group count", "have-read-terms": "I have read {0} and {1}", "homepage": "Homepage", + "in-the-future": "in the future", + "in-the-past": "in the past", "include-tags": "Include tags", "input-tags": "Tags (comma separated)", "jump-to-actions": "Jump to actions", @@ -27,6 +30,7 @@ "latest-uploads": "Latest uploads", "logged-in-as": "Logged in as", "login": "Log in", + "manage-accounts": "Manage accounts", "month": "Month", "n-days-ago": "{0} days ago", "n-hours-ago": "{0} hours ago", @@ -40,6 +44,7 @@ "not-found-text-2": "does not exist.", "not-logged-in": "Not logged in", "note-history": "Page history", + "notes-by-date": "Pages by date", "notes-count": "Number of pages", "notes-count-with-url": "Number of pages with URL set", "notes-month-empty": "None found :(", @@ -62,10 +67,13 @@ "search-results": "Search results for", "search-no-results": "No results for", "show-all": "Show all", + "show-more-years": "Show more years", "sign-up": "Sign up", "tags": "Tags", "terms-of-service": "Terms of Service", "upload-file": "Upload file", + "user-generated-warning": "Any content in this site is user-generated and", + "user-liability-warning": "each user is responsible for what they publish", "username": "Username", "users-count": "Number of users", "welcome": "Welcome to {0}!", diff --git a/i18n/salvi.it.json b/i18n/salvi.it.json index 711625d..6580835 100644 --- a/i18n/salvi.it.json +++ b/i18n/salvi.it.json @@ -9,6 +9,7 @@ "back-to": "Torna a", "backlinks": "Collegamenti in entrata", "backlinks-empty": "Nessuna altra pagina punta qui. Questa pagina è orfana?", + "bad-request": "Richiesta non conforme", "calculate": "Calcola", "calendar": "Calendario", "confirm-password": "Conferma password", diff --git a/salvi/__init__.py b/salvi/__init__.py index f6ada0d..f6c016a 100644 --- a/salvi/__init__.py +++ b/salvi/__init__.py @@ -8,27 +8,21 @@ Pages are stored in SQLite/MySQL databases. Markdown is used for text formatting. ''' -__version__ = '1.0.0' +__version__ = '1.1.0-dev36' from flask import ( - Flask, abort, flash, g, jsonify, make_response, redirect, + Flask, g, make_response, redirect, request, render_template, send_from_directory ) from markupsafe import Markup -from flask_login import LoginManager, login_user, logout_user, current_user, login_required +from flask_login import LoginManager from flask_wtf import CSRFProtect -from flask_sqlalchemy import SQLAlchemy from sqlalchemy import select from suou.configparse import ConfigOptions, ConfigParserConfigSource, ConfigValue from suou.flask import add_context_from_config -from werkzeug.security import generate_password_hash, check_password_hash from werkzeug.routing import BaseConverter -import datetime, hashlib, html, importlib, json, markdown, os, random, \ - re, sys, warnings -from functools import lru_cache, partial -from urllib.parse import quote -import gzip -from getpass import getpass +import html, os +from functools import partial from configparser import ConfigParser import dotenv @@ -40,11 +34,13 @@ dotenv.load_dotenv(os.path.join(APP_BASE_DIR, '.env')) class SiteConfig(ConfigOptions): app_name = ConfigValue(default='Salvi', legacy_src='site.title', public=True) + ## v--- will change to server_name in 2.0.0 domain_name = ConfigValue(public=True) database_url = ConfigValue(legacy_src='database.url', required=True) secret_key = ConfigValue(required=True) default_group = ConfigValue(prefix='salvi_', cast=int, default=1) material_icons_url = ConfigValue(default='https://fonts.googleapis.com/icon?family=Material+Icons', public=True) + # v--- pending deprecation default_items_per_page = ConfigValue(prefix='salvi_', legacy_src='appearance.items_per_page', default=20, cast=int) ... diff --git a/salvi/constants.py b/salvi/constants.py index a5b425b..d37ff4f 100644 --- a/salvi/constants.py +++ b/salvi/constants.py @@ -9,7 +9,7 @@ PING_RE = r'(? 0 - else: - return _BitComparator(self._column, self._flag) - def __set__(self, obj, val): - if obj: - orig = getattr(obj, self._column.name) - if val: - orig |= self._flag - else: - orig &= ~(self._flag) - setattr(obj, self._column.name, orig) - else: - raise NotImplementedError - - # Helper for interactive session management -from suou.sqlalchemy import create_session, declarative_base +from suou.sqlalchemy import create_session, declarative_base, BitSelector CSI = create_session_interactively = partial(create_session, app_config.database_url) @@ -221,11 +170,11 @@ class Page(db.Model): calendar = Column(DateTime, index=True, nullable=True) owner_id = Column(Integer, ForeignKey('user.id'), nullable=True) flags = Column(BigInteger, default=0) - is_redirect = BitSelector(flags, 1) - is_sync = BitSelector(flags, 2) - is_math_enabled = BitSelector(flags, 4) - is_locked = BitSelector(flags, 8) - is_cw = BitSelector(flags, 16) + is_redirect: Mapped[bool] = BitSelector(flags, 1) + is_sync: Mapped[bool] = BitSelector(flags, 2) + is_math_enabled: Mapped[bool] = BitSelector(flags, 4) + is_locked: Mapped[bool] = BitSelector(flags, 8) + is_cw: Mapped[bool] = BitSelector(flags, 16) revisions: Relationship[List[PageRevision]] = relationship("PageRevision", back_populates = 'page') owner: Relationship[User] = relationship('User', back_populates = 'owned_pages') @@ -237,7 +186,7 @@ class Page(db.Model): def latest(self) -> PageRevision: return db.session.execute( - db.select(PageRevision).where(PageRevision.page_id == self.id).order_by(PageRevision.pub_date.desc()).limit(1) + select(PageRevision).where(PageRevision.page_id == self.id).order_by(PageRevision.pub_date.desc()).limit(1) ).scalar() def get_url(self): @@ -247,6 +196,7 @@ class Page(db.Model): def by_url(cls, url: str): return db.session.execute(db.select(Page).where(Page.url == url)).scalar() + @deprecated('usage of inefficient and deprecated remove_tags()') def short_desc(self) -> str: if self.is_cw: return '(Content Warning: we are not allowed to show a description.)' @@ -298,10 +248,6 @@ class Page(db.Model): tags = [x.name for x in self.tags] ) - @not_implemented - def ldjson(): - ... - @deprecated('meta name="keywords" is nowadays ignored by search engines') def seo_keywords(self): kw = [] diff --git a/salvi/renderer.py b/salvi/renderer.py index b286048..4d4df75 100644 --- a/salvi/renderer.py +++ b/salvi/renderer.py @@ -22,7 +22,7 @@ def md_and_toc(text) -> tuple[Markup, Any | None]: try: converter: markdown.Markdown = markdown.Markdown(extensions=extensions, extension_configs=extension_configs) markup = Markup(converter.convert(text)) - toc = converter.toc + toc = Markup(converter.toc) return markup, toc except Exception as e: return error_p('Error during rendering: {0}: {1}') diff --git a/salvi/templates/400.html b/salvi/templates/400.html index 1e02791..dc58259 100644 --- a/salvi/templates/400.html +++ b/salvi/templates/400.html @@ -1,7 +1,7 @@ {% extends "base.html" %} {% from "macros/title.html" import title_tag with context %} -{% block title %}{{ title_tag(T('Bad Request'), false) }}{% endblock %} +{% block title %}{{ title_tag(T('bad-request'), false) }}{% endblock %} {% block content %}
    diff --git a/salvi/templates/base.html b/salvi/templates/base.html index 9f3deb1..f3539d1 100644 --- a/salvi/templates/base.html +++ b/salvi/templates/base.html @@ -25,7 +25,6 @@ } {% block json_info %}{% endblock %} - {% block ldjson %}{% endblock %}
    @@ -63,7 +62,7 @@