Contact forms app.
[prawokultury.git] / prawokultury / helpers.py
1 # -*- coding: utf-8 -*-
2 # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later.
3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 #
5 from django.conf import settings
6 from django.http import HttpResponse, HttpResponseRedirect
7 from textile import Textile
8
9
10 class TextilePL(Textile):
11     glyph_defaults = [(name, repl) 
12         for (name, repl) in Textile.glyph_defaults
13         if name != 'txt_quote_double_open']
14     glyph_defaults.append(('txt_quote_double_open', '„'))
15
16
17 def textile_pl(text):
18     return TextilePL().textile(text)
19
20
21 def textile_restricted_pl(text):
22     return TextilePL(restricted=True, lite=True,
23                    noimage=True, auto_link=False).textile(
24                         text, rel='nofollow')
25
26
27 class LazyUGettextLazy(object):
28     """You can use it to internationalize strings in settings.
29
30     Just import this class as gettext.
31     """
32     _ = lambda s: s
33     real = False
34
35     def __init__(self, text):
36         self.text = text
37
38     def __unicode__(self):
39         if not self.real:
40             from django.utils.translation import ugettext_lazy
41             LazyUGettextLazy._ = staticmethod(ugettext_lazy)
42             LazyUGettextLazy.real = True
43         return unicode(self._(self.text))
44
45
46 class AppSettings(object):
47     """Allows specyfying custom settings for an app, with default values.
48
49     Just subclass, set some properties and instantiate with a prefix.
50     Getting a SETTING from an instance will check for prefix_SETTING
51     in project settings if set, else take the default. The value will be
52     then filtered through _more_SETTING method, if there is one.
53
54     """
55     def __init__(self, prefix):
56         self._prefix = prefix
57
58     def __getattribute__(self, name):
59         if name.startswith('_'):
60             return object.__getattribute__(self, name)
61         value = getattr(settings,
62                          "%s_%s" % (self._prefix, name),
63                          object.__getattribute__(self, name))
64         more = "_more_%s" % name
65         if hasattr(self, more):
66             value = getattr(self, more)(value)
67         return value
68
69
70 def serve_file(url):
71     if settings.X_ACCEL_REDIRECT:
72         response = HttpResponse()
73         response['Content-Type'] = ""
74         response['X-Accel-Redirect'] = url
75         return response
76     else:
77         return HttpResponseRedirect(url)