Login

Naked CSS Day TemplateTag

Author:
webology
Posted:
April 7, 2009
Language:
Python
Version:
1.2
Score:
4 (after 4 ratings)

This TemplateTag simply returns a True when it is Naked CSS Day allowing you to hide your CSS or display a custom message on that date. Allows allows for parameters should the date change (again).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    from django import template
    from datetime import datetime, timedelta

    register = template.Library()

    class NakedDayNode(template.Node):

        def __init__(self, params, varname):
            self.params = params
            self.varname = varname

        def render(self, context):
            naked_date = datetime(year=self.params['year'],
                month=self.params['month'], day=self.params['day'])
            start = naked_date - timedelta(hours=12)
            end = naked_date + timedelta(hours=36)
            now = datetime.utcnow()
            is_naked = now > start and now < end

            if self.varname:
                context[self.varname] = is_naked

            return ''


    def do_naked_day(parser, token):
        """
        Returns True or False if today is Naked CSS Day (2009-04-09ish) and spans
        ~48 hours. Optionally accepts ``year``, ``month``, and ``day`` values in
        case Naked CSS Day changes in the future. Optionally takes a template
        context with a variable containing that value, whose name is defined by
        the ``as`` clause.

        Usage::

            {% is_naked_day [year:integer] [month:integer] [day:integer] as [varname] %}

        Example::

            {% is_naked_day as is_naked %}
            {% if is_naked %}
            <!-- naked day has no styles -->
            {% else %}
            <link rel="stylesheet" type="text/css" href="/media/example.css" />
            {% endif %}

        Example with parameters::

            {% is_naked_day month:4 day:9 as is_naked %}
            {% if is_naked %}
            <!-- naked day has no styles -->
            {% else %}
            <link rel="stylesheet" type="text/css" href="/media/example.css" />
            {% endif %}

        Additional information:

        For more information on Naked CSS Day and to add your site to the list:
        http://naked.dustindiaz.com/

        """
        params = {
            'year': datetime.utcnow().year,
            'month': 4,
            'day': 9,
        }
        varname = None
        items = token.split_contents()

        for index, item in enumerate(items):
            if item == 'as':
                varname = items[index + 1]
            elif ':' in item:
                param, value = item.split(':')
                param, value = param.strip(), value.strip()
                if param in params:
                    if value[0] == '"':
                        value = value[1:-1]
                    try:
                        params[param] = int(value)
                    except ValueError:
                        raise template.TemplateSyntaxError(
                            'Parameter "%s" can only be an integer field.' % (param))

        return NakedDayNode(params, varname)

    register.tag('is_naked_day', do_naked_day)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.