Naked CSS Day TemplateTag

 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. date range by mackenzie_kearl 6 years, 2 months ago
  2. Sumar dias habiles / Working days by javinievas 6 years, 7 months ago
  3. SelectDateWidget with format: day, month, year by zenx 4 years, 4 months ago
  4. Past days template filter by ramen 4 years, 5 months ago
  5. Format date range by bmihelac 5 years ago

Comments

(Forgotten your password?)