Skip to content

Commit

Permalink
feat: add template tags
Browse files Browse the repository at this point in the history
  • Loading branch information
shellfly committed Dec 17, 2022
1 parent 89a6f59 commit b2e8eb4
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,7 @@
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10"
"Programming Language :: Python :: 3.11"
],
)
11 changes: 8 additions & 3 deletions test/templates/test/comments.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{% load vote %}
{% for comment in comments %}
{{comment.content}} {% vote_exists comment user %}
{% endfor %}
<ol>
{% for comment in comments %}
<li>
{{comment.content}} - up:{% vote_count comment "up" %} - down: {% vote_count comment "down" %} - exists_up:
{%vote_exists comment user %} - exists_down: {% vote_exists comment user "down"%}
</li>
{% endfor %}
</ol>
17 changes: 15 additions & 2 deletions vote/templatetags/vote.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,26 @@
from django import template
from django.contrib.auth.models import AnonymousUser

from vote.models import UP
from vote.models import UP, DOWN

register = template.Library()


@register.simple_tag
def vote_exists(model, user=AnonymousUser(), action=UP):
def vote_exists(model, user=AnonymousUser(), action="UP"):
if user.is_anonymous:
return False
if action.lower() == "up":
action = UP
else:
action = DOWN
return model.votes.exists(user.pk, action=action)


@register.simple_tag
def vote_count(model, action="UP"):
if action.lower() == "up":
action = UP
else:
action = DOWN
return model.votes.count(action=action)

0 comments on commit b2e8eb4

Please sign in to comment.