django-allauth-ens/example/app/apps.py
2017-08-03 12:55:30 +02:00

73 lines
2.3 KiB
Python

from django.apps import AppConfig
from django.db.models.signals import post_migrate
def prepare_site(sender, **kwargs):
from django.contrib.sites.models import Site
Site.objects.filter(id=1).update(name="Demo Site", domain="localhost")
def prepare_superuser(sender, **kwargs):
from django.contrib.auth import get_user_model
User = get_user_model()
root, created = User.objects.get_or_create(
username='root',
defaults={
'is_staff': True,
'is_superuser': True,
},
)
if created:
root.set_password('root')
root.save()
print('Superuser created - Credentials: root:root')
def setup_dummy_social(sender, **kwargs):
from django.contrib.sites.models import Site
from django.utils.module_loading import import_string
from allauth.socialaccount.models import SocialApp
from allauth.socialaccount.providers import registry
need_credentials = [
'allauth.socialaccount.providers.oauth.provider.OAuthProvider',
'allauth.socialaccount.providers.oauth2.provider.OAuth2Provider',
]
classes = tuple([import_string(cls_path) for cls_path in need_credentials])
site = Site.objects.get_current()
dummy_installed = []
for provider in registry.get_list():
if isinstance(provider, classes):
try:
SocialApp.objects.get(provider=provider.id, sites=site)
except SocialApp.DoesNotExist:
app = SocialApp.objects.create(
provider=provider.id,
secret='secret',
client_id='client-id',
name='Dummy %s app' % provider.id,
)
app.sites.add(site)
dummy_installed.append(provider.id)
if dummy_installed:
print(
"Dummy application credentials installed for: %s.\n"
"Authentication via these providers will not work until you "
"configure proper credentials via the Django admin (`SocialApp` "
"model)."
% ', '.join(sorted(dummy_installed))
)
class BasicAppConfig(AppConfig):
name = 'app'
def ready(self):
post_migrate.connect(prepare_site, sender=self)
post_migrate.connect(prepare_superuser, sender=self)
post_migrate.connect(setup_dummy_social, sender=self)