From 2b46f5c81991057f14e5ce270fd0b0dc3bfc42c3 Mon Sep 17 00:00:00 2001 From: Aevann Date: Thu, 13 Jun 2024 22:57:02 +0300 Subject: [PATCH] fix bug with drafts + dont give effortpost notifs to jannies if post doesnt meet requirements --- files/classes/post.py | 25 +++++++++++++++++++++++++ files/routes/admin.py | 18 +----------------- files/routes/posts.py | 9 +++++++-- files/templates/post.html | 2 +- files/templates/post_listing.html | 2 +- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/files/classes/post.py b/files/classes/post.py index a3df8224a..ae1b59952 100644 --- a/files/classes/post.py +++ b/files/classes/post.py @@ -2,6 +2,7 @@ import random import time from urllib.parse import urlparse from flask import g +from bs4 import BeautifulSoup from sqlalchemy import Column, FetchedValue, ForeignKey from sqlalchemy.orm import deferred, relationship @@ -432,3 +433,27 @@ class Post(Base): return v.admin_level >= PERMS['POST_COMMENT_MODERATION'] else: return v.admin_level >= PERMS['POST_COMMENT_MODERATION'] or (v.mods_hole(self.hole)) or self.author_id == v.id + + @property + @lazy + def can_be_effortpost(self): + if SITE_NAME == 'WPD': + min_chars = 2000 + min_lines = 10 + else: + min_chars = 3000 + min_lines = 20 + + if self.body_html.count('

') < min_lines: + return False + + soup = BeautifulSoup(self.body_html, 'lxml') + tags = soup.html.body.find_all(lambda tag: tag.name in {'p','ul','table'} and tag.text, recursive=False) + post_char_count = 0 + for tag in tags: + post_char_count += len(tag.text) + + if post_char_count < min_chars: + return False + + return True diff --git a/files/routes/admin.py b/files/routes/admin.py index 37720d39f..b63738c8b 100644 --- a/files/routes/admin.py +++ b/files/routes/admin.py @@ -2043,25 +2043,9 @@ def mark_effortpost(pid, v): if p.effortpost: abort(400, "Post is already marked as an effortpost!") - if SITE_NAME == 'WPD': - min_chars = 2000 - min_lines = 10 - else: - min_chars = 3000 - min_lines = 20 - - if p.body_html.count('

') < min_lines: + if not p.can_be_effortpost: abort(403, "Post is too short!") - soup = BeautifulSoup(p.body_html, 'lxml') - tags = soup.html.body.find_all(lambda tag: tag.name in {'p','ul','table'} and tag.text, recursive=False) - post_char_count = 0 - for tag in tags: - post_char_count += len(tag.text) - - if post_char_count < min_chars: - abort(403, "Post is too short.") - p.effortpost = True g.db.add(p) diff --git a/files/routes/posts.py b/files/routes/posts.py index a7a43b52e..78a6a0965 100644 --- a/files/routes/posts.py +++ b/files/routes/posts.py @@ -92,9 +92,11 @@ def publish(pid, v): execute_snappy(p, v) - if p.effortpost and not (SITE_NAME == 'WPD' and v.truescore < 500): + if p.effortpost and not (SITE_NAME == 'WPD' and v.truescore < 500) and p.can_be_effortpost: body = f"@{v.username} has requested that {p.textlink} be marked as an effortpost!" alert_admins(body) + + p.effortpost = False return {"message": "Post has been published successfully!"} @@ -736,10 +738,13 @@ def submit_post(v, hole=None): generate_thumb = (not p.thumburl and p.url and p.domain != SITE) gevent.spawn(postprocess_post, p.url, p.body, p.body_html, p.id, generate_thumb, False) - if not p.draft and flag_effortpost and not (SITE_NAME == 'WPD' and v.truescore < 500): + if not p.draft and flag_effortpost and not (SITE_NAME == 'WPD' and v.truescore < 500) and p.can_be_effortpost: body = f"@{v.username} has requested that {p.textlink} be marked as an effortpost!" alert_admins(body) + if p.draft and flag_effortpost and not (SITE_NAME == 'WPD' and v.truescore < 500): + p.effortpost = True + if v.client: return p.json else: p.voted = 1 diff --git a/files/templates/post.html b/files/templates/post.html index be8883df6..1e88947e7 100644 --- a/files/templates/post.html +++ b/files/templates/post.html @@ -96,7 +96,7 @@ CHILD WARNING {% endif %} - {% if p.effortpost %} + {% if p.effortpost and not p.draft %} EFFORTPOST {% endif %} diff --git a/files/templates/post_listing.html b/files/templates/post_listing.html index 0f9493c12..db7b55954 100644 --- a/files/templates/post_listing.html +++ b/files/templates/post_listing.html @@ -95,7 +95,7 @@ CHILD WARNING {% endif %} - {% if p.effortpost %} + {% if p.effortpost and not p.draft %} EFFORTPOST {% endif %}