def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('price', type=int, required=True) parser.add_argument('description', type=str, required=True) parser.add_argument('category', type=str, required=True) parser.add_argument('availability', type=int, required=True) args = parser.parse_args(strict=True) try: utils.non_empty_str(args['name'], 'name') except ValueError: return None, BAD_REQUEST if args["availability"] < 0: return None, BAD_REQUEST obj = Item.create( uuid=uuid.uuid4(), name=args["name"], price=args["price"], description=args["description"], category=args["category"], availability=args["availability"] ) return obj.json(), CREATED
def put(self, uuid): try: obj = Item.get(uuid=uuid) except Item.DoesNotExist: return None, NOT_FOUND parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('price', type=int, required=True) parser.add_argument('description', type=str, required=True) parser.add_argument('category', type=str, required=True) parser.add_argument('availability', type=int, required=True) args = parser.parse_args(strict=True) try: utils.non_empty_str(args['name'], 'name') except ValueError: return None, BAD_REQUEST if args["availability"] < 0: return None, BAD_REQUEST obj.name = args["name"] obj.price = args["price"] obj.description = args["description"] obj.category = args["category"] obj.availability = args["availability"] obj.save() return obj.json(), OK
def put(self, uuid): if not g.current_user.superuser: return None, UNAUTHORIZED try: obj = Item.get(Item.uuid == uuid) except Item.DoesNotExist: return None, NOT_FOUND parser = reqparse.RequestParser() parser.add_argument('name', type=str, required=True) parser.add_argument('price', type=int, required=True) parser.add_argument('description', type=str, required=True) parser.add_argument('category', type=str, required=True) parser.add_argument('availability', type=int, required=True) args = parser.parse_args(strict=True) try: utils.non_empty_str(args['name'], 'name') except ValueError: return None, BAD_REQUEST if args['availability'] < 0: return None, BAD_REQUEST obj.name = args['name'] obj.price = args['price'] obj.description = args['description'] obj.category = args['category'] obj.availability = args['availability'] obj.save() return obj.json(), OK
def patch(self, uuid): try: obj = Item.get(Item.uuid == uuid) except Item.DoesNotExist: return None, NOT_FOUND parser = reqparse.RequestParser() parser.add_argument('name', type=str) parser.add_argument('price', type=int) parser.add_argument('description', type=str) parser.add_argument('category', type=str) parser.add_argument('availability', type=int) args = parser.parse_args(strict=True) try: utils.non_empty_str(args['name'], 'name') except ValueError: return None, BAD_REQUEST if args['availability'] is not None: if args['availability'] < 0: return None, BAD_REQUEST for attr in args.keys(): if args.get(attr) is not None: setattr(obj, attr, args[attr]) obj.save() return obj.json(), OK
def put(self, uuid): if not g.current_user.superuser: return None, UNAUTHORIZED try: obj = Item.get(Item.uuid == uuid) except Item.DoesNotExist: return None, NOT_FOUND jsondata = request.get_json() try: utils.non_empty_str(jsondata['name'], 'name') except ValueError: return None, BAD_REQUEST try: Item.verify_json(jsondata) except ValidationError as ver_json_error: return ver_json_error.message, BAD_REQUEST obj.name = jsondata['name'] obj.price = jsondata['price'] obj.description = jsondata['description'] obj.category = jsondata['category'] obj.availability = jsondata['availability'] obj.save() return obj.json(), OK
def main(first_name, last_name, email, password): click.echo( '####################\n####################\nADMIN USER SCRIPT\n') click.echo('####################\n####################\n') click.echo( 'Here you can create an admin user. For eachone you have to insert:\n') click.echo('first name\n-last name\n-email\n-password') if not first_name: first_name = click.prompt('Please enter your first name') if not last_name: last_name = click.prompt('Please enter your last name') if not email: email = click.prompt('Please enter a valid email') if not password: password = click.prompt('Please enter your password') request_data = { 'first_name': first_name, 'last_name': last_name, 'email': email, 'password': password } for field in request_data: try: value = request_data[field] non_empty_str(value, field) except ValueError: print('ERROR! Some fields are empty or required') sys.exit(-1) # If email is present in the database return a ERROR and close the program. if User.exists(request_data['email']): print('ERROR! email already exists') sys.exit(-1) User.create(uuid=uuid.uuid4(), first_name=first_name, last_name=last_name, email=email, password=User.hash_password(password), admin=True) print("Great! Insert successfully")
def post(self): if not g.current_user.superuser: return None, UNAUTHORIZED jsondata = request.get_json() try: utils.non_empty_str(jsondata['name'], 'name') except ValueError: return None, BAD_REQUEST try: Item.verify_json(jsondata) except ValidationError as ver_json_error: return ver_json_error.message, BAD_REQUEST obj = Item.create(uuid=uuid.uuid4(), name=jsondata['name'], price=jsondata['price'], description=jsondata['description'], category=jsondata['category'], availability=jsondata['availability']) return obj.json(), CREATED