-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.py
308 lines (243 loc) · 9.36 KB
/
api.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#-*-coding: utf-8-*-
import flask
import datetime
import json
import re
from flask import Flask, Response
from flask import request
from flask.ext import restful
from flask.ext.restful import Resource, reqparse, fields, marshal_with
from flask.ext.cors import CORS
from pymongo import MongoClient
from bson import json_util
from DB import dbHelper
mongoURI = ('mongodb://heroku_app32258670:'
'5hcl5oso685va7pcpo8e9ku1f5@ds061360'
'.mongolab.com:61360/heroku_app32258670')
db = MongoClient(mongoURI).heroku_app32258670
app = Flask(__name__)
api = restful.Api(app)
CORS(app, allow_headers='Content-Type',
methods=['GET', 'HEAD', 'POST', 'OPTIONS', 'PUT', 'PATCH', 'DELETE'])
event_fields = {
'_id': fields.String,
'department': fields.List(fields.Integer),
'name': fields.String,
'time': fields.String,
'agendas': fields.List(fields.Integer),
'nextAgendas': fields.List(fields.Integer),
'nextEvent': fields.String,
'prevEvents': fields.List(fields.String)
}
events_fields = {
'events': fields.List(fields.Nested(event_fields))
}
class Events(Resource):
dateParser = reqparse.RequestParser()
dateParser.add_argument('date', type=unicode, required=True,
help='Date (yyyy-mm-dd) is required')
@marshal_with(events_fields)
def get(self):
args = self.dateParser.parse_args()
return {'events': dbHelper.getEvents(args['date'])}
class Event(Resource):
eventParser = reqparse.RequestParser()
eventParser.add_argument('date', type=unicode, required=True,
help='Date is required')
eventParser.add_argument('time', type=unicode, required=True,
help='Time is required')
eventParser.add_argument('name', type=unicode, required=True,
help='Event name is required')
eventParser.add_argument('departments', type=unicode, required=True,
help='Departments are required')
def post(self):
args = self.eventParser.parse_args()
eventTime = datetime.datetime.strptime(args['date'] +
' ' + args['time'], '%Y-%m-%d %H:%M')
departments = [int(n) for n in args['departments'].split(',')]
return {'_id': dbHelper.insertEvent(
eventTime, args['name'], departments)}, 201
def put(self, eventID):
idParser = reqparse.RequestParser()
idParser.add_argument('nextEventID', type=unicode, default=None)
args = idParser.parse_args()
if args['nextEventID'] is None:
return {'_id': dbHelper.updateNextEvent(eventID, None)}
return {'_id': dbHelper.updateNextEvent(eventID, args['nextEventID'])};
def get(self, eventID):
return dbHelper.getEvent(eventID)
class EventDates(Resource):
def get(self, date):
if re.match('^[0-9]{4}-[01]?[0-9]$', date) is None:
return {'error': 'invalid date'}
date = date.split('-')
year = int(date[0])
month = int(date[1])
if not 1 <= month <= 12:
return {'error': 'invalid date'}
return {'dates': dbHelper.getEventDates(year, month)}
depatment_fields = {
'_id': fields.Integer,
'name': fields.String
}
departments_fields = {
'departments': fields.List(fields.Nested(depatment_fields))
}
class Departments(Resource):
@marshal_with(departments_fields)
def get(self):
return {'departments': dbHelper.getDepartments()}
class Department(Resource):
deptParser = reqparse.RequestParser()
deptParser.add_argument('name', type=unicode, required=True,
help='Name is required')
def post(self):
args = self.deptParser.parse_args()
return {'_id': dbHelper.insertDepartment(args['name'])}, 201
person_fields = {
'_id': fields.Integer,
'name': fields.String,
'departments': fields.List(fields.Integer)
}
people_fields = {
'people': fields.List(fields.Nested(person_fields))
}
class People(Resource):
deptParser = reqparse.RequestParser()
deptParser.add_argument('departments', type=unicode, required=True,
help='Departments are required')
@marshal_with(people_fields)
def get(self):
args = self.deptParser.parse_args()
departments = [int(n) for n in args['departments'].split(',')]
return {'people': dbHelper.getPeople(departments)}
class Person(Resource):
personParser = reqparse.RequestParser()
personParser.add_argument('name', type=unicode, required=True,
help='Name is required')
personParser.add_argument('departments', type=unicode, required=True,
help='Departments are required')
def post(self):
args = self.personParser.parse_args()
departments = [int(n) for n in args['departments'].split(',')]
return {'_id': dbHelper.insertPerson(args['name'], departments)}, 201
agenda_fields = {
'_id': fields.Integer,
'name': fields.String,
'description': fields.String(default = '')
}
agendas_fields = {
'agendas': fields.List(fields.Nested(agenda_fields))
}
event_and_agendas_fields = {
'event': fields.Nested(event_fields),
'agendas': fields.List(fields.Nested(agenda_fields))
}
class Agendas(Resource):
idParser = reqparse.RequestParser()
idParser.add_argument('eventID', type=unicode, required=True,
help='EventID is required')
@marshal_with(agendas_fields)
def get(self):
args = self.idParser.parse_args()
return {'agendas': dbHelper.getAgendas(args['eventID'])}
class AgendaList(Resource):
def get(self):
cur = db.agendas.find()
return [agenda for agenda in cur]
class Agenda(Resource):
idParser = reqparse.RequestParser()
idParser.add_argument('eventID', type=unicode, required=True,
help='EventID is required')
agendaParser = reqparse.RequestParser()
agendaParser.add_argument('eventID', type=unicode, required=True,
help='EventID is required')
agendaParser.add_argument('name', type=unicode, required=True,
help='Name is required')
@marshal_with(agenda_fields)
def post(self):
args = self.agendaParser.parse_args()
return dbHelper.insertAgenda(args['name'], args['eventID'])
def delete(self, agendaID):
args = self.idParser.parse_args()
return {'result': str(dbHelper.deleteAgenda(agendaID, args['eventID']))}
@marshal_with(agenda_fields)
def put(self, agendaID):
args = self.idParser.parse_args()
return dbHelper.insertExistingAgenda(agendaID, args['eventID'])
class NextAgendas(Resource):
idParser = reqparse.RequestParser()
idParser.add_argument('eventID', type=unicode, required=True,
help='EventID is required')
@marshal_with(event_and_agendas_fields)
def get(self):
args = self.idParser.parse_args()
event, agendas = dbHelper.getNextAgendas(args['eventID'])
return {'event': event, 'agendas': agendas}
class NextAgenda(Resource):
idParser = reqparse.RequestParser()
idParser.add_argument('eventID', type=unicode, required=True,
help='EventID is required')
agendaParser = reqparse.RequestParser()
agendaParser.add_argument('eventID', type=unicode, required=True,
help='EventID is required')
agendaParser.add_argument('name', type=unicode, required=True,
help='Name is required')
@marshal_with(agenda_fields)
def post(self):
args = self.agendaParser.parse_args()
return dbHelper.insertNextAgenda(args['name'], args['eventID'])
@marshal_with(agenda_fields)
def put(self, agendaID):
args = self.idParser.parse_args()
return dbHelper.insertExistingNextAgenda(agendaID, args['eventID'])
def delete(self, agendaID):
args = self.idParser.parse_args()
return {'result': str(dbHelper.deleteNextAgenda(agendaID, args['eventID']))}
class Report(Resource):
def get(self, reportID):
return dbHelper.generateReport(reportID)
def put(self, reportID):
parser = reqparse.RequestParser()
parser.add_argument('report', type=unicode, required=True,
help='Report is required')
args = parser.parse_args()
report = json.loads(args['report'])
result = dbHelper.updateReport(reportID, report)
return {'result': result['nModified'] == 1}
class Iplookup(Resource):
def get(self):
ip = db.connectip.find().sort('time').limit(1)[0]
print ip
return {'ip':ip['ip']}
@app.errorhandler(404)
def not_found(error=None):
message = {
'status': 404,
'message': 'Not Found: ' + request.url,
}
resp = Response(
response=json.dumps(message,
separators=(',', ':'),
default=json_util.default),
status=200,
mimetype='application/json')
resp.status_code = 404
return resp
api.add_resource(Events, '/events')
api.add_resource(Event, '/event', '/event/<string:eventID>')
api.add_resource(EventDates, '/events/<string:date>')
api.add_resource(Departments, '/departments')
api.add_resource(Department, '/department')
api.add_resource(People, '/people')
api.add_resource(Person, '/person')
api.add_resource(Agendas, '/agendas')
api.add_resource(Agenda, '/agenda', '/agenda/<int:agendaID>')
api.add_resource(NextAgendas, '/next-agendas')
api.add_resource(NextAgenda, '/next-agenda', '/next-agenda/<int:agendaID>')
api.add_resource(Report, '/report/<string:reportID>')
api.add_resource(AgendaList, '/agenda-list')
api.add_resource(Iplookup, '/ip')
if __name__ == '__main__':
app.debug = True
app.run()