def on_get(self, req, resp, event_id=None): token = req.context['token'] user_id = Token.getUserId(token) db_session = req.context['session'] auth_events = Token.getAuthEvents(token, session=db_session) # Get the base for the events list events = db_session.query(Event).filter(Event.id.in_(auth_events)) # Get the filters from the querystring past = req.get_param_as_bool('past') mine = req.get_param_as_bool('mine') gps = req.get_param('gps') if past and past is not None: events = events.filter(Event.is_past == false) if mine: events = events.filter(Event.owner_id == user_id) if gps: # TODO: Implement near-me GPS info pass print(events.all()) if event_id: events = events.filter(Event.id == event_id) ret = [] for event in events.all(): e = event.json() e['mine'] = e['owner_id'] == user_id ret.append(e) resp.body = json.dumps(ret) db_session.close()
def on_get(self, req, resp, activity_id=None, event_id=None): token = req.context['token'] user_id = Token.getUserId(token) db_session = req.context['session'] auth_events = Token.getAuthEvents(token, session=db_session) activities = db_session.query(Activity).filter( and_(Activity.event_id.in_(auth_events))) if activity_id: activities = activities.filter(Activity.id == activity_id) print(req.params) for key, value in req.params.items(): if key in TAG_CATEGORIES: print("Requested param: ", key) activities = activities.join(activity_tag).join(Tag).filter( Tag.tag_category_id == TAG_CATEGORIES[key]).filter( Tag.name == value) ret = [] for activity in activities: a = activity.json() ret.append(a) resp.body = json.dumps(ret) db_session.close()
def on_put(self, req, resp, activity_id=None): if not activity_id: raise falcon.HTTPBadRequest('Invalid Request', 'You must specify which room to edit') event_id = req.get_param_as_int('event_id') token = req.context['token'] session = req.context['session'] auth_events = Token.getAuthEvents(token, session=session, include_past=true) if event_id not in auth_events: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to edit rooms for this location." + str(event_id) + str(auth_events)) activity = session.query(Activity).get(int(activity_id)) if not activity.event_id == event_id: raise falcon.HTTPUnauthorized("Unauthorized", activity.json()) activity.update(req.params) session.commit() session.flush() session.refresh(activity) resp.body = json.dumps(activity.json())
def on_post(self, req, resp): event_id = req.get_param_as_int('event_id') token = req.context['token'] session = req.context['session'] auth_events = Token.getAuthEvents(token, session=session, include_past=true) if event_id not in auth_events: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to add activities to this event." + str(event_id) + str(auth_events)) activity = Activity() session.add(activity) m2m_params = {"tags": req.params['tags']} req.params.pop('tags') activity.update(req.params) session.commit() session.flush() session.refresh(activity) # session.refresh(activity) activity.update(m2m_params) # print(req) resp.body = json.dumps(activity.json())
def on_get(self, req, resp, location_id=None, floor_id=None, room_id=None): token = req.context['token'] db_session = req.context['session'] auth_locations = Token.getAuthLocations(token, session=db_session) rooms = db_session.query(Room).join(Floor).filter( Floor.location_id.in_(auth_locations)) if not room_id: room_id = req.get_param('room_id') if not location_id: location_id = req.get_param('location_id') if not floor_id: floor_id = req.get_param('floor_id') if room_id: rooms = rooms.filter(Room.id == room_id) if floor_id: rooms = rooms.filter(Room.floor_id == floor_id) if location_id: rooms = rooms.filter(Floor.location_id == location_id) if not (location_id or room_id or floor_id): db_session.close() raise falcon.HTTPBadRequest( "error", "Your request was too broad. Please specify a location or floor." ) ret = [] for room in rooms.all(): ret.append(room.json()) db_session.close() resp.body = json.dumps(ret)
def on_get(self, req, resp, tag_id=None): token = req.context['token'] user_id = Token.getUserId(token) db_session = req.context['session'] auth_events = Token.getAuthEvents(token, session=db_session) tags = db_session.query(Tag) if tag_id: tags = tags.filter(Tag.id == tag_id) if req.get_param('event_id'): tags = tags.join(Activity).filter( Activity.event_id == req.get_param('event_id')).filter( Activity.event_id.in_(auth_events)) ret = [tag.json() for tag in tags] resp.body = json.dumps(ret)
def on_post(self, req, resp): event_id = req.get_param_as_int('event_id') token = req.context['token'] session = req.context['session'] auth_events = Token.getAuthEvents(token, session=session, include_past=true) if not event_id in auth_events: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to add hosts to this event." + str(event_id) + str(auth_events)) host = Host() session.add(host) auth_activities = Token.getAuthActivities(token, session) if 'activities' in req.params: req_activities = req.params.pop('activities') else: req_activities = [] fk_params = { "activities": [ activity for activity in req_activities if activity in auth_activities ] } host.update(req.params) session.commit() session.flush() session.refresh(host) host.update(fk_params) resp.body = json.dumps(host.json())
def on_post(self, req, resp): token = req.context['token'] session = req.context['session'] if not Token.getUserId(token): raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to create tags") tag = Tag() session.add(tag) tag.update(req.params) session.commit() session.flush() session.refresh(tag) resp.body = json.dumps(tag.json())
def on_get(self, req, resp, location_id=None, floor_id=None): token = req.context['token'] user_id = Token.getUserId(token) db_session = req.context['session'] auth_locations = Token.getAuthLocations(token, session=db_session) floors = db_session.query(Floor).filter( Floor.location_id.in_(auth_locations)) if not location_id: location_id = req.get_param('location_id') if location_id: floors = floors.filter(Floor.location_id == location_id) if not floor_id: floor_id = req.get_param('floor_id') if floor_id: print("Floor ID!", floor_id) floors = floors.filter(Floor.id == floor_id) ret = [] for floor in floors.all(): ret.append(floor.json()) resp.body = json.dumps(ret) db_session.close()
def on_post(self, req, resp): floor_id = req.get_param_as_int('floor_id') token = req.context['token'] session = req.context['session'] auth_floors = Token.getAuthLocations(token, session=session) if floor_id not in auth_floors: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to add rooms to this floor.") room = Room() session.add(room) room.update(req.params) session.commit() session.flush() session.refresh(room) resp.body = json.dumps(room.json())
def on_post(self, req, resp): location_id = req.get_param_as_int('location_id') token = req.context['token'] session = req.context['session'] auth_locations = Token.getAuthLocations(token, session=session) if not location_id in auth_locations: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to add floors to this location.") floor = Floor() session.add(floor) floor.update(req.params) session.commit() session.flush() session.refresh(floor) resp.body = json.dumps(floor.json())
def on_delete(self, req, resp, activity_id=None): if not activity_id: raise falcon.HTTPBadRequest( 'Invalid Request', 'You must specify which activity to delete') token = req.context['token'] session = req.context['session'] auth_events = Token.getAuthEvents(token, session=session) activity = session.query(Activity).get(int(activity_id)) if activity.event_id not in auth_events: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to delete activities for this event") session.delete(activity) session.commit() resp.body = "{}" resp.status = falcon.HTTP_204
def on_put(self, req, resp, host_id=None): if not host_id: raise falcon.HTTPBadRequest('Invalid Request', 'You must specify which host to edit') token = req.context['token'] session = req.context['session'] auth_events = Token.getAuthEvents(token, session=session) host = session.query(Host).get(int(host_id)) if host.event_id not in auth_events: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to edit hosts for this event") host.update(req.params) session.commit() session.flush() session.refresh(host) resp.body = json.dumps(host.json())
def on_delete(self, req, resp, host_id=None): if not host_id: raise falcon.HTTPBadRequest( 'Invalid Request', 'You must specify which host to delete') token = req.context['token'] session = req.context['session'] auth_events = Token.getAuthEvents(token, session=session) host = session.query(Host).get(int(host_id)) if host.event_id not in auth_events: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to delete hosts for this event") session.delete(host) session.commit() resp.body = "{}" resp.status = falcon.HTTP_204
def on_delete(self, req, resp, room_id=None): if not room_id: raise falcon.HTTPBadRequest( 'Invalid Request', 'You must specify which room to delete') token = req.context['token'] session = req.context['session'] auth_locations = Token.getAuthLocations(token, session=session) room = session.query(Room).get(req.get_param_as_int('id')) if room.floor.location_id not in auth_locations: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to delete rooms for this location.") session.delete(room) session.commit() resp.body = "{}" resp.status = falcon.HTTP_204
def on_get(self, req, resp, event_id=None, activity_id=None, host_id=None): token = req.context['token'] db_session = req.context['session'] auth_events = Token.getAuthEvents(token, session=db_session) if not activity_id: activity_id = req.get_param('activity_id') if not event_id: event_id = req.get_param('event_id') if not host_id: host_id = req.get_param('host_id') hosts = db_session.query(Host) if activity_id: hosts = hosts.join(Activity).filter( and_(Activity.id == activity_id, Activity.event_id.in_(auth_events))) elif event_id: hosts = hosts.filter( and_(Host.event_id.in_(auth_events), Host.event_id == event_id)) elif host_id: hosts = [hosts.get(host_id)] else: db_session.close() raise falcon.HTTPBadRequest( "error", "Your request was too broad. Please specify an activity or event." ) ret = [] for host in hosts: if host: ret.append(host.json()) resp.body = json.dumps(ret) db_session.close()
def on_post(self, req, resp): # start_date = arrow.get(req.get_param('start')).datetime if req.get_param else None # end_date = arrow.get(req.get_param('end')).datetime if req.get_param else None token = req.context['token'] user_id = Token.getUserId(token) location_id = req.get_param_as_int('location_id') location = get_location_by_id(location_id) if location.owner_id != user_id: raise falcon.HTTPBadRequest( "Not Authorized", "This user is not authorized to access that location.") new_event = Event( title=req.get_param('title'), start=req.get_param_as_datetime('start', '%Y-%m-%dT%H:%M:%S.%fZ'), end=req.get_param_as_datetime('end', '%Y-%m-%dT%H:%M:%S.%fZ'), location_id=req.get_param('location'), owner_id=user_id) returned_event = add_to_db(new_event) resp.body = json.dumps(returned_event)
def on_put(self, req, resp, room_id=None): if not room_id: raise falcon.HTTPBadRequest('Invalid Request', 'You must specify which room to edit') # room_id = req.get_param_as_int('event_id') token = req.context['token'] session = req.context['session'] auth_locations = Token.getAuthLocations(token, session=session) room = session.query(Room).get(req.get_param_as_int('id')) if room.floor.location_id not in auth_locations: raise falcon.HTTPUnauthorized( "Unauthorized", "You are not allowed to edit rooms for this location.") room.update(req.params) session.commit() session.flush() session.refresh(room) resp.body = json.dumps(room.json())
from conconnect.cc_data.controllers.TagController import TagController # Import all controllers from conconnect.cc_data.controllers.TokenController import Token from conconnect.cc_data.controllers.UserController import UserController app = application = falcon.API( 'application/json', middleware=[ Authentication(), JSONDecoding(), CrossOrigin(), Session() ] ) token_controller = Token() activity_controller = ActivityController() activity_tag_controller = ActivityTagController() event_controller = EventController() floor_controller = FloorController() host_controller = HostController() location_controller = LocationController() room_controller = RoomController() tag_controller = TagController() tag_category_controller = TagCategoryController() user_controller = UserController() app.add_route('/activity', activity_controller) app.add_route('/activity/{activity_id}', activity_controller)