1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

33 lines
1015 B
Python
Raw Normal View History

from django.db.models import CharField, TextField, Lookup
from .fields import CachedValueField
2023-02-07 13:24:57 +01:00
class Empty(Lookup):
"""
Filter on whether a string is empty.
"""
lookup_name = 'empty'
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
return 'CAST(LENGTH(%s) AS BOOLEAN) != %s' % (lhs, rhs), params
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-07 13:24:57 +01:00
CharField.register_lookup(Empty)
CachedValueField.register_lookup(NetContainsOrEquals)