2023-02-06 14:00:34 +01:00
|
|
|
from django.db.models import CharField, TextField, Lookup
|
2023-02-07 09:40:25 +01:00
|
|
|
from .fields import CachedValueField
|
2021-07-01 15:17:46 -04:00
|
|
|
|
2023-02-07 13:24:57 +01:00
|
|
|
|
2021-07-01 15:17:46 -04:00
|
|
|
class Empty(Lookup):
|
|
|
|
"""
|
|
|
|
Filter on whether a string is empty.
|
|
|
|
"""
|
|
|
|
lookup_name = 'empty'
|
2023-05-25 15:20:08 -04:00
|
|
|
prepare_rhs = False
|
|
|
|
|
|
|
|
def as_sql(self, compiler, connection):
|
|
|
|
sql, params = compiler.compile(self.lhs)
|
|
|
|
if self.rhs:
|
|
|
|
return f"CAST(LENGTH({sql}) AS BOOLEAN) IS NOT TRUE", params
|
|
|
|
else:
|
|
|
|
return f"CAST(LENGTH({sql}) AS BOOLEAN) IS TRUE", params
|
2021-07-01 15:17:46 -04:00
|
|
|
|
|
|
|
|
2023-02-06 14:00:34 +01:00
|
|
|
class NetContainsOrEquals(Lookup):
|
|
|
|
"""
|
|
|
|
This lookup has the same functionality as the one from the ipam app except lhs is cast to inet
|
|
|
|
"""
|
|
|
|
lookup_name = 'net_contains_or_equals'
|
|
|
|
|
|
|
|
def as_sql(self, qn, connection):
|
|
|
|
lhs, lhs_params = self.process_lhs(qn, connection)
|
|
|
|
rhs, rhs_params = self.process_rhs(qn, connection)
|
|
|
|
params = lhs_params + rhs_params
|
2023-02-18 19:29:57 +01:00
|
|
|
return 'CAST(%s AS INET) >>= %s' % (lhs, rhs), params
|
2023-02-06 14:00:34 +01:00
|
|
|
|
2023-02-07 13:24:57 +01:00
|
|
|
|
2021-07-01 15:17:46 -04:00
|
|
|
CharField.register_lookup(Empty)
|
2023-02-07 09:40:25 +01:00
|
|
|
CachedValueField.register_lookup(NetContainsOrEquals)
|