-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.py
123 lines (104 loc) · 4.77 KB
/
admin.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
from collections import defaultdict
import bcrypt
import peewee
from flask import Blueprint, abort, request, flash, redirect, url_for
from peewee import JOIN
from mailing import Mailsender, RegistrationMail
from util import auth_required, templated, register_eventmanager, pre_encode_password, pw_gen
def get_blueprint() -> Blueprint:
from database import Instance, EventManagerRelation, Event, EventManager
blueprint = Blueprint('admin', __name__, url_prefix='/admin')
@blueprint.route('/')
@auth_required(requires_site_admin=True)
@templated('admin.html')
def index():
instance_list = list(Instance.select(Instance.hostname, Instance.name).namedtuples())
manager_list = list(EventManager.select(
EventManager.username, EventManager.email, EventManager.site_admin).namedtuples())
return dict(instances=instance_list, managers=manager_list)
@blueprint.route('/event/', methods=['POST'])
@auth_required(requires_site_admin=True)
def register_event():
event_name = request.form.get('name')
instance_id = request.form.get('instance')
e = Event.create(name=event_name, instance=instance_id)
return redirect(url_for('event.admin', event_id=e.event_id))
@blueprint.route('/application/<application_id>')
@auth_required(requires_site_admin=True)
def application(application_id: str):
return str(application_id)
@blueprint.route('/event-manager/', methods=['POST'])
@auth_required(requires_site_admin=True)
def create_user():
username = request.form.get('username')
email = request.form.get('email')
is_site_admin = request.form.get('is-admin')
if is_site_admin == 'on':
is_site_admin = True
else:
is_site_admin = False
password = pw_gen(10)
if username is None or email is None or is_site_admin is None:
flash('Please check you\'re input')
return redirect(url_for('admin.index'))
try:
register_eventmanager(username, email, password, site_admin=is_site_admin)
flash('Success')
except peewee.IntegrityError:
flash('Username is already taken!')
with Mailsender() as sender:
RegistrationMail(sender).send(email, username, password)
return redirect(url_for('admin.index'))
@blueprint.route('/event-manager/password', methods=['POST'])
@auth_required(requires_site_admin=True)
def change_password():
password = request.form.get('password')
manager = request.form.get('manager') # todo errors
EventManager.update({EventManager.password: bcrypt.hashpw(
pre_encode_password(password), bcrypt.gensalt())}).where(EventManager.username == manager).execute()
return redirect(url_for('admin.index'))
@blueprint.route('/instance/', methods=['POST'])
@auth_required(requires_site_admin=True)
def register_instance():
instance_name = request.form.get('name')
if not instance_name:
flash('Missing Name')
return redirect(url_for('.index'))
api_key = request.form.get('api_key').rstrip().lstrip()
if not api_key:
flash("No API KEY")
return redirect(url_for('.index'))
hostname = request.form.get('hostname').rstrip().lstrip()
if not hostname:
flash('Missing hostname')
return redirect(url_for('.index'))
i, created = Instance.get_or_create(name=instance_name, hostname=hostname,
api_key=api_key)
if not created:
flash('This instance name is already in use.')
return redirect(url_for('.index'))
return redirect(url_for('.instance_details', instance_id=instance_name))
@blueprint.route('/instance/<instance_id>')
@auth_required(requires_site_admin=True)
@templated('instance.html')
def instance_details(instance_id):
managers = defaultdict(list)
i = Instance.get_or_none(Instance.name == instance_id)
if i is None:
return abort(404) # todo 404 handler
event_list = list(Event.select(Event, EventManagerRelation.manager)
.join(EventManagerRelation, on=EventManagerRelation.event == Event.event_id,
join_type=JOIN.LEFT_OUTER)
.where(Event.instance == instance_id)
.group_by(Event.event_id)
.namedtuples())
for ev in event_list:
managers[ev.event_id].append(ev.manager)
return dict(
events=event_list,
managers=managers,
name=instance_id,
hostname=i.hostname,
api_key=i.api_key
)
return blueprint