def signup(credentials): """ Signup API. If credentials are valid, set the session cookie to log the requester in. :param dict credentials: The sign in credentials. :return: Status of the request. 200 if valid, 400 if not. :rtype: tuple[dict, int, dict] """ username = credentials.get('username', None) email = credentials.get('email', None) password = credentials.get('password', None) if username is not None and \ email is not None and \ password is not None: password_hash = hash_password(password) if re.match(r'[a-zA-Z0-9_\-]{4,30}', username) is not None: APP.logger.info('Creating user %s', username) try: user = User(username=username, email=email, password_hash=password_hash) DB.session.add(user) DB.session.commit() return ok_response('User created') except SQLAlchemyError: return INTERNAL_SERVER_ERROR_JSON_RESPONSE return BAD_REQUEST_JSON_RESPONSE
def checkin_event(checkin_data): """Endpoint for Checking into an event; creates a new Checkin entry""" device_key = checkin_data.get('device_key', None) event_id = checkin_data.get('event_id', None) user_latitude = checkin_data.get('user_latitude', None) user_longitude = checkin_data.get('user_longitude', None) try: account = Account.query.filter_by(device_key=device_key).first() if Checkin.query.filter_by(account_id=account.id, event_id=event_id).count() > 0: return {'msg': 'Cannot checkin to this event again.'}, 409, JSON_CT event = Event.query.filter_by(id=event_id).first() if event: if haversine_distance(event.latitude, event.longitude, user_latitude, user_longitude) <= 100: checkin = Checkin(account_id=account.id, event_id=event_id) DB.session.add(checkin) DB.session.commit() return ok_response('Successfully checked in to this event %d' % (event_id)) return BAD_REQUEST_JSON_RESPONSE return BAD_REQUEST_JSON_RESPONSE except SQLAlchemyError as exception: APP.logger.exception("Failed to checkin to this event: %s", exception) return INTERNAL_SERVER_ERROR_JSON_RESPONSE
def logout(): """ Log out the requester. :return: Status of the request. 200. :rtype: tuple[dict, int, dict] """ session.pop('username', None) return ok_response('Logged out')
def update_item(_, item_data, item_id): """ Update an item. :param dict _: Unused positional argument that use_args needs. :param dict item_data: Dict with a subset of the Item fields. :param int item_id: id of the item. :return: Status of the request. 200 if valid, 400 or 500 if not. :rtype: tuple[dict, int, dict] """ user_id = session['user_id'] name = item_data.get('name', None) try: item = DB.session.query(Item).filter_by(id=item_id, user_id=user_id) if item is None: return BAD_REQUEST_JSON_RESPONSE concrete_item = item.first() item.update({ Item.location_id: item_data.get('location_id', None) or concrete_item.location_id, Item.description: item_data.get('description', None) or concrete_item.description, Item.name: item_data.get('name', None) or concrete_item.name, Item.purchase_date: item_data.get('purchase_date', None) or concrete_item.purchase_date, Item.purchase_price: item_data.get('purchase_price', None) or concrete_item.purchase_price, Item.sell_date: item_data.get('sell_date', None) or concrete_item.sell_date, Item.sell_price: item_data.get('sell_price', None) or concrete_item.sell_price, Item.listed_price: item_data.get('listed_price', None) or concrete_item.listed_price }) DB.session.commit() return ok_response('Updated item {name}'.format(name=name)) except SQLAlchemyError as exception: APP.logger.exception('Failed to update item %s, id %d: %s', name, item_id, exception) return INTERNAL_SERVER_ERROR_JSON_RESPONSE
def delete_item(item_id): """ Delete the given item. :param int item_id: The id of the item to delete. :return: Status of the request. 200 if valid, 400 if not. :rtype: tuple[dict, int, dict] """ user_id = session['user_id'] try: row = Item.query.filter_by(id=item_id, user_id=user_id).one() DB.session.delete(row) DB.session.commit() return ok_response('Ok, this has been deleted') except SQLAlchemyError as exception: APP.logger.error('Failed to delete item %s: %s', item_id, exception) return BAD_REQUEST_JSON_RESPONSE
def hype_event(hype_data): """Endpoint for hyping an event; creates a new Hype entry""" device_key = hype_data.get('device_key', None) event_id = hype_data.get('event_id', None) try: account = Account.query.filter_by(device_key=device_key).first() if Hype.query.filter_by(account_id=account.id, event_id=event_id).count() > 0: return {'msg': 'Cannot hype an event again.'}, 409, JSON_CT hype = Hype(account_id=account.id, event_id=event_id) DB.session.add(hype) DB.session.commit() return ok_response('Successfully hyped event %d' % (event_id)) except SQLAlchemyError as exception: APP.logger.exception("Failed to hype event: %s", exception) return INTERNAL_SERVER_ERROR_JSON_RESPONSE
def login(credentials): """ Login API. If credentials are valid, set the session cookie to log the requester in. :param dict credentials: The sign in credentials. :return: Status of the request. 200 if valid, 400 if not. :rtype: tuple[dict, int, dict] """ username = credentials['username'] password_hash = hash_password(credentials['password']) user = User.query.filter_by(username=username, password_hash=password_hash).first() if user is None: return BAD_REQUEST_JSON_RESPONSE session['username'] = username session['user_id'] = user.id return ok_response('Logged in')
def create_event(event_data): """ Create an event. :param dict event_data: Dict with a subset of the Event fields. :return: Status of the request. 200 if valid, 400 or 500 if not. :rtype: tuple[dict, int, dict] """ device_key = event_data.get('device_key', None) latitude = event_data.get('latitude', None) longitude = event_data.get('longitude', None) time = event_data.get('time', None) description = event_data.get('description', None) category = event_data.get('category', None) group_size_min = event_data.get('group_size_min', None) group_size_max = event_data.get('group_size_max', None) title = event_data.get('title', None) APP.logger.info('Creating event at (%f,%f)', latitude, longitude) try: account = Account.query.filter_by(device_key=device_key).first() event = Event(account_id=account.id, latitude=latitude, longitude=longitude, time=time, group_size_max=group_size_max, group_size_min=group_size_min, title=title, description=description, category=category) DB.session.add(event) DB.session.commit() return ok_response('Added event at (%f,%f)' % (latitude, longitude)) except SQLAlchemyError as exception: APP.logger.exception('Failed to create event: %s', exception) return INTERNAL_SERVER_ERROR_JSON_RESPONSE
def create_item(item_data): """ Create an item. :param dict item_data: Dict with a subset of the Item fields. :return: Status of the request. 200 if valid, 400 or 500 if not. :rtype: tuple[dict, int, dict] """ user_id = session['user_id'] location_id = item_data.get('location_id', None) description = item_data.get('description', None) name = item_data.get('name', None) purchase_date = item_data.get('purchase_date', None) purchase_price = item_data.get('purchase_price', None) sell_date = item_data.get('sell_date', None) sell_price = item_data.get('sell_price', None) listed_price = item_data.get('listed_price', None) if name and ' ' in name: return BAD_REQUEST_JSON_RESPONSE APP.logger.info('Creating item %s', name) try: item = Item(user_id=user_id, location_id=location_id, description=description, name=name, purchase_date=purchase_date, purchase_price=purchase_price, sell_date=sell_date, sell_price=sell_price, listed_price=listed_price) DB.session.add(item) DB.session.commit() return ok_response('Added item {name}'.format(name=name)) except SQLAlchemyError as exception: APP.logger.exception('Failed to create item %s: %s', name, exception) return INTERNAL_SERVER_ERROR_JSON_RESPONSE