aboutsummaryrefslogtreecommitdiff
path: root/stream/templatetags/media.py
blob: 2e8299f09b50c098860c6abc2e342aa3da4936a6 (plain)
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
#  gLifestream Copyright (C) 2009, 2010, 2013 Wojciech Polak
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 3 of the License, or (at your
#  option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License along
#  with this program.  If not, see <http://www.gnu.org/licenses/>.

import os
import hashlib
from django.conf import settings
from django import template
from django.template import Library

register = Library()


class MediaUrl (template.Node):
    def render(self, ctx):
        url = settings.MEDIA_URL
        if 'is_secure' in ctx and ctx['is_secure']:
            url = url.replace('http://', 'https://')
        return url


@register.tag
def static(parser, token):
    """Return the string contained in the setting MEDIA_URL."""
    return MediaUrl()


class MediaUrlHash (template.Node):
    def __init__(self, path):
        self.path = path
        self.hash = None
        try:
            f = open(os.path.join(settings.MEDIA_ROOT, path))
            self.hash = hashlib.md5(f.read()).hexdigest()[:5]
            f.close()
        except:
            pass

    def render(self, ctx):
        url = settings.MEDIA_URL
        if 'is_secure' in ctx and ctx['is_secure']:
            url = url.replace('http://', 'https://')
        url += self.path
        if self.hash:
            url += '?v=' + self.hash
        return url


@register.tag
def static_hash(parser, token):
    """Return a static URL for the given relative static file path."""
    try:
        tag_name, path = token.split_contents()
    except ValueError:
        raise template.TemplateSyntaxError, \
            "%r tag requires a single argument" % token.contents.split()[0]
    return MediaUrlHash(path)

Return to:

Send suggestions and report system problems to the System administrator.