def customer_care_terminate_order(self, reason): try: self.is_terminated = True self.termination_reason = reason cash_on_delivery = pym.CashOnDelivery.read_cash_on_delivery( payment_id=self.payment[0].payment_id) if cash_on_delivery: cash_on_delivery.status = "cancelled" if self.customer.email: mail_ = order_cancelled_email mail_.recipients = [self.customer.email] mail_.text = "We have cancelled the following order:{order}, because of the following reason: \n{reason}".format( order=self.order_ref_simple_version, reason=reason) mail_.send() session.commit() return True else: session.commit() return True except Exception as e: session.rollback() print("Terminating order error: ", e) return False
def read_product_rate(cls, product_id): try: pdt_ratings = session.query(func.count(cls.rate), cls.rate)\ .group_by(cls.rate).filter_by(product_id=product_id).all() numerator = 0 denomenator = 0 for rate in pdt_ratings: denomenator += rate[0] if rate[1] == 5: numerator += rate[0] * 100 elif rate[1] == 4: numerator += rate[0] * 80 elif rate[1] == 3: numerator += rate[0] * 60 elif rate[1] == 2: numerator += rate[0] * 40 elif rate[1] == 1: numerator += rate[0] * 20 if pdt_ratings: return int(((numerator / denomenator) / 100) * 5) else: return 0 except: session.rollback()
def __call__(self, **kwargs): try: self.product_id = kwargs.get("product_id") self.price = kwargs.get("price") self.from_date = kwargs.get("from_date") self.to_date = kwargs.get("to_date") self.is_scheduled = kwargs.get("is_scheduled") if self.is_scheduled: product_discount = self.read_product_discount(self.product_id) if product_discount: kwargs["products_discount"] = product_discount return self.update_promotional_price(**kwargs) else: product = pdts.Products.read_product(self.product_id) if product and product.price > self.price: product.promotional_price_set = True session.add(self) session.commit() else: return False return True except Exception as e: print("Adding product discount error: ", e) session.rollback() return False
def __call__(self, **kwargs): try: self.product_id = kwargs.get("product_id") self.customer_id = kwargs.get("customer_id") self.product_name = kwargs.get("product_name") self.product_image = kwargs.get("product_image") self.unit_price = kwargs.get("unit_price") self.quantity = kwargs.get("quantity") self.served_with = kwargs.get("served_with", "none") self.free_delivery = kwargs.get("free_delivery", False) self.restaurant = kwargs.get("restaurant", "clickEat") item_exists = self.query.filter_by( customer_id = self.customer_id, product_id = self.product_id, is_ordered = False ).first() if item_exists: item_exists.quantity += int(self.quantity) session.commit() # if item_exists.quantity > product.quantity: # session.rollback() # print("Product already exists on cart and quantity you are specifying is more than what is available.") # return False # else: # session.commit() else: session.add(self) session.commit() return True except Exception as e: print("Error While adding to Cart: ", e) session.rollback() return False
def home_products(cls): try: home_products = [] home_products.append({ "id": 1, "title": "Favorite Food & Snacks", "products": sample(list(food_snacks_generator(cls.query.all())), 2) }) home_products.append({ "id": 2, "title": "Drinks & Beverages", "products": sample( list( drinks_generator( cls.query.filter_by(sub_category_id=6).all())), 2) }) return home_products except: session.rollback()
def customer_order_exists(cls, customer_id): try: return session.query(cls).filter_by(customer_id=customer_id, is_paid=False, is_terminated=False).first() except: session.rollback()
def home_images(cls): try: images = cls.query.all() return [image.serialize() for image in images] except: session.rollback()
def read_product(cls, id): try: product = cls.query.filter_by(product_id=id).first() if product: return product except: session.rollback()
def check_user(cls,telephone,password): try: user = cls.query.filter_by(contact=telephone).first() if user and user.verify_password(password): token = TokenGenerator(user).generate_api_token() user = user.serializer() user["token"] = token return user elif not user: user = cls.query.filter_by(email=telephone).first() if user and user.verify_password(password): token = TokenGenerator(user).generate_api_token() user = user.serializer() user["token"] = token return user elif not user or not user.verify_password(password): return False else: return False except Exception as e: print(">>>>>>>>>>>>", e) session.rollback()
def __call__(self, **kwargs): try: self.name = kwargs.get("name") self.product_picture = kwargs.get("product_picture") self.description = kwargs.get("description") self.price = kwargs.get("price") self.resturant_id = kwargs.get("resturant_id") brand_name = kwargs.get("brand") self.sub_category_id = kwargs.get("sub_category_id") brand_exists = brnd.Brand.read_brand_filter( brnd.Brand.name.like("%{}%".format(brand_name))) if brand_exists: self.brand = brand_exists else: brand = brnd.Brand(name=brand_name) self.brand = brand self.buying_price = kwargs.get("buying_price", 0) self.selling_price = kwargs.get("selling_price", 0) self.served_with = kwargs.get("served_with", 'none') self.commission_fee = kwargs.get("commission_fee", 0.0) self.headsup = kwargs.get("headsup", "clickEat") session.add(self) session.commit() return True except Exception as e: print("Error while adding product: ", e) session.rollback() return False
def read_customer_by_contact(cls, **kwargs): try: customer = cls.query.filter_by(contact=kwargs.get("telephone")).first() return customer except: session.rollback()
def __call__(self, **kwargs): try: self.courier_name = kwargs.get("courier_name") self.driver_license_number = kwargs.get("driver_license_number") self.contact = kwargs.get("contact") self.second_contact = kwargs.get("second_contact") self.email = kwargs.get("email") self.address = kwargs.get("address") self.district = kwargs.get("district") self.vehicle_type = kwargs.get("vehicle_type") self.vehicle_license_plate_number = kwargs.get( "vehicle_license_plate_number") self.courier_pic = kwargs.get("courier_pic") self.local_council_1_letter = kwargs.get("local_council_1_letter") self.agreement_letter = kwargs.get("agreement_letter") self.national_id_number = kwargs.get("national_id_number") session.add(self) session.commit() return True except Exception as e: print("Error whilst adding courier: ", e) session.rollback() return False
def __call__(self, **kwargs): try: self.county = kwargs.get("county") self.sub_county = kwargs.get("sub_county") self.village = kwargs.get("village") self.other_details = kwargs.get("other_details") self.is_default = kwargs.get("is_default", self.is_default) self.customer_id = kwargs.get("customer_id") if self.is_default: self.query.filter_by(customer_id=self.customer_id).update( {"is_default": False}) if not self.query.filter_by(customer_id=self.customer_id).first(): self.is_default = True session.add(self) session.commit() return True except Exception as e: print("Error whilst saving customer address: ", e) session.rollback() return None
def read_customer_orders(cls, customer_id): try: customer_orders = cls.query.filter_by( customer_id=customer_id).order_by(desc(cls.order_date)).all() return [item.serialize() for item in customer_orders] except: session.rollback()
def customer_order_count(cls, customer_id): try: count = session.query(func.count(cls.id)).filter_by( is_paid=True, is_terminated=False).scalar() return count except: session.rollback()
def read_all_top_discount_products(cls): try: return list( all_top_selling_pdts_generator( cls.query.order_by(TopSellingProducts.id.desc()).all())) except Exception as e: session.rollback() print("Error While retriving records: ", e)
def product_comments(cls, product_id): try: comments = cls.query.filter_by(product_id=product_id).order_by( desc(cls.date)).all() return comments except: session.rollback()
def delete_pdt_from_top_selling(cls, product_id): try: cls.query.filter_by(product_id=product_id).delete() session.commit() return True except Exception as e: print("Error while deleting product from top selling product", e) session.rollback() return False
def change_password(self, old_password, new_password): if(self.verify_password(old_password)): new_password = pwd_context.hash(new_password) self._password = new_password session.commit() return True else: session.rollback() return False
def read_customer_count(cls): try: return session.query(func.count(cls.id)).scalar() except: session.rollback()
def read_all_orders_delivery_details_filter(cls, *args): try: return session.query(cls).join(cls.delivery_details)\ .filter( *args ).order_by(cls.id.desc()).options( lazyload("*")).all() except: session.rollback()
def get_product_comments(cls, product_id): try: comments = cls.query.filter_by(product_id=product_id).order_by( desc(cls.date)).all() pdt_comments = (comment.serialize() for comment in comments) return pdt_comments except: session.rollback()
def delete_customer_address(cls, id): try: session.query(cls).filter_by(id=id).delete() session.commit() return True except Exception as e: print("Error: >>>>>>>>>>>>>", e) session.rollback() return False
def get_customer_addresses(cls, customer_id): try: customer_addresses = cls.query.filter_by( customer_id=customer_id).all() return [ customer_address.serialize() for customer_address in customer_addresses ] except: session.rollback()
def update_employee_details(self, **kwargs): try: self.email = kwargs.get("email", self.email) self.name = kwargs.get("name", self.name) self.contact = kwargs.get("contact", self.contact) self.address = kwargs.get("address", self.address) session.commit() except Exception as e: session.rollback() raise Exception(f"Updating user error: {e}")
def __call__(self, name): try: self.name = name session.add(self) session.commit() except Exception as e: print("Adding brand error: ", e) session.rollback() raise
def read_product_by_sub_cat(cls, sub_category_id): try: product = cls.query.filter_by( sub_category_id=sub_category_id).first() if product.approved and product.suspend != True: return product else: return None except: session.rollback()
def read_customer_cart_items(cls, customer_id): try: cart_items = cls.query.filter_by( customer_id=customer_id, is_ordered=False ).all() return cls.update_cart_items_prices(cart_items) except: session.rollback()
def read_orders_not_prepared_count(cls): try: return session.query(func.count(cls.id))\ .filter( and_( cls.is_prepared==False, cls.is_terminated == False ) ).scalar() except: session.rollback()
def delete_cart_item(cls, id): try: product = cls.query.filter_by(product_id=id).first() cls.query.filter_by(product_id=id).delete() session.commit() return cls.read_customer_cart_items(product.customer_id) except Exception as e: print("Error whilst deleting cart item!!.", e) session.rollback() return False