5877e0b1c75007ef54207cc6ae23525cc8e7850c
[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 textile import Textile
7
8
9 class TextilePL(Textile):
10     glyph_defaults = [(name, repl) 
11         for (name, repl) in Textile.glyph_defaults
12         if name != 'txt_quote_double_open']
13     glyph_defaults.append(('txt_quote_double_open', '„'))
14
15
16 def textile_pl(text):
17     return TextilePL().textile(text)
18
19
20 def textile_restricted_pl(text):
21     return TextilePL(restricted=True, lite=True,
22                    noimage=True, auto_link=False).textile(
23                         text, rel='nofollow')
24
25
26 class LazyUGettextLazy():
27     """You can use it to internationalize strings in settings.
28
29     Just import this class as gettext.
30     """
31     _ = lambda s: s
32     real = False
33
34     def __init__(self, text):
35         self.text = text
36
37     def __unicode__(self):
38         if not self.real:
39             from django.utils.translation import ugettext_lazy
40             LazyUGettextLazy._ = staticmethod(ugettext_lazy)
41             LazyUGettextLazy.real = True
42         return unicode(self._(self.text))
43
44
45 class AppSettings(object):
46     """Allows specyfying custom settings for an app, with default values.
47
48     Just subclass, set some properties and instantiate with a prefix.
49     Getting a SETTING from an instance will check for prefix_SETTING
50     in project settings if set, else take the default. The value will be
51     then filtered through _more_SETTING method, if there is one.
52
53     """
54     def __init__(self, prefix):
55         self._prefix = prefix
56
57     def __getattribute__(self, name):
58         if name.startswith('_'):
59             return object.__getattribute__(self, name)
60         value = getattr(settings,
61                          "%s_%s" % (self._prefix, name),
62                          object.__getattribute__(self, name))
63         more = "_more_%s" % name
64         if hasattr(self, more):
65             value = getattr(self, more)(value)
66         return value