You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB
Python

from __future__ import unicode_literals
default_app_config = 'nano.tools.apps.NanoToolsConfig'
import logging
import unicodedata
try: # py3
from itertools import zip_longest
except ImportError: # py2
from itertools import izip_longest as zip_longest
_LOG = logging.getLogger(__name__)
from django.apps import apps
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.shortcuts import render
from django.template import RequestContext, loader, Context
class SiteProfileNotAvailable(Exception):
pass
def nullfunction(return_this=None, *args, **kwargs):
return return_this
def pop_error(request):
error = request.session.get('error', None)
if 'error' in request.session:
del request.session['error']
return error
def asciify(string):
"""Convert unicode string to ascii, normalizing with NFKD
Strips away all non-ascii symbols"""
string = unicodedata.normalize('NFKD', string)
return string.encode('ascii', 'ignore').decode('ascii')
def grouper(n, iterable, fillvalue=None):
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(fillvalue=fillvalue, *args)
def get_profile_model(raise_on_error=True):
if not getattr(settings, 'AUTH_PROFILE_MODULE', False):
error = "AUTH_PROFILE_MODULE isn't set in the settings, couldn't fetch profile"
_LOG.error(error)
if raise_on_error:
raise SiteProfileNotAvailable(error)
return None
try:
app_label, model_name = settings.AUTH_PROFILE_MODULE.split('.')
model = apps.get_model(app_label, model_name)
except ImportError:
error = "Could not import the profile '%s' given in AUTH_PROFILE_MODULE" % settings.AUTH_PROFILE_MODULE
_LOG.error(error)
if raise_on_error:
raise SiteProfileNotAvailable(error)
return None
except ImproperlyConfigured:
error = "An unknown error happened while fetching the profile model"
if raise_on_error:
raise SiteProfileNotAvailable(error)
return None
return model
if 'nano.blog' in settings.INSTALLED_APPS:
try:
from nano.blog import add_entry_to_blog
except ImportError:
add_entry_to_blog = nullfunction
else:
add_entry_to_blog = None