def get_extra_restriction_citus(self, alias, related_alias): """ Return a pair condition used for joining and subquery pushdown. The condition is something that responds to as_sql(compiler, connection) method. Note that currently referring both the 'alias' and 'related_alias' will not work in some conditions, like subquery pushdown. A parallel method is get_extra_descriptor_filter() which is used in instance.fieldname related object fetching. """ if not (related_alias and alias): return None # Fetch tenant column names for both sides of the relation lhs_model = self.model rhs_model = self.related_model lhs_tenant_id = get_tenant_column(lhs_model) rhs_tenant_id = get_tenant_column(rhs_model) # Fetch tenant fields for both sides of the relation lhs_tenant_field = lhs_model._meta.get_field(lhs_tenant_id) rhs_tenant_field = rhs_model._meta.get_field(rhs_tenant_id) # Get references to both tenant columns lookup_lhs = lhs_tenant_field.get_col(related_alias) lookup_rhs = rhs_tenant_field.get_col(alias) # Create "AND lhs.tenant_id = rhs.tenant_id" as a new condition lookup = lhs_tenant_field.get_lookup("exact")(lookup_lhs, lookup_rhs) condition = WhereNode() condition.add(lookup, "AND") return condition
def test_date_field(self): where = WhereNode() where.add( self._build_lookup("birthday", 'exact', '2013-09-03', field=DateField), AND) self.assertEqual(self._where_as_ldap(where), "(birthday=2013-09-03)")
def get_extra_restriction(self, where_class, alias, related_alias): constraint = WhereNode(connector=AND) for remote, local in self._raw_fields.items(): lookup = local.get_lookup(self, self.related_model._meta.get_field(remote), alias) if lookup: constraint.add(lookup, AND) if constraint.children: return constraint else: return None
def test_timestamp_field(self): dt = datetime.datetime(2018, 6, 25, 20, 21, 22, tzinfo=UTC) where = WhereNode() where.add( self._build_lookup("shadowLastChange", 'exact', dt, field=fields.TimestampField), AND) self.assertEqual(self._where_as_ldap(where), "(shadowLastChange=1529958082)")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or a OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import WhereNode, SubqueryConstraint, AND, OR root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [get_normalized_value(value, self.lhs) for value in self.rhs] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class(target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: return super(RelatedIn, self).as_sql(compiler, connection)
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): assert self.rhs_is_direct_value() self.rhs = get_normalized_value(self.rhs, self.lhs) from django.db.models.sql.where import WhereNode, AND root_constraint = WhereNode() for target, source, val in zip(self.lhs.targets, self.lhs.sources, self.rhs): lookup_class = target.get_lookup(self.lookup_name) root_constraint.add( lookup_class(target.get_col(self.lhs.alias, source), val), AND) return root_constraint.as_sql(compiler, connection) return super().as_sql(compiler, connection)
def test_char_field_contains(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'contains', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=*test*)", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'contains', "te*st"), AND) self.assertEquals(where_as_ldap(where), ("(cn=*te\\2ast*)", []))
def test_char_field_in(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'in', ["foo", "bar"]), AND) self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(cn=bar))", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'in', ["(foo)", "(bar)"]), AND) self.assertEquals(where_as_ldap(where), ("(|(cn=\\28foo\\29)(cn=\\28bar\\29))", []))
def test_char_field_endswith(self): where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "test"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*test)") where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "te*st"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*te\\2ast)")
def test_char_field_contains(self): where = WhereNode() where.add(self._build_lookup("cn", 'contains', "test"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*test*)") where = WhereNode() where.add(self._build_lookup("cn", 'contains', "te*st"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=*te\\2ast*)")
def test_list_field_contains(self): where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', 'foouser', field=fields.ListField), AND) self.assertEqual(self._where_as_ldap(where), "(memberUid=foouser)") where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', '(foouser)', field=fields.ListField), AND) self.assertEqual(self._where_as_ldap(where), "(memberUid=\\28foouser\\29)")
def test_char_field_exact(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'exact', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=test)", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'exact', "(test)"), AND) self.assertEquals(where_as_ldap(where), ("(cn=\\28test\\29)", []))
def test_char_field_in(self): where = WhereNode() where.add(self._build_lookup("cn", 'in', ["foo", "bar"]), AND) self.assertEqual(self._where_as_ldap(where), "(|(cn=foo)(cn=bar))") where = WhereNode() where.add(self._build_lookup("cn", 'in', ["(foo)", "(bar)"]), AND) self.assertEqual(self._where_as_ldap(where), "(|(cn=\\28foo\\29)(cn=\\28bar\\29))")
def test_char_field_exact(self): where = WhereNode() where.add(self._build_lookup('cn', 'exact', "test"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=test)") where = WhereNode() where.add(self._build_lookup('cn', 'exact', "(test)"), AND) self.assertEqual(self._where_as_ldap(where), "(cn=\\28test\\29)")
def test_list_field_contains(self): where = WhereNode() where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', 'foouser'), AND) self.assertEquals(where_as_ldap(where), ("(memberUid=foouser)", [])) where = WhereNode() where.add((Constraint("memberUid", "memberUid", ListField()), 'contains', '(foouser)'), AND) self.assertEquals(where_as_ldap(where), ("(memberUid=\\28foouser\\29)", []))
def test_integer_field(self): where = WhereNode() where.add(self._build_lookup("uid", 'exact', 1, field=IntegerField), AND) self.assertEqual(where_as_ldap(where), "(uid=1)") where = WhereNode() where.add(self._build_lookup("uid", 'gte', 1, field=IntegerField), AND) self.assertEqual(where_as_ldap(where), "(uid>=1)") where = WhereNode() where.add(self._build_lookup("uid", 'lte', 1, field=IntegerField), AND) self.assertEqual(where_as_ldap(where), "(uid<=1)")
def test_float_field(self): where = WhereNode() where.add(self._build_lookup("uid", 'exact', 1.2, field=FloatField), AND) self.assertEqual(where_as_ldap(where), "(uid=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'gte', 1.2, field=FloatField), AND) self.assertEqual(where_as_ldap(where), "(uid>=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'lte', 1.2, field=FloatField), AND) self.assertEqual(where_as_ldap(where), "(uid<=1.2)")
def test_integer_field(self): where = WhereNode() where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND) self.assertEquals(where_as_ldap(where), ("(uid=1)", [])) where = WhereNode() where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND) self.assertEquals(where_as_ldap(where), ("(uid>=1)", [])) where = WhereNode() where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND) self.assertEquals(where_as_ldap(where), ("(uid<=1)", []))
def test_float_field(self): where = WhereNode() where.add((Constraint("uid", "uid", FloatField()), 'exact', 1.2), AND) self.assertEquals(where_as_ldap(where), ("(uid=1.2)", [])) where = WhereNode() where.add((Constraint("uid", "uid", FloatField()), 'gte', 1.2), AND) self.assertEquals(where_as_ldap(where), ("(uid>=1.2)", [])) where = WhereNode() where.add((Constraint("uid", "uid", FloatField()), 'lte', 1.2), AND) self.assertEquals(where_as_ldap(where), ("(uid<=1.2)", []))
def get_extra_restriction(self, alias, remote_alias): field = self.remote_field.model._meta.get_field( self.content_type_field_name) contenttype_pk = self.get_content_type().pk lookup = field.get_lookup('exact')(field.get_col(remote_alias), contenttype_pk) return WhereNode([lookup], connector=AND)
def update_batch(obj, pk_list, values, using): obj.add_update_values(values) for offset in range(0, len(pk_list), GET_ITERATOR_CHUNK_SIZE): obj.where = WhereNode() obj.add_q( Q(pk__in=pk_list[offset:offset + GET_ITERATOR_CHUNK_SIZE])) add_tenant_filters_on_query(obj) obj.get_compiler(using).execute_sql(NO_RESULTS)
def test_char_field_endswith(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'endswith', "test"), AND) self.assertEqual(where_as_ldap(where), ("(cn=*test)", [])) where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'endswith', "te*st"), AND) self.assertEqual(where_as_ldap(where), ("(cn=*te\\2ast)", []))
def test_datetime_field(self): dt = datetime.datetime(2018, 6, 25, 20, 21, 22, tzinfo=UTC) where = WhereNode() where.add(self._build_lookup("modifyTimestamp", 'exact', dt, field=fields.DateTimeField,), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp=20180625202122.000000Z)") where = WhereNode() where.add(self._build_lookup("modifyTimestamp", 'lte', dt, field=fields.DateTimeField,), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp<=20180625202122.000000Z)") where = WhereNode() where.add(self._build_lookup("modifyTimestamp", 'gte', dt, field=fields.DateTimeField,), AND) self.assertEqual(self._where_as_ldap(where), "(modifyTimestamp>=20180625202122.000000Z)")
def test_or(self): where = WhereNode() where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND) where.add((Constraint("givenName", "givenName", CharField()), 'exact', "bar"), OR) self.assertEquals(where_as_ldap(where), ("(|(cn=foo)(givenName=bar))", []))
def test_list_field_contains(self): where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', 'foouser', field=ListField), AND) self.assertEqual(where_as_ldap(where), "(memberUid=foouser)") where = WhereNode() where.add(self._build_lookup("memberUid", 'contains', '(foouser)', field=ListField), AND) self.assertEqual(where_as_ldap(where), "(memberUid=\\28foouser\\29)")
def test_list_field_startswith(self): where = WhereNode() where.add((Constraint("cn", "cn", ListField()), 'startswith', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=test*)", [])) where = WhereNode() where.add((Constraint("cn", "cn", ListField()), 'startswith', "te*st"), AND) self.assertEquals(where_as_ldap(where), ("(cn=te\\2ast*)", []))
def test_or(self): where = WhereNode() where.add(self._build_lookup("cn", 'exact', "foo", field=CharField), AND) where.add( self._build_lookup("givenName", 'exact', "bar", field=CharField), OR) self.assertEqual(where_as_ldap(where), "(|(cn=foo)(givenName=bar))")
def test_char_field_contains(self): where = WhereNode() where.add(self._build_lookup("cn", 'contains', "test"), AND) self.assertEqual(where_as_ldap(where), "(cn=*test*)") where = WhereNode() where.add(self._build_lookup("cn", 'contains', "te*st"), AND) self.assertEqual(where_as_ldap(where), "(cn=*te\\2ast*)")
def test_char_field_endswith(self): where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "test"), AND) self.assertEqual(where_as_ldap(where), "(cn=*test)") where = WhereNode() where.add(self._build_lookup("cn", 'endswith', "te*st"), AND) self.assertEqual(where_as_ldap(where), "(cn=*te\\2ast)")
def test_char_field_in(self): where = WhereNode() where.add(self._build_lookup("cn", 'in', ["foo", "bar"]), AND) self.assertEqual(where_as_ldap(where), "(|(cn=foo)(cn=bar))") where = WhereNode() where.add(self._build_lookup("cn", 'in', ["(foo)", "(bar)"]), AND) self.assertEqual(where_as_ldap(where), "(|(cn=\\28foo\\29)(cn=\\28bar\\29))")
def test_char_field_exact(self): where = WhereNode() where.add(self._build_lookup('cn', 'exact', "test"), AND) self.assertEqual(where_as_ldap(where), "(cn=test)") where = WhereNode() where.add(self._build_lookup('cn', 'exact', "(test)"), AND) self.assertEqual(where_as_ldap(where), "(cn=\\28test\\29)")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or an OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import WhereNode, SubqueryConstraint, AND, OR root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [get_normalized_value(value, self.lhs) for value in self.rhs] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class(target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: if (not getattr(self.rhs, 'has_select_fields', True) and not getattr(self.lhs.field.target_field, 'primary_key', False)): self.rhs.clear_select_clause() if (getattr(self.lhs.output_field, 'primary_key', False) and self.lhs.output_field.model == self.rhs.model): # A case like Restaurant.objects.filter(place__in=restaurant_qs), # where place is a OneToOneField and the primary key of # Restaurant. target_field = self.lhs.field.name else: target_field = self.lhs.field.target_field.name self.rhs.add_fields([target_field], True) return super().as_sql(compiler, connection)
def remove_published_where(queryset): """ By default the versioned queryset filters out so that only versions that are published are returned. If you need to return the full queryset this method can be used. It will modify the sql to remove `where state = 'published'` """ where_children = queryset.query.where.children all_except_published = [ lookup for lookup in where_children if not (lookup.lookup_name == 'exact' and lookup.rhs == PUBLISHED and lookup.lhs.field.name == 'state') ] queryset.query.where = WhereNode() queryset.query.where.children = all_except_published return queryset
def export_as_csv(self, request, queryset): field_names = self.csv_fields field_titles = self.csv_titles if self.csv_titles else self.csv_fields if queryset.count() > 50: new_where = WhereNode(children=queryset.query.where.children[:-1]) queryset.query.where = new_where queryset = queryset.all() date = timezone.localtime(timezone.now()).date() response = HttpResponse(content_type='text/csv') filename = self.csv_filename.format( day=date.strftime('%d'), month=date.strftime('%b').lower(), year=date.strftime('%Y'), ) response['Content-Disposition'] = f'attachment; filename={filename}' writer = csv.writer(response, encoding='utf-8') writer.writerow(field_titles) for obj in queryset: row = [] for field in field_names: if hasattr(self, field) and callable(getattr(self, field)): row.append(getattr(self, field)(obj)) continue if hasattr(obj, field) and callable(getattr(obj, field)): row.append(getattr(obj, field)()) continue item = obj for subfield in field.split('__'): if hasattr(item, subfield): if callable(getattr(item, subfield)): item = getattr(item, subfield)() else: item = getattr(item, subfield) else: item = None break row.append(item) writer.writerow(row) return response
def test_or(self): where = WhereNode() where.add(self._build_lookup("cn", 'exact', "foo", field=CharField), AND) where.add(self._build_lookup("givenName", 'exact', "bar", field=CharField), OR) self.assertEqual(where_as_ldap(where), "(|(cn=foo)(givenName=bar))")
def test_date_field(self): where = WhereNode() where.add((Constraint("birthday", "birthday", DateField()), 'exact', '2013-09-03'), AND) self.assertEquals(where_as_ldap(where), ("(birthday=2013-09-03)", []))
from django.db.models.lookups import (
def test_float_field(self): where = WhereNode() where.add(self._build_lookup("uid", 'exact', 1.2, field=FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'gte', 1.2, field=FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid>=1.2)") where = WhereNode() where.add(self._build_lookup("uid", 'lte', 1.2, field=FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid<=1.2)")
def test_integer_field(self): where = WhereNode() where.add(self._build_lookup("uid", 'exact', 1, field=IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(uid=1)") where = WhereNode() where.add(self._build_lookup("uid", 'gte', 1, field=IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(uid>=1)") where = WhereNode() where.add(self._build_lookup("uid", 'lte', 1, field=IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(uid<=1)") where = WhereNode() where.add(self._build_lookup("uid", 'in', [1, 2], field=IntegerField), AND) self.assertEqual(self._where_as_ldap(where), "(|(uid=1)(uid=2))")
def test_timestamp_field(self): dt = datetime.datetime(2018, 6, 25, 20, 21, 22, tzinfo=UTC) where = WhereNode() where.add(self._build_lookup("shadowLastChange", 'exact', dt, field=fields.TimestampField), AND) self.assertEqual(self._where_as_ldap(where), "(shadowLastChange=1529958082)")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or an OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import (AND, OR, SubqueryConstraint, WhereNode) root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [ get_normalized_value(value, self.lhs) for value in self.rhs ] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class( target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: if (not getattr(self.rhs, 'has_select_fields', True) and not getattr(self.lhs.field.target_field, 'primary_key', False)): self.rhs.clear_select_clause() if (getattr(self.lhs.output_field, 'primary_key', False) and self.lhs.output_field.model == self.rhs.model): # A case like Restaurant.objects.filter(place__in=restaurant_qs), # where place is a OneToOneField and the primary key of # Restaurant. target_field = self.lhs.field.name else: target_field = self.lhs.field.target_field.name self.rhs.add_fields([target_field], True) return super().as_sql(compiler, connection)
def get_queryset(self, request): qs = super(LinkAdmin, self).get_queryset(request).select_related( 'created_by', ).prefetch_related('tags', 'capture_job') qs.query.where = WhereNode() # reset filters to include "deleted" objs return qs
def get_queryset(self, request): qs = super(OrganizationAdmin, self).get_queryset( request).select_related('registrar').prefetch_related('users') qs.query.where = WhereNode() # reset filters to include "deleted" objs return qs
def test_list_field_exact(self): where = WhereNode() where.add((Constraint("cn", "cn", ListField()), 'exact', "test"), AND) self.assertEquals(where_as_ldap(where), ("(cn=test)", []))
def get_extra_restriction(self, alias, remote_alias): cond = WhereNode() cond.add(self.get_content_type_lookup(alias, remote_alias), 'AND') cond.add(self.get_object_id_lookup(alias, remote_alias), 'AND') return cond
def test_boolean_field(self): where = WhereNode() where.add(self._build_lookup("isSuperuser", 'exact', True, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=TRUE)") where = WhereNode() where.add(self._build_lookup("isSuperuser", 'exact', False, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=FALSE)") where = WhereNode() where.add(self._build_lookup("isSuperuser", 'exact', 1, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=TRUE)") where = WhereNode() where.add(self._build_lookup("isSuperuser", 'exact', 0, field=fields.BooleanField), AND) self.assertEqual(self._where_as_ldap(where), "(isSuperuser=FALSE)")
def test_date_field(self): where = WhereNode() where.add(self._build_lookup("birthday", 'exact', '2013-09-03', field=DateField), AND) self.assertEqual(where_as_ldap(where), "(birthday=2013-09-03)")
def as_sql(self, compiler, connection): if isinstance(self.lhs, MultiColSource): # For multicolumn lookups we need to build a multicolumn where clause. # This clause is either a SubqueryConstraint (for values that need to be compiled to # SQL) or a OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. from django.db.models.sql.where import WhereNode, SubqueryConstraint, AND, OR root_constraint = WhereNode(connector=OR) if self.rhs_is_direct_value(): values = [ get_normalized_value(value, self.lhs) for value in self.rhs ] for value in values: value_constraint = WhereNode() for source, target, val in zip(self.lhs.sources, self.lhs.targets, value): lookup_class = target.get_lookup('exact') lookup = lookup_class( target.get_col(self.lhs.alias, source), val) value_constraint.add(lookup, AND) root_constraint.add(value_constraint, OR) else: root_constraint.add( SubqueryConstraint( self.lhs.alias, [target.column for target in self.lhs.targets], [source.name for source in self.lhs.sources], self.rhs), AND) return root_constraint.as_sql(compiler, connection) else: return super(RelatedIn, self).as_sql(compiler, connection)