Ejemplo n.º 1
0
    def get_by_name(self, name, category, **options):
        """
        Returns product info.

        :param category:
        :param name:
        :return:
        """

        unique_name = generate_product_unique_name(name, category)

        store = get_current_transaction_store()
        entity = store.find(
            ProductsEntity,
            ProductsEntity.product_unique_name == unique_name).one()

        if entity is None:
            raise ProductsException("Product [{0}] not found".format(name))

        product = DynamicObject(entity_to_dic(entity))

        fetch_details = options.get('fetch_details')
        if fetch_details is None:
            fetch_details = True

        if fetch_details:
            store = get_current_transaction_store()

            product.colors = []
            colors = store.find(
                ProductsColorsEntity,
                ProductsColorsEntity.product_id == product.product_id)
            product.colors.extend(
                [DynamicObject(entity_to_dic(e)) for e in colors])

            product.sizes = []
            sizes = store.find(
                ProductsSizesEntity,
                ProductsSizesEntity.product_id == product.product_id)
            product.sizes.extend(
                [DynamicObject(entity_to_dic(e)) for e in sizes])

            product.brands = []
            brands = store.find(
                ProductsBrandsEntity,
                ProductsBrandsEntity.product_id == product.product_id)
            product.brands.extend(
                [DynamicObject(entity_to_dic(e)) for e in brands])

        return product
Ejemplo n.º 2
0
    def get_product_brands(self, product_id, **options):
        '''
        Returns product brands.

        :param product_id:
        :return:
        '''

        store = get_current_transaction_store()

        statement = \
            Select(columns=[ProductsBrandsEntity.product_brand,
                            ProductsBrandsEntity.product_brand_id],
                   where=And(ProductsBrandsEntity.product_id == product_id),
                   tables=[ProductsBrandsEntity])

        results = []
        for (product_brand, product_brand_id) in store.execute(statement):
            results.append(
                DynamicObject(product_brand=product_brand,
                              product_brand_id=product_brand_id))

        concat_results = options.get('concat_results')
        if concat_results is None:
            concat_results = False

        if concat_results:
            return ','.join([b.product_brand for b in results])

        return results
Ejemplo n.º 3
0
    def _get(self, id):
        '''
        Returns user entity:
        '''

        store = get_current_transaction_store()
        return store.get(UserEntity, id)
Ejemplo n.º 4
0
    def get_users(self, **options):
        """
        Returns users using given options.

        @return: [DynamicObject<user info...>]
        """

        expressions = []

        statuses = options.get('statuses')
        if statuses is not None and len(statuses) > 0:
            expressions.append(In(UserEntity.user_status, statuses))

        types = options.get('types')
        if types is not None and len(types) > 0:
            expressions.append(In(UserEntity.user_type, types))

        store = get_current_transaction_store()
        entities = store.find(UserEntity, And(*expressions))

        results = []
        for user in entities:
            if user.user_id not in ('admin', 'root', 'support'):
                results.append(DynamicObject(entity_to_dic(user)))

        return results
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    def find(self, **options):
        """
        Searches product histories.

        :param optiosn:
        :return:
        """

        from_edit_date = options.get('from_edit_date')
        to_edit_date = options.get('to_edit_date')
        from_price = options.get('from_price')
        to_price = options.get('to_price')
        name = options.get('name')
        categories = options.get('categories')
        include_out_of_stock = options.get('include_out_of_stock')
        if include_out_of_stock is None:
            include_out_of_stock = False

        expressions = []
        if not include_out_of_stock:
            expressions.append(
                ProductsHistoryEntity.product_history_status ==
                ProductsHistoryEntity.ProductHistoryStatusEnum.IN_STOCK)
        if from_edit_date is not None:
            expressions.append(ProductsHistoryEntity.product_history_edit_date
                               >= from_edit_date)
        if to_edit_date is not None:
            expressions.append(
                ProductsHistoryEntity.product_history_edit_date <= to_edit_date
            )
        if from_price is not None:
            expressions.append(
                ProductsHistoryEntity.product_history_price >= from_price)
        if to_price is not None:
            expressions.append(
                ProductsHistoryEntity.product_history_price >= to_price)
        if name is not None and name.strip() != "":
            expressions.append(
                Like(ProductsHistoryEntity.product_history_name,
                     "%{0}%".format(name)))
        if categories is not None and len(categories) > 0:
            expressions.append(
                In(ProductsHistoryEntity.product_history_category, categories))

        store = get_current_transaction_store()
        entities = store.find(
            ProductsHistoryEntity, And(*expressions)).order_by(
                ProductsHistoryEntity.product_history_edit_date)

        results = []
        for entity in entities:
            results.append(DynamicObject(entity_to_dic(entity)))

        return results
Ejemplo n.º 7
0
 def get_permission(self, id):
     '''
     Returns specified permission information.
     @param id:
     '''
     
     store = get_current_transaction_store()
     entity = store.get(PermissionEntity, unicode(id))
     if entity is None:
         message = _('Permission [{permission_id}] not found.')
         raise PermissionNotFoundException(message.format(permission_id = id))
     return DynamicObject(entity_to_dic(entity))
Ejemplo n.º 8
0
    def get_role(self, id):
        '''
        Returns role information by given role ID.
        
        @param role_id: role ID
        '''

        store = get_current_transaction_store()
        entity = store.get(RoleEntity, unicode(id))
        if entity is None:
            message = _('Role [{role_id}] not found.')
            raise RoleNotFoundException(message.format(role_id = id))
        return DynamicObject(entity_to_dic(entity))
Ejemplo n.º 9
0
 def remove_permission(self, id):
     '''
     Removes specified permission.
     
     @param id: permission ID
     '''
     
     store = get_current_transaction_store()
     entity = store.get(PermissionEntity, unicode(id))
     if entity is None:
         message = _('Permission [{id}] not found.')
         raise PermissionNotFoundException(message.format(id = id))
     store.remove(entity)
     store.flush()
Ejemplo n.º 10
0
 def create_permission(self, id, name, **options):
     '''
     Creates a new permission.
     @param id: permission ID
     @param name: permission name
     @param **options: 
     '''
     
     store = get_current_transaction_store()
     entity = PermissionEntity()
     entity.id = unicode(id)
     entity.name = unicode(name)
     store.add(entity)
     store.flush() 
Ejemplo n.º 11
0
 def remove_role(self, id):
     '''
     Removes specified role.
     
     @param id: role ID
     '''
     
     store = get_current_transaction_store()
     entity = store.get(RoleEntity, unicode(id))
     if entity is None:
         message = _('Role [{role_id}] not found.')
         raise RoleNotFoundException(message.format(role_id = id))
     store.remove(entity)
     store.flush()
Ejemplo n.º 12
0
 def update_permission(self, id, **params):
     '''
     Updates specified permission using given parameters. 
     @param id: permission ID
     @param **params: 
     '''
     
     store = get_current_transaction_store()
     entity = store.get(PermissionEntity, unicode(id))
     if entity is None:
         message = _('Permission [{permission_id}] not found.')
         raise PermissionNotFoundException(message.format(permission_id = id))
     entity.name = unicode(params.get('name', entity.name))
     store.flush()
Ejemplo n.º 13
0
    def remove_user(self, id):
        """
        Removes the given user.

        @param id: user name
        """

        user = self.get_user_by_id(id)
        if user.user_status != UserEntity.UserStatusEnum.USER_REGISTERED:
            raise UserSecurityException("Only registered user can be removed.")

        store = get_current_transaction_store()
        user_entity = self._get(user.id)
        store.remove(user_entity)
Ejemplo n.º 14
0
 def remove_user(self, id):
     '''
     Removes the given user.
     
     @param id: user name
     '''
     
     store = get_current_transaction_store()
     entity = store.get(UserEntity, unicode(id))
     if entity is None:
         message = _('User [{user_id}] not found.')
         raise UserNotFoundException(message.format(user_id = id))
     store.remove(entity)
     store.flush()
Ejemplo n.º 15
0
    def create_role(self, id, name, **options):
        '''
        Creates a new role.
        
        @param id: role ID
        @param name: role name 
        @param **options:
        '''

        store = get_current_transaction_store()
        entity = RoleEntity()
        entity.id = unicode(id)
        entity.name = unicode(name)
        store.add(entity)
        store.flush()
Ejemplo n.º 16
0
    def _get(self, purchase_id):
        """
        Returns Purchase entity.

        :param Purchase_id:
        :return:
        """

        store = get_current_transaction_store()
        entity = store.get(PurchasesEntity, purchase_id)

        if entity is None:
            raise PurchaseException("Purchase [{0}] not found.".format(purchase_id))

        return entity
Ejemplo n.º 17
0
 def get_user(self, id):
     '''
     Returns user information by specified name
     
     @param id: user name
     
     @return: user data as DynamicObject
     '''
     
     store = get_current_transaction_store()
     entity = store.get(UserEntity, unicode(id))
     if entity is None:
         message = 'User [{user_id}] not found.'
         raise UserNotFoundException(message.format(user_id = id))
     return DynamicObject(entity_to_dic(entity))
Ejemplo n.º 18
0
 def update_role(self, id, **params):
     '''
     Removes specified role.
     
     @param id: role ID
     @param **params: 
     '''
     
     store = get_current_transaction_store()
     entity = store.get(RoleEntity, unicode(id))
     if entity is None:
         message = _('Role [{role_id}] not found.')
         raise RoleNotFoundException(message.format(role_id = id))
     entity.role_name = unicode(params.get('role_name', entity.role_name))
     store.flush()
Ejemplo n.º 19
0
    def _get(self, invoice_id):
        """
        Returns invoice entity.

        :param invoice_id:
        :return:
        """

        store = get_current_transaction_store()
        entity = store.get(InvoiceEntity, invoice_id)

        if entity is None:
            raise InvoiceException(
                "Invoice [{0}] not found.".format(invoice_id))

        return entity
Ejemplo n.º 20
0
    def get_permission_roles(self, permission_id, **options):
        '''
        Returns all roles which are including specified permission.
        
        @param permission_id: permission ID
        
        @return: [role as DynamicObject]
        '''

        store = get_current_transaction_store()
        entities = store.find(RoleEntity, And(RoleEntity.id == Role2PermissionEntity.role_id,
                                              Role2PermissionEntity.permission_id == unicode(permission_id)))
        results = []
        for entity in entities:
            results.append(DynamicObject(entity_to_dic(entity)))
        return results
Ejemplo n.º 21
0
 def get_role_users(self, role_id, **options):
     '''
     Returns all user which are assigned to specified role.
     
     @param role_id: role ID
     
     @return: [user as DynamicObject]
     '''
     
     store = get_current_transaction_store()
     entities = store.find(UserEntity, And(UserEntity.id == User2RoleEntity.user_id,
                                           User2RoleEntity.role_id == unicode(role_id)))
     results = []
     for entity in entities:
         results.append(DynamicObject(entity_to_dic(entity)))
     return results
Ejemplo n.º 22
0
    def get_user(self, id, **options):
        """
        Returns user information by specified name

        @param id: user name

        @return: user data as DynamicObject
        """

        store = get_current_transaction_store()
        user = store.find(UserEntity, And(UserEntity.id == id)).one()

        if user is None:
            return None

        return DynamicObject(entity_to_dic(user))
Ejemplo n.º 23
0
    def _get(self, product_history_id):
        """
        Returns product history entity.

        :param product_history_id:
        :return:
        """

        store = get_current_transaction_store()
        entity = store.get(ProductsHistoryEntity, product_history_id)

        if entity is None:
            raise ProductsHistoryException(
                "Product history [{0}] not found.".format(product_history_id))

        return entity
Ejemplo n.º 24
0
 def deny_permission(self, role_id, permission_ids):
     '''
     Denies given permissions.
     
     @param role_id: role ID
     @param permission_ids: list of permission ID
     '''
     
     store = get_current_transaction_store()
     for permission_id in permission_ids:
         entity = store.find(Role2PermissionEntity,
                             And(Role2PermissionEntity.role_id == unicode(role_id),
                                 Role2PermissionEntity.permission_id == unicode(permission_id))).one()
         if entity is not None:
             store.remove(entity)
     store.flush()
Ejemplo n.º 25
0
 def unassign_role(self, user_id, role_ids):
     '''
     Unassigns given roles from specified user.
     
     @param user_id: user ID
     @param role_ids: list role ID
     '''
     
     store = get_current_transaction_store()
     for role_id in role_ids:
         entity = store.find(User2RoleEntity, 
                             And(User2RoleEntity.user_id == unicode(user_id),
                                 User2RoleEntity.role_id == unicode(role_id))).one()
         if entity is not None:
             store.remove(entity)
     store.flush()
Ejemplo n.º 26
0
    def _get(self, product_id):
        """
        Returns product entity.

        :param product_id:
        :return:
        """

        store = get_current_transaction_store()
        entity = store.get(ProductsEntity, unicode(product_id))

        if entity is None:
            raise ProductsException(
                "Product [{0}] not found".format(product_id))

        return entity
Ejemplo n.º 27
0
 def create_user(self, id, password, fullname, **options):
     '''
     Creates a new user.
     
     @param id: user ID
     @param password: password
     @param fullname: full name
     @param **options: 
     
     '''
     
     store = get_current_transaction_store()
     entity = UserEntity()
     entity.id = unicode(id)
     entity.fullname = unicode(fullname)
     entity.password = unicode(self.encrypt_password(id, password))
     store.add(entity)
     store.flush()
Ejemplo n.º 28
0
    def update_user(self, id, **params):
        '''
        Updates specified user with given parameters.
        
        @param id: user name
        @param **options: 
        '''

        store = get_current_transaction_store()
        entity = store.get(UserEntity, unicode(id))
        if entity is None:
            message = _('User [{user_id}] not found.')
            raise UserNotFoundException(message.format(user_id = id))
        entity.fullname = unicode(params.get('fullname', entity.fullname))
        password = params.get('password')
        if password is not None:
            entity.password = unicode(self.encrypt_password(id, password))
        store.flush()
Ejemplo n.º 29
0
    def _validate_product_unique_name(self, name, category):
        """
        Validates product name uniqueness.
        """

        unique_name = generate_product_unique_name(name, category)

        store = get_current_transaction_store()
        result = \
            store.execute(Select(columns=[Count(1)],
                                 where=And(ProductsEntity.product_unique_name == unicode(unique_name)),
                                 tables=[ProductsEntity])).get_one()

        if result is None:
            return

        result, = result
        if result > 0:
            raise ProductsException("Please select another name for product.")
Ejemplo n.º 30
0
 def grant_permission(self, role_id, permission_ids):
     '''
     Grants given permissions.
     
     @param role_id: role ID
     @param permission_ids: list of permission ID
     '''
     
     store = get_current_transaction_store()
     for permission_id in permission_ids:
         entity = store.find(Role2PermissionEntity,
                             And(Role2PermissionEntity.role_id == unicode(role_id),
                                 Role2PermissionEntity.permission_id == unicode(permission_id))).one()
         if entity is None:
             r2p = Role2PermissionEntity()
             r2p.permission_id = unicode(permission_id)
             r2p.role_id = unicode(role_id)
             store.add(r2p)
     store.flush()