Skip to content

Commit b2e8eb4

Browse files
committed
feat: add template tags
1 parent 89a6f59 commit b2e8eb4

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,7 @@
3838
"Programming Language :: Python :: 3.7",
3939
"Programming Language :: Python :: 3.8",
4040
"Programming Language :: Python :: 3.9",
41+
"Programming Language :: Python :: 3.10"
42+
"Programming Language :: Python :: 3.11"
4143
],
4244
)

test/templates/test/comments.html

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
{% load vote %}
2-
{% for comment in comments %}
3-
{{comment.content}} {% vote_exists comment user %}
4-
{% endfor %}
2+
<ol>
3+
{% for comment in comments %}
4+
<li>
5+
{{comment.content}} - up:{% vote_count comment "up" %} - down: {% vote_count comment "down" %} - exists_up:
6+
{%vote_exists comment user %} - exists_down: {% vote_exists comment user "down"%}
7+
</li>
8+
{% endfor %}
9+
</ol>

vote/templatetags/vote.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,26 @@
33
from django import template
44
from django.contrib.auth.models import AnonymousUser
55

6-
from vote.models import UP
6+
from vote.models import UP, DOWN
77

88
register = template.Library()
99

1010

1111
@register.simple_tag
12-
def vote_exists(model, user=AnonymousUser(), action=UP):
12+
def vote_exists(model, user=AnonymousUser(), action="UP"):
1313
if user.is_anonymous:
1414
return False
15+
if action.lower() == "up":
16+
action = UP
17+
else:
18+
action = DOWN
1519
return model.votes.exists(user.pk, action=action)
20+
21+
22+
@register.simple_tag
23+
def vote_count(model, action="UP"):
24+
if action.lower() == "up":
25+
action = UP
26+
else:
27+
action = DOWN
28+
return model.votes.count(action=action)

0 commit comments

Comments
 (0)