-
Notifications
You must be signed in to change notification settings - Fork 0
/
aas.py
129 lines (97 loc) · 3.49 KB
/
aas.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
import concurrent.futures
import hashlib
import hmac
import json
import subprocess
import os
import requests
from flask import Flask, request, Response, abort
from flask_restful import Api, Resource
def deploy_static_sticky():
deploy_service = os.getenv("DEPLOY_SERVICE")
# Make sure the user running this has root privileges to start this command, e.g. by adding it to a sudoers file
subprocess.run(
[
"sudo",
"/usr/bin/systemd-run",
"--no-block",
"--property",
f"After={deploy_service}",
"--",
"systemctl",
"start",
deploy_service,
],
check=True,
)
class GitHub(Resource):
# Share this secret with GitHub to authenticate this hook
SECRET = os.environ["GITHUB_SECRET"].encode()
def post(self):
## Check authentication ##
# remove the sha1= prefix of the signature
# TODO: find more elegant way to do this
signature = request.headers.get("X-Hub-Signature")[5:]
if not hmac.compare_digest(
signature,
hmac.new(self.SECRET, request.get_data(), hashlib.sha1).hexdigest(),
):
abort(401)
response_payload = request.get_json()
deploy_static_sticky()
class Pretix(Resource):
TOKEN = os.environ["PRETIX_TOKEN"]
def post(self):
payload = request.get_json()
url = (
f'https://pretix.svsticky.nl/api/v1/organizers/{payload["organizer"]}/'
f'events/{payload["event"]}/orders/{payload["code"]}/'
)
response = requests.get(url, headers={"Authorization": f"Token {self.TOKEN}"})
response.raise_for_status()
data = response.json()
position = data["positions"][0]
answers = {}
for answer in position["answers"]:
identifier = answer["question_identifier"]
value = answer["answer"]
answers[identifier] = value
if answers.get("aeskwadraat_signup") != "True":
return Response(status=204)
aes_studie = {
"Informatica": "IC",
"Informatiekunde": "IK",
"Dubbele bachelor Informatica/Informatiekunde": "IC/IK",
}.get(answers.get("studies"))
voornaam = position["attendee_name_parts"]["given_name"]
achternaam = position["attendee_name_parts"]["family_name"]
email = data["email"]
payload = {
"email": email,
"voornaam": voornaam,
"tussenvoegsel": "",
"achternaam": achternaam,
"geboortedatum": answers.get("geboortedatum"),
"studentnummer": answers.get("studentnummer"),
"straat": "unknown",
"huisnummer": "unknown",
"postcode": "unknown",
"plaats": "unknown",
"mobiel": data["phone"],
"studie": aes_studie,
}
if data.get("testmode"):
aas.logger.warning(f"Got a test mode signup: {payload}")
else:
response = requests.get(
"https://www.a-eskwadraat.nl/Leden/Intro/Aanmelden", params=payload
)
response.raise_for_status()
return Response(status=201)
aas = Flask(__name__)
aas_api = Api(aas, catch_all_404s=True)
contentful_endpoint = os.getenv("CONTENTFUL_SECRET_ENDPOINT", "missing")
aas_api.add_resource(GitHub, "/webhook/github")
aas_api.add_resource(Pretix, "/webhook/pretix")
if __name__ == "__main__":
aas.run()