def generate_product_unique_name(name, category): """ Generates product unique name. """ current_user_id = get_current_user().id return u"{0}]*[{1}]*[{2}".format(name, category, current_user_id)
def register(self, invoice_items): ''' Registers new invoice. :param invoice_items: :return: ''' if invoice_items is None or len(invoice_items) <= 0: raise InvoiceException('At least one item should be included.') current_user = get_current_user() store = get_current_transaction_store() invoice = InvoiceEntity() invoice.invoice_consumer_user_id = current_user.id invoice.invoice_date = datetime.datetime.now() invoice.invoice_status = InvoiceEntity.InvoiceStatusEnum.ORDERED invoice.invoice_id = unicode(unique_id_services.get_id('uuid')) store.add(invoice) counter = 1 result = DynamicObject(entity_to_dic(invoice)) result.invoice_items = [] for item in invoice_items: item_entity = InvoiceItemEntity() item_entity.invoice_id = invoice.invoice_id item_entity.item_color = unicode(item.get('color')) item_entity.item_size = unicode(item.get('size')) item_entity.item_brand = unicode(item.get('brand')) item_entity.item_id = unicode(unique_id_services.get_id('uuid')) item_entity.item_price = Decimal(str(item.get('price'))) item_entity.item_quantity = int(item.get('quantity')) item_entity.item_row = counter item_entity.item_product_id = unicode(item.get('product_id')) products_services.decrease_product_counter( item_entity.item_product_id) product = products_services.get(item_entity.item_product_id, fetch_details=False) if (product.product_whole_sale_type == ProductsEntity.ProductWholesaleTypeEnum.WHOLESALE and current_user.user_production_type != UserEntity.UserProductionTypeEnum.PRODUCER): raise InvoiceException( "User [{0} - {1}] is not producer one and can not register " "product [{2}] which is wholesale product type.".format( current_user.user_id, current_user.user_name, product.product_name)) counter += 1 store.add(item_entity) result.invoice_items.append( DynamicObject(entity_to_dic(item_entity))) return result
def new_func(*args, **kwds): """ The function that creates a new command. """ user = session_services.get_current_user() if user is not None: authorization_services.authorize(user.id, set(permissions)) # If user has permission on the transaction we execute the main function return f(*args, **kwds)
def reset_password(self, user_id, new_password): ''' Resets password of the given user to new password. @param user_id: user ID @param new_password: user new password ''' user = session_services.get_current_user() if not self.is_superuser(user.id): raise SecurityManagerException('You are not super user.') self.update_user(user_id, password = self.encrypt_password(user_id, new_password))
def new_func(*args, **kwds): """ The function that creates a new command. """ user = session_services.get_current_user() if user is not None: if not user.is_superuser: raise AuthorizationException('You are not super user.') # If user has permission on the transaction we execute the main function return f(*args, **kwds)
def change_password(self, user_id, current_password, new_password, **options): ''' Changes password of current user. @paran user_id: user ID @param current_password: user current password @param new_password: user new password ''' user = session_services.get_current_user() if self.encrypt_password(user.id, current_password) != user.password: raise SecurityManagerException('Your password is incorrect.') self.update_user(user.id, password = self.encrypt_password(user.id, new_password))
def is_in_role(self, permission_ids, user_id=None): ''' Returns True if current user has permissions. @param permission_ids: list of permission ID @param user_id: user_id to check it's roles @return: bool ''' if user_id is None: user_id = session_services.get_current_user().id try: self.authorize(user_id, permission_ids) return True except AuthorizationException: return False
def write_history(self, product, colors, sizes, brands, **options): """ Writes product history. :param product: :param colors: :param sizes: :param brands: :param options: :return: """ history = ProductsHistoryEntity() history.product_history_edit_date = datetime.datetime.now() history.product_history_id = unicode(unique_id_services.get_id('uuid')) history.product_id = product.product_id history.product_history_name = product.product_name history.product_history_price = product.product_price history.product_history_category = product.product_category history.product_history_image = product.product_image history.product_history_status = product.product_status history.product_history_unique_name = product.product_unique_name if colors is not None and len(colors) > 0: history.product_history_colors = '-'.join(colors) if sizes is not None and len(sizes) > 0: history.product_history_sizes = '-'.join(sizes) if brands is not None and len(brands) > 0: history.product_history_brands = '-'.join(brands) history.product_history_counter = product.product_counter history.product_history_age_category = product.product_age_category history.product_history_gender = product.product_gender history.product_history_comment = product.product_comment history.product_history_editor_id = get_current_user().id store = get_current_transaction_store() store.add(history)
def find(self, **options): """ Searches products. :param optiosn: :return: """ current_user = get_current_user() from_creation_date = options.get('from_creation_date') to_creation_date = options.get('to_creation_date') from_price = options.get('from_price') to_price = options.get('to_price') name = options.get('name') size = options.get('size') brand = options.get('brand') categories = options.get('categories') if not isinstance(categories, (list, tuple)): categories = [categories] age_categories = options.get('age_categories') if not isinstance(age_categories, (list, tuple)): age_categories = [age_categories] gender = options.get('gender') if not isinstance(gender, (list, tuple)): gender = [gender] include_out_of_stock = options.get('include_out_of_stock') if include_out_of_stock is None: include_out_of_stock = False wholesale_type = options.get('wholesale_type') if wholesale_type in (None, -1): wholesale_type = ProductsEntity.ProductWholesaleTypeEnum.RETAIL if wholesale_type == ProductsEntity.ProductWholesaleTypeEnum.WHOLESALE: if current_user.user_production_type != UserEntity.UserProductionTypeEnum.PRODUCER: raise ProductsException( "Consumer user can not search wholesale products.") just_current_user = options.get('just_current_user') if just_current_user is None: just_current_user = False if just_current_user and current_user.user_production_type != UserEntity.UserProductionTypeEnum.PRODUCER: raise ProductsException( "Consumer user can not search its own products.") expressions = [ ProductsEntity.product_whole_sale_type == wholesale_type ] if not include_out_of_stock: expressions.append(ProductsEntity.product_status == ProductsEntity.ProductStatusEnum.IN_STOCK) if from_creation_date is not None: if not isinstance(from_creation_date, datetime.datetime): from_creation_date = parser.parse(from_creation_date) expressions.append( ProductsEntity.product_creation_date >= from_creation_date) if to_creation_date is not None: if not isinstance(to_creation_date, datetime.datetime): to_creation_date = parser.parse(to_creation_date) expressions.append( ProductsEntity.product_creation_date <= to_creation_date) if from_price not in (None, 0, "", "0"): expressions.append( ProductsEntity.product_price >= Decimal(from_price)) if to_price not in (None, 0, "", "0"): expressions.append( ProductsEntity.product_price <= Decimal(to_price)) if name is not None and name.strip() != "": name = unicode(name) expressions.append( Like(ProductsEntity.product_name, "%{0}%".format(name.strip()))) if size is not None and size.strip() != "": size = unicode(size) expressions.append( Exists( Select(columns=[1], where=And( ProductsSizesEntity.product_id == ProductsEntity.product_id, Like(ProductsSizesEntity.product_size, "%{0}%".format(size.strip()))), tables=[ProductsSizesEntity]))) if brand is not None and brand.strip() != "": brand = unicode(brand) expressions.append( Exists( Select(columns=[1], where=And( ProductsBrandsEntity.product_id == ProductsEntity.product_id, Like(ProductsBrandsEntity.product_brand, "%{0}%".format(brand.strip())))))) if categories is not None and len( categories) > 0 and -1 not in categories: expressions.append(In(ProductsEntity.product_category, categories)) if age_categories is not None and len( age_categories) > 0 and -1 not in age_categories: expressions.append( In(ProductsEntity.product_age_category, age_categories)) if gender is not None and len(gender) > 0 and -1 not in gender: expressions.append(In(ProductsEntity.product_gender, gender)) if just_current_user: expressions.append( ProductsEntity.product_producer_user_id == current_user.id) offset = options.get("__offset__") if offset is None: offset = 0 else: offset = int(offset) limit = options.get("__limit__") if limit in (None, 0): limit = Undef else: limit = int(limit) statement = \ Select(columns=[ProductsEntity.product_id, ProductsEntity.product_name, ProductsEntity.product_category, ProductsEntity.product_image, ProductsEntity.product_age_category, ProductsEntity.product_comment, ProductsEntity.product_creation_date, ProductsEntity.product_price, ProductsEntity.product_gender, ProductsEntity.product_whole_sale_type], where=And(*expressions), tables=[ProductsEntity], order_by=[Desc(ProductsEntity.product_creation_date)], offset=offset, limit=limit) store = get_current_transaction_store() results = [] for (product_id, product_name, product_category, product_image, product_age_category, product_comment, product_creation_date, product_price, product_gender, product_whole_sale_type) in store.execute(statement): results.append( DynamicObject(product_id=product_id, product_name=product_name, product_category=product_category, product_image=product_image, product_age_category=product_age_category, product_comment=product_comment, product_creation_date=product_creation_date, product_price=product_price, product_gender=product_gender, product_whole_sale_type=product_whole_sale_type, product_colors=self.get_product_colors( product_id, concat_results=True), product_sizes=self.get_product_sizes( product_id, concat_results=True), product_brands=self.get_product_brands( product_id, concat_results=True))) return results
def create(self, name, price, category, colors, sizes, brands, **options): """ Creates product. :param name: :param price: :param category: :param colors: :param sizes: :param brands: :param options: :return: """ if name is None or name.strip() == "": raise ProductsException("Product name could not be nothing.") if price is None or price <= 1000: raise ProductsException( "Product price could not be nothing or invalid.") if colors is None or len(colors) <= 0: raise ProductsException( "At least one color for product should be selected.") if sizes is None or len(sizes) <= 0: raise ProductsException( "At least one size for product should be selected.") if brands is None or len(brands) <= 0: raise ProductsException( "At least one brand for product should be selected.") current_user = get_current_user() if current_user.user_production_type != UserEntity.UserProductionTypeEnum.PRODUCER: raise ProductsException("Consumer user can not create product.") self._validate_product_unique_name(name, category) product = ProductsEntity() product.product_id = unicode(unique_id_services.get_id('uuid')) product.product_name = name product.product_price = Decimal(price) product.product_category = category product.product_producer_user_id = current_user.id product.product_unique_name = unicode( generate_product_unique_name(name, category)) product.product_creation_date = datetime.datetime.now() product_image = options.get('image') if product_image not in (None, ""): product.product_image = str(product_image) status = options.get('status') if status is None: status = ProductsEntity.ProductStatusEnum.IN_STOCK product.product_status = status counter = options.get('counter') if counter is None: counter = 0 product.product_counter = counter age_category = options.get('age_category') if age_category is None: age_category = ProductsEntity.ProductAgeCategoryEnum.ADULT product.product_age_category = age_category gender = options.get('gender') if gender is None: gender = ProductsEntity.ProductGenderEnum.BOTH product.product_gender = gender wholesale_type = options.get('wholesale_type') if wholesale_type is None: wholesale_type = ProductsEntity.ProductWholesaleTypeEnum.RETAIL product.product_whole_sale_type = wholesale_type comment = options.get("comment") if comment is not None and len(comment) > 0: product.product_comment = comment store = get_current_transaction_store() store.add(product) for color in colors: if color == "": continue color_entity = ProductsColorsEntity() color_entity.product_color_id = unicode( unique_id_services.get_id('uuid')) color_entity.product_id = product.product_id color_entity.product_color_hex = unicode(color.strip()) store.add(color_entity) for size in sizes: if size == "": continue size_entity = ProductsSizesEntity() size_entity.product_size_id = unicode( unique_id_services.get_id('uuid')) size_entity.product_id = product.product_id size_entity.product_size = unicode(size.strip()) store.add(size_entity) for brand in brands: if brand == "": continue brand_entity = ProductsBrandsEntity() brand_entity.product_brand_id = unicode( unique_id_services.get_id('uuid')) brand_entity.product_id = product.product_id brand_entity.product_brand = unicode(brand.strip()) store.add(brand_entity) history_services.write_history(product, colors, sizes, brands, **options) return DynamicObject(entity_to_dic(product))
def find(self, **options): ''' Searches Purchases. :param options: :return: ''' from_purchase_date = options.get('from_purchase_date') to_purchase_date = options.get('to_purchase_date') statuses = options.get('statuses') consumer_user_id = options.get('consumer_user_id') producer_user_id = options.get('producer_user_id') show_only_current_user = options.get('show_only_current_user') if show_only_current_user is None: show_only_current_user = True expressions = [] current_user_id = get_current_user().id if from_purchase_date is not None: if not isinstance(from_purchase_date, datetime.datetime): from_purchase_date = parser.parse(from_purchase_date) expressions.append(PurchasesEntity.purchase_date >= from_purchase_date) if to_purchase_date is not None: if not isinstance(to_purchase_date, datetime.datetime): to_purchase_date = parser.parse(to_purchase_date) expressions.append(PurchasesEntity.purchase_date <= to_purchase_date) if statuses is not None and len(statuses) > 0: expressions.append(In(PurchasesEntity.purchase_status, statuses)) if consumer_user_id is not None: expressions.append(PurchasesEntity.purchase_consumer_user_id == consumer_user_id) if producer_user_id is not None: expressions.append(PurchasesEntity.purchase_producer_user_id == producer_user_id) if show_only_current_user: expressions.append(Or(PurchasesEntity.purchase_consumer_user_id == current_user_id, PurchasesEntity.purchase_producer_user_id == current_user_id)) offset = options.get("__offset__") if offset is None: offset = 0 else: offset = int(offset) limit = options.get("__limit__") if limit in (None, 0): limit = Undef else: limit = int(limit) statement = Select(columns=[PurchasesEntity.purchase_id, PurchasesEntity.purchase_date, PurchasesEntity.purchase_status, PurchasesEntity.purchase_comment, PurchasesEntity.purchase_consumer_user_id, PurchasesEntity.purchase_producer_user_id, PurchasesEntity.purchase_product_id, PurchasesCommentEntity.comment_id, PurchasesCommentEntity.purchase_comment, PurchasesCommentEntity.purchase_comment_date, PurchasesCommentEntity.purchase_comment_user_id, PurchasesCommentEntity.purchase_comment_consumer_last_check, PurchasesCommentEntity.purchase_comment_producer_last_check, ProductsEntity.product_name, ProductsEntity.product_creation_date, ProductsEntity.product_comment, ProductsEntity.product_image, ProductsEntity.product_age_category, ProductsEntity.product_gender], where=And(Exists(Select(columns=[1], where=And(*expressions), order_by=[Desc(PurchasesEntity.purchase_date)], offset=offset, limit=limit)), PurchasesCommentEntity.purchase_id == PurchasesEntity.purchase_id, PurchasesEntity.purchase_product_id == ProductsEntity.product_id), tables=[PurchasesEntity, PurchasesCommentEntity, ProductsEntity], order_by=[Desc(PurchasesEntity.purchase_date)]) results = {} store = get_current_transaction_store() for (purchase_id, purchase_date, purchase_status, comment, purchase_consumer_user_id, purchase_producer_user_id, purchase_product_id, comment_id, purchase_comment, purchase_comment_date, purchase_comment_user_id, purchase_comment_consumer_last_check, purchase_comment_producer_last_check, product_name, product_creation_date, product_comment, product_image, product_age_category, product_gender) in store.execute(statement): Purchase = results.get(purchase_id) if Purchase is None: purchase = DynamicObject(purchase_id=purchase_id, purchase_date=purchase_date, purchase_status=purchase_status, purchase_comment=comment, purchase_consumer_user_id=purchase_consumer_user_id, purchase_producer_user_id=purchase_producer_user_id, purchase_product_id=purchase_product_id, purchase_comments=[], product=DynamicObject(product_id=purchase_product_id, product_name=product_name, product_creation_date=product_creation_date, product_comment=product_comment, product_image=product_image, product_age_category=product_age_category, product_gender=product_gender)) results[purchase_id] = purchase purchase.purchase_items.append(DynamicObject(comment_id=comment_id, purchase_comment=purchase_comment, purchase_comment_date=purchase_comment_date, purchase_comment_user_id=purchase_comment_user_id, purchase_comment_consumer_last_check=purchase_comment_consumer_last_check, purchase_comment_producer_last_check=purchase_comment_producer_last_check)) return list(reversed(sorted(results.values(), key=lambda k: k['purchase_date'])))
def find(self, **options): ''' Searches invoices. :param options: :return: ''' from_invoice_date = options.get('from_invoice_date') to_invoice_date = options.get('to_invoice_date') statuses = options.get('statuses') consumer_user_id = options.get('consumer_user_id') show_only_current_user = options.get('show_only_current_user') if show_only_current_user is None: show_only_current_user = True expressions = [] if from_invoice_date is not None: if not isinstance(from_invoice_date, datetime.datetime): from_invoice_date = parser.parse(from_invoice_date) expressions.append(InvoiceEntity.invoice_date >= from_invoice_date) if to_invoice_date is not None: if not isinstance(to_invoice_date, datetime.datetime): to_invoice_date = parser.parse(to_invoice_date) expressions.append(InvoiceEntity.invoice_date <= to_invoice_date) if statuses is not None and len(statuses) > 0: expressions.append(In(InvoiceEntity.invoice_status, statuses)) if consumer_user_id is not None: expressions.append( InvoiceEntity.invoice_consumer_user_id == consumer_user_id) if show_only_current_user: expressions.append(InvoiceEntity.invoice_consumer_user_id == get_current_user().id) offset = options.get("__offset__") if offset is None: offset = 0 else: offset = int(offset) limit = options.get("__limit__") if limit in (None, 0): limit = Undef else: limit = int(limit) statement = Select( columns=[ InvoiceEntity.invoice_id, InvoiceEntity.invoice_date, InvoiceEntity.invoice_status, InvoiceEntity.invoice_comment, InvoiceEntity.invoice_consumer_user_id, InvoiceItemEntity.item_id, InvoiceItemEntity.item_product_id, InvoiceItemEntity.item_price, InvoiceItemEntity.item_quantity, InvoiceItemEntity.item_row, InvoiceItemEntity.item_color, InvoiceItemEntity.item_size, InvoiceItemEntity.item_brand, ProductsEntity.product_name, ProductsEntity.product_creation_date, ProductsEntity.product_comment, ProductsEntity.product_image, ProductsEntity.product_age_category, ProductsEntity.product_gender ], where=And( Exists( Select(columns=[1], where=And(*expressions), order_by=[Desc(InvoiceEntity.invoice_date)], offset=offset, limit=limit)), InvoiceItemEntity.invoice_id == InvoiceEntity.invoice_id, InvoiceItemEntity.item_product_id == ProductsEntity.product_id), tables=[InvoiceEntity, InvoiceItemEntity, ProductsEntity], order_by=[ Desc(InvoiceEntity.invoice_date), InvoiceItemEntity.item_row ]) results = {} store = get_current_transaction_store() for (invoice_id, invoice_date, invoice_status, invoice_comment, invoice_consumer_user_id, item_id, item_product_id, item_price, item_quantity, item_row, item_color, item_size, item_brand, product_name, product_creation_date, product_comment, product_image, product_age_category, product_gender) in store.execute(statement): invoice = results.get(invoice_id) if invoice is None: invoice = DynamicObject( invoice_id=invoice_id, invoice_date=invoice_date, invoice_status=invoice_status, invoice_comment=invoice_comment, invoice_consumer_user_id=invoice_consumer_user_id, total_invoce_price=0, invoice_items=[]) results[invoice_id] = invoice invoice.invoice_items.append( DynamicObject(item_id=item_id, item_product_id=item_product_id, item_price=item_price, item_quantity=item_quantity, item_row=item_row, item_color=item_color, item_size=item_size, item_brand=item_brand, product=DynamicObject( product_id=item_product_id, product_name=product_name, product_creation_date=product_creation_date, product_comment=product_comment, product_image=product_image, product_age_category=product_age_category, product_gender=product_gender))) invoice.total_invoce_price += item_quantity * item_price return list( reversed(sorted(results.values(), key=lambda k: k['invoice_date'])))
def get_producer_registered_invoices(self, **options): ''' Returns producer registered invoices. :return: ''' current_user = get_current_user() if current_user.user_production_type != UserEntity.UserProductionTypeEnum.PRODUCER: return [] from_invoice_date = options.get('from_invoice_date') to_invoice_date = options.get('to_invoice_date') wholesale_type = options.get('wholesale_type') if wholesale_type in (None, -1): wholesale_type = ProductsEntity.ProductWholesaleTypeEnum.RETAIL expressions = \ [InvoiceEntity.invoice_status == InvoiceEntity.InvoiceStatusEnum.ORDERED, ProductsEntity.product_producer_user_id == current_user.id, ProductsEntity.product_whole_sale_type == wholesale_type] if from_invoice_date is not None: if not isinstance(from_invoice_date, datetime.datetime): from_invoice_date = parser.parse(from_invoice_date) expressions.append(InvoiceEntity.invoice_date >= from_invoice_date) if to_invoice_date is not None: if not isinstance(to_invoice_date, datetime.datetime): to_invoice_date = parser.parse(to_invoice_date) expressions.append(InvoiceEntity.invoice_date <= to_invoice_date) offset = options.get("__offset__") if offset is None: offset = 0 else: offset = int(offset) limit = options.get("__limit__") if limit in (None, 0): limit = Undef else: limit = int(limit) statement = Select( columns=[ InvoiceEntity.invoice_id, InvoiceEntity.invoice_date, InvoiceEntity.invoice_status, InvoiceEntity.invoice_comment, InvoiceEntity.invoice_consumer_user_id, InvoiceItemEntity.item_id, InvoiceItemEntity.item_product_id, InvoiceItemEntity.item_price, InvoiceItemEntity.item_quantity, InvoiceItemEntity.item_row, InvoiceItemEntity.item_color, InvoiceItemEntity.item_size, InvoiceItemEntity.item_brand, ProductsEntity.product_name, ProductsEntity.product_creation_date, ProductsEntity.product_comment, ProductsEntity.product_image, ProductsEntity.product_age_category, ProductsEntity.product_gender ], where=And( Exists( Select(columns=[1], where=And(*expressions), order_by=[Desc(InvoiceEntity.invoice_date)], offset=offset, limit=limit)), InvoiceItemEntity.invoice_id == InvoiceEntity.invoice_id, InvoiceItemEntity.item_product_id == ProductsEntity.product_id), tables=[InvoiceEntity, InvoiceItemEntity, ProductsEntity], order_by=[ Desc(InvoiceEntity.invoice_date), InvoiceItemEntity.item_row ]) results = [] store = get_current_transaction_store() for (invoice_id, invoice_date, invoice_status, invoice_comment, invoice_consumer_user_id, item_id, item_product_id, item_price, item_quantity, item_row, item_color, item_size, item_brand, product_name, product_creation_date, product_comment, product_image, product_age_category, product_gender) in store.execute(statement): invoice_item = DynamicObject( invoice_id=invoice_id, invoice_date=invoice_date, invoice_status=invoice_status, invoice_comment=invoice_comment, invoice_consumer_user_id=invoice_consumer_user_id, item_id=item_id, item_product_id=item_product_id, item_price=item_price, item_quantity=item_quantity, item_row=item_row, item_color=item_color, item_size=item_size, item_brand=item_brand, product=DynamicObject( product_id=item_product_id, product_name=product_name, product_creation_date=product_creation_date, product_comment=product_comment, product_image=product_image, product_age_category=product_age_category, product_gender=product_gender)) results.append(invoice_item) return results
def __check_permissions(self): if len(self.__permissions) != 0: user = session_services.get_current_user() authorization_services.authorize(user.id, self.__permissions)