def put(self): user_data = request.get_json() user = UserModel.find_by_id(user_data.get('id', None)) if not user: return get_not_found_error('user') user_level = get_jwt_claims()["user_level"] current_user_id = get_jwt_identity() # user want to update other user info if current_user_id != user.id: # the user is guest and he is not allowed to do this action if user_level != UserLevel.ADMIN and user_level != UserLevel.SYS_ADMIN: return { 'message': 'you are not allowed to do this action.' }, 405 # admin or sys admin want to activate user user_data = create_request_parser([_role_arg, _status_arg]).parse_args() user.role = user_data['role'] user.activated = user_data['activated'] user.save_to_db() return {'message': 'change success.', 'user': user.json()}, 201 # update profile info if "name" in user_data: user.name = user_data.get("name") if "password" in user_data: user.password = bcrypt.generate_password_hash( user_data['password']).decode('utf-8') user.save_to_db() return {'message': 'change success.', 'user': user.json()}, 201
def put(self): data = create_request_parser([_detail_id, _created]).parse_args() detail = OrderDetailModel.find_by_id(data['detail_id']) if not detail: return get_not_found_error('detail') detail.created = data['created'] detail.user_id = get_jwt_identity() detail.save_to_db() return {'message': 'Update Success.'}
def delete(self): data = create_request_parser([_id_arg]).parse_args() order = OrderModel.find_by_id(data['id']) if not order: return get_not_found_error('order') try: order.delete_from_db() except Exception as e: return get_internal_server_error() return {'message': 'order deleted successfully'}
def get(self): details = create_request_parser( [{"name": "details", "type": inputs.boolean}]).parse_args().get('details') result = {"categories": []} if details: result['categories'] = [category.details_json() for category in CategoryModel.get_all()] else: result['categories'] = [category.json() for category in CategoryModel.get_all()] return result
def put(self): user_level = get_jwt_claims()['user_level'] if user_level == UserLevel.GUEST: return get_forbidden_error() data = create_request_parser([_name_arg, _id_arg]).parse_args() category = CategoryModel.find_by_id(data['id']) if category: category.name = data['name'] else: category = CategoryModel(data['name']) category.save_to_db() return {'category': category.json()}, 201
def post(self): user_level = get_jwt_claims()['user_level'] if user_level == UserLevel.GUEST: return get_forbidden_error() data = create_request_parser([_name_arg]).parse_args() category = CategoryModel(data['name']) if CategoryModel.find_by_name(data['name']): return {"message": "category with same name exists try another name."}, 400 try: category.save_to_db() except Exception as ex: return get_internal_server_error() return { "category": category.json() }, 201
def delete(self): user_level = get_jwt_claims()['user_level'] if user_level == UserLevel.GUEST: return get_forbidden_error() data = create_request_parser([{**_id_arg, "location": "json"}]).parse_args() product = ProductModel.find_by_id(data['id']) if not product: return get_not_found_error('product') try: product.delete_from_db() except Exception as ex: return get_internal_server_error() return { "message": "product deleted successfully" }
def post(self): data = create_request_parser( [_customer_name, _description_arg, _order_details]).parse_args() order_details = data['details'] del data['details'] user_id = get_jwt_identity() order = OrderModel(**data) order.add_details(order_details, user_id) order.user_id = user_id try: order.save_to_db() except Exception as e: return get_internal_server_error() return {'order': order.json()}, 201
def post(self): user_level = get_jwt_claims()['user_level'] if user_level == UserLevel.GUEST: return get_forbidden_error() data = create_request_parser([_name_arg, _price_arg, _category_id_arg, _receivers_args]).parse_args() image = request.files.get("image", None) product = ProductModel(data['name'], data['price'], data['category_id'], "") product.add_receivers(data.get('receivers', None)) try: product.set_image(image) product.save_to_db() except Exception as ex: return get_internal_server_error() return { "product": product.json() }, 201
def delete(self): user_level = get_jwt_claims()['user_level'] if user_level == UserLevel.GUEST: return get_forbidden_error() data = create_request_parser([_id_arg]).parse_args() category = CategoryModel.find_by_id(data['id']) if not category: return get_not_found_error('category') try: if category.category_used(): return { "message": "category used in product can't be deleted." }, 405 category.delete_from_db() except Exception as ex: return get_internal_server_error() return {"message": "category deleted successfully."}
def post(self): data = create_request_parser(auth_list).parse_args() user = UserModel.find_by_username(data['username']) if not user or not bcrypt.check_password_hash(user.password, data['password']): return {"message": "invalid username or passwrod."}, 401 if not user.activated: return { 'message': 'not allowed to login please contact admin.' }, 405 return { "message": "Login Success.", "token": create_user_token(user.id), "user": user.json() }
def put(self): data = create_request_parser( [_customer_name, _description_arg, _order_details, _id_arg]).parse_args() order = OrderModel.find_by_id(data['id']) order_details = data['details'] del data['details'] order_id = data['id'] user_id = get_jwt_identity() del data['id'] if order: order.update_order(data['customer_name'], data['description'], order_details, user_id) else: order = OrderModel(**data) order.add_details(order_details, user_id) order.user_id = user_id order.save_to_db() return {'order': order.json()}
def post(self): data = create_request_parser([*auth_list, _name_arg]).parse_args() password_required_lenght = 5 if len(data['password']) < password_required_lenght: return { "message": f"password must have at least\ {password_required_lenght} characters" }, 400 if UserModel.find_by_username(data['username']): return {"message": "username already exists"}, 400 user = UserModel(**data) user.password = bcrypt.generate_password_hash( user.password).decode('utf-8') try: user.save_to_db() except Exception as e: return get_internal_server_error() return { "message": "Registration success you must contact admin to activate your account." }, 405
def get(self): data = create_request_parser([_name_arg]).parse_args() product = ProductModel.find_by_name(data['name']) if not product: return get_not_found_error('product') return { "product": product.json() }
def get(self): data = create_request_parser([_name_arg]).parse_args() category = CategoryModel.find_by_name(data['name']) if not category: return get_not_found_error('category') return { "category": category.details_json() }
def get(self): data = create_request_parser([_id_arg]).parse_args() order = OrderModel.find_by_id(data['id']) if not order: return get_not_found_error('order') return {'order': order.json()}
def post(self): data = create_request_parser([_id_arg]).parse_args() order = OrderModel.find_by_id(data['id']) if not order: return get_not_found_error('order') try: from escpos import printer from datetime import datetime p = printer.Usb(0x0483, 0x5743, 0, 0x81, 0x03) # header p.set(align="center", text_type="B", width=2) p.text("Di Sultano Shop\n\n") p.set(align="center") p.text("Borj.Tyr\n") p.text("Tel : 71516540\n") p.text(datetime.utcnow().strftime('%d/%b/%Y %H:%M') + "\n") p.set(align="left", text_type="B") p.text(f"{'Take away':<9s}{order.customer_name:>39s}") p.text("\n") # table p.set(align="left") dash = '-' * 48 p.text(dash) p.text('{:<25s}{:>3s}{:>10s}{:>10s}'.format( "Item", "Qty", "Price", "Tot")) p.text(dash) total_price = 0 for detail in order.details: itemName = detail.product.name name = "sdf" if len(itemName) > 25: itemName = '{:.<25}'.format(itemName[:22]) quantity = detail.quantity price = "{:,.0f}".format(detail.detail_price) tot = "{:,.0f}".format(detail.quantity * detail.detail_price) total_price += detail.quantity * detail.detail_price p.text('{:<25s}{:>3d}{:>10s}{:>10s}'.format( itemName, quantity, price, tot)) p.text("\n") p.text("-" * 48) p.text("\n") # footer p.set(align="right", text_type="B") p.text("Total Invoice : {:,} L.L\n".format(total_price)) p.set(text_type="A") p.text('\n') p.set(align="left") p.text("Served by : Darine Mustafa.\n") p.text("Thank you for choosing disultano.\n\n") p.set(width=2, align="center") p.text("Challenge Test.\n\n") p.set(text_type="A") p.set(align="right") p.text("Owner: Jalal hicham.\n") p.cut() except Exception as e: return get_internal_server_error() return {'message': "order printed"}