def get_investor_filter(self, investors): """ Get investor filter :param investors: :return: """ return Filter(variable='_id', operator='in', value=investors)
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()
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
def get_investor_filter(self, investors): return Filter(variable='operational_stakeholder', operator='in', value=investors)
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)
def to_filter(self): return Filter(self.variable, self.operator, self.parsed_value, str(self), key=self.key)