django-allauth-ens/example/app/apps.py

82 lines
2.6 KiB
Python
Raw Permalink Normal View History

2017-08-03 12:41:30 +02:00
from django.apps import AppConfig
from django.db.models.signals import post_migrate
from django.utils.module_loading import import_string
2017-08-03 12:41:30 +02:00
from allauth.socialaccount.providers import registry
2017-08-03 12:41:30 +02:00
def setup_site(sender, **kwargs):
2017-08-03 12:41:30 +02:00
from django.contrib.sites.models import Site
Site.objects.filter(id=1).update(name="Demo Site", domain="localhost")
def setup_users(sender, **kwargs):
2017-08-03 12:41:30 +02:00
from django.contrib.auth import get_user_model
User = get_user_model()
root, r_created = User.objects.get_or_create(username='root', defaults={
'email': 'root@mail.net',
'is_staff': True,
'is_superuser': True,
})
if r_created:
2017-08-03 12:41:30 +02:00
root.set_password('root')
root.save()
2017-08-03 12:55:30 +02:00
print('Superuser created - Credentials: root:root')
2017-08-03 12:41:30 +02:00
user, u_created = User.objects.get_or_create(username='user', defaults={
'email': 'user@mail.net',
})
if u_created:
user.set_password('user')
user.save()
print('User created - Credentials: user:user')
2017-08-03 12:41:30 +02:00
def setup_dummy_social(sender, **kwargs):
from django.contrib.sites.models import Site
from allauth.socialaccount.models import SocialApp
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(setup_site, sender=self)
2017-08-03 12:41:30 +02:00
post_migrate.connect(setup_dummy_social, sender=self)
post_migrate.connect(setup_users, sender=self)