def catch_order(self, _order_id, _delivery_id, offer_info): from src.app import db from src.auth.models.order_table import OrderModel from src.auth.services.user_service import UserService from src.auth.models.user_table import NormalUserModel, DeliveryUserModel order = OrderModel.get_instance(_order_id) user_service = UserService() if order.payWithPoints: #chequeo que el que acepto la orden sea un usuario normal, no delivery try: delivery_user = user_service.get_normal_user(_delivery_id) user = user_service.get_normal_user(order.user_id) user.favourPoints -= offer_info["points"] order.favourPoints = offer_info["points"] except: raise NotFoundException("ID invalido: Solo los usuarios comunes pueden aceptar favores") else: try: order.delivery_price = offer_info["delivery_price"] order.delivery_pay = offer_info["delivery_pay"] user_service.get_delivery_user(_delivery_id) except: raise NotFoundException("ID invalido: Delivery inexistente") user_service.user_start_working(_delivery_id, _order_id) user_service.wait_order(order.user_id) order.delivery_id = _delivery_id self.change_order_state(_order_id, "onWay") if order.discount: user_service.discount_favour_point(order.user_id, 10) order.save() return order
def cancel_premium_subscription(_id): service = UserService() user = service.get_normal_user(_id) if not user: raise NotFoundException("user with that id doesn't exist.") service.update_user(int(_id),{"suscripcion": "flat"}) return jsonify({"msg":"subscription updated to flat"}),200
def user_order_by_favour(self, user_id, points_for_favour): from src.auth.models.user_table import NormalUserModel try: user = self.get_user(user_id) except NotFoundException: raise NotFoundException( "ID invalido: Solo los usuarios comunes pueden solicitar favores" ) return (user.favourPoints >= points_for_favour)
def set_premium_subscription(_id): service = UserService() user = service.get_normal_user(_id) if not user: raise NotFoundException("user with that id doesn't exist.") content = request.get_json() card = card_schema.load(content) service.update_user(int(_id),{"suscripcion": "premium"}) return jsonify({"msg":"subscription updated to premium"}),200
def _get_userModel_email(self, email): from src.auth.models.user_table import UserModel try: user = UserModel.query.filter_by(email=email).one() except NoResultFound: raise NotFoundException("No user with provided email") except: raise else: return user
def get_sample_products(self, quantity): filename = 'src/auth/services/default_products.json' try: with open(filename, 'r') as f: datastore = json.load(f) products = random.sample(datastore["products"], quantity) except: raise NotFoundException( "Error con el archivo '{}'".format(filename)) else: return products
def update_user(self, _id, data): try: user = self.get_normal_user(_id) except Exception: user = self.get_delivery_user(_id) except Exception: user = None finally: if not user: raise NotFoundException( "El user id que se quiere actualziar es invalido") user_data = self.get_user(_id, dict_format=True) user_data.update(data) assert user.update(user_data) == True
def set_order_price(self, order_id): from src.auth.models.order_table import OrderProductsModel from src.auth.models.product_table import ProductModel order = self.get_order(order_id) order_products = OrderProductsModel.query.filter_by(order_id=order_id) price = 0.0 for order_product in order_products: product = ProductModel.query.get(order_product.product_id) if not product: raise NotFoundException("El producto que quiere agregar no existe") price += (product.price * order_product.units) if order.discount: price = price - 0.2*price order.price = price order.save()
def get_instance(cls, id): response = cls.query.get(id) if not response: raise NotFoundException("Invalid ID") return response
def get_user(user_id): response = NormalUserModel.query.get(user_id) if not response: raise NotFoundException("Invalid ID") return response
def get_delivery(user_id): response = DeliveryUserModel.query.get(user_id) if not response: raise NotFoundException("Invalid ID") return response
def get_product(product_id): response = ProductModel.query.get(product_id) if not response: raise NotFoundException("Invalid ID") return response
def get_offer(offer_id): response = OrderOffersModel.query.get(offer_id) if not response: raise NotFoundException("Invalid ID") return response