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 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_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 test_and(self): where = WhereNode() where.add(self._build_lookup("cn", 'exact', "foo", field=CharField), AND) where.add( self._build_lookup("givenName", 'exact', "bar", field=CharField), AND) self.assertEqual(where_as_ldap(where), "(&(cn=foo)(givenName=bar))")
def test_float_field(self): where = WhereNode() where.add( self._build_lookup("uid", 'exact', 1.2, field=fields.FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid=1.2)") where = WhereNode() where.add( self._build_lookup("uid", 'gte', 1.2, field=fields.FloatField), AND) self.assertEqual(self._where_as_ldap(where), "(uid>=1.2)") where = WhereNode() where.add( self._build_lookup("uid", 'lte', 1.2, field=fields.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 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 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): 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 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) 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((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 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 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 resolve_expression(self, *args, **kwargs): return WhereNode([ child.resolve_expression(*args, **kwargs) for child in self.children ])
======= return super().get_prep_lookup() >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 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 <<<<<<< HEAD # SQL) or a OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. ======= # SQL) or an OR-combined list of (col1 = val1 AND col2 = val2 AND ...) clauses. >>>>>>> 37c99181c9a6b95433d60f8c8ef9af5731096435 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)
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)", []))