Пример #1
0
 def get_investor_filter(self, investors):
     """
     Get investor filter
     :param investors:
     :return:
     """
     return Filter(variable='_id', operator='in', value=investors)
Пример #2
0
    def load_filters(self, exclude=[]):
        filters = {}
        parent_company_filters, tertiary_investor_filters = {}, {}
        if self.request:
            session_filters = self.request.session.get(
                '%s:filters' % self.get_filter_doc_type(), {}) or {}
        else:
            session_filters = {}

        for filter_name, filter_dict in session_filters.items():
            if 'preset_id' in filter_dict:
                filter = PresetFilter.from_session(filter_dict)
            else:
                filter = Filter.from_session(filter_dict)
                if filter['variable'] in exclude:
                    continue
            # FIXME: Make this work for filters presets too (no variable set)
            if 'variable' in filter and filter['variable'].startswith(
                    'parent_stakeholder_'):
                filter['variable'] = filter['variable'].replace(
                    'parent_stakeholder_', '')
                parent_company_filters[filter_name] = filter
                filter['variable'] = filter['variable'].replace(
                    'tertiary_investor_', '')
            elif 'variable' in filter and filter['variable'].startswith(
                    'tertiary_investor_'):
                tertiary_investor_filters[filter_name] = filter
            else:
                filters[filter_name] = filter
        # Create subquery for investor variable queries
        if parent_company_filters:
            query = {
                'bool': self.format_filters(parent_company_filters.values())
            }
            raw_result_list = self.execute_elasticsearch_query(
                query, 'investor')
            operational_companies = []
            for result in raw_result_list:
                ids = result['_source']['parent_company_of']
                operational_companies.extend([str(id) for id in ids])
            filters['parent_company'] = self.get_investor_filter(
                operational_companies)
        if tertiary_investor_filters:
            query = self.format_filters(tertiary_investor_filters.values())
            raw_result_list = self.execute_elasticsearch_query(
                query, self.doc_type)
            operational_companies = []
            for result in raw_result_list:
                ids = result['_source']['tertiary_investor_of']
                operational_companies.extend([str(id) for id in ids])
            filters['tertiary_investor'] = self.get_investor_filter(
                operational_companies)

        filters.update(self.load_filters_from_url(exclude=exclude))

        # note: passing only Filters, not (name, filter) dict!
        formatted_filters = self.format_filters(filters.values(),
                                                exclude=exclude)

        return formatted_filters
Пример #3
0
    def load_filters(self, exclude=[]):
        filters = {}
        parent_company_filters, tertiary_investor_filters = {}, {}
        if self.request:
            session_filters = self.request.session.get('filters', {}) or {}
        else:
            session_filters = {}

        for filter_name, filter_dict in session_filters.items():
            if 'preset_id' in filter_dict:
                filter = PresetFilter.from_session(filter_dict)
            else:
                filter = Filter.from_session(filter_dict)
                if filter['variable'] in exclude:
                    continue
            # FIXME: Make this work for filters presets too (no variable set)
            if 'variable' in filter and filter['variable'].startswith('parent_stakeholder_'):
                filter['variable'] = filter['variable'].replace('parent_stakeholder_', '')
                parent_company_filters[filter_name] = filter
                filter['variable'] = filter['variable'].replace('tertiary_investor_', '')
            elif 'variable' in filter and filter['variable'].startswith('tertiary_investor_'):
                tertiary_investor_filters[filter_name] = filter
            else:
                filters[filter_name] = filter
        # Create subquery for investor variable queries
        if parent_company_filters:
            query = {'bool': self.format_filters(parent_company_filters.values())}
            raw_result_list = self.execute_elasticsearch_query(query, 'investor')
            operational_companies = []
            for result in raw_result_list:
                ids = result['_source']['parent_company_of']
                operational_companies.extend([str(id) for id in ids])
            filters['parent_company'] = Filter(variable='operational_stakeholder',
                operator='in', value=operational_companies)
        if tertiary_investor_filters:
            query = self.format_filters(tertiary_investor_filters.values())
            raw_result_list = self.execute_elasticsearch_query(query, self.doc_type)
            operational_companies = []
            for result in raw_result_list:
                ids = result['_source']['tertiary_investor_of']
                operational_companies.extend([str(id) for id in ids])
            filters['tertiary_investor'] = Filter(variable='operational_stakeholder',
                operator='in', value=operational_companies)

        filters.update(self.load_filters_from_url(exclude=exclude))

        # note: passing only Filters, not (name, filter) dict!
        formatted_filters = self.format_filters(filters.values(), exclude=exclude)


        return formatted_filters
Пример #4
0
 def set_country_region_filter(self, data):
     filter_values = {}
     # Country or region filter set?
     if data.get('country', None) or data.get('region', None):
         stored_filters = self.request.session.get('filters', {})
         if not stored_filters:
             stored_filters = {}
         if data.get('country', None):
             filter_values['variable'] = 'target_country'
             filter_values['operator'] = 'is'
             filter_values['value'] = data.get('country')
             try:
                 country = Country.objects.defer('geom').get(
                     pk=data.get('country'))
                 filter_values['display_value'] = country.name
             except:
                 pass
             filter_values['name'] = 'country'
             filter_values['label'] = _('Target country')
             data.pop('country')
         elif data.get('region', None):
             filter_values['variable'] = 'target_region'
             filter_values['operator'] = 'is'
             filter_values['value'] = data.get('region')
             try:
                 region = Region.objects.get(pk=data.get('region'))
                 filter_values['display_value'] = region.name
             except:
                 pass
             filter_values['name'] = 'region'
             filter_values['label'] = str(_('Target region'))
             data.pop('region')
         # Remove existing target country/region filters
         filters = filter(lambda f: f.get('name') in ('country', 'region'),
                          stored_filters.values())
         for stored_filter in list(filters):
             stored_filters.pop(stored_filter['name'], None)
         if filter_values:
             # Set filter
             new_filter = Filter(variable=filter_values['variable'],
                                 operator=filter_values['operator'],
                                 value=filter_values['value'],
                                 name=filter_values.get('name', None),
                                 label=filter_values['label'],
                                 display_value=filter_values.get(
                                     'display_value', None))
             stored_filters[new_filter.name] = new_filter
             self.request.session['filters'] = stored_filters
     else:
         self.remove_country_region_filter()
Пример #5
0
    def load_filters_from_url(self, exclude=[]):
        '''
        Read any querystring param filters. Preset filters not allowed.
        '''
        if self.request:
            variables = self.request.GET.getlist('variable')
            operators = self.request.GET.getlist('operator')
            values = self.request.GET.getlist('value')
        else:
            variables = []
            operators = []
            values = []
        combined = zip(variables, operators, values)

        filters = {
            f[0]: Filter(f[0], f[1], f[2])
            for f in combined if f[0] not in exclude
        }

        return filters
Пример #6
0
 def get_investor_filter(self, investors):
     return Filter(variable='operational_stakeholder',
                   operator='in',
                   value=investors)
Пример #7
0
    def post(self, request, *args, **kwargs):
        """
        Set or remove filters to/from current session cookie.
        Used within the filter section of map, data and chart views.
        """
        # TODO: make this a PATCH, and more RESTful.
        # TODO: use a serializer for param parsing
        stored_filters = self.get_object()

        # TODO: This works, but it's not very standard DRF.
        # Maybe convert to using the POST body for args in future?
        request_data = request.query_params.copy()
        request_data.update(request.data)

        action = request_data.get('action', 'nothing').lower()
        name = request_data.get('name', None)
        if not name:
            name = 'filter_%i' % (len(request.session.get('filters', [])) + 1)

        if action == 'set':
            if 'preset' in request_data:
                # Check for duplicates
                for filter_name, stored_filter in stored_filters.items():
                    if stored_filter.get('preset_id',
                                         '') == request_data['preset']:
                        return Response(stored_filters)
                new_filter = PresetFilter(request_data['preset'], name=name)
            else:
                try:
                    variable = request_data['variable']
                    operator = request_data['operator']
                    value = request_data['value']
                    display_value = request_data.get('display_value', None)
                except KeyError as err:
                    raise serializers.ValidationError(
                        {err.args[0]: _("This field is required.")})
                # Check for duplicates
                for filter_name, stored_filter in stored_filters.items():
                    if (stored_filter.get('variable', '') == variable
                            and stored_filter.get('operator', '') == operator
                            and stored_filter.get('value', '') == value):
                        return Response(stored_filters)
                label = get_field_label(variable)
                new_filter = Filter(variable=variable,
                                    operator=operator,
                                    value=value,
                                    label=label,
                                    name=name,
                                    display_value=display_value)
            stored_filters[new_filter.name] = new_filter
        elif action == 'remove':
            try:
                del stored_filters[name]
                # Default filter?
                if 'default_preset' in name:
                    # Convert default filters to custom filters
                    stored_filters = dict((k.replace('default_', ''), v)
                                          for k, v in stored_filters.items())
                    # Disable default filters
                    request.session['set_default_filters'] = False
            except KeyError:
                pass
        elif action == 'set_default_filters':
            request.session[
                'set_default_filters'] = 'set_default_filters' in request_data
        request.session['filters'] = stored_filters

        return Response(stored_filters)
Пример #8
0
 def to_filter(self):
     return Filter(self.variable,
                   self.operator,
                   self.parsed_value,
                   str(self),
                   key=self.key)