forked from carlchan/spotipo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manage.py
250 lines (221 loc) · 9.41 KB
/
manage.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#! flask/bin/python
from os.path import abspath
import arrow
import getpass
from flask import current_app
from flask_script import Manager
from flask_assets import ManageAssets
from flask_migrate import Migrate, MigrateCommand
from unifispot import create_app
from unifispot.core.db import db
from unifispot.utils.translation import (add_translations, compile_translations,
update_translations,
add_plugin_translations,
compile_plugin_translations,
update_plugin_translations)
import logging
logging.basicConfig(level=logging.DEBUG)
logging.getLogger().addHandler(logging.StreamHandler())
logger = logging.getLogger()
app = create_app(mode='development')
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
@manager.command
def init_data():
with app.app_context():
from sqlalchemy.exc import OperationalError
from flask_security.utils import encrypt_password
from unifispot.core.models import User,Account,Admin
import uuid
try:
account = Account.query.filter_by(id=1).first()
users = Admin.query.count()
except :
app.logger.debug( "No Account table Entry found,could be running migration ")
else:
enc_pass = encrypt_password('password')
if not account:
#create default admin user
account = Account()
account.token = str(uuid.uuid4())
db.session.add(account)
db.session.commit()
if not users:
admin_user = Admin(email='[email protected]',password=enc_pass,displayname= "Admin User",active=1)
admin_user.account_id = account.id
db.session.add(admin_user)
db.session.commit()
@manager.command
def init_demo():
with app.app_context():
from sqlalchemy.exc import OperationalError
from flask_security.utils import encrypt_password
from unifispot.core.models import User,Account,Admin,Client,Wifisite,Landingpage
from unifispot.core.guestutils import init_track,assign_guest_entry
from unifispot.modules.analytics.methods import update_daily_stat
from tests.helpers import randomMAC,randomGuestEmailForm
from random import randint
account = Account.query.filter_by(id=1).first()
enc_pass = encrypt_password('password')
client1 = Client.query.filter_by(email='[email protected]').first()
if not client1:
#add a client
client1 = Client(email='[email protected]',password=enc_pass,
displayname= "Client User",active=1)
client1.account_id = account.id
client1.save()
site1 = Wifisite.query.get(1)
if not site1:
site1 = Wifisite(name='Site1',sitekey='site1')
site1.client_id = client1.id
site1.account_id = account.id
site1.save()
landing1 = Landingpage()
landing1.siteid = site1.id
landing1.save()
now = arrow.now()
month_start = now.replace(days=-30)
days = (now - month_start).days
for d in range(days):
day_start = month_start.replace(days=d).floor('day')
logger.warn('-------Generating data for :%s'%day_start )
for i in range(randint(5,10)):
track = init_track(site1,guestmac=randomMAC(),apmac=randomMAC())
track.timestamp = day_start.replace(minutes=+i*1).naive
track.loginstat = {'num_visits':1,'auth_email':1}
track.save()
if i%2:
f = randomGuestEmailForm()
g = assign_guest_entry(site1,track,f)
g.createdat = day_start.replace(minutes=+i*1).naive
g.save()
track.save()
logger.warn('track added for email ID;%s for :%s'%(track.id,day_start))
#create random
#half of them new user
for i in range(randint(5,10)):
track = init_track(site1,guestmac=randomMAC(),apmac=randomMAC())
track.timestamp = day_start.replace(minutes=+i*1).naive
track.loginstat = {'num_visits':1,'auth_facebook':1,'fbcheckedin':1}
track.save()
if i%2:
g= assign_guest_entry(site1,track,f)
g.createdat = day_start.replace(minutes=+i*1).naive
g.save()
track.updatestat('fbliked',1)
track.save()
track.save()
logger.warn('track added for FB ID;%s for :%s'%(track.id,day_start))
update_daily_stat(site1,day_start)
@manager.command
def reset_admin():
with app.app_context():
from unifispot.core.models import User
from flask_security.utils import encrypt_password
admin = User.query.filter_by(id=1).first()
enc_pass = encrypt_password('password')
admin.password = enc_pass
db.session.commit()
@manager.command
def reset_pass():
with app.app_context():
from unifispot.core.models import User
from flask_security.utils import encrypt_password
def get_admin(iteration=1):
if (iteration > 1):
print "Invalid id entered. Try again"
admin_id = raw_input("Select the id of admin you want to reset password: ")
selected_admin = User.query.get(admin_id)
if not selected_admin:
return get_admin(iteration=iteration+1)
else:
return selected_admin
def get_password(iteration=1):
if (iteration > 1):
print "Passwords did not match. Try again"
password1 = getpass.getpass("Give a new password: ")
password2 = getpass.getpass("Confirm new password: ")
if(password1 != password2):
return get_password(iteration=iteration+1)
else:
return password1
admins = User.query.all()
print "id\temail"
for admin in admins:
print admin.id, "\t", admin.email
selected_admin = get_admin()
password = get_password()
enc_pass = encrypt_password(password)
selected_admin.password = enc_pass
db.session.commit()
@manager.command
def rebuild_monthly_stats():
from unifispot.core.models import User,Account,Admin,Client,Wifisite,Landingpage
from unifispot.modules.analytics.methods import update_daily_stat
site1 = Wifisite.query.get(1)
now = arrow.now()
month_start = now.floor('month')
days = (now.ceil('month') - month_start).days
for d in range(days):
day_start = month_start.replace(days=d).floor('day')
logger.warn('-------Generating analytics data for :%s'%day_start )
update_daily_stat(site1,day_start)
@manager.command
def get_notifications():
with app.app_context():
from unifispot.core.tasks import celery_get_notification
from unifispot.core.models import Wifisite
celery_get_notification()
@manager.command
def send_weekly_reports():
from unifispot.modules.analytics.tasks import celery_weekly_report
celery_weekly_report()
@manager.command
def send_monthly_reports():
from unifispot.modules.analytics.tasks import celery_monthly_report
celery_monthly_report()
@manager.option('-l', '--lang', help='Language',dest='lang')
@manager.option('-p', '--plugin', help='Adds a new language to a plugin.',
dest='plugin')
def new_translation(lang,plugin=None):
"""Adds a new language to the translations. "lang" is the language code
of the language, like, "de_AT"."""
from unifispot.ext.plugins import plugin_manager
if plugin:
print 'Adding :%s translation for plugin :%s'%(lang,plugin)
add_plugin_translations(plugin, lang)
else:
add_translations(lang)
for p_name in plugin_manager.plugins:
print 'Adding :%s translation for plugin :%s'%(lang,p_name)
add_plugin_translations(p_name, lang)
@manager.command
@manager.option('is_all', '--all')
@manager.option('-l', '--lang', help='Language')
def update_translation(is_all=True, plugin=None):
"""Updates all translations."""
if is_all:
is_all = True
if plugin is not None:
print"[+] Updating language files for plugin {}..."\
.format(plugin)
update_plugin_translations(plugin)
else:
print"[+] Updating language files..."
update_translations(include_plugins=is_all)
@manager.command
@manager.option('is_all', '--all')
@manager.option('-p', '--plugin', help='Adds a new language to a plugin.')
def compile_translation(is_all=False, plugin=None):
"""Compiles the translations."""
if is_all:
is_all = True
if plugin is not None:
print"[+] Compiling language files for plugin {}..."\
.format(plugin)
compile_plugin_translations(plugin)
else:
print "[+] Compiling language files..."
compile_translations(include_plugins=is_all)
manager.run()