fix bug and move render context partly to model

This commit is contained in:
justcool393 2023-03-09 07:22:01 -06:00
parent 9895fa1bba
commit fde6d8a67a
3 changed files with 35 additions and 21 deletions

View file

@ -1,7 +1,7 @@
from os import environ
import re
import time
from typing import Optional
from typing import Literal, Optional
from urllib.parse import urlencode, urlparse, parse_qs
from flask import *
from sqlalchemy import *
@ -15,6 +15,8 @@ from random import randint
from .votes import CommentVote
from math import floor
CommentRenderContext = Literal['comments', 'volunteer']
class Comment(Base):
__tablename__ = "comments"
@ -86,6 +88,29 @@ class Comment(Base):
comment_age_seconds = int(time.time()) - self.created_utc
comment_age_hours = comment_age_seconds / (60*60)
return comment_age_hours < app.config['SCORE_HIDING_TIME_HOURS']
@lazy
def _score_context_str(self, score_type:Literal['score', 'upvotes', 'downvotes'],
context:CommentRenderContext) -> str:
if self.is_message: return '' # don't show scores for messages
if context == 'volunteer': return '' # volunteer: hide scores
if self.should_hide_score: return '' # hide scores for new comments
if score_type == 'upvotes': return str(self.upvotes)
if score_type == 'score': return str(self.score)
if score_type == 'downvotes': return str(self.downvotes)
@lazy
def upvotes_str(self, context:CommentRenderContext) -> str:
return self._score_context_str('upvotes', context)
@lazy
def score_str(self, context:CommentRenderContext) -> str:
return self._score_context_str('score', context)
@lazy
def downvotes_str(self, context:CommentRenderContext) -> str:
return self._score_context_str('downvotes', context)
@property
@lazy
@ -472,7 +497,7 @@ class Comment(Base):
if not v: return -2
if v.id == self.author_id: return 1
return getattr(self, 'voted', 0)
@lazy
def active_flags(self, v):
return len(self.flags(v))

View file

@ -3,16 +3,12 @@
{# "level" represents the nested level in this call; always starts at 1 #}
{# this is distinct from "comment.level", which is the global depth of this comment #}
{% macro single_comment(c, level) %}
{% if should_hide_score or c.should_hide_score %}
{% set ups="" %}
{% set downs="" %}
{% set score="" %}
{% else %}
{% set ups=c.upvotes %}
{% set downs=c.downvotes %}
{% set score=c.score %}
{% endif %}
{%- if render_ctx is not defined -%}
{% set render_ctx = 'comments' %}
{%- endif -%}
{%- set ups = c.upvotes_str(render_ctx) -%}
{%- set score = c.score_str(render_ctx) -%}
{%- set downs = c.downvotes_str(render_ctx) -%}
{%- set replies = c.replies(v) -%}
@ -61,7 +57,7 @@
{% else %}
{%- set voted = c.vote_display(v) -%}
{%- set voted = c.voted_display(v) -%}
{% if standalone and level==1 %}
<div class="post-info post-row-cid-{{c.id}} mb-1 mr-2 {% if request.path == '/notifications' %}mt-5{% else %}mt-3{% endif %}">

View file

@ -6,16 +6,13 @@
<p class="text">Please rate these comments. You must choose a rating for each one. If you're undecided, just trust your gut!<p>
<p class="text"><a href="/rules">Here's a link to the full rules</a> if you need a reference. Open this in a new tab; you may not be able to return to this page otherwise.</p>
</div>
<hr />
{% for c in duty.comments() %}
<div>
<div class="comment">
{% with comments=[c] %}
{% set should_hide_username = true %}
{% set should_hide_score = true %}
{% set render_ctx = 'volunteer' %}
{% include "comments.html" %}
{% endwith %}
</div>
@ -52,11 +49,7 @@
</div>
</div>
</div>
<hr />
{% endfor %}
</div>
{% endmacro %}