X-Git-Url: https://git.mdrn.pl/prawokultury.git/blobdiff_plain/44090c986d9a16f6913047c25319c446bb9308ad..a55d79e4c27c893841712145e38d4a4a61e04b20:/prawokultury/helpers.py diff --git a/prawokultury/helpers.py b/prawokultury/helpers.py index aa8e3dd..9b943de 100644 --- a/prawokultury/helpers.py +++ b/prawokultury/helpers.py @@ -2,6 +2,8 @@ # This file is part of PrawoKultury, licensed under GNU Affero GPLv3 or later. # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information. # +from django.conf import settings +from django.http import HttpResponse, HttpResponseRedirect from textile import Textile @@ -22,8 +24,7 @@ def textile_restricted_pl(text): text, rel='nofollow') - -class LazyUGettextLazy(): +class LazyUGettextLazy(object): """You can use it to internationalize strings in settings. Just import this class as gettext. @@ -40,3 +41,37 @@ class LazyUGettextLazy(): LazyUGettextLazy._ = staticmethod(ugettext_lazy) LazyUGettextLazy.real = True return unicode(self._(self.text)) + + +class AppSettings(object): + """Allows specyfying custom settings for an app, with default values. + + Just subclass, set some properties and instantiate with a prefix. + Getting a SETTING from an instance will check for prefix_SETTING + in project settings if set, else take the default. The value will be + then filtered through _more_SETTING method, if there is one. + + """ + def __init__(self, prefix): + self._prefix = prefix + + def __getattribute__(self, name): + if name.startswith('_'): + return object.__getattribute__(self, name) + value = getattr(settings, + "%s_%s" % (self._prefix, name), + object.__getattribute__(self, name)) + more = "_more_%s" % name + if hasattr(self, more): + value = getattr(self, more)(value) + return value + + +def serve_file(url): + if settings.X_ACCEL_REDIRECT: + response = HttpResponse() + response['Content-Type'] = "" + response['X-Accel-Redirect'] = url + return response + else: + return HttpResponseRedirect(url)