From 0c442c1c2e1b5a66fd0df2e45378877603c12f7f Mon Sep 17 00:00:00 2001 From: Terje Kvernes Date: Wed, 7 Aug 2024 14:33:58 +0200 Subject: [PATCH] Refactoring cleanup. --- mreg/api/v1/filters.py | 128 +++++++++++++---------------------------- 1 file changed, 41 insertions(+), 87 deletions(-) diff --git a/mreg/api/v1/filters.py b/mreg/api/v1/filters.py index ba88b9cc..59451293 100644 --- a/mreg/api/v1/filters.py +++ b/mreg/api/v1/filters.py @@ -32,7 +32,18 @@ INT_OPERATORS: OperatorList = ["exact", "in", "gt", "lt"] EXACT_OPERATORS: OperatorList = ["exact"] - +HOST_FIELDS = { + "host": INT_OPERATORS, + "host__comment": STRING_OPERATORS, + "host__contact": STRING_OPERATORS, + "host__name": STRING_OPERATORS, + "host__ttl": INT_OPERATORS, +} + +CREATED_UPDATED = { + "created_at": INT_OPERATORS, + "updated_at": INT_OPERATORS, +} class CIDRFieldExactFilter(filters.CharFilter): pass @@ -43,11 +54,7 @@ class Meta: model = BACnetID fields = { "id": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, + **HOST_FIELDS, } @@ -56,15 +63,10 @@ class Meta: model = Cname fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "name": STRING_OPERATORS, "ttl": INT_OPERATORS, + **HOST_FIELDS, + **CREATED_UPDATED, } @@ -73,9 +75,8 @@ class Meta: model = ForwardZone fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "name": STRING_OPERATORS, + **CREATED_UPDATED, } @@ -84,11 +85,10 @@ class Meta: model = ForwardZoneDelegation fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "name": STRING_OPERATORS, "nameservers": INT_OPERATORS, "comment": STRING_OPERATORS, + **CREATED_UPDATED, } @@ -141,8 +141,6 @@ class Meta: model = Host fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "name": STRING_OPERATORS, "contact": STRING_OPERATORS, "ttl": INT_OPERATORS, @@ -178,6 +176,7 @@ class Meta: "srvs__weight": INT_OPERATORS, "srvs__port": INT_OPERATORS, "srvs__ttl": INT_OPERATORS, + **CREATED_UPDATED, } @@ -186,13 +185,12 @@ class Meta: model = HostGroup fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "description": STRING_OPERATORS, "hosts": INT_OPERATORS, "name": STRING_OPERATORS, "owners": INT_OPERATORS, "parent": INT_OPERATORS, + **CREATED_UPDATED, } @@ -203,11 +201,7 @@ class Meta: "id": INT_OPERATORS, "ipaddress": STRING_OPERATORS, "macaddress": STRING_OPERATORS, - "host": INT_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__comment": STRING_OPERATORS, + **HOST_FIELDS, } @@ -216,10 +210,9 @@ class Meta: model = Label fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "description": STRING_OPERATORS, "name": STRING_OPERATORS, + **CREATED_UPDATED, } @@ -227,12 +220,8 @@ class LocFilterSet(filters.FilterSet): class Meta: model = Loc fields = { - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "loc": STRING_OPERATORS, + **HOST_FIELDS, } @@ -241,15 +230,10 @@ class Meta: model = Mx fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "priority": INT_OPERATORS, "mx": STRING_OPERATORS, + **HOST_FIELDS, + **CREATED_UPDATED } @@ -258,10 +242,9 @@ class Meta: model = NameServer fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "name": STRING_OPERATORS, "ttl": INT_OPERATORS, + **CREATED_UPDATED, } @@ -270,19 +253,14 @@ class Meta: model = Naptr fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "preference": INT_OPERATORS, "order": INT_OPERATORS, "flag": STRING_OPERATORS, "service": STRING_OPERATORS, "regex": STRING_OPERATORS, "replacement": STRING_OPERATORS, + **HOST_FIELDS, + **CREATED_UPDATED, } @@ -293,11 +271,10 @@ class Meta: model = NetGroupRegexPermission fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "group": STRING_OPERATORS, "regex": STRING_OPERATORS, "labels": INT_OPERATORS, + **CREATED_UPDATED, } @@ -308,8 +285,6 @@ class Meta: model = Network fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "description": STRING_OPERATORS, "vlan": INT_OPERATORS, "dns_delegated": EXACT_OPERATORS, @@ -317,6 +292,7 @@ class Meta: "location": STRING_OPERATORS, "frozen": EXACT_OPERATORS, "reserved": INT_OPERATORS, + **CREATED_UPDATED, } @@ -325,8 +301,6 @@ class Meta: model = NetworkExcludedRange fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "network": INT_OPERATORS, "network__description": STRING_OPERATORS, "network__vlan": INT_OPERATORS, @@ -337,6 +311,7 @@ class Meta: "network__reserved": INT_OPERATORS, "start_ip": STRING_OPERATORS, "end_ip": STRING_OPERATORS, + **CREATED_UPDATED, } @@ -345,17 +320,13 @@ class Meta: model = PtrOverride fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "ipaddress": EXACT_OPERATORS, + **HOST_FIELDS, + **CREATED_UPDATED, } + class ReverseZoneFilterSet(filters.FilterSet): network = CIDRFieldExactFilter(field_name="network") @@ -363,9 +334,8 @@ class Meta: model = ReverseZone fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "name": STRING_OPERATORS, + **CREATED_UPDATED, } @@ -374,13 +344,12 @@ class Meta: model = ReverseZoneDelegation fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, "name": STRING_OPERATORS, "nameservers": INT_OPERATORS, "comment": STRING_OPERATORS, "zone": INT_OPERATORS, "zone__name": STRING_OPERATORS, + **CREATED_UPDATED, } @@ -389,18 +358,13 @@ class Meta: model = Srv fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "name": STRING_OPERATORS, "priority": INT_OPERATORS, "weight": INT_OPERATORS, "port": INT_OPERATORS, "ttl": INT_OPERATORS, + **HOST_FIELDS, + **CREATED_UPDATED, } @@ -409,16 +373,11 @@ class Meta: model = Sshfp fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "algorithm": INT_OPERATORS, "hash_type": INT_OPERATORS, "fingerprint": STRING_OPERATORS, + **HOST_FIELDS, + **CREATED_UPDATED, } @@ -427,12 +386,7 @@ class Meta: model = Txt fields = { "id": INT_OPERATORS, - "created_at": INT_OPERATORS, - "updated_at": INT_OPERATORS, - "host": INT_OPERATORS, - "host__comment": STRING_OPERATORS, - "host__contact": STRING_OPERATORS, - "host__name": STRING_OPERATORS, - "host__ttl": INT_OPERATORS, "txt": STRING_OPERATORS, + **HOST_FIELDS, + **CREATED_UPDATED, }