Skip to content

Commit

Permalink
Merge pull request #41 from titipata/fix_api
Browse files Browse the repository at this point in the history
API updates
  • Loading branch information
bluenex authored Mar 5, 2019
2 parents 18a6302 + 5540979 commit 8ceec38
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,13 @@
api = Api(app, catch_all_404s=True)


class ShowEvent(Resource):
class GetEvent(Resource):
"""
Return events base on days ahead
"""
input_args = {
'days': fields.Int(missing=None, required=False),
'school': fields.String(missing=None, required=False),
'category': fields.String(missing=None, required=False)
'owner': fields.String(missing=None, required=False)
}

@use_args(input_args)
Expand All @@ -54,46 +53,32 @@ def get(self, args):
# add school query, parse datetime
events_query = []
for event in events:
event['school_query'] = '-'.join(event['school'].lower().replace('/', ' ').split())
event['date_dt'] = dateutil.parser.parse(event['date'])
event['owner_query'] = '-'.join(event['owner'].lower().replace('/', ' ').split())
if event['date_dt'].strip() != '':
event['date_dt'] = dateutil.parser.parse(event['date_dt'])
else:
event['date_dt'] = datetime.today()
events_query.append(event)

# filter school name
if args['school'] is not None:
events_query = list(filter(lambda x: x['school_query'] == args['school'],
events_query))

# filter date
# filter days
if args['days'] is not None:
date_retrieve = datetime.today() + timedelta(days=args['days'])
events_query = list(filter(lambda x: x['date_dt'] >= datetime.today() and x['date_dt'] <= date_retrieve,
events_query))

# filter category
if args['category'] is not None:
events_query = list(filter(lambda x: x['category'].lower() == args['category'],
# filter owners
if args['owner'] is not None:
events_query = list(filter(lambda x: x['owner'].lower() == args['owner'],
events_query))

# remove generated keys

for event in events_query:
event.pop('date_dt', None)
event.pop('school_query', None)
event['date_dt'] = event['date_dt'].strftime("%A, %d %B")

return events_query


class GetSimilarEvents(Resource):
def get(self, event_id):
file_path = 'similar_events.json'
if os.path.exists(file_path):
similar_events = json.load(open(file_path, 'r'))
else:
similar_events = {}
return similar_events.get(event_id, [])

api.add_resource(GetEvent, '/api/v1/getevent')

api.add_resource(ShowEvent, '/api/v1/getevent')
api.add_resource(GetSimilarEvents, '/api/v1/getsimilarevents/<string:event_id>')

if __name__ == '__main__':
app.run(port=5001, debug=True)

0 comments on commit 8ceec38

Please sign in to comment.