-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.py
203 lines (156 loc) · 7.68 KB
/
event.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
import json
from random import random
import peewee
import requests
from flask import Blueprint, abort, request, flash, redirect, url_for, request
from util import templated, auth_required, has_access_to_event, pw_gen, register_event_attendee, register_event_group
from rdyapi import RdyApi
def get_blueprint():
from database import Event, Instance, EventManagerRelation, EventManager, EventAttendee, Groups, DoesNotExist
blueprint = Blueprint('event', __name__, url_prefix='/event')
@blueprint.route('/<int:event_id>/admin')
@auth_required(has_access_to_event('event_id'))
@templated('event/admin.html')
def admin(event_id: int):
e: Event = Event.get_or_none(event_id)
if e is None:
abort(404)
try:
i: Instance = Instance.get(Instance.name == e.instance)
api = RdyApi(i.hostname, i.api_key)
groups = list(Groups.select().where(Groups.event_id == event_id).namedtuples())
except json.decoder.JSONDecodeError:
abort(500, 'API Key invalid!')
except DoesNotExist:
abort(500, 'Instance does not Exist!')
return dict(name=e.name,
event_id=e.event_id,
instance=i.name,
instance_host=i.hostname,
groups=groups
)
@blueprint.route('/<int:event_id>/admin/eventmanager', methods=['POST'])
@auth_required(has_access_to_event('event_id'))
def add_event_manager(event_id: int):
admin_identifier = request.form.get('admin-identifier')
if admin_identifier is None:
flash('Missing Email')
return redirect(url_for(".admin", event_id=event_id))
eventmanager = EventManager.get_or_none((EventManager.username == admin_identifier)
| (EventManager.email == admin_identifier))
if eventmanager is None:
flash('Invalid username or email')
return redirect(url_for(".admin", event_id=event_id))
try:
EventManagerRelation.create(manager_id=eventmanager.username, event_id=event_id)
flash('Success')
except peewee.IntegrityError:
flash('Already satisfied')
return redirect(url_for(".admin", event_id=event_id))
@blueprint.route('/<int:event_id>/admin/eventattendee', methods=['POST'])
@auth_required(has_access_to_event('event_id'))
def add_event_attendee(event_id):
username = request.form.get('username')
groupID = request.form.get('group-id')
email = request.form.get('email')
if username is None:
flash("please enter a username")
return redirect(url_for('.admin', event_id=event_id))
if groupID is None:
flash("please choose a Group")
return redirect(url_for('.admin', event_id=event_id))
instance = list(Instance.select(Instance)
.join(Event, on=(Event.instance_id == Instance.name))
.where(Event.event_id == event_id)
.namedtuples())
if email is None:
flash("please enter a email for sending user credentials")
return redirect(url_for('.admin', event_id=event_id))
# emails = list(Event.select()
# .join(EventManagerRelation, on=Event.event_id == EventManagerRelation.event_id)
# .join(EventManager, on=EventManager.username == EventManagerRelation.manager_id)
# .namedtuples())
register_event_attendee(instance[0].hostname,
instance[0].api_key,
event_id,
username,
pw_gen(8),
groupID,
sendmail=True,
email=email)
return redirect(url_for(".admin", event_id=event_id))
@blueprint.route('/<int:event_id>/admin/eventgroup', methods=['POST'])
@auth_required(has_access_to_event('event_id'))
def add_event_group(event_id):
instance = list(Instance.select(Instance)
.join(Event, on=(Event.instance_id == Instance.name))
.where(Event.event_id == event_id)
.namedtuples())
groupname = request.form.get('groupname')
register_event_group(instance[0].hostname, instance[0].api_key, event_id, groupname, False)
return redirect(url_for(".admin", event_id=event_id))
@auth_required(has_access_to_event('event_id'))
@blueprint.route('/<int:event_id>/admin/groupsyncstate', methods=['POST'])
def groupsyncstate(event_id):
req = json.loads(request.data)
group_id = req['group_id']
try:
state = not req['state'] == "True"
except:
return redirect(url_for(".admin", event_id=event_id))
hostname, api_key = list(Instance.select(Instance.hostname, Instance.api_key)
.join(Event, on=Event.instance_id == Instance.name)
.join(Groups, on=Groups.event_id == Event.event_id)
.where(Event.event_id == event_id)
.namedtuples())[0]
with RdyApi(hostname, api_key) as api:
api.set_sync_npcs(group_id, state)
print(state)
print(group_id)
Groups.update(has_synced_npcs=state).where(group_id == group_id).execute()
return redirect(url_for(".admin", event_id=event_id))
@blueprint.route('/eventinvitation/<string:event_id>', methods=['GET'])
def eventinvitation(event_id):
# TODO create group
return None
@blueprint.route('/registerattendee/<string:groupId>')
@templated('invitation system/group.html')
def registerattendee(groupId):
if not groupId is None:
group = Groups.get_or_none((Groups.group_id == groupId))
if group is None:
# group-id not in database -> abort
abort(404)
else:
event = Event.get_or_none(Event.event_id == group.event_id)
if event is None:
# event_id in group is not registered
abort(500)
else:
instance = Instance.get_or_none(Instance.name == event.instance_id)
if instance is None:
abort(500)
else:
abort(400)
# PLSS WHY DIDN'T I CHOOSE A JOIN
registersuccess = request.args.get('registersuccess')
if registersuccess is None:
registersuccess = False
username = request.args.get('username')
return dict(group=group,
event=event,
instance=instance,
registersuccess=registersuccess,
username=username)
@blueprint.route('/registerattendee/<string:groupId>', methods=['POST'])
def handleattendeereg(groupId):
username = request.form.get('username')
email = request.form.get('email')
queryresp = list(Groups.select(Instance.hostname, Instance.api_key, Event.event_id)
.join(Event, on=(Groups.event_id == Event.event_id))
.join(Instance, on=(Event.instance_id == Instance.name))
.where(Groups.group_id == groupId)
.namedtuples())
register_event_attendee(queryresp[0].hostname, queryresp[0].api_key, queryresp[0].event_id, username, pw_gen(8), groupId, email=email)
return redirect(url_for('.registerattendee', groupId=groupId, registersuccess=True, username=username))
return blueprint