Source code for django_oneall.app
# -*- coding: utf-8 -*-
from datetime import timedelta
from django.conf import settings as django_settings
try:
from django.contrib.auth import get_user_model
except ImportError:
from django.db.models import get_user_model
[docs]class MissingOneAllSettings(KeyError):
def __init__(self, msg=None):
msg = 'Missing or invalid settings. Check django-oneall documentation for further info. ' + (msg or '')
super(MissingOneAllSettings, self).__init__(msg)
[docs]class AppSettings(object):
def __init__(self):
if not hasattr(django_settings, 'ONEALL'):
raise MissingOneAllSettings
self._settings = django_settings.ONEALL
if not isinstance(self._settings, dict):
raise MissingOneAllSettings
self._default_login_widget = {
None: "This is a dummy marker to indicate that the Django settings haven't been loaded yet.",
'providers': 'google facebook twitter openid'.split(),
}
self._default_share_widget = {
None: "This is a dummy marker to indicate that the Django settings haven't been loaded yet.",
'providers': 'linkedin twitter facebook'.split(),
}
@property
def credentials(self):
""" Provides the credentials dictionary for the OneAll connection. """
try:
return self._settings['credentials']
except KeyError:
raise MissingOneAllSettings('OneAll site credentials missing.')
@property
def share_widget(self):
""" Provides the share widget configuration. """
if None in self._default_share_widget:
del self._default_share_widget[None]
self._default_share_widget.update(self._settings.get('share_widget', {}))
return self._default_share_widget
@property
def store_user_info(self):
""" Whether to store personally identifiable information from users. Defaults to True. """
return bool(self._settings.get('store_user_info', True))
@property
def max_username_length(self):
"""
Maximum length of usernames that are generated by the authentication app. Defaults to 30, which is compatible
with the full Django 1.x branch.
When ``store_user_info`` is set to ``False``, this will be the exact username length of authenticated users,
and this calculated username will be used to determine whether that's a new user or not.
If you used django-oneall<1.3 with Django>=1.10, some of the users will be duplicate with the larger length.
Pseudorandom usernames, however, provide plenty of entropy at 30 characters, so we're restring to that at
django-oneall>=1.3. Verify your data so your existing user accounts are not mis-identified.
"""
user_model = get_user_model()
fld_len = user_model._meta.get_field('username').max_length
max_len = int(self._settings.get("max_username_length", 30))
if max_len > fld_len:
raise MissingOneAllSettings("OneAll setting 'max_username_length' is set to value %d, which is greater "
"than the database field's length: %d" % (max_len, fld_len))
return max_len
@property
def token_expiration(self):
""" The amount of time an e-mail login token is valid for. """
expires = self._settings.get('email_token_expiration_hours', 3)
return timedelta(hours=expires)
@property
def default_url(self):
""" Default redirect URL after login if ``next`` is not provided. This is just a standard Django setting. """
return django_settings.LOGIN_REDIRECT_URL # Can be assumed to be set.
settings = AppSettings()