From 7f87d357d23920e5f97f0a5c45ba9680ca134396 Mon Sep 17 00:00:00 2001 From: jordan Date: Sat, 9 Apr 2011 17:50:45 +0000 Subject: [PATCH] Creating a Random number template tag generator. We'll need this one in order to prevent the caching of the JS files in IE8/9. git-svn-id: http://svn.osqa.net/svnroot/osqa/trunk@951 0cfe37f9-358a-4d5e-be75-b63607b5c754 --- forum/templatetags/extra_tags.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/forum/templatetags/extra_tags.py b/forum/templatetags/extra_tags.py index 76935fe..0a3c421 100644 --- a/forum/templatetags/extra_tags.py +++ b/forum/templatetags/extra_tags.py @@ -5,6 +5,7 @@ import datetime import math import re import logging +import random from django import template from django.utils.encoding import smart_unicode from django.utils.safestring import mark_safe @@ -249,3 +250,27 @@ def do_declare(parser, token): nodelist = parser.parse(('enddeclare',)) parser.delete_first_token() return DeclareNode(nodelist) + +# Usage: {% random 1 999 %} +# Generates random number in the template +class RandomNumberNode(template.Node): + # We get the limiting numbers + def __init__(self, int_from, int_to): + self.int_from = int(int_from) + self.int_to = int(int_to) + + # We generate the random number using the standard python interface + def render(self, context): + return str(random.randint(self.int_from, self.int_to)) + +@register.tag(name="random") +def random_number(parser, token): + # Try to get the limiting numbers from the token + try: + tag_name, int_from, int_to = token.split_contents() + except ValueError: + # If we had no success -- raise an exception + raise template.TemplateSyntaxError, "%r tag requires exactly two arguments" % token.contents.split()[0] + + # Call the random Node + return RandomNumberNode(int_from, int_to) -- 2.39.5