def get_purchase_order_by_id(self, entity_id, supplier_id=None, distributors=[]): table = 'brewoptix-purchase-orders' purchase_order = self._storage.get(table, entity_id) if purchase_order: purchase_order = clean(purchase_order) if supplier_id and purchase_order["supplier_id"] != supplier_id: raise NoSuchEntity("Purchase Order") if purchase_order["distributor_id"] in distributors: raise NoSuchEntity("Purchase Order") purchase_order['order_date'] = datetime.utcfromtimestamp( purchase_order['order_date']).isoformat().split('T')[0] if "pack_date" in purchase_order: purchase_order['pack_date'] = maya.to_iso8601( datetime.utcfromtimestamp( purchase_order['pack_date'])).split('T')[0] if "ship_date" in purchase_order: purchase_order['ship_date'] = maya.to_iso8601( datetime.utcfromtimestamp( purchase_order['ship_date'])).split('T')[0] else: raise NoSuchEntity("Purchase Order") return purchase_order
def is_product_exists(self, supplier_id, product_id): table = 'brewoptix-products' product_type = self._storage.get(table, product_id) if not product_type: raise NoSuchEntity("Product") else: product_type = clean(product_type) if product_type["supplier_id"] != supplier_id: raise NoSuchEntity("Product")
def get_count_by_id(self, supplier_id, entity_id): count = self._storage.get(entity_id) if count: count = clean(count) if count["supplier_id"] != supplier_id: raise NoSuchEntity("Count") count['count_date'] = datetime.utcfromtimestamp( count['count_date']).isoformat().split('T')[0] else: raise NoSuchEntity("Count") return count
def delete_count_by_id(self, supplier_id, entity_id): obj_type = 'counts' count = self._storage.get(entity_id) if count: obj = clean(count) if obj["supplier_id"] != supplier_id: raise NoSuchEntity("Count") obj["active"] = False self._storage.save(obj_type, obj) self.sns_publish("counts", obj) # publish notification else: raise NoSuchEntity("Count")
def get_all_users_in_supplier(self, supplier_id): if not self.is_current_user_admin(supplier_id): raise NotAnAdminUser # get supplier obj supplier = self._storage.get('brewoptix-suppliers', supplier_id) if supplier: supplier = clean(supplier) else: raise NoSuchEntity("Supplier") users = [] if "users" in supplier: for user in supplier["users"]: user_id = user["user_id"] # get email id user_obj = self._storage.get_by_user_id('brewoptix-users', user_id) if not user_obj: raise NoSuchEntity user_obj = clean(user_obj) users.append( { "email": user_obj["email"], "name": user.get("user_name", None) or user["name"], "role": user["role"], "user_id": user_id } ) return users
def delete_adjustment_record_by_id(self, supplier_id, entity_id): table = 'brewoptix-adjustment-inventory' adjustment = self._storage.get(table, entity_id) if adjustment: obj = clean(adjustment) if obj["supplier_id"] != supplier_id: raise NoSuchEntity("Adjustment") obj["active"] = False self._storage.save(table, obj) self.sns_publish("adjustments", obj) # publish notification else: raise NoSuchEntity("Adjustment")
def get_adjustment_record_by_id(self, supplier_id, entity_id): table = 'brewoptix-adjustment-inventory' adjustment = self._storage.get(table, entity_id) if adjustment: adjustment = clean(adjustment) if adjustment["supplier_id"] != supplier_id: raise NoSuchEntity("Adjustment") adjustment['adjustment_date'] = datetime.utcfromtimestamp( adjustment['adjustment_date']).isoformat().split('T')[0] else: raise NoSuchEntity("Adjustment") return adjustment
def get_all_users_in_distributor(self, distributor_id): if not self.is_current_user_distributor_admin(distributor_id): raise NotAnAdminUser # get distributor obj distributor = self._storage.get('brewoptix-distributors', distributor_id) if distributor: distributor = clean(distributor) else: raise NoSuchEntity("distributor") users = [] if "users" in distributor: for user in distributor["users"]: user_id = user["user_id"] # get email id user_obj = self._storage.get_by_user_id('brewoptix-users', user_id) if not user_obj: raise NoSuchEntity user_obj = clean(user_obj) users.append( { "email": user_obj["email"], "name": user.get("user_name", None) or user["name"], "role": user["role"], "user_id": user_id } ) return users
def delete_user_in_supplier(self, supplier_id, retiring_user_id): # get supplier obj supplier = self._storage.get('brewoptix-suppliers', supplier_id) if supplier: supplier = clean(supplier) else: raise NoSuchEntity("Supplier") if not self.is_current_user_admin(supplier_id): raise NotAnAdminUser if not any(retiring_user_id == user["user_id"] for user in supplier["users"]): raise NoSuchEntity("User Not Associated with Supplier") supplier["users"] = [user for user in supplier.get("users", []) if user["user_id"] != retiring_user_id] supplier_obj = self._storage.save("brewoptix-suppliers", supplier) self.sns_publish("suppliers", supplier) # publish notification self.remove_supplier_from_app_metadata(supplier_id, user_id=retiring_user_id) return True
def delete_purchase_order_by_id(self, entity_id, supplier_id=None, distributors=[]): table = 'brewoptix-purchase-orders' purchase_order = self._storage.get(table, entity_id) if purchase_order: obj = clean(purchase_order) if supplier_id and obj["supplier_id"] != supplier_id: raise NoSuchEntity("Purchase Order") if obj["distributor_id"] in distributors: raise NoSuchEntity("Purchase Order") obj["active"] = False self._storage.save(table, obj) self.sns_publish("purchase-orders", obj) # publish notification else: raise NoSuchEntity("Purchase Order")
def delete_user_in_distributor(self, distributor_id, retiring_user_id): # get distributor obj distributor = self._storage.get('brewoptix-distributors', distributor_id) if distributor: distributor = clean(distributor) else: raise NoSuchEntity("distributor") if not self.is_current_user_distributor_admin(distributor_id): raise NotAnAdminUser if not any(retiring_user_id == user["user_id"] for user in distributor["users"]): raise NoSuchEntity("User Not Associated with distributor") distributor["users"] = [user for user in distributor.get("users", []) if user["user_id"] != retiring_user_id] distributor_obj = self._storage.save("brewoptix-distributors", distributor) self.sns_publish("distributors", distributor) # publish notification self.remove_distributor_from_app_metadata(distributor_id, user_id=retiring_user_id) return True
def delete_distributor_by_id(self, entity_id): table = 'brewoptix-distributors' distributor = self._storage.get(table, entity_id) if distributor: obj = clean(distributor) if not self.is_current_user_distributor_admin(obj['entity_id']): raise NotAnAdminUser obj["active"] = False self._storage.save(table, obj) self.sns_publish("distributors", obj) # publish notification else: raise NoSuchEntity("distributor")
def update_purchase_order(self, obj): table = 'brewoptix-purchase-orders' check_for_required_keys(obj, purchase_order_attributes) # check if content datatype is right content = {k: v for k, v in obj.items() if k not in base_attributes} check_properties_datatypes(content, purchase_order_attributes) if 'entity_id' not in obj: raise BadParameters obj['user_id'] = self._user_id purchase_order = self._storage.get(table, obj['entity_id']) if not purchase_order: raise NoSuchEntity("Purchase Order") obj['order_date'] = maya.parse(obj['order_date']).epoch if "pack_date" in obj: obj['pack_date'] = maya.parse(obj['pack_date']).epoch if "ship_date" in obj: obj['ship_date'] = maya.parse(obj['ship_date']).epoch purchase_order_obj = self._storage.save(table, obj) self.sns_publish("purchase-orders", obj) # publish notification purchase_order = clean(purchase_order_obj) purchase_order['order_date'] = datetime.utcfromtimestamp( purchase_order['order_date']).isoformat().split('T')[0] if "pack_date" in purchase_order: purchase_order['pack_date'] = maya.to_iso8601( datetime.utcfromtimestamp( purchase_order['pack_date'])).split('T')[0] if "ship_date" in purchase_order: purchase_order['ship_date'] = maya.to_iso8601( datetime.utcfromtimestamp( purchase_order['ship_date'])).split('T')[0] return purchase_order