def create_query(self):
        where_condition = ''

        if self.filters.keys():
            filter_queries = []
            for filter_name, filter_value in self.filters.items():
                querystring_operator = filter_name.split('__')[-1]
                operator = self.mapping_operator.get(querystring_operator, 'Eq')

                filter_name = to_camel(filter_name.split('__')[0])
                if operator in self.date_operators:
                    column_type, value = 'DateTime', "{}T00:00:00Z".format(filter_value)  # 2016-03-26
                    query = '<{}><FieldRef Name="{}" /><Value Type="{}">{}</Value></{}>'.format(
                        operator, filter_name, column_type, value, operator)
                elif operator == 'Contains':
                    column_type, values = 'Note', filter_value
                    query = '<{}><FieldRef Name="{}" /><Value Type="{}">{}</Value></{}>'.format(
                            operator, filter_name, column_type, filter_value, operator)
                else:
                    column_type, values = 'Text', filter_value.split(',')
                    query = ''
                    for value in values:
                        query = query + '<{}><FieldRef Name="{}" /><Value Type="{}">{}</Value></{}>'.format(
                            operator, filter_name, column_type, value, operator)
                    if len(values) > 1:
                        query = f'<Or>{query}</Or>'
                filter_queries.append(query)
            where_condition = recursive_builder(filter_queries)
            if len(filter_queries) > 1:
                where_condition = f"<And>{where_condition}</And>"

        scope = f' Scope="{self.scope}"' if self.scope else ''
        query = f'<View{scope}><Query><Where>{where_condition}</Where></Query></View>'
        return query
Exemplo n.º 2
0
    def get_filter_querystring(self):
        filter_queries = []
        for filter_name, filter_value in self.filters.items():
            # operator
            querystring_operator = filter_name.split('__')[-1]
            operator = self.mapping_operator.get(querystring_operator, 'eq')
            # filter
            filter_name = to_camel(filter_name.split('__')[0])
            if operator in self.date_operators:
                values = ["{}T00:00:00Z".format(filter_value)]  # 2016-03-26
                query = ' or '.join([f"{filter_name} {operator} datetime'{value}'" for value in values])
            elif operator == 'substringof':
                values = filter_value.split(',')
                query = ' or '.join([f"{operator}('{value}', {filter_name})" for value in values])

            else:
                values = filter_value.split(',')
                query = ' or '.join([f"{filter_name} {operator} '{value}'" for value in values])
                if len(values) > 1:
                    query = f'({query})'
            filter_queries.append(query)
            logger.info(query)
        return str(" and ".join(filter_queries))