async def regen_api_key(req, resp): if 'authorization_key' in req.params: data = get_db_data( 'users', filter_by={'api_reset.key': req.params['authorization_key']}) resp.media = data elif 'email' in req.params: email = req.params['email'] data = get_db_data('users', filter_by={'email': email}) reset_key = update_db_data( 'users', filter_by={'email': email}, data={ '$set': { 'api_reset': { 'key': generate_api_key(35), 'expiration': maya.now().add(minutes=5).datetime() } } }, )['api_reset'] @api.background.task def key_reset(): send_reset_key_email(to=email, reset_key=reset_key) await key_reset() resp.text = 'An email with your Reset Key will be sent to you!' else: resp.status_code = 400 resp.text = 'You must supply an email or an authorization_key'
def get_conference_data(**kwargs): """returns a single conference as a ConferenceSchema item""" if '_id' in kwargs: conference_data = get_db_data(collection=collection, _id=kwargs['_id']) many = False else: conference_data = get_db_data(collection=collection, _id=None, **kwargs) many = True return ConferenceSchema(many=many).dump(conference_data)
async def on_post(self, req, resp, *, conference_id): """Creates New Conference Item""" request_media = await req.media(format='json') insert_id = load_db_data(collection='conferences', data=request_media) conference_data = get_db_data('conferences', _id=insert_id) conferences = ConferenceSchema() resp.media = conferences.dump(conference_data)
async def on_post(self, req, resp): """Creates User Account""" request_media = await req.media(format='json') email_address = request_media['email'] if not (get_db_data('users', filter_by={'email': email_address})): api_key = generate_api_key() request_media['api_key'] = api_key user_data = update_db_data('users', request_media, upsert=True) resp.media = UserSchema().dump(user_data) self.confirmation_email(resp.media[0]) else: resp.text = f'Email Account {email_address} already exists' resp.status_code = 400
async def on_put(self, req, resp, *, conference_id): """Updates an Existing Conference Item""" api_key = req.headers['Authorization'] request_media = await req.media(format='json') resp.media = get_db_data(collection='conferences', _id=insert_id)