From 87c616e041857152e0d2e029d116bafce3d718ae Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 11 Jul 2019 16:00:32 +0300 Subject: [PATCH] shorten wiki url --- royalnet/database/tables/wikipages.py | 5 +++++ .../web/royalprints/wikiview/templates/wikiview_index.html | 2 +- royalnet/web/shortcuts.py | 4 ++-- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/royalnet/database/tables/wikipages.py b/royalnet/database/tables/wikipages.py index 914a59b8..0ea21aea 100644 --- a/royalnet/database/tables/wikipages.py +++ b/royalnet/database/tables/wikipages.py @@ -3,6 +3,7 @@ from sqlalchemy import Column, \ String from sqlalchemy.dialects.postgresql import UUID from sqlalchemy.ext.declarative import declared_attr +from royalnet.web.shortcuts import to_urluuid class WikiPage: @@ -31,3 +32,7 @@ class WikiPage: @declared_attr def css(self): return Column(String) + + @property + def page_short_id(self): + return to_urluuid(self.page_id) diff --git a/royalnet/web/royalprints/wikiview/templates/wikiview_index.html b/royalnet/web/royalprints/wikiview/templates/wikiview_index.html index 84a2bcb0..fe8a9969 100644 --- a/royalnet/web/royalprints/wikiview/templates/wikiview_index.html +++ b/royalnet/web/royalprints/wikiview/templates/wikiview_index.html @@ -19,7 +19,7 @@ {% endif %} diff --git a/royalnet/web/shortcuts.py b/royalnet/web/shortcuts.py index 96875205..3a2bd8e3 100644 --- a/royalnet/web/shortcuts.py +++ b/royalnet/web/shortcuts.py @@ -9,8 +9,8 @@ def error(code, reason): def to_urluuid(uuid: _uuid.UUID) -> str: """Return a base64 url-friendly short UUID.""" - return str(base64.urlsafe_b64encode(uuid.bytes), encoding="ascii") + return str(base64.urlsafe_b64encode(uuid.bytes), encoding="ascii").rstrip("=") def from_urluuid(b: str) -> _uuid.UUID: - return _uuid.UUID(bytes=base64.urlsafe_b64decode(bytes(b, encoding="ascii"))) + return _uuid.UUID(bytes=base64.urlsafe_b64decode(bytes(b + "==", encoding="ascii")))