-
Notifications
You must be signed in to change notification settings - Fork 46
/
configure.py
executable file
·136 lines (103 loc) · 3.54 KB
/
configure.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python
"""Help new users configure the database for use with social networks.
"""
import os
from datetime import datetime
# Fix Python 2.x.
try:
input = raw_input
except NameError:
pass
import django
from django.conf import settings
from django.core.management.utils import get_random_secret_key
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
# TEMPLATES structure changed in Django 1.10
settings.configure(
DEBUG=True,
TEMPLATES=[dict(
# DEBUG = True,
BACKEND='django.template.backends.django.DjangoTemplates',
APP_DIRS=True,
DIRS=[
os.path.join(BASE_DIR, 'config'),
],
)],
)
try:
django.setup() # for Django >= 1.7
except AttributeError:
pass # must be < Django 1.7
from django.template.loader import get_template
from django.template import engines # Django >= 1.11
commands_template = engines['django'].from_string("""
Run these commands:
python manage.py makemigrations allauthdemo_auth
python manage.py migrate
python manage.py createsuperuser # optional
{% if facebook %}# Facebook
python manage.py set_auth_provider facebook {{facebook.client_id}} {{facebook.secret}}{% endif %}
{% if google %}# Google
python manage.py set_auth_provider google {{google.client_id}} {{google.secret}}{% endif %}
If you have other providers you can add them like:
python manage.py set_auth_provider <name e.g. 'google'> <client id> <secret>
Finally:
python manage.py runserver
Load http://127.0.0.1:8000/ in your browser.
""")
settings_template = get_template("settings.template.py")
def heading(text):
text = text.strip()
line = '-' * len(text)
print("\n%s\n%s\n%s\n" % (line, text, line))
def ask_yes_no(msg):
msg = "\n" + msg.strip() + ' (y/n): '
confirm = input(msg)
while True:
confirm = confirm.strip().lower()
if confirm not in ('yes', 'y', 'no', 'n'):
confirm = input('Please enter y or n (or "yes" or "no"): ')
continue
return confirm in ('yes', 'y')
def ask_text(need, default=None):
need = need.strip()
if default:
msg = "\n%s? Default: [%s] > " % (need, default)
else:
msg = "\n%s? > " % need
while True:
response = input(msg)
if response:
return response
elif default is not None:
return default
else:
pass # raw_input('Please enter a value.')
def ask_facebook():
secret = ask_text("Facebook App Secret")
client_id = ask_text("Facebook App ID (not client token)")
return dict(secret=secret, client_id=client_id)
def ask_google():
secret = ask_text("Google Secret")
client_id = ask_text("Google Client ID")
return dict(secret=secret, client_id=client_id)
if __name__ == "__main__":
context = {
'now': str(datetime.now()),
'secret_key': get_random_secret_key(),
}
heading("Facebook")
if ask_yes_no("Do you want to configure auth via Facebook?\n"
"You'll need the app secret and client."):
context['facebook'] = ask_facebook()
heading("Google")
if ask_yes_no("Do you want to configure auth via Google?\n"
"You'll need the app secret and client."):
context['google'] = ask_google()
heading("Rendering settings...")
with open('config/settings.py', 'w') as out:
out.write(settings_template.render(context, request=None))
print("OK")
heading("Next steps")
print(commands_template.render(context, request=None))
heading("Done")