-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
194 lines (133 loc) · 5.42 KB
/
main.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
#!/usr/bin/env python
import json
import logging
import os
import traceback
import webapp2
from webapp2_extras import routes
from google.appengine.api import urlfetch
from google.appengine.ext.webapp import template
from config import TOKEN
def slack_invite(email):
url = "https://musicoin.slack.com/api/users.admin.invite?email=" \
+ email + "&token=" + TOKEN + "&set_active=true"
try:
urlfetch.set_default_fetch_deadline(30)
result = urlfetch.fetch(
url=url,
payload={},
method=urlfetch.GET,
headers={'Accept': 'application/json'}
)
except:
stacktrace = traceback.format_exc()
logging.error("urlfetch URL failure \n\n%s", stacktrace)
return
logging.info(result.content)
return result.content
class HomePageHandler(webapp2.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'template/index.html')
self.response.out.write(template.render(path, {}))
class HowItWorksHandler(webapp2.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'template/howItWorks.html')
self.response.out.write(template.render(path, {}))
class MusiciansHandler(webapp2.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'template/musicians.html')
self.response.out.write(template.render(path, {}))
class DevelopersHandler(webapp2.RequestHandler):
def get(self):
path = os.path.join(os.path.dirname(__file__), 'template/developers.html')
self.response.out.write(template.render(path, {}))
class SlackInvitePageHandler(webapp2.RequestHandler):
def get(self):
template_values = {
}
logging.info("Opening Slack invite page")
path = os.path.join(os.path.dirname(__file__), 'template/slack_invite.html')
self.response.out.write(template.render(path, template_values))
class SlackInviteSuccessPageHandler(webapp2.RequestHandler):
def get(self):
email = self.request.get("email", "")
if email == "":
self.redirect("/join/")
return
template_values = {
"email": email
}
path = os.path.join(os.path.dirname(__file__), 'template/slack_invited.html')
self.response.out.write(template.render(path, template_values))
class SlackInviteActionHandler(webapp2.RequestHandler):
def get(self):
self.response.write("Method GET not allowed")
self.response.set_status(405)
def post(self):
self.response.headers.add_header("Access-Control-Allow-Origin", "*")
self.response.headers["Content-Type"] = "application/json"
email = self.request.get("email")
if not email:
self.response.write(json.dumps({"status": "error", "message": "PARAMETER_MISSING"}))
return
logging.info("email = " + email)
try:
invite_result = slack_invite(email)
json_object = json.loads(invite_result)
logging.info(json_object["ok"])
logging.info(json_object["ok"] is True)
if json_object["ok"] is True:
self.response.write(json.dumps({
"status": "success",
"message": ""
}))
return
else:
self.response.write(json.dumps({
"status": "failed",
"message": json_object["error"]
}))
except:
stacktrace = traceback.format_exc()
logging.error("slack_invite failure \n\n%s", stacktrace)
self.response.write(json.dumps({
"status": "failed",
"message": ""
}))
class ReturnHomePageHandler(webapp2.RequestHandler):
def get(self):
self.redirect("/")
class BlogRedirectHandler(webapp2.RequestHandler):
def get(self):
self.redirect("https://medium.com/@musicoin")
app = webapp2.WSGIApplication([
# routes.DomainRoute(r'<:(slack\.hongcoin\.org|localhost)>', [
routes.DomainRoute(r'<:(blog.musicoin.org)>', [
webapp2.Route('/', BlogRedirectHandler),
webapp2.Route('/.*', BlogRedirectHandler),
]),
routes.DomainRoute(r'<:(musicoin.org|www.musicoin.org|localhost|layout2.musicoin-web.appspot.com)>', [
webapp2.Route('/', HomePageHandler),
webapp2.Route('/about', HowItWorksHandler),
webapp2.Route('/musicians', MusiciansHandler),
webapp2.Route('/developers', DevelopersHandler),
]),
# Slack signup
routes.DomainRoute(r'<:(slack.musicoin.org|slack.default.musicoin-web.appspot.com)>', [
webapp2.Route('/', SlackInvitePageHandler),
webapp2.Route('/join', SlackInvitePageHandler),
webapp2.Route('/join/', SlackInvitePageHandler),
webapp2.Route('/join/invited', SlackInviteSuccessPageHandler),
webapp2.Route('/join/slack/invite', SlackInviteActionHandler),
]),
# Slack signup for localhost
routes.DomainRoute(r'<:(localhost)>', [
webapp2.Route('/', HomePageHandler),
webapp2.Route('/join', SlackInvitePageHandler),
webapp2.Route('/join/', SlackInvitePageHandler),
webapp2.Route('/join/invited', SlackInviteSuccessPageHandler),
webapp2.Route('/join/slack/invite', SlackInviteActionHandler),
]),
('/', HomePageHandler),
('/.*', ReturnHomePageHandler),
])