-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.py
executable file
·152 lines (134 loc) · 4.7 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
#!/usr/bin/env python2
# -*- coding: utf8 -*-
#from __future__ import unicode_literals
'''
This file is part of FIXME Events.
FIXME Events is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
FIXME Events is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with FIXME Events. If not, see <http://www.gnu.org/licenses/>.
'''
from flask import Flask, render_template, request, Response, url_for, redirect, session
from urllib import urlencode
import random, sys
from services import *
import config as cfg
from apiclient.discovery import build
from oauth2client.client import OAuth2WebServerFlow
from IPython import embed
# embed()
app = Flask(__name__)
if cfg.secret_key == '':
print 'configure secret_key!'
sys.exit(0)
app.debug = True # FIXME: remove on production
app.secret_key = cfg.secret_key
#
# PAGES
#
@app.route('/')
def home():
session['username'] = random.getrandbits(32)
return render_template('form.html', data={
'js': url_for('static', filename='main.js'),
'css': url_for('static', filename='style.css'),
})
@app.route('/test')
def test():
if 'service' in request.args and request.args['service'] in smap.keys():
svc = request.args['service']
return Response(json.dumps({
'name': svc,
'status': smap[svc][1](),
}), mimetype='application/json')
pass
else:
res = []
for s in smap.keys():
r = smap[s][1]()
res.append({'name': s, 'status': r})
return render_template('test.html', data={
'services': res,
'css': url_for('static', filename='style.css'),
})
@app.route('/fbauth')
@app.route('/fbauth/')
def fbauth():
if 'code' in request.args and request.args['state'] == cfg.facebook['state']:
data = {
'client_id': cfg.facebook['client_id'],
'client_secret': cfg.facebook['client_secret'],
'redirect_uri': request.url_root + 'fbauth/',
'code': request.args['code'],
}
r = requests.get(cfg.facebook['url_auth'] + '?%s' % urlencode(data) , headers={'User-Agent': cfg.user_agent})
return r.content
else:
return '<a href="%s?client_id=%s&redirect_uri=%s&scope=manage_pages,publish_stream&state=%s">Click here</a>' % (\
cfg.facebook['oauth'],
cfg.facebook['client_id'],
request.url_root + 'fbauth/',
cfg.facebook['state'],
)
@app.route('/gcalauth')
def gcalauth():
if 'code' in request.args:
code = request.args['code']
try:
http = auth_goog(code)
#embed()
service = build('calendar', 'v3', http=http)
except TypeError, e:
return e
return 'OK ' + request.args['code']
elif 'error' in request.args:
return request.args['error']
else:
FLOW = get_flow(request.url_root)
url_redir = FLOW.step1_get_authorize_url()
return redirect(url_redir)
@app.route('/send', methods=['POST', 'GET'])
def send():
error = None
services = []
if request.method == 'POST':
try:
data = {
'title': request.form['ev_title'],
'date_from': request.form['ev_date_from'],
'date_to': request.form['ev_date_to'],
'time_from': request.form['ev_time_from'],
'time_to': request.form['ev_time_to'],
'cp': request.form['ev_cp'],
'city': request.form['ev_city'],
'address': request.form['ev_address'],
'url': request.form['ev_url'],
'free': request.form['ev_free'],
'tags': request.form['ev_tags'],
'description': request.form['ev_description'],
'twitter': request.form['ev_twitter'],
'type': request.form['ev_type'],
}
fserv = request.form.getlist('ev_services')
for service in fserv:
if service in smap.keys():
services.append(smap[service][0](data))
except KeyError, e:
error = e
print e
return render_template('send.html', data={
'services': services,
'error': error
})
return redirect('/')
#
# MAIN
#
if __name__ == '__main__':
app.run()