Improve SEO + add perms helper

This commit is contained in:
Yusur 2023-07-12 11:58:19 +02:00
parent ea0ccc0d0d
commit 09bbbd74a4
3 changed files with 45 additions and 2 deletions

View file

@ -8,11 +8,12 @@
+ Added `restrictions` field to `User`. + Added `restrictions` field to `User`.
+ Pages now can have a Content Warning. It prevents them to show up in previews, and adds a + Pages now can have a Content Warning. It prevents them to show up in previews, and adds a
caution message when viewing them. caution message when viewing them.
+ SEO improvement: added `keywords` and `description` meta tags to viewing pages.
+ Added Terms, Privacy Policy and Rules. + Added Terms, Privacy Policy and Rules.
+ Changed user page URLs (contributions page) from `/u/user` to `/@user`. + 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/` is now a list of all managing options, including export/import and the brand new
`/manage/accounts`. `/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. + 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 + Now logged-in users have an “Edit” button below the first heading. All users can access page history
by clicking the last modified time. by clicking the last modified time.

39
app.py
View file

@ -178,6 +178,17 @@ class User(BaseModel):
UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group) UserGroup.select().join(UserGroupMembership, on=UserGroupMembership.group)
.where(UserGroupMembership.user == self) .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) # page perms (used as bitmasks)
@ -241,7 +252,7 @@ class Page(BaseModel):
return '/' + self.url + '/' if self.url else '/p/{}/'.format(self.id) return '/' + self.url + '/' if self.url else '/p/{}/'.format(self.id)
def short_desc(self): def short_desc(self):
if self.is_cw: if self.is_cw:
return '(Content Warning)' return '(Content Warning: we are not allowed to show a description.)'
full_text = self.latest.text full_text = self.latest.text
text = remove_tags(full_text, convert = not self.is_math_enabled and not _getconf('appearance', 'simple_remove_tags', False)) 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 '') 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: if self.is_locked and self.owner.id != user.id:
perm &= PERM_LOCK perm &= PERM_LOCK
return perm 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): class PageText(BaseModel):
@ -535,6 +557,18 @@ def init_db_and_create_first_user():
) )
print('Installed successfully!') 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 #### #### WIKI SYNTAX ####
def md_and_toc(text, expand_magic=False, toc=True, math=True): 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']) @app.route('/create/', methods=['GET', 'POST'])
@login_required @login_required
def create(): 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.method == 'POST':
if request.form.get('preview'): if request.form.get('preview'):
return savepoint(request.form, is_preview=True) return savepoint(request.form, is_preview=True)

View file

@ -2,6 +2,11 @@
{% block title %}{{ p.title }} - {{ app_name }}{% endblock %} {% block title %}{{ p.title }} - {{ app_name }}{% endblock %}
{% block meta %}
<meta name="description" content="{{ p.short_desc() }}" />
<meta name="keywords" content="{{ p.seo_keywords() }}" />
{% endblock %}
{% block json_info %}<script>window.page_info={{ p.js_info()|tojson|safe }};</script>{% endblock %} {% block json_info %}<script>window.page_info={{ p.js_info()|tojson|safe }};</script>{% 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(math = request.args.get('math') not in ['0', 'false', 'no', 'off']) %}