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 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 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, 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 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 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 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 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 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 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 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 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 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)
def update(self, old_product_id: int = None, old_product_name: str = "", old_store_name: str = "", old_price: float = None, old_category: str = "", old_amount: int = None, old_purchase_type: int = None, new_product_name: str = "", new_store_name: str = "", new_price: float = None, new_category: str = "", new_amount: int = None, new_purchase_type: 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_product_id is None): const_lst.append( Expression(self.__tbl.product_id, OP.EQ, old_product_id)) if not (old_product_name == ""): const_lst.append( Expression(self.__tbl.product_name, OP.EQ, old_product_name)) if not (old_store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, old_store_name)) if not (old_price is None): const_lst.append(Expression(self.__tbl.price, OP.EQ, old_price)) if not (old_category == ""): const_lst.append( Expression(self.__tbl.category, OP.EQ, old_category)) if not (old_amount is None): const_lst.append(Expression(self.__tbl.amount, OP.EQ, old_amount)) if not (old_purchase_type is None): const_lst.append( Expression(self.__tbl.purchase_type, OP.EQ, old_purchase_type)) where_expr = and_exprs(const_lst) attributes_as_dictionary = {} if not (new_product_name == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_product_name]] = new_product_name if not (new_store_name == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_store_name]] = new_store_name if not (new_price is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_price]] = new_price if not (new_category == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_category]] = new_category if not (new_amount is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_amount]] = new_amount if not (new_purchase_type is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_purchase_type]] = new_purchase_type if len(attributes_as_dictionary) == 0: raise AttributeError("Nothing to update") return DbProxy.get_instance().update(self.__tbl, attributes_as_dictionary, where_expr)
def update(self, old_appointee_username: str = "", old_store_name: str = "", old_appointer_username: str = "", old_can_edit_inventory: bool = None, old_can_edit_policies: bool = None, old_can_appoint_owner: bool = None, old_can_delete_owner: bool = None, old_can_appoint_manager: bool = None, old_can_edit_manager_permissions: bool = None, old_can_delete_manager: bool = None, old_can_close_store: bool = None, old_can_answer_user_questions: bool = None, old_can_watch_purchase_history: bool = None, new_appointee_username: str = "", new_store_name: str = "", new_appointer_username: str = "", new_can_edit_inventory: bool = None, new_can_edit_policies: bool = None, new_can_appoint_owner: bool = None, new_can_delete_owner: bool = None, new_can_appoint_manager: bool = None, new_can_edit_manager_permissions: bool = None, new_can_delete_manager: bool = None, new_can_close_store: bool = None, new_can_answer_user_questions: bool = None, new_can_watch_purchase_history: bool = None): """ Update store manager 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))) if not (old_can_edit_inventory is None): const_lst.append((Expression(self.__tbl.can_edit_inventory, OP.EQ, old_can_edit_inventory))) if not (old_can_edit_policies is None): const_lst.append((Expression(self.__tbl.can_edit_policies, OP.EQ, old_can_edit_policies))) if not (old_can_appoint_owner is None): const_lst.append((Expression(self.__tbl.can_appoint_owner, OP.EQ, old_can_appoint_owner))) if not (old_can_delete_owner is None): const_lst.append((Expression(self.__tbl.can_delete_owner, OP.EQ, old_can_delete_owner))) if not (old_can_appoint_manager is None): const_lst.append((Expression(self.__tbl.can_appoint_manager, OP.EQ, old_can_appoint_manager))) if not (old_can_edit_manager_permissions is None): const_lst.append( (Expression(self.__tbl.can_edit_manager_permissions, OP.EQ, old_can_edit_manager_permissions))) if not (old_can_delete_manager is None): const_lst.append((Expression(self.__tbl.can_delete_manager, OP.EQ, old_can_delete_manager))) if not (old_can_close_store is None): const_lst.append((Expression(self.__tbl.can_close_store, OP.EQ, old_can_close_store))) if not (old_can_answer_user_questions is None): const_lst.append( (Expression(self.__tbl.can_answer_user_questions, OP.EQ, old_can_answer_user_questions))) if not (old_can_watch_purchase_history is None): const_lst.append( (Expression(self.__tbl.can_watch_purchase_history, OP.EQ, old_can_watch_purchase_history))) 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 not (new_can_edit_inventory is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_edit_inventory]] = \ new_can_edit_inventory if not (new_can_edit_policies is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_edit_policies]] = \ new_can_edit_policies if not (new_can_appoint_owner is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_appoint_owner]] = \ new_can_appoint_owner if not (new_can_delete_owner is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_delete_owner]] = \ new_can_delete_owner if not (new_can_appoint_manager is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_appoint_manager]] = \ new_can_appoint_manager if not (new_can_edit_manager_permissions is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_edit_manager_permissions]] = \ new_can_edit_manager_permissions if not (new_can_delete_manager is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_delete_manager]] = \ new_can_delete_manager if not (new_can_close_store is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_close_store]] = \ new_can_close_store if not (new_can_answer_user_questions is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_answer_user_questions]] = \ new_can_answer_user_questions if not (new_can_watch_purchase_history is None): attributes_as_dictionary[self.__attributes_as_dictionary[self.__attr_can_watch_purchase_history]] = \ new_can_watch_purchase_history 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], discount_policy_id: int = None, policy_name: str = "", store_name="", product_name: str = "", percentage: float = None, valid_until: datetime = None, is_active: bool = None, precondition_product: str = "", precondition_min_amount: int = None, precondition_min_basket_price: int = None, policy1_ref: str = "", policy2_ref: str = "", flag: int = None): """ Read discount policy 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: list = list( self.__attributes_as_dictionary.keys()) attributes_to_read.insert(len(attributes_to_read), 'precondition_product') attributes_to_read.insert(len(attributes_to_read), 'precondition_min_amount') attributes_to_read.insert(len(attributes_to_read), 'precondition_min_basket_price') attributes_to_read.insert(len(attributes_to_read), 'policy1_ref') attributes_to_read.insert(len(attributes_to_read), 'policy2_ref') attributes_to_read.insert(len(attributes_to_read), 'flag') # attributes_to_read.remove(self.__attr_discount_policy_id) 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)) if not (policy1_ref is None): const_lst.append( Expression(CompositeDiscountPolicy.policy1_ref, OP.EQ, policy1_ref)) if not (policy2_ref is None): const_lst.append( Expression(CompositeDiscountPolicy.policy2_ref, OP.EQ, policy2_ref)) if not (flag is None): const_lst.append( Expression(CompositeDiscountPolicy.flag, OP.EQ, flag)) if not (precondition_product == ""): const_lst.append( Expression(ConditionalDiscountPolicy.precondition_product, OP.EQ, precondition_product)) if not (precondition_min_amount is None): const_lst.append( Expression(ConditionalDiscountPolicy.precondition_min_amount, OP.EQ, precondition_min_amount)) if not (precondition_min_basket_price is None): const_lst.append( Expression( ConditionalDiscountPolicy.precondition_min_basket_price, OP.EQ, precondition_min_basket_price)) where_expr = and_exprs(const_lst) result = (DbProxy.get_instance()).read_discount_policies(where_expr) output_lst = [] for data_obj in result: data_as_dictionary = {} if self.__attr_discount_policy_id in attributes_to_read: data_as_dictionary[ self. __attr_discount_policy_id] = data_obj.discount_policy_id if self.__attr_policy_name in attributes_to_read: data_as_dictionary[ self.__attr_policy_name] = data_obj.policy_name if self.__attr_store_name in attributes_to_read: data_as_dictionary[ self.__attr_store_name] = data_obj.store_name if self.__attr_product_name in attributes_to_read: data_as_dictionary[ self.__attr_product_name] = data_obj.product_name if self.__attr_percentage in attributes_to_read: data_as_dictionary[ self.__attr_percentage] = data_obj.percentage if self.__attr_valid_until in attributes_to_read: data_as_dictionary[ self.__attr_valid_until] = data_obj.valid_until if self.__attr_is_active in attributes_to_read: data_as_dictionary[self.__attr_is_active] = data_obj.is_active if "precondition_product" in attributes_to_read: data_as_dictionary[ "precondition_product"] = data_obj.precondition_product if "precondition_min_amount" in attributes_to_read: data_as_dictionary[ "precondition_min_amount"] = data_obj.precondition_min_amount if "precondition_min_basket_price" in attributes_to_read: data_as_dictionary[ "precondition_min_basket_price"] = data_obj.precondition_min_basket_price if "policy1_ref" in attributes_to_read: data_as_dictionary["policy1_ref"] = ( DbProxy.get_instance()).read_discount_policies( Expression(self.__tbl.discount_policy_id, OP.EQ, data_obj.policy1_ref.discount_policy_id)) if "policy2_ref" in attributes_to_read: data_as_dictionary["policy2_ref"] = ( DbProxy.get_instance()).read_discount_policies( Expression(self.__tbl.discount_policy_id, OP.EQ, data_obj.policy2_ref.discount_policy_id)) if "flag" in attributes_to_read: data_as_dictionary["flag"] = data_obj.flag output_lst.append(data_as_dictionary) return output_lst
def read(self, attributes_to_read: [str], date: datetime = None, guests: int = None, subscribers: int = None, store_managers: int = None, store_owners: int = None, system_managers: int = None): """ Read statistics 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 date: :param guests: :param subscribers: :param store_managers: :param store_owners: :param system_managers: :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.__attributes_as_dictionary.keys() 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) 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_date in attributes_to_read: user_data_as_dictionary[self.__attr_date] = data_obj.date if self.__attr_guests in attributes_to_read: user_data_as_dictionary[self.__attr_guests] = data_obj.guests if self.__attr_subscribers in attributes_to_read: user_data_as_dictionary[ self.__attr_subscribers] = data_obj.subscribers if self.__attr_store_managers in attributes_to_read: user_data_as_dictionary[ self.__attr_store_managers] = data_obj.store_managers if self.__attr_store_owners in attributes_to_read: user_data_as_dictionary[ self.__attr_store_owners] = data_obj.store_owners if self.__attr_system_managers in attributes_to_read: user_data_as_dictionary[ self.__attr_system_managers] = data_obj.system_managers output_lst.append(user_data_as_dictionary) return output_lst
def update(self, old_date: datetime = None, old_guests: int = None, old_subscribers: int = None, old_store_managers: int = None, old_store_owners: int = None, old_system_managers: int = None, new_date: datetime = None, new_guests: int = None, new_subscribers: int = None, new_store_managers: int = None, new_store_owners: int = None, new_system_managers: int = None): """ Update statistics 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). :return: the number of updated rows. """ const_lst = [] if not (old_date is None): const_lst.append(Expression(self.__tbl.date, OP.EQ, old_date)) if not (old_guests is None): const_lst.append(Expression(self.__tbl.guests, OP.EQ, old_guests)) if not (old_subscribers is None): const_lst.append( Expression(self.__tbl.subscribers, OP.EQ, old_subscribers)) if not (old_store_managers is None): const_lst.append( Expression(self.__tbl.store_managers, OP.EQ, old_store_managers)) if not (old_store_owners is None): const_lst.append( Expression(self.__tbl.store_owners, OP.EQ, old_store_owners)) if not (old_system_managers is None): const_lst.append( Expression(self.__tbl.system_managers, OP.EQ, old_system_managers)) where_expr = and_exprs(const_lst) attributes_as_dictionary = {} if not (new_date is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_date]] = new_date if not (new_guests is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_guests]] = new_guests if not (new_subscribers is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_subscribers]] = new_subscribers if not (new_store_managers is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_store_managers]] = new_store_managers if not (new_store_owners is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_store_owners]] = new_store_owners if not (new_system_managers is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_system_managers]] = new_system_managers 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], policy_ref: int = None, policy1_ref: int = None, policy2_ref: int = None, flag: int = None): """ Read discount policy 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: list = list( self.__attributes_as_dictionary.keys()) attributes_to_read.remove(self.__attr_policy_ref) 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) result = (DbProxy.get_instance()).read(self.__tbl, where_expr=where_expr) output_lst = [] for data_obj in result: data_as_dictionary = {} if self.__attr_policy_ref in attributes_to_read: data_as_dictionary[ self.__attr_policy_ref] = data_obj.discount_policy_id if self.__attr_policy1_ref in attributes_to_read: data_as_dictionary[ self.__attr_policy1_ref] = data_obj.policy1_ref.policy_name if self.__attr_policy2_ref in attributes_to_read: data_as_dictionary[ self.__attr_policy2_ref] = data_obj.policy2_ref.policy_name if self.__attr_flag in attributes_to_read: data_as_dictionary[self.__attr_flag] = data_obj.flag output_lst.append(data_as_dictionary) return output_lst
def read(self, attributes_to_read: [str], appointee_username: str = "", store_name: str = "", appointer_username: str = "", can_edit_inventory: bool = None, can_edit_policies: bool = None, can_appoint_owner: bool = None, can_delete_owner: bool = None, can_appoint_manager: bool = None, can_edit_manager_permissions: bool = None, can_delete_manager: bool = None, can_close_store: bool = None, can_answer_user_questions: bool = None, can_watch_purchase_history: bool = None): """ Read store manager 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: :param can_edit_inventory: :param can_edit_policies: :param can_appoint_owner: :param can_delete_owner: :param can_appoint_manager: :param can_edit_manager_permissions: :param can_delete_manager: :param can_close_store: :param can_answer_user_questions: :param can_watch_purchase_history: :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: # TODO: Check if work. 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))) if not (can_edit_inventory is None): const_lst.append((Expression(self.__tbl.can_edit_inventory, OP.EQ, can_edit_inventory))) if not (can_edit_policies is None): const_lst.append((Expression(self.__tbl.can_edit_policies, OP.EQ, can_edit_policies))) if not (can_appoint_owner is None): const_lst.append((Expression(self.__tbl.can_appoint_owner, OP.EQ, can_appoint_owner))) if not (can_delete_owner is None): const_lst.append((Expression(self.__tbl.can_delete_owner, OP.EQ, can_delete_owner))) if not (can_appoint_manager is None): const_lst.append((Expression(self.__tbl.can_appoint_manager, OP.EQ, can_appoint_manager))) if not (can_edit_manager_permissions is None): const_lst.append( (Expression(self.__tbl.can_edit_manager_permissions, OP.EQ, can_edit_manager_permissions))) if not (can_delete_manager is None): const_lst.append((Expression(self.__tbl.can_delete_manager, OP.EQ, can_delete_manager))) if not (can_close_store is None): const_lst.append((Expression(self.__tbl.can_close_store, OP.EQ, can_close_store))) if not (can_answer_user_questions is None): const_lst.append((Expression(self.__tbl.can_answer_user_questions, OP.EQ, can_answer_user_questions))) if not (can_watch_purchase_history is None): const_lst.append((Expression(self.__tbl.can_watch_purchase_history, OP.EQ, can_watch_purchase_history))) 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 if self.__attr_can_edit_inventory in attributes_to_read: data_as_dictionary[ self. __attr_can_edit_inventory] = data_obj.can_edit_inventory if self.__attr_can_edit_policies in attributes_to_read: data_as_dictionary[ self.__attr_can_edit_policies] = data_obj.can_edit_policies if self.__attr_can_appoint_owner in attributes_to_read: data_as_dictionary[ self.__attr_can_appoint_owner] = data_obj.can_appoint_owner if self.__attr_can_delete_owner in attributes_to_read: data_as_dictionary[ self.__attr_can_delete_owner] = data_obj.can_delete_owner if self.__attr_can_appoint_manager in attributes_to_read: data_as_dictionary[ self. __attr_can_appoint_manager] = data_obj.can_appoint_manager if self.__attr_can_edit_manager_permissions in attributes_to_read: data_as_dictionary[ self. __attr_can_edit_manager_permissions] = data_obj.can_edit_manager_permissions if self.__attr_can_delete_manager in attributes_to_read: data_as_dictionary[ self. __attr_can_delete_manager] = data_obj.can_delete_manager if self.__attr_can_close_store in attributes_to_read: data_as_dictionary[ self.__attr_can_close_store] = data_obj.can_close_store if self.__attr_can_answer_user_questions in attributes_to_read: data_as_dictionary[ self. __attr_can_answer_user_questions] = data_obj.can_answer_user_questions if self.__attr_can_watch_purchase_history in attributes_to_read: data_as_dictionary[ self. __attr_can_watch_purchase_history] = data_obj.can_watch_purchase_history output_lst.append(data_as_dictionary) return output_lst
def update(self, old_discount_policy_id: int = None, old_policy_name: str = "", old_store_name: str = "", old_product_name: str = "", old_percentage: float = None, old_valid_until: datetime = None, old_is_active: bool = None, new_policy_name: str = "", new_store_name="", new_product_name: str = "", new_percentage: float = None, new_valid_until: datetime = None, new_is_active: 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_<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_discount_policy_id is None): const_lst.append( Expression(self.__tbl.discount_policy_id, OP.EQ, old_discount_policy_id)) if not (old_policy_name == ""): const_lst.append( Expression(self.__tbl.policy_name, OP.EQ, old_policy_name)) if not (old_store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, old_store_name)) if not (old_product_name == ""): const_lst.append( Expression(self.__tbl.product_name, OP.EQ, old_product_name)) if not (old_percentage is None): const_lst.append( Expression(self.__tbl.percentage, OP.EQ, old_percentage)) if not (old_valid_until is None): const_lst.append( Expression(self.__tbl.valid_until, OP.EQ, old_valid_until)) if not (old_is_active is None): const_lst.append( Expression(self.__tbl.is_active, OP.EQ, old_is_active)) where_expr = and_exprs(const_lst) attributes_as_dictionary = {} if not (new_policy_name == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_policy_name]] = new_policy_name if not (new_store_name == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_store_name]] = new_store_name if not (new_product_name == ""): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_product_name]] = new_product_name if not (new_percentage is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_percentage]] = new_percentage if not (new_valid_until is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_valid_until]] = new_valid_until if not (new_is_active is None): attributes_as_dictionary[self.__attributes_as_dictionary[ self.__attr_is_active]] = new_is_active 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], product_id: int = None, product_name: str = "", store_name: str = "", price: float = None, category: str = "", amount: int = None, purchase_type: int = 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 product_id: :param product_name: :param price: :param category: :param amount: :param purchase_type: :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 = list(self.__attributes_as_dictionary.keys()) attributes_to_read.remove(self.__attr_product_id) const_lst = [] if not (product_id is None): const_lst.append( Expression(self.__tbl.product_id, OP.EQ, product_id)) if not (product_name == ""): const_lst.append( Expression(self.__tbl.product_name, OP.EQ, product_name)) if not (store_name == ""): const_lst.append( Expression(self.__tbl.store_name, OP.EQ, store_name)) if not (price is None): const_lst.append(Expression(self.__tbl.price, OP.EQ, price)) if not (category == ""): const_lst.append(Expression(self.__tbl.category, OP.EQ, category)) if not (amount is None): const_lst.append(Expression(self.__tbl.amount, OP.EQ, amount)) if not (purchase_type is None): const_lst.append( Expression(self.__tbl.purchase_type, OP.EQ, purchase_type)) 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_product_id in attributes_to_read: user_data_as_dictionary[ self.__attr_product_id] = data_obj.product_id if self.__attr_product_name in attributes_to_read: user_data_as_dictionary[ self.__attr_product_name] = data_obj.product_name if self.__attr_store_name in attributes_to_read: user_data_as_dictionary[ self.__attr_store_name] = data_obj.store_name if self.__attr_price in attributes_to_read: user_data_as_dictionary[self.__attr_price] = data_obj.price if self.__attr_category in attributes_to_read: user_data_as_dictionary[ self.__attr_category] = data_obj.category if self.__attr_amount in attributes_to_read: user_data_as_dictionary[self.__attr_amount] = data_obj.amount if self.__attr_purchase_type in attributes_to_read: user_data_as_dictionary[ self.__attr_purchase_type] = data_obj.purchase_type output_lst.append(user_data_as_dictionary) return output_lst
def delete(self, appointee_username: str = "", store_name: str = "", appointer_username: str = "", can_edit_inventory: bool = None, can_edit_policies: bool = None, can_appoint_owner: bool = None, can_delete_owner: bool = None, can_appoint_manager: bool = None, can_edit_manager_permissions: bool = None, can_delete_manager: bool = None, can_close_store: bool = None, can_answer_user_questions: bool = None, can_watch_purchase_history: bool = None): """ Delete store manager 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))) if not (can_edit_inventory is None): const_lst.append((Expression(self.__tbl.can_edit_inventory, OP.EQ, can_edit_inventory))) if not (can_edit_policies is None): const_lst.append((Expression(self.__tbl.can_edit_policies, OP.EQ, can_edit_policies))) if not (can_appoint_owner is None): const_lst.append((Expression(self.__tbl.can_appoint_owner, OP.EQ, can_appoint_owner))) if not (can_delete_owner is None): const_lst.append((Expression(self.__tbl.can_delete_owner, OP.EQ, can_delete_owner))) if not (can_appoint_manager is None): const_lst.append((Expression(self.__tbl.can_appoint_manager, OP.EQ, can_appoint_manager))) if not (can_edit_manager_permissions is None): const_lst.append( (Expression(self.__tbl.can_edit_manager_permissions, OP.EQ, can_edit_manager_permissions))) if not (can_delete_manager is None): const_lst.append((Expression(self.__tbl.can_delete_manager, OP.EQ, can_delete_manager))) if not (can_close_store is None): const_lst.append((Expression(self.__tbl.can_close_store, OP.EQ, can_close_store))) if not (can_answer_user_questions is None): const_lst.append((Expression(self.__tbl.can_answer_user_questions, OP.EQ, can_answer_user_questions))) if not (can_watch_purchase_history is None): const_lst.append((Expression(self.__tbl.can_watch_purchase_history, OP.EQ, can_watch_purchase_history))) where_expr = and_exprs(const_lst) return (DbProxy.get_instance()).delete(self.__tbl, where_expr=where_expr)