From 49d707b9897c9d6a1d2a92483a968c2c81d5a909 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Canek=20Pel=C3=A1ez=20Vald=C3=A9s?= Date: Tue, 22 Oct 2024 16:32:24 -0600 Subject: [PATCH] Some fixes. --- fotos/templatetags/galeria.py | 17 ++++++++++++----- fotos/views.py | 21 ++++++++++++++------- 2 files changed, 26 insertions(+), 12 deletions(-) diff --git a/fotos/templatetags/galeria.py b/fotos/templatetags/galeria.py index ab5f410..c1cac72 100644 --- a/fotos/templatetags/galeria.py +++ b/fotos/templatetags/galeria.py @@ -74,10 +74,13 @@ def get_view_url(photo, user): @register.filter(name='comment_return') def comment_return(value): if value.endswith('.jpg'): - return value + '?moderation=true' + r = value + '?moderation=true' + return r + while '&' in value: + value = value.replace('&', '&') if '.jpg?' in value: i = value.index('.jpg?') + 5 - get = value[i:-1] + get = value[i:] parms = get.split('&') newparms = '' for parm in parms: @@ -85,9 +88,13 @@ def comment_return(value): newparms += (parm + '&') continue t = parm.split('=') - if t == 'moderation' or t == 'c': + if t[0] == 'moderation' or t[0] == 'c': continue newparms += (parm + '&') newparms = newparms[:-1] - return value[:i] + newparms - return value + '&moderation=true' + r = value[:i] + 'moderation=true' + if newparms != '': + r += ('&' + newparms) + return r + r = value + '&moderation=true' + return r diff --git a/fotos/views.py b/fotos/views.py index 82a6b10..dfff32b 100644 --- a/fotos/views.py +++ b/fotos/views.py @@ -98,6 +98,12 @@ def get_moderated_comments(): def get_approved_comments(): return Comment.objects.filter(is_public=True) +def get_comment_by_id(id): + try: + return Comment.objects.get(id=id) + except Exception: + return None + def index(request): link = message = '' page_title = GALERIA_INFO['title'] @@ -1001,21 +1007,22 @@ def comments(request): remove = int(request.GET.get('remove', '-1')) message = '' if approve != -1: - comment = Comment.objects.get(id=approve) - if not comment.is_public: + comment = get_comment_by_id(approve) + if comment and not comment.is_public: comment.is_public = True comment.save() message = _('Message approved') if moderate != -1: - comment = Comment.objects.get(id=moderate) - if comment.is_public: + comment = get_comment_by_id(moderate) + if comment and comment.is_public: comment.is_public = False comment.save() message = _('Message moderated') if remove != -1: - comment = Comment.objects.get(id=remove) - comment.delete() - message = _('Message removed') + comment = get_comment_by_id(remove) + if comment: + comment.delete() + message = _('Message removed') moderated = get_moderated_comments() approved = get_approved_comments() tab = 'moderated' if 'tab' not in request.GET else request.GET['tab'] -- GitLab