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 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 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 delete_merchandise_by_id(self, supplier_id, entity_id): table = 'brewoptix-merchandise' merchandise = self._storage.get(table, entity_id) if merchandise: obj = clean(merchandise) if obj["supplier_id"] != supplier_id: raise NoSuchEntity # dynamodb data_adapter cannot process float type inside nested dict # type casting to help data_adapter sizes = [] if "sizes" in obj: for item in obj["sizes"]: if "price" in item and isinstance(item["price"], float): item["price"] = Decimal( '{val:.3f}'.format(val=item["price"])) sizes.append(item) obj["sizes"] = sizes obj["active"] = False self._storage.save(table, obj) # type-casting decimal to float for dicts in list sizes = [] if "sizes" in obj: for item in obj["sizes"]: if "price" in item and isinstance(item["price"], Decimal): item["price"] = float(item["price"]) sizes.append(item) obj["sizes"] = sizes self.sns_publish("merchandise", obj) # publish notification else: raise NoSuchEntity
def update_user_app_metadata(self, app_metadata): table = 'brewoptix-users' # get user profile user_obj = self._storage.get_by_user_id(table, self._user_id) if not user_obj: raise NoSuchEntity user_obj = clean(user_obj) # check if an update is required if "app_metadata" in user_obj: old_app_metadata = user_obj["app_metadata"] old_hash = json.dumps(old_app_metadata, sort_keys=True) new_hash = json.dumps(app_metadata, sort_keys=True) if old_hash == new_hash: return user_obj # update app_metadata user_obj["app_metadata"] = app_metadata user_obj = self._storage.save(table, user_obj) self.sns_publish("users", user_obj) # publish notification return user_obj
def get_all_on_hands(self, supplier_id): table = 'brewoptix-on-hand-inventory' query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id), 'FilterExpression': (Attr('latest').eq(True) & Attr('active').eq(True)), 'IndexName': 'by_supplier_id' } response = self._storage.get_items(table, query) on_hands_obj = [] for item in response['Items']: # The 4 lines below can be uncommented if we move # from ALL to KEYS_ONLY for the table # entity_id = item['EntityID'] # on_hand = self._storage.get(table, entity_id) # on_hand = clean(on_hand) on_hand = json_util.loads(clean(item)) on_hand['observation_date'] = maya.to_iso8601( datetime.utcfromtimestamp( on_hand['observation_date'])).split('T')[0] on_hands_obj.append(on_hand) return on_hands_obj
def get_all_counts(self, supplier_id): obj_type = 'counts' query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id) & Key('obj_type').eq(obj_type), 'FilterExpression': (Attr('latest').eq(True) & Attr('active').eq(True)), 'IndexName': 'by_supplier_id_and_obj_type' } response = self._storage.get_items(query) counts_obj = [] for item in response['Items']: # The 4 lines below can be uncommented if we move # from ALL to KEYS_ONLY for the table # entity_id = item['EntityID'] # count = self._storage.get(table, entity_id) # count = clean(count) count = json_util.loads(clean(item)) count['count_date'] = maya.to_iso8601( datetime.utcfromtimestamp(count['count_date'])).split('T')[0] counts_obj.append(count) return counts_obj
def get_all_production(self, supplier_id): table = 'brewoptix-production' query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id), 'FilterExpression': (Attr('latest').eq(True) & Attr('active').eq(True)), 'IndexName': 'by_supplier_id_and_production_date' } response = self._storage.get_items(table, query) production_obj = [] for item in response['Items']: # The 4 lines below can be uncommented if we move # from ALL to KEYS_ONLY for the table # entity_id = item['EntityID'] # production = self._storage.get(table, entity_id) # production = clean(production) production = json_util.loads(clean(item)) production['production_date'] = datetime.utcfromtimestamp( production['production_date']).isoformat().split('T')[0] production_obj.append(production) return production_obj
def save_brand(self, obj): table = 'brewoptix-brands' check_for_required_keys(obj, brand_attributes) content = {k: v for k, v in obj.items() if k not in base_attributes} check_properties_datatypes(content, brand_attributes) obj['user_id'] = self._user_id # check if brand with same name already exists query = { 'KeyConditionExpression': Key('name').eq(obj['name']), 'FilterExpression': Attr('latest').eq(True) & Attr('active').eq(True) & Attr('supplier_id').eq(obj['supplier_id']), 'IndexName': 'by_brand_name' } # only gets partition keys response = self._storage.get_items(table, query) if response["Count"] > 0: if 'entity_id' in obj: # filter the current obj from the list. The list always returns the current obj in this case if len([ item for item in response['Items'] if item['entity_id'] != obj['entity_id'] ]) > 0: raise CannotModifyEntityStates else: raise CannotModifyEntityStates # upload logo png to s3 bucket image_string = None if "logo" in obj: image_string = obj.pop("logo") brand_obj = self._storage.save(table, obj) self.sns_publish("brands", obj) # publish notification brand = clean(brand_obj) if image_string: logo_filepath = self.base64_to_png(image_string) if 'S3_ENDPOINT' in os.environ: s3 = boto3.client('s3', endpoint_url=os.environ['S3_ENDPOINT']) else: s3 = boto3.client('s3') bucket_name = os.environ['S3_UPLOADS_BUCKET_NAME'] s3_filepath = "{SUPPLIER_ID}/brands/{BRAND_ID}/logo.png".format( SUPPLIER_ID=obj["supplier_id"], BRAND_ID=brand_obj["entity_id"]) s3.upload_file(logo_filepath, bucket_name, s3_filepath) return brand
def delete_distributor_supplier_by_access_code(self, supplier_id, access_code): table = 'brewoptix-distributor-suppliers' query = { 'KeyConditionExpression': Key('access_code').eq(access_code), 'FilterExpression': (Attr('latest').eq(True) & Attr('active').eq(True)), 'IndexName': 'by_access_code' } response = self._storage.get_items(table, query) print(response) if response["Count"] > 0: obj = response['Items'][0] obj = json_util.loads(obj) else: raise NoSuchEntity if obj: cleaned_obj = clean(obj) if cleaned_obj["supplier_id"] != supplier_id: raise NoSuchEntity cleaned_obj["active"] = False self._storage.save(table, cleaned_obj) else: raise NoSuchEntity
def get_all_adjustments(self, supplier_id): table = 'brewoptix-adjustment-inventory' query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id), 'FilterExpression': (Attr('Latest').eq(True) & Attr('Active').eq(True)), 'IndexName': 'by_supplier_id_and_adjustment_date' } response = self._storage.get_items(table, query) adjustments_obj = [] for item in response['Items']: # The 4 lines below can be uncommented if we move # from ALL to KEYS_ONLY for the table # entity_id = item['EntityID'] # adjustment_resp = self._storage.get(table, entity_id) # adjustment = adjustment_resp['Items'][0] # adjustment = clean(adjustment) adjustment = json_util.loads(clean(item)) adjustment['adjustment_date'] = maya.to_iso8601( datetime.utcfromtimestamp( adjustment['adjustment_date'])).split('T')[0] adjustments_obj.append(adjustment) return adjustments_obj
def get_or_create_profile(self): table = 'brewoptix-users' user_obj = self._storage.get_by_user_id('brewoptix-users', self._user_id) if not user_obj: for _ in range(1000): affiliate_id = generate_affiliate_id() query = { 'KeyConditionExpression': Key('affiliate_id').eq(affiliate_id), 'IndexName': 'by_affiliate_id' } response = self._storage.get_items(table, query) if response['Count'] <= 0: break user_obj = { 'entity_id': self._user_id, 'user_id': self._user_id, 'firstname': '', 'lastname': '', 'email': self._email, 'affiliate_id': affiliate_id, } user_obj = self._storage.save(table, user_obj) self.sns_publish("users", user_obj) # publish notification user_obj = clean(user_obj) return user_obj
def get_distributor_by_id(self, entity_id): table = 'brewoptix-distributors' distributor = self._storage.get(table, entity_id) if distributor: distributor = clean(distributor) else: raise NoSuchEntity return distributor
def get_supplier_by_id(self, entity_id): table = 'brewoptix-suppliers' supplier = self._storage.get(table, entity_id) if supplier: supplier = clean(supplier) else: raise NoSuchEntity return supplier
def get_adjustment_by_adjustment_date_range(self, supplier_id, min_adjustment_date, max_adjustment_date=None): table = 'brewoptix-adjustment-inventory' min_adjustment_date = maya.parse( min_adjustment_date.split('T')[0]).epoch if max_adjustment_date: max_adjustment_date = maya.parse( max_adjustment_date.split('T')[0]).epoch if max_adjustment_date: query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id) & Key('adjustment_date').between(min_adjustment_date, max_adjustment_date), 'FilterExpression': Attr('latest').eq(True) & Attr('active').eq(True), 'IndexName': 'by_supplier_id_and_adjustment_date' } else: query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id) & Key('adjustment_date').gt(min_adjustment_date), 'FilterExpression': Attr('latest').eq(True) & Attr('active').eq(True), 'IndexName': 'by_supplier_id_and_adjustment_date' } response = self._storage.get_items(table, query) adjustments_obj = [] for item in response['Items']: # The 4 lines below can be uncommented if we move # from ALL to KEYS_ONLY for the table # entity_id = item['EntityID'] # adjustment_resp = self._storage.get(table, entity_id) # adjustment = adjustment_resp['Items'][0] # adjustment = clean(adjustment) adjustment = json_util.loads(clean(item)) adjustment['adjustment_date'] = maya.to_iso8601( datetime.utcfromtimestamp( adjustment['adjustment_date'])).split('T')[0] adjustments_obj.append(adjustment) return adjustments_obj
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_user_app_metadata(self): obj_type = 'user' # get user profile user_obj = self._storage.get_by_user_id(self._user_id) if not user_obj: raise NoSuchEntity user_obj = clean(user_obj) app_metadata = user_obj.get("app_metadata", {}) return app_metadata
def delete_profile(self): table = 'brewoptix-users' profile = self._storage.get(table, self._user_id) if profile: obj = clean(profile) obj["active"] = False self._storage.save(table, obj) self.sns_publish("users", obj) # publish notification else: raise NoSuchEntity
def get_user_app_metadata(self): table = 'brewoptix-users' # get user profile user_obj = self._storage.get_by_user_id(table, self._user_id) if not user_obj: raise NoSuchEntity user_obj = clean(user_obj) app_metadata = user_obj.get("app_metadata", {}) return app_metadata
def get_merchandise_by_id(self, supplier_id, entity_id): table = 'brewoptix-merchandise' merchandise = self._storage.get(table, entity_id) if merchandise: merchandise = clean(merchandise) if merchandise["supplier_id"] != supplier_id: raise NoSuchEntity else: raise NoSuchEntity return merchandise
def get_container_by_id(self, supplier_id, entity_id): table = 'brewoptix-containers' container = self._storage.get(table, entity_id) if container: container = clean(container) if container["supplier_id"] != supplier_id: raise NoSuchEntity else: raise NoSuchEntity return container
def get_package_type_by_id(self, supplier_id, entity_id): table = 'brewoptix-package-types' package_type = self._storage.get(table, entity_id) if package_type: package_type = clean(package_type) if package_type["supplier_id"] != supplier_id: raise NoSuchEntity else: raise NoSuchEntity return package_type
def get_retail_package_by_id(self, supplier_id, entity_id): table = 'brewoptix-retail-packages' retail_package = self._storage.get(table, entity_id) if retail_package: retail_package = clean(retail_package) if retail_package["supplier_id"] != supplier_id: raise NoSuchEntity else: raise NoSuchEntity return retail_package
def get_supplier_distributor_by_id(self, supplier_id, entity_id): table = 'brewoptix-supplier-distributors' distributor = self._storage.get(table, entity_id) if distributor: distributor = clean(distributor) if distributor["supplier_id"] != supplier_id: raise NoSuchEntity else: raise NoSuchEntity return distributor
def get_product_by_id(self, supplier_id, entity_id): table = 'brewoptix-products' product_type = self._storage.get(table, entity_id) if product_type: product_type = clean(product_type) if product_type["supplier_id"] != supplier_id: raise NoSuchEntity else: raise NoSuchEntity return product_type
def get_production_by_production_date_range(self, supplier_id, min_production_date, max_production_date=None): table = 'brewoptix-production' min_production_date = maya.parse( min_production_date.split('T')[0]).epoch if max_production_date: max_production_date = maya.parse( max_production_date.split('T')[0]).epoch if max_production_date: query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id) & Key('production_date').between(min_production_date, max_production_date), 'FilterExpression': Attr('latest').eq(True) & Attr('active').eq(True), 'IndexName': 'by_supplier_id_and_production_date' } else: query = { 'KeyConditionExpression': Key('supplier_id').eq(supplier_id) & Key('production_date').gt(min_production_date), 'FilterExpression': Attr('latest').eq(True) & Attr('active').eq(True), 'IndexName': 'by_supplier_id_and_production_date' } response = self._storage.get_items(table, query) production_obj = [] for item in response['Items']: # The 4 lines below can be uncommented if we move # from ALL to KEYS_ONLY for the table # entity_id = item['EntityID'] # production = self._storage.get(table, entity_id) # production = clean(production) production = json_util.loads(clean(item)) production['production_date'] = datetime.utcfromtimestamp( production['production_date']).isoformat().split('T')[0] production_obj.append(production) return production_obj
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 get_brand_by_id(self, supplier_id, entity_id): table = 'brewoptix-brands' brand = self._storage.get(table, entity_id) if brand: brand = clean(brand) if brand["supplier_id"] != supplier_id: raise NoSuchEntity else: raise NoSuchEntity return brand
def delete_profile(self): obj_type = 'user' profile = self._storage.get(self._user_id) if profile: obj = clean(profile) obj["active"] = False self._storage.save(obj_type, obj) self.sns_publish('sp-' + os.environ['STAGE'] + '-' + obj_type, obj) # publish notification else: raise NoSuchEntity
def get_all_suppliers(self, suppliers): table = 'brewoptix-suppliers' supplier_ids = suppliers.keys() suppliers_obj = [] for supplier_id in supplier_ids: supplier = self._storage.get(table, supplier_id) if supplier: supplier = clean(supplier) suppliers_obj.append(supplier) return suppliers_obj