forked from feabell/agentapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservices_admin.py
More file actions
158 lines (125 loc) · 5.08 KB
/
services_admin.py
File metadata and controls
158 lines (125 loc) · 5.08 KB
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
from flask import render_template, Blueprint, url_for, redirect, request
from flask.ext.basicauth import BasicAuth
from multiprocessing import Pool, Process
import requests
import pprint
from services_util import *
from services_discord import remove_roles
services_admin = Blueprint('services_admin', __name__)
basic_auth = BasicAuth()
#hack
@services_admin.record_once
def on_load(state):
basic_auth.init_app(state.app)
@services_admin.route('/')
@basic_auth.required
def adminpage():
"""
Method returns accounts with pending New/Delete status.
"""
add_to_slack_query = query_db('SELECT name, email from pilots '
'WHERE keyid NOT NULL '
'AND vcode NOT NULL '
'AND slack_active=0 '
'AND active_account=1 '
'AND in_alliance=1')
add_to_slack=""
for add in add_to_slack_query:
add_to_slack = add_to_slack + add['name'] + " <"+ add['email']+">,"
delete_from_slack = query_db('SELECT name, email, keyid, vcode, id '
'FROM pilots '
'where slack_active=1 '
'AND (active_account=0 '
'OR in_alliance=0)')
return render_template('services-admin.html', add=add_to_slack, delete=delete_from_slack)
@services_admin.route('/accounts-added', methods=['POST'])
@basic_auth.required
def admin_accounts_added():
"""
Method updates DB with new pilots.
"""
update_query = insert_db('UPDATE pilots '
'SET slack_active=1 '
'WHERE keyid NOT NULL '
'AND vcode NOT NULL '
'AND slack_active=0 '
'AND active_account=1 '
'AND in_alliance=1')
return redirect(url_for('services_admin.adminpage'), code=302)
@services_admin.route('/accounts-deleted', methods=['POST'])
@basic_auth.required
def admin_accounts_deleted():
"""
Method updates DB with deleted pilots.
"""
users_to_delete = query_db('SELECT email, discordid '
'FROM pilots '
'WHERE slack_active=1 '
'AND (active_account=0 '
'OR in_alliance=0)')
update_query = insert_db('UPDATE pilots '
'SET slack_active=0, keyid=NULL, vcode=NULL, active_account=0, in_alliance=0 '
'WHERE slack_active=1 '
'AND (active_account=0 '
'OR in_alliance=0)')
#remove discord roles
for user in users_to_delete:
discordid = user['discordid']
email = user['email']
#skip if we don't have a discordid
if not discordid:
continue
remove_roles(discordid, email)
return redirect(url_for('services_admin.adminpage'), code=302)
@services_admin.route('/checkaccounts')
@basic_auth.required
def checkaccounts():
"""
Method checks DB for expired accounts, updates DB with the prep-for-delete flag.
"""
active_accounts = query_db('SELECT email, keyid, vcode from pilots '
'WHERE active_account=1 '
'AND in_alliance=1 '
'AND slack_active=1')
print("[LOG] Checking {n} accounts.".format(n = len(active_accounts)))
#turn our dictionary into a list
accountlist = []
for account in active_accounts:
accountlist.append([account['email'], str(account['keyid']), account['vcode']])
#kickoff the background_check as a non-blocking process
p = Process(target=background_check, args=(accountlist,))
p.start()
return redirect(url_for('services_admin.adminpage'), code=302)
def background_check(accountlist):
pool = Pool(15)
pilots_to_delete = pool.starmap(in_alliance_and_active, accountlist)
pool.close()
pool.join()
filtered_delete = list(filter(None.__ne__, pilots_to_delete))
pp = pprint.PrettyPrinter(indent=4)
pp.pprint(filtered_delete)
for email in filtered_delete:
update_query = insert_db('UPDATE pilots '
'SET active_account=0, in_alliance=0 '
'WHERE lower(email) = ?', [email.lower()])
return
@services_admin.route('/markactive', methods=['POST'])
@basic_auth.required
def admin_mark_active():
"""
Method marks a provided pilot as in corp and active
"""
pilotid = request.form['id']
insert_db('UPDATE pilots SET active_account=1, in_alliance=1 WHERE id=?', [pilotid])
print("[INFO] pilot {pilotid} marked as active".format(pilotid = pilotid))
return redirect(url_for('services_admin.adminpage'), code=302)
@services_admin.route('/markallactive', methods=['GET'])
@basic_auth.required
def admin_mark_all_active():
"""
Method updates DB with pilots that have been mistakenly marked for deletion.
"""
update_query = insert_db('UPDATE pilots '
'SET active_account=1, in_alliance=1 '
'WHERE slack_active=1;')
return redirect(url_for('services_admin.adminpage'), code=302)