Coverage for netbox/models/lookups.py: 77%

26 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.db.models import Lookup 

2 

3__all__ = ( 

4 'Ancestor', 

5 'AncestorOrEqual', 

6 'Descendant', 

7 'DescendantOrEqual', 

8) 

9 

10 

11class Ancestor(Lookup): 

12 """ 

13 `path` is a strict ancestor of the queried path: path @> rhs AND path <> rhs. 

14 

15 Use `ancestor_or_equal` for the inclusive form (`@>`). 

16 """ 

17 lookup_name = 'ancestor' 

18 

19 def as_sql(self, compiler, connection): 

20 lhs, lhs_params = self.process_lhs(compiler, connection) 

21 rhs, rhs_params = self.process_rhs(compiler, connection) 

22 return f'({lhs} @> {rhs} AND {lhs} <> {rhs})', lhs_params + rhs_params + lhs_params + rhs_params 

23 

24 

25class AncestorOrEqual(Lookup): 

26 """`path` is an ancestor of (or equal to) the queried path: path @> rhs""" 

27 lookup_name = 'ancestor_or_equal' 

28 

29 def as_sql(self, compiler, connection): 

30 lhs, lhs_params = self.process_lhs(compiler, connection) 

31 rhs, rhs_params = self.process_rhs(compiler, connection) 

32 return f'{lhs} @> {rhs}', lhs_params + rhs_params 

33 

34 

35class Descendant(Lookup): 

36 """ 

37 `path` is a strict descendant of the queried path: path <@ rhs AND path <> rhs. 

38 

39 Use `descendant_or_equal` for the inclusive form (`<@`). 

40 """ 

41 lookup_name = 'descendant' 

42 

43 def as_sql(self, compiler, connection): 

44 lhs, lhs_params = self.process_lhs(compiler, connection) 

45 rhs, rhs_params = self.process_rhs(compiler, connection) 

46 return f'({lhs} <@ {rhs} AND {lhs} <> {rhs})', lhs_params + rhs_params + lhs_params + rhs_params 

47 

48 

49class DescendantOrEqual(Lookup): 

50 """`path` is a descendant of (or equal to) the queried path: path <@ rhs""" 

51 lookup_name = 'descendant_or_equal' 

52 

53 def as_sql(self, compiler, connection): 

54 lhs, lhs_params = self.process_lhs(compiler, connection) 

55 rhs, rhs_params = self.process_rhs(compiler, connection) 

56 return f'{lhs} <@ {rhs}', lhs_params + rhs_params