def test_write(self): (DbProxy.get_instance()).write = MagicMock(return_value=1) res = UserData.get_instance().write("eytan", "pswd") self.assertEqual(1, res) (DbProxy.get_instance()).write = MagicMock(return_value=0) res = UserData.get_instance().write("eytan", "pswd") self.assertEqual(0, res)
def test_delete(self): (DbProxy.get_instance()).delete = MagicMock(return_value=1) res = (UserData.get_instance()).delete(username="******") self.assertEqual(1, res) res = (UserData.get_instance()).delete(password="******") self.assertEqual(1, res) (DbProxy.get_instance()).delete = MagicMock(return_value=0) res = (UserData.get_instance()).delete(is_system_manager=True) self.assertEqual(0, res)
def delete(self, policy_ref: int = None, precondition_product: str = "", precondition_min_amount: int = None, precondition_min_basket_price: float = None): """ Delete users from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: the number of deleted rows. """ const_lst = [] if not (policy_ref is None): const_lst.append( Expression(self.__tbl.policy_ref, OP.EQ, policy_ref)) if not (precondition_product == ""): const_lst.append( Expression(self.__tbl.precondition_product, OP.EQ, precondition_product)) if not (precondition_min_amount is None): const_lst.append( Expression(self.__tbl.precondition_min_amount, OP.EQ, precondition_min_amount)) if not (precondition_min_basket_price is None): const_lst.append( Expression(self.__tbl.precondition_min_basket_price, OP.EQ, precondition_min_basket_price)) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)
def write(self, product_name: str, store_name: str, price: float, category: str, amount: int, purchase_type: int): """ Write a product to db. :param purchase_type: :param product_name: pk :param store_name: pk :param price: :param category: :param amount: :return: size of user tbl after inserting the new one. """ attributes_as_dictionary = { self.__attributes_as_dictionary[self.__attr_product_name]: product_name, self.__attributes_as_dictionary[self.__attr_store_name]: store_name, self.__attributes_as_dictionary[self.__attr_price]: price, self.__attributes_as_dictionary[self.__attr_category]: category, self.__attributes_as_dictionary[self.__attr_amount]: amount, self.__attributes_as_dictionary[self.__attr_purchase_type]: purchase_type } return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def delete(self, purchase_id: int = None, product_name: str = "", product_purchase_price: float = None, amount: int = None): """ Delete store owner appointments from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: the number of deleted rows. """ const_lst = [] if not (purchase_id is None): const_lst.append((Expression(self.__tbl.purchase_id, OP.EQ, purchase_id))) if not (product_name == ""): const_lst.append((Expression(self.__tbl.product_name, OP.EQ, product_name))) if not (product_purchase_price is None): const_lst.append((Expression(self.__tbl.product_purchase_price, OP.EQ, product_purchase_price))) if not (amount is None): const_lst.append((Expression(self.__tbl.amount, OP.EQ, amount))) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)
def read(self, attributes_to_read: [str], username: str = "", password: str = "", is_system_manager: bool = None): """ Read users from db. Raise exception if an attribute in attributes_to_read is illegal. <attribute> will composite a constraint of where to read. example(if old_username != ""), it will composite the constraint- where(user.username == username). :param attributes_to_read: the list of attributes that will return from the db. type: [str] :param username: pk :param password: :param is_system_manager: :return: list of dictionaries that contain attributes_to_read fields. """ if len(attributes_to_read) > 0: for attribute in attributes_to_read: if attribute.lower( ) not in self.__attributes_as_dictionary.keys(): raise AttributeError("Attribute " + attribute + " doesn't exist in " + str(type(self.__tbl)) + ".") else: attributes_to_read = [ self.__attr_username, self.__attr_password, self.__attr_is_system_manager ] const_lst = [] if not (username == ""): const_lst.append(Expression(self.__tbl.username, OP.EQ, username)) if not (password == ""): const_lst.append(Expression(self.__tbl.password, OP.EQ, password)) if is_system_manager is not None: const_lst.append( Expression(self.__tbl.is_system_manager, OP.EQ, is_system_manager)) where_expr = and_exprs(const_lst) result = (DbProxy.get_instance()).read(self.__tbl, where_expr=where_expr) output_lst = [] for data_obj in result: user_data_as_dictionary = {} if self.__attr_username in attributes_to_read: user_data_as_dictionary[ self.__attr_username] = data_obj.username if self.__attr_password in attributes_to_read: user_data_as_dictionary[ self.__attr_password] = data_obj.password if self.__attr_is_system_manager in attributes_to_read: user_data_as_dictionary[ self.__attr_is_system_manager] = data_obj.is_system_manager output_lst.append(user_data_as_dictionary) return output_lst
def delete(self, username: str = "", password: str = "", is_system_manager: bool = None): """ Delete users from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :param username: pk. will composite a constraint of where to update. :param password: will composite a constraint of where to update. :param is_system_manager: will composite a constraint of where to update. :return: the number of deleted rows. """ const_lst = [] if not (username == ""): const_lst.append(Expression(self.__tbl.username, OP.EQ, username)) if not (password == ""): const_lst.append(Expression(self.__tbl.password, OP.EQ, password)) if is_system_manager is not None: const_lst.append( Expression(self.__tbl.is_system_manager, OP.EQ, is_system_manager)) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)
def delete(self, policy_ref: int = None, policy1_ref: int = None, policy2_ref: int = None, flag: int = None): """ Delete users from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: the number of deleted rows. """ const_lst = [] if not (policy_ref is None): const_lst.append( Expression(self.__tbl.policy_ref, OP.EQ, policy_ref)) if not (policy1_ref is None): const_lst.append( Expression(self.__tbl.policy1_ref, OP.EQ, policy1_ref)) if not (policy2_ref is None): const_lst.append( Expression(self.__tbl.policy2_ref, OP.EQ, policy2_ref)) if not (flag is None): const_lst.append(Expression(self.__tbl.flag, OP.EQ, flag)) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)
def update(self, old_username: str = "", old_product_ref: int = None, old_amount: int = None, new_username: str = "", new_product_ref: int = None, new_amount: int = None): """ Update products in baskets in the db. old_<attribute> will composite a constraint of where to update. example(if old_username != ""), it will composite the constraint- where(user.username == old_username). new_<attribute> will update the <attribute> to the new value. example(if new_username != ""), it will update- update(user.username = new_username). :return: the number of updated rows. """ const_lst = [] if not (old_username == ""): const_lst.append(Expression(self.__tbl.username, OP.EQ, old_username)) if not (old_product_ref is None): const_lst.append(Expression(self.__tbl.product_ref, OP.EQ, old_product_ref)) if not (old_amount is None): const_lst.append(Expression(self.__tbl.amount, OP.EQ, old_amount)) where_expr = and_exprs(const_lst) attributes_as_dictionary = {} if not (new_username == ""): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_username]] = new_username if not (new_product_ref is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_product_ref]] = new_product_ref if not (new_amount is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_amount]] = new_amount if len(attributes_as_dictionary) == 0: raise AttributeError("Nothing to update") return DbProxy.get_instance().update(self.__tbl, attributes_as_dictionary, where_expr)
def delete(self, appointee_username: str = "", store_name: str = "", appointer_username: str = ""): """ Delete store owner appointments from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: the number of deleted rows. """ const_lst = [] if not (appointee_username == ""): const_lst.append((Expression(self.__tbl.appointee_username, OP.EQ, appointee_username))) if not (store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, store_name)) if not (appointer_username == ""): const_lst.append((Expression(self.__tbl.appointer_username, OP.EQ, appointer_username))) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)
def write(self, policy_name: str, store_name: str, product_name: str, percentage: float, valid_until: datetime, is_active: bool = True): """ Write a store to db. :param store_name: :param founder_username: :return: size of user tbl after inserting the new one. """ attributes_as_dictionary = { self.__attributes_as_dictionary[self.__attr_policy_name]: policy_name, self.__attributes_as_dictionary[self.__attr_store_name]: store_name, self.__attributes_as_dictionary[self.__attr_product_name]: product_name, self.__attributes_as_dictionary[self.__attr_percentage]: percentage, self.__attributes_as_dictionary[self.__attr_valid_until]: valid_until, self.__attributes_as_dictionary[self.__attr_is_active]: is_active } return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def write(self, date: datetime = datetime(datetime.now().year, datetime.now().month, datetime.now().day), guests: int = 0, subscribers: int = 0, store_managers: int = 0, store_owners: int = 0, system_managers: int = 0): """ Write a statistics to db. :return: size of user tbl after inserting the new one. """ attributes_as_dictionary = { self.__attributes_as_dictionary[self.__attr_date]: date, self.__attributes_as_dictionary[self.__attr_guests]: guests, self.__attributes_as_dictionary[self.__attr_subscribers]: subscribers, self.__attributes_as_dictionary[self.__attr_store_managers]: store_managers, self.__attributes_as_dictionary[self.__attr_store_owners]: store_owners, self.__attributes_as_dictionary[self.__attr_system_managers]: system_managers } return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def write(self, policy_ref: int = None, precondition_product: str = "", precondition_min_amount: int = None, precondition_min_basket_price: float = None): """ Write a store to db. :param store_name: :param founder_username: :return: size of user tbl after inserting the new one. """ attributes_as_dictionary = { self.__attributes_as_dictionary[self.__attr_policy_ref]: policy_ref, self.__attributes_as_dictionary[self.__attr_precondition_product]: precondition_product, self.__attributes_as_dictionary[self.__attr_precondition_min_amount]: precondition_min_amount, self.__attributes_as_dictionary[self.__attr_precondition_min_basket_price]: precondition_min_basket_price } return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def read(self, attributes_to_read: [str], appointee_username: str = "", store_name: str = "", appointer_username: str = ""): """ Read store owner appointment from db. Raise exception if an attribute in attributes_to_read is illegal. <attribute> will composite a constraint of where to read. example(if old_username != ""), it will composite the constraint- where(user.username == username). :param attributes_to_read: lst of attributes to read. :param appointee_username: pk. :param store_name: pk. :param appointer_username: :return: dict of the result data. """ if len(attributes_to_read) > 0: for attribute in attributes_to_read: if attribute.lower( ) not in self.__attributes_as_dictionary.keys(): raise AttributeError("Attribute " + attribute + " doesn't exist in " + str(type(self.__tbl)) + ".") else: attributes_to_read = self.__attributes_as_dictionary.keys() const_lst = [] if not (appointee_username == ""): const_lst.append((Expression(self.__tbl.appointee_username, OP.EQ, appointee_username))) if not (store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, store_name)) if not (appointer_username == ""): const_lst.append((Expression(self.__tbl.appointer_username, OP.EQ, appointer_username))) where_expr = and_exprs(const_lst) result = (DbProxy.get_instance()).read(self.__tbl, where_expr=where_expr) output_lst = [] for data_obj in result: data_as_dictionary = {} if self.__attr_appointee_username in attributes_to_read: data_as_dictionary[ self. __attr_appointee_username] = data_obj.appointee_username if self.__attr_store_name in attributes_to_read: data_as_dictionary[ self.__attr_store_name] = data_obj.store_name if self.__attr_appointer_username in attributes_to_read: data_as_dictionary[ self. __attr_appointer_username] = data_obj.appointer_username output_lst.append(data_as_dictionary) return output_lst
def test_update(self): try: (UserData.get_instance()).update() self.fail() except AttributeError: pass except Exception: self.fail() DbProxy.get_instance().update = MagicMock(return_value=1) res = UserData.get_instance().update(new_is_system_manager=True) self.assertEqual(res, 1) DbProxy.get_instance().update = MagicMock(return_value=1) res = UserData.get_instance().update(old_password="******", old_is_system_manager=True, new_is_system_manager=False) self.assertEqual(res, 1)
def update(self, old_username: str = "", old_password: str = "", old_is_system_manager: bool = None, new_username: str = "", new_password: str = "", new_is_system_manager: bool = None): """ Update users in the db. old_<attribute> will composite a constraint of where to update. example(if old_username != ""), it will composite the constraint- where(user.username == old_username). new_<attribure> will update the <attribute> to the new value. example(if new_username != ""), it will update- update(user.username = new_username). :param old_username: pk. will composite a constraint on the username. :param old_password: will composite a constraint on the password. :param old_is_system_manager: will composite a constraint on the is_system_manager. :param new_username: pk. will update the username to the new value. :param new_password: will update the password to the new value. :param new_is_system_manager: will update the is_system_manager to the new value. :return: the number of updated rows. """ const_lst = [] if not (old_username == ""): const_lst.append( Expression(self.__tbl.username, OP.EQ, old_username)) if not (old_password == ""): const_lst.append( Expression(self.__tbl.password, OP.EQ, old_password)) if old_is_system_manager is not None: const_lst.append( Expression(self.__tbl.is_system_manager, OP.EQ, old_is_system_manager)) where_expr = and_exprs(const_lst) attributes_as_dictionary = {} if not (new_username == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_username]] = new_username if not (new_password == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_password]] = new_password if new_is_system_manager is not None: attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_is_system_manager]] = \ new_is_system_manager if len(attributes_as_dictionary) == 0: raise AttributeError("Nothing to update") return DbProxy.get_instance().update(self.__tbl, attributes_as_dictionary, where_expr)
def read(self, attributes_to_read: [str], store_name="", founder_username=""): """ Read users from db. Raise exception if an attribute in attributes_to_read is illegal. <attribute> will composite a constraint of where to read. example(if old_username != ""), it will composite the constraint- where(user.username == username). :param founder_username: :param store_name: :param attributes_to_read: the list of attributes that will return from the db. type: [str] :return: list of dictionaries that contain attributes_to_read fields. """ if len(attributes_to_read) > 0: for attribute in attributes_to_read: if attribute.lower( ) not in self.__attributes_as_dictionary.keys(): raise AttributeError("Attribute " + attribute + " doesn't exist in " + str(type(self.__tbl)) + ".") else: attributes_to_read = [ self.__attr_store_name, self.__attr_founder_username ] const_lst = [] if not (store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, store_name)) if not (founder_username == ""): const_lst.append( Expression(self.__tbl.founder_username, OP.EQ, founder_username)) where_expr = and_exprs(const_lst) result = (DbProxy.get_instance()).read(self.__tbl, where_expr=where_expr) output_lst = [] for data_obj in result: data_as_dictionary = {} if self.__attr_store_name in attributes_to_read: data_as_dictionary[ self.__attr_store_name] = data_obj.store_name if self.__attr_founder_username in attributes_to_read: data_as_dictionary[ self.__attr_founder_username] = data_obj.founder_username output_lst.append(data_as_dictionary) return output_lst
def write(self, username: str = "", store_name: str = "", total_price: float = None, date: datetime = None): """ write store owner appointment to db. :return: """ attributes_as_dictionary = {self.__attributes_as_dictionary[self.__attr_username]: username, self.__attributes_as_dictionary[self.__attr_store_name]: store_name, self.__attributes_as_dictionary[self.__attr_total_price]: total_price, self.__attributes_as_dictionary[self.__attr_date]: date} return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def write(self, username: str, product_ref: int, amount: int): """ Write a products in basket to db. :param username: :param product_ref: :param amount: :return: size of user tbl after inserting the new one. """ attributes_as_dictionary = {self.__attributes_as_dictionary[self.__attr_username]: username, self.__attributes_as_dictionary[self.__attr_product_ref]: product_ref, self.__attributes_as_dictionary[self.__attr_amount]: amount} return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def update(self, old_policy_ref: int = None, old_policy1_ref: int = None, old_policy2_ref: int = None, old_flag: int = None, new_policy1_ref: int = None, new_policy2_ref: int = None, new_flag: int = None): """ Update users in the db. old_<attribute> will composite a constraint of where to update. example(if old_username != ""), it will composite the constraint- where(user.username == old_username). new_<attribute> will update the <attribute> to the new value. example(if new_username != ""), it will update- update(user.username = new_username). :return: the number of updated rows. """ const_lst = [] if not (old_policy_ref is None): const_lst.append( Expression(self.__tbl.policy_ref, OP.EQ, old_policy_ref)) if not (old_policy1_ref is None): const_lst.append( Expression(self.__tbl.policy1_ref, OP.EQ, old_policy1_ref)) if not (old_policy2_ref is None): const_lst.append( Expression(self.__tbl.policy2_ref, OP.EQ, old_policy2_ref)) if not (old_flag is None): const_lst.append(Expression(self.__tbl.flag, OP.EQ, old_flag)) where_expr = and_exprs(const_lst) attributes_as_dictionary = {} if not (new_policy1_ref == ""): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_policy1_ref]] = \ new_policy1_ref if not (new_policy2_ref is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_policy2_ref]] = \ new_policy2_ref if not (new_flag is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_flag]] = \ old_flag if len(attributes_as_dictionary) == 0: raise AttributeError("Nothing to update") return DbProxy.get_instance().update(self.__tbl, attributes_as_dictionary, where_expr)
def read(self, attributes_to_read: [str], purchase_id: int = None, username: str = "", store_name: str = "", total_price: float = None, date: datetime = None): """ Read store owner appointment from db. Raise exception if an attribute in attributes_to_read is illegal. <attribute> will composite a constraint of where to read. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: dict of the result data. """ if len(attributes_to_read) > 0: for attribute in attributes_to_read: if attribute.lower() not in self.__attributes_as_dictionary.keys(): raise AttributeError("Attribute " + attribute + " doesn't exist in " + str(type(self.__tbl)) + ".") else: attributes_to_read: list = list(self.__attributes_as_dictionary.keys()) attributes_to_read.remove(self.__attr_purchase_id) const_lst = [] if not (purchase_id is None): const_lst.append((Expression(self.__tbl.purchase_id, OP.EQ, purchase_id))) if not (username == ""): const_lst.append((Expression(self.__tbl.username, OP.EQ, username))) if not (store_name == ""): const_lst.append(Expression(self.__tbl.store_name, OP.EQ, store_name)) if not (total_price is None): const_lst.append((Expression(self.__tbl.total_price, OP.EQ, total_price))) if not (date is None): const_lst.append((Expression(self.__tbl.date, OP.EQ, date))) where_expr = and_exprs(const_lst) result = (DbProxy.get_instance()).read(self.__tbl, where_expr=where_expr) output_lst = [] for data_obj in result: data_as_dictionary = {} if self.__attr_purchase_id in attributes_to_read: data_as_dictionary[self.__attr_purchase_id] = data_obj.purchase_id if self.__attr_username in attributes_to_read: data_as_dictionary[self.__attr_username] = data_obj.username.username if self.__attr_store_name in attributes_to_read: data_as_dictionary[self.__attr_store_name] = data_obj.store_name.store_name if self.__attr_total_price in attributes_to_read: data_as_dictionary[self.__attr_total_price] = data_obj.total_price if self.__attr_date in attributes_to_read: data_as_dictionary[self.__attr_date] = data_obj.date output_lst.append(data_as_dictionary) return output_lst
def test_read(self): try: (UserData.get_instance()).read(["not_attribute"]) self.fail() except AttributeError: pass except Exception: self.fail() (DbProxy.get_instance()).read = MagicMock( return_value=[StubUserData("eytan", "password", False)]) res = (UserData.get_instance()).read([], username="******") self.assertEqual((res[0]['username'], res[0]['password'], res[0]['is_system_manager']), ("eytan", "password", False)) res = (UserData.get_instance()).read([], password="******") self.assertEqual((res[0]['username'], res[0]['password'], res[0]['is_system_manager']), ("eytan", "password", False)) (DbProxy.get_instance()).read = MagicMock(return_value=[]) res = (UserData.get_instance()).read([], is_system_manager=True) self.assertListEqual([], res)
def update(self, old_appointee_username: str = "", old_store_name: str = "", old_appointer_username: str = "", new_appointee_username: str = "", new_store_name: str = "", new_appointer_username: str = ""): """ Update store owner appointment in the db. old_<attribute> will composite a constraint of where to update. example(if old_username != ""), it will composite the constraint- where(user.username == old_username). new_<attribute> will update the <attribute> to the new value. example(if new_username != ""), it will update- update(user.username = new_username). :return: the number of updated rows. """ const_lst = [] if not (old_appointee_username == ""): const_lst.append((Expression(self.__tbl.appointee_username, OP.EQ, old_appointee_username))) if not (old_store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, old_store_name)) if not (old_appointer_username == ""): const_lst.append((Expression(self.__tbl.appointer_username, OP.EQ, old_appointer_username))) where_expr = and_exprs(const_lst) attributes_as_dictionary = {} if not (new_appointee_username == ""): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_appointee_username]] = \ new_appointee_username if not (new_store_name == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_store_name]] = new_store_name if not (new_appointer_username == ""): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_appointer_username]] = \ new_appointer_username if len(attributes_as_dictionary) == 0: raise AttributeError("Nothing to update") return DbProxy.get_instance().update(self.__tbl, attributes_as_dictionary, where_expr)
def read(self, attributes_to_read: [str], username: str = "", product_ref: int = None, amount: int = None): """ Read products in baskets from db. Raise exception if an attribute in attributes_to_read is illegal. <attribute> will composite a constraint of where to read. example(if old_username != ""), it will composite the constraint- where(user.username == username). :param username: pk. :param product_ref: pk. :param amount: :param attributes_to_read: the list of attributes that will return from the db. type: [str] :return: list of dictionaries that contain attributes_to_read fields. """ if len(attributes_to_read) > 0: for attribute in attributes_to_read: if attribute.lower() not in self.__attributes_as_dictionary.keys(): raise AttributeError("Attribute " + attribute + " doesn't exist in " + str(type(self.__tbl)) + ".") else: attributes_to_read = list(self.__attributes_as_dictionary.keys()) const_lst = [] if not (username == ""): const_lst.append(Expression(self.__tbl.username, OP.EQ, username)) if not (product_ref is None): const_lst.append(Expression(self.__tbl.product_ref, OP.EQ, product_ref)) if not (amount is None): const_lst.append(Expression(self.__tbl.amount, OP.EQ, amount)) where_expr = and_exprs(const_lst) result = (DbProxy.get_instance()).read(self.__tbl, where_expr=where_expr) output_lst = [] for data_obj in result: user_data_as_dictionary = {} if self.__attr_username in attributes_to_read: user_data_as_dictionary[self.__attr_username] = data_obj.username.username if self.__attr_product_ref in attributes_to_read: user_data_as_dictionary["store_name"] = data_obj.product_ref.store_name.store_name user_data_as_dictionary["product_name"] = data_obj.product_ref.product_name if self.__attr_amount in attributes_to_read: user_data_as_dictionary[self.__attr_amount] = data_obj.amount output_lst.append(user_data_as_dictionary) return output_lst
def write(self, store_name="", founder_username=""): """ Write a store to db. :param store_name: :param founder_username: :return: size of user tbl after inserting the new one. """ attributes_as_dictionary = { self.__attributes_as_dictionary[self.__attr_store_name]: store_name, self.__attributes_as_dictionary[self.__attr_founder_username]: founder_username } return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def delete(self, username: str = "", product_ref: int = None, amount: int = None): """ Delete products in baskets from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: the number of deleted rows. """ const_lst = [] if not (username == ""): const_lst.append(Expression(self.__tbl.username, OP.EQ, username)) if not (product_ref is None): const_lst.append(Expression(self.__tbl.product_ref, OP.EQ, product_ref)) if not (amount is None): const_lst.append(Expression(self.__tbl.amount, OP.EQ, amount)) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)
def delete(self, discount_policy_id: int = None, policy_name: str = "", store_name="", product_name: str = "", percentage: float = None, valid_until: datetime = None, is_active: bool = None): """ Delete users from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: the number of deleted rows. """ const_lst = [] if not (discount_policy_id is None): const_lst.append( Expression(self.__tbl.discount_policy_id, OP.EQ, discount_policy_id)) if not (policy_name == ""): const_lst.append( Expression(self.__tbl.policy_name, OP.EQ, policy_name)) if not (store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, store_name)) if not (product_name == ""): const_lst.append( Expression(self.__tbl.product_name, OP.EQ, product_name)) if not (percentage is None): const_lst.append( Expression(self.__tbl.percentage, OP.EQ, percentage)) if not (valid_until is None): const_lst.append( Expression(self.__tbl.valid_until, OP.EQ, valid_until)) if not (is_active is None): const_lst.append(Expression(self.__tbl.is_active, OP.EQ, is_active)) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)
def write(self, appointee_username: str, store_name: str, appointer_username: str): """ write store owner appointment to db. :param appointee_username: pk :param store_name:pk :param appointer_username: :return: """ attributes_as_dictionary = { self.__attributes_as_dictionary[self.__attr_appointee_username]: appointee_username, self.__attributes_as_dictionary[self.__attr_store_name]: store_name, self.__attributes_as_dictionary[self.__attr_appointer_username]: appointer_username } return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def write(self, username: str, password: str, is_system_manager: bool = False): """ Write a user to db. :param username: pk :param password: user password. :param is_system_manager: :return: size of user tbl after inserting the new one. """ attributes_as_dictionary = { self.__attributes_as_dictionary[self.__attr_username]: username, self.__attributes_as_dictionary[self.__attr_password]: password, self.__attributes_as_dictionary[self.__attr_is_system_manager]: is_system_manager } return (DbProxy.get_instance()).write(self.__tbl, attributes_as_dictionary)
def delete(self, date: datetime = None, guests: int = None, subscribers: int = None, store_managers: int = None, store_owners: int = None, system_managers: int = None): """ Delete statistics from the DB. <attribute> will composite a constraint of where to delete. example(if old_username != ""), it will composite the constraint- where(user.username == username). :return: the number of deleted rows. """ const_lst = [] if not (date is None): const_lst.append(Expression(self.__tbl.date, OP.EQ, date)) if not (guests is None): const_lst.append(Expression(self.__tbl.guests, OP.EQ, guests)) if not (subscribers is None): const_lst.append( Expression(self.__tbl.subscribers, OP.EQ, subscribers)) if not (store_managers is None): const_lst.append( Expression(self.__tbl.store_managers, OP.EQ, store_managers)) if not (store_owners is None): const_lst.append( Expression(self.__tbl.store_owners, OP.EQ, store_owners)) if not (system_managers is None): const_lst.append( Expression(self.__tbl.system_managers, OP.EQ, system_managers)) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)