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 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 delete(self): user_level = get_jwt_claims()["user_level"] if user_level != UserLevel.ADMIN and user_level != UserLevel.SYS_ADMIN: return {'message': 'you are not allowed to do this action.'}, 405 data = request.get_json() user = UserModel.find_by_id(data['id']) if user: try: user.delete_from_db() return {"message": "user deleted"} except Exception as e: return get_internal_server_error() return get_not_found_error('user')
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 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"}
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() }