def delete(cls, name: str): item = ItemModel.find_by_name(name) if item: item.delete_from_db() return {"message": gettext('item_deleted')}, 200 return {"message": gettext('item_not_found')}, 404
def delete(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"message": gettext('user_not_found')}, 404 user.delete_from_db() return {"message": gettext('user_deleted')}, 200
def delete(cls, name: str): store = StoreModel.find_by_name(name) if store: store.delete_from_db() return {"message": gettext('store_deleted')}, 200 return {"message": gettext('store_not_found')}, 404
def post(cls, name: str): if StoreModel.find_by_name(name): return { "message": gettext('store_name_already_exists').format(name) }, 400 store = StoreModel(name=name) try: store.save_to_db() except: return {"message": gettext('store_error_inserting')}, 500 return store_schema.dump(store), 201
def post(cls, name: str): if ItemModel.find_by_name(name): return { "message": gettext('item_name_already_exists').format(name) }, 400 item_json = request.get_json() item_json["name"] = name item = item_schema.load(item_json) try: item.save_to_db() except: return {"message": gettext('item_error_inserting')}, 500 return item_schema.dump(item), 201
def send_email(cls, email: List[str], subject: str, text: str, html: str) -> Response: if not cls.MAILGUN_DOMAIN or not cls.MAILGUN_API_KEY: raise MailGunException(gettext('mailgun_failed_load_vars')) response = post( f"https://api.mailgun.net/v3/{cls.MAILGUN_DOMAIN}/messages", auth=("api", cls.MAILGUN_API_KEY), data={ "from": f"{cls.FROM_TITLE} <{cls.FROM_EMAIL}>", "to": email, "subject": subject, "text": text, "html": html }, ) if response.status_code != 200: raise MailGunException(gettext('mailgun_error_sending_email')) return response
def get(cls, confirmation_id: str): """Return confirmation HTML page.""" confirmation = ConfirmationModel.find_by_id(confirmation_id) if not confirmation: return {'message': gettext('confirmation_not_found')} if confirmation.expired: return {'message': gettext('confirmation_expired')} if confirmation.confirmed: return {'message': gettext('confirmation_already_confirmed')} confirmation.confirmed = True confirmation.save_to_db() headers = {'Content-Type': 'text/html'} return make_response( render_template('confirmation_page.html', email=confirmation.user.email), 200, headers)
def post(cls): user_json = request.get_json() user = user_schema.load(user_json) if UserModel.find_by_username(user.username): return {"message": gettext('user_already_exists')}, 400 if UserModel.find_by_email(user.email): return {"message": gettext('user_email_already_exists')}, 400 try: user.save_to_db() confirmation = ConfirmationModel(user.id) confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext('user_success_register_message')}, 201 except MailGunException as e: user.delete_from_db() return {"message": str(e)}, 500 except: traceback.print_exc() user.delete_from_db() return {"message": gettext('user_failed_to_create')}, 500
def post(cls): user_json = request.get_json() user_data = user_schema.load( user_json, partial=("email", ) ) # partial=("email",) tells marshmallow to ignore the email field if it is not present user = UserModel.find_by_username(user_data.username) if user and safe_str_cmp(user_data.password, user.password): confirmation = user.most_recent_confirmation if confirmation and confirmation.confirmed: access_token = create_access_token(identity=user.id, fresh=True) refresh_token = create_refresh_token(user.id) return { "access_token": access_token, "refresh_token": refresh_token }, 200 return { "message": gettext('user_not_confirmed_error').format(user.username) }, 400 return {"message": gettext('user_invalid_credentials')}, 401
def post(cls, user_id: int): """Resend confirmation email.""" user = UserModel.find_by_id(user_id) if not user: return {'message': gettext('user_not_found')}, 404 try: confirmation = user.most_recent_confirmation if confirmation: if confirmation.confirmed: return { 'message': gettext('confirmation_already_confirmed') }, 400 confirmation.force_to_expire() new_confirmation = ConfirmationModel(user_id) new_confirmation.save_to_db() user.send_confirmation_email() return {"message": gettext('confirmation_resend_successful')}, 201 except MailGunException as e: return {'message': str(e)}, 500 except: traceback.print_exc() return {'message': gettext('confirmation_resend_fail')}, 500
def get(cls, user_id: int): """Return confirmations for a given user. Use for testing.""" user = UserModel.find_by_id(user_id) if not user: return {'message': gettext('user_not_found')}, 404 return ( { 'current_time': int(time()), 'confirmation': [ confirmation_schema.dump(each) for each in user.confirmation.order_by(ConfirmationModel.expire_at) ] }, 200, )
def post(cls): jti = get_raw_jwt()[ "jti"] # jti is "JWT ID", a unique identifier for a JWT. user_id = get_jwt_identity() BLACKLIST.add(jti) return {"message": gettext('user_logget_out').format(user_id)}, 200
def get(cls, user_id: int): user = UserModel.find_by_id(user_id) if not user: return {"message": gettext('user_not_found')}, 404 return user_schema.dump(user), 200
def get(cls, name: str): item = ItemModel.find_by_name(name) if item: return item_schema.dump(item), 200 return {"message": gettext('item_not_found')}, 404
def get(cls, name: str): store = StoreModel.find_by_name(name) if store: return store_schema.dump(store), 200 return {"message": gettext('store_not_found')}, 404