def get_db_fields(self): if not self._db_fields: self._db_fields = [] fields = self.fields.values() if isinstance(self.fields, dict) else self.fields for field in fields: if RE_FORMATTED.match(field): self._db_fields.extend(RE_FORMATTED.findall(field)) else: self._db_fields.append(field) return self._db_fields
def get_row(self, row): '''Format a single row (if necessary)''' if isinstance(self.fields, dict): return dict([(key, text_type(value).format( **row) if RE_FORMATTED.match(value) else row[value]) for key, value in self.fields.items()]) else: return [ text_type(field).format( **row) if RE_FORMATTED.match(field) else row[field] for field in self.fields ]
def get_row(self, row): """Format a single row (if necessary)""" if isinstance(self.fields, dict): return dict( [ (key, text_type(value).format(**row) if RE_FORMATTED.match(value) else row[value]) for key, value in self.fields.items() ] ) else: return [ text_type(field).format(**row) if RE_FORMATTED.match(field) else row[field] for field in self.fields ]
def search_col_4(self, search, queryset): idx=4 field = self.get_field(idx) fields = RE_FORMATTED.findall(field) if RE_FORMATTED.match(field) else [field] if not search.startswith('-'): if self.dt_data['bRegex_%s' % idx]: criterions = [Q(**{'%s__iregex' % field: search}) for field in fields if self.can_regex(field)] if len(criterions) > 0: search = reduce(or_, criterions) queryset = queryset.filter(search) else: for term in search.split(): criterions = (Q(**{'%s__icontains' % field: term}) for field in fields) search = reduce(or_, criterions) queryset = queryset.filter(search) return queryset
def test_formatted_nester_token(self): '''Should match a formatted field description with a single token''' matches = RE_FORMATTED.findall('{nested__field_0}-{nested__field_1}: {nested__field_2}') self.assertEqual(len(matches), 3) for i in xrange(3): self.assertEqual(matches[i], 'nested__field_%s' % i) return
def test_formatted_nester_token(self): '''Should match a formatted field description with a single token''' matches = RE_FORMATTED.findall( '{nested__field_0}-{nested__field_1}: {nested__field_2}') self.assertEqual(len(matches), 3) for i in range(3): self.assertEqual(matches[i], 'nested__field_%s' % i)
def search_col_4(self, search, queryset): idx = 4 field = self.get_field(idx) fields = RE_FORMATTED.findall(field) if RE_FORMATTED.match( field) else [field] if not search.startswith('-'): if self.dt_data['bRegex_%s' % idx]: criterions = [ Q(**{'%s__iregex' % field: search}) for field in fields if self.can_regex(field) ] if len(criterions) > 0: search = reduce(or_, criterions) queryset = queryset.filter(search) else: for term in search.split(): criterions = (Q(**{'%s__icontains' % field: term}) for field in fields) search = reduce(or_, criterions) queryset = queryset.filter(search) return queryset
def test_formatted_single_token(self): '''Should match a formatted field description with a single token''' matches = RE_FORMATTED.findall('{field}') self.assertEqual(len(matches), 1) self.assertEqual(matches[0], 'field')
def test_not_formatted(self): '''Should not match unformatted field descriptions''' self.assertIsNone(RE_FORMATTED.match('my_field'))