43 lines
2.6 KiB
Python
43 lines
2.6 KiB
Python
"""Self-contained starter implementation; shared framework APIs remain dependencies."""
|
|
import time
|
|
from flask import Blueprint, current_app, flash, g, redirect, render_template, request, url_for
|
|
from web_framework.auth import can, store
|
|
from web_framework.config import safe_url
|
|
from web_framework.modules import Widget
|
|
from web_framework.plugins import Plugin, Setting
|
|
|
|
STARTED = time.monotonic()
|
|
|
|
def make_collection(plugin_id, name, kind="notes"):
|
|
blueprint = Blueprint("plugin_" + plugin_id.replace("-", "_"), __name__, template_folder="templates")
|
|
|
|
@blueprint.route("/", methods=["GET", "POST"])
|
|
def index():
|
|
if request.method == "POST":
|
|
if not can("content.write"):
|
|
from flask import abort
|
|
abort(403)
|
|
try:
|
|
if request.form.get("action") == "delete":
|
|
store().delete_item(plugin_id, request.form.get("item_id", ""))
|
|
else:
|
|
title = request.form.get("title", "").strip()
|
|
value = request.form.get("value", "").strip()
|
|
if not title or len(title) > 160 or len(value) > 10000:
|
|
raise ValueError("Add a title (up to 160 characters) and content (up to 10,000 characters).")
|
|
if kind == "bookmarks" and not safe_url(value):
|
|
raise ValueError("Bookmarks need a safe HTTP(S) or root-relative URL.")
|
|
store().put_item(plugin_id, {"title": title, "value": value})
|
|
store().audit(g.user["username"], "plugin.content.changed", plugin_id)
|
|
flash("Content updated.", "success")
|
|
except ValueError as error:
|
|
flash(str(error), "error")
|
|
return redirect(url_for(blueprint.name + ".index"))
|
|
return render_template(f"{plugin_id}/collection.html", title=name, collection=plugin_id, kind=kind, items=store().items(plugin_id), description=current_app.extensions["plugins"].configuration(plugin_id)["intro"])
|
|
|
|
return Plugin(plugin_id, name, version="0.2.1", settings_schema={"intro": Setting("Page introduction", default="A shared collection for your workspace.")}, description="Shared notes with persistent content." if kind == "notes" else "A shared collection of useful links.", blueprint=blueprint, endpoint=blueprint.name + ".index",
|
|
widgets={"summary": Widget(f"{plugin_id}/collection_summary.html", context=lambda config: {"items": store().items(plugin_id)[:5], "kind": kind, "endpoint": blueprint.name + ".index"})})
|
|
|
|
def make_bookmarks(plugin_id="bookmarks", name="Bookmarks"):
|
|
return make_collection(plugin_id, name, "bookmarks")
|