snipt/snipts/templatetags/snipt_tags.py

35 lines
756 B
Python
Raw Normal View History

2012-04-12 14:28:30 -07:00
from django import template
2012-04-12 19:04:37 -07:00
from templatetag_sugar.register import tag
from templatetag_sugar.parser import Variable, Constant
2012-04-12 14:28:30 -07:00
from snipts.models import Favorite
2012-04-13 10:01:28 -07:00
import hashlib
2012-04-12 14:28:30 -07:00
register = template.Library()
2012-04-12 19:04:37 -07:00
@tag(register, [Constant('as'), Variable()])
def snipt_is_favorited_by_user(context, asvar):
user = context['request'].user
snipt = context['snipt']
is_favorited = False
if user.is_authenticated():
if snipt.user != user:
try:
is_favorited = Favorite.objects.get(snipt=snipt, user=user).id
except Favorite.DoesNotExist:
pass
context[asvar] = is_favorited
return ''
2012-04-13 10:01:28 -07:00
@register.filter
def md5(string):
return hashlib.md5(string.lower()).hexdigest()