Skip to content

Commit 49456ee

Browse files
committed
Add support for Django 6.0 and modernize setup
1 parent efc5fa8 commit 49456ee

7 files changed

Lines changed: 185 additions & 102 deletions

File tree

netfields/lookups.py

Lines changed: 50 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
11
import warnings
22
from django.core.exceptions import FieldError
33
from django.db.models import Lookup, Transform, IntegerField
4-
from django.db.models.lookups import EndsWith, IEndsWith, StartsWith, IStartsWith, Regex, IRegex
4+
from django.db.models.lookups import (
5+
EndsWith,
6+
IEndsWith,
7+
StartsWith,
8+
IStartsWith,
9+
Regex,
10+
IRegex,
11+
)
512
import ipaddress
613
from netfields.fields import InetAddressField, CidrAddressField
714

@@ -10,6 +17,7 @@ class InvalidLookup(Lookup):
1017
"""
1118
Emulate Django 1.9 error for unsupported lookups
1219
"""
20+
1321
def as_sql(self, qn, connection):
1422
raise FieldError("Unsupported lookup '%s'" % self.lookup_name)
1523

@@ -18,21 +26,28 @@ class InvalidSearchLookup(Lookup):
1826
"""
1927
Emulate Django 1.9 error for unsupported search lookup
2028
"""
21-
lookup_name = 'search'
29+
30+
lookup_name = "search"
2231

2332
def as_sql(self, qn, connection):
24-
raise NotImplementedError("Full-text search is not implemented for this database backend")
33+
raise NotImplementedError(
34+
"Full-text search is not implemented for this database backend"
35+
)
2536

2637

2738
class NetFieldDecoratorMixin(object):
2839
def process_lhs(self, qn, connection, lhs=None):
2940
lhs = lhs or self.lhs
3041
lhs_string, lhs_params = qn.compile(lhs)
31-
if isinstance(lhs.source if hasattr(lhs, 'source') else lhs.output_field, InetAddressField):
32-
lhs_string = 'HOST(%s)' % lhs_string
33-
elif isinstance(lhs.source if hasattr(lhs, 'source') else lhs.output_field, CidrAddressField):
34-
lhs_string = 'TEXT(%s)' % lhs_string
35-
return lhs_string, lhs_params
42+
if isinstance(
43+
lhs.source if hasattr(lhs, "source") else lhs.output_field, InetAddressField
44+
):
45+
lhs_string = "HOST(%s)" % lhs_string
46+
elif isinstance(
47+
lhs.source if hasattr(lhs, "source") else lhs.output_field, CidrAddressField
48+
):
49+
lhs_string = "TEXT(%s)" % lhs_string
50+
return lhs_string, list(lhs_params)
3651

3752

3853
class EndsWith(NetFieldDecoratorMixin, EndsWith):
@@ -61,7 +76,7 @@ class IRegex(NetFieldDecoratorMixin, IRegex):
6176

6277
class NetworkLookup(object):
6378
def get_prep_lookup(self):
64-
if hasattr(self.rhs, 'resolve_expression'):
79+
if hasattr(self.rhs, "resolve_expression"):
6580
return self.rhs
6681
if isinstance(self.rhs, ipaddress._BaseNetwork):
6782
return str(self.rhs)
@@ -70,75 +85,75 @@ def get_prep_lookup(self):
7085

7186
class AddressLookup(object):
7287
def get_prep_lookup(self):
73-
if hasattr(self.rhs, 'resolve_expression'):
88+
if hasattr(self.rhs, "resolve_expression"):
7489
return self.rhs
7590
if isinstance(self.rhs, ipaddress._BaseAddress):
7691
return str(self.rhs)
7792
return str(ipaddress.ip_interface(self.rhs))
7893

7994

8095
class NetContains(AddressLookup, Lookup):
81-
lookup_name = 'net_contains'
96+
lookup_name = "net_contains"
8297

8398
def as_sql(self, qn, connection):
8499
lhs, lhs_params = self.process_lhs(qn, connection)
85100
rhs, rhs_params = self.process_rhs(qn, connection)
86101
params = lhs_params + rhs_params
87-
return '%s >> %s' % (lhs, rhs), params
102+
return "%s >> %s" % (lhs, rhs), params
88103

89104

90105
class NetContained(NetworkLookup, Lookup):
91-
lookup_name = 'net_contained'
106+
lookup_name = "net_contained"
92107

93108
def as_sql(self, qn, connection):
94109
lhs, lhs_params = self.process_lhs(qn, connection)
95110
rhs, rhs_params = self.process_rhs(qn, connection)
96111
params = lhs_params + rhs_params
97-
return '%s << %s' % (lhs, rhs), params
112+
return "%s << %s" % (lhs, rhs), params
98113

99114

100115
class NetContainsOrEquals(AddressLookup, Lookup):
101-
lookup_name = 'net_contains_or_equals'
116+
lookup_name = "net_contains_or_equals"
102117

103118
def as_sql(self, qn, connection):
104119
lhs, lhs_params = self.process_lhs(qn, connection)
105120
rhs, rhs_params = self.process_rhs(qn, connection)
106121
params = lhs_params + rhs_params
107-
return '%s >>= %s' % (lhs, rhs), params
122+
return "%s >>= %s" % (lhs, rhs), params
108123

109124

110125
class NetContainedOrEqual(NetworkLookup, Lookup):
111-
lookup_name = 'net_contained_or_equal'
126+
lookup_name = "net_contained_or_equal"
112127

113128
def as_sql(self, qn, connection):
114129
lhs, lhs_params = self.process_lhs(qn, connection)
115130
rhs, rhs_params = self.process_rhs(qn, connection)
116131
params = lhs_params + rhs_params
117-
return '%s <<= %s' % (lhs, rhs), params
132+
return "%s <<= %s" % (lhs, rhs), params
118133

119134

120135
class NetOverlaps(NetworkLookup, Lookup):
121-
lookup_name = 'net_overlaps'
136+
lookup_name = "net_overlaps"
122137

123138
def as_sql(self, qn, connection):
124139
lhs, lhs_params = self.process_lhs(qn, connection)
125140
rhs, rhs_params = self.process_rhs(qn, connection)
126141
params = lhs_params + rhs_params
127-
return '%s && %s' % (lhs, rhs), params
142+
return "%s && %s" % (lhs, rhs), params
128143

129144

130145
class HostMatches(AddressLookup, Lookup):
131-
lookup_name = 'host'
146+
lookup_name = "host"
132147

133148
def as_sql(self, qn, connection):
134149
lhs, lhs_params = self.process_lhs(qn, connection)
135150
rhs, rhs_params = self.process_rhs(qn, connection)
136151
params = lhs_params + rhs_params
137-
return 'HOST(%s) = HOST(%s)' % (lhs, rhs), params
152+
return "HOST(%s) = HOST(%s)" % (lhs, rhs), params
138153

139154

140155
class Family(Transform):
141-
lookup_name = 'family'
156+
lookup_name = "family"
142157

143158
def as_sql(self, compiler, connection):
144159
lhs, params = compiler.compile(self.lhs)
@@ -154,11 +169,13 @@ class _PrefixlenMixin(object):
154169

155170
def as_sql(self, qn, connection):
156171
warnings.warn(
157-
'min_prefixlen and max_prefixlen will be depreciated in the future; '
158-
'use prefixlen__gte and prefixlen__lte respectively',
159-
DeprecationWarning
172+
"min_prefixlen and max_prefixlen will be depreciated in the future; "
173+
"use prefixlen__gte and prefixlen__lte respectively",
174+
DeprecationWarning,
160175
)
161-
assert self.format_string is not None, "Prefixlen lookups must specify a format_string"
176+
assert (
177+
self.format_string is not None
178+
), "Prefixlen lookups must specify a format_string"
162179
lhs, lhs_params = self.process_lhs(qn, connection)
163180
rhs, rhs_params = self.process_rhs(qn, connection)
164181
params = lhs_params + rhs_params
@@ -167,25 +184,25 @@ def as_sql(self, qn, connection):
167184
def process_lhs(self, qn, connection, lhs=None):
168185
lhs = lhs or self.lhs
169186
lhs_string, lhs_params = qn.compile(lhs)
170-
lhs_string = 'MASKLEN(%s)' % lhs_string
187+
lhs_string = "MASKLEN(%s)" % lhs_string
171188
return lhs_string, lhs_params
172189

173190
def get_prep_lookup(self):
174191
return str(int(self.rhs))
175192

176193

177194
class MaxPrefixlen(_PrefixlenMixin, Lookup):
178-
lookup_name = 'max_prefixlen'
179-
format_string = '%s <= %s'
195+
lookup_name = "max_prefixlen"
196+
format_string = "%s <= %s"
180197

181198

182199
class MinPrefixlen(_PrefixlenMixin, Lookup):
183-
lookup_name = 'min_prefixlen'
184-
format_string = '%s >= %s'
200+
lookup_name = "min_prefixlen"
201+
format_string = "%s >= %s"
185202

186203

187204
class Prefixlen(Transform):
188-
lookup_name = 'prefixlen'
205+
lookup_name = "prefixlen"
189206

190207
def as_sql(self, compiler, connection):
191208
lhs, params = compiler.compile(self.lhs)

pyproject.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[build-system]
2+
requires = ["setuptools", "wheel"]
3+
build-backend = "setuptools.build_meta"

setup.cfg

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
[metadata]
2+
name = django-netfields
3+
version = 1.3.2
4+
description = Django PostgreSQL netfields implementation
5+
long_description = file: README.rst
6+
url = https://github.com/jimfunk/django-postgresql-netfields
7+
author = James Oakley
8+
author_email = jfunk@funktronics.ca
9+
license = BSD
10+
classifiers =
11+
Development Status :: 5 - Production/Stable
12+
Environment :: Web Environment
13+
Framework :: Django
14+
Intended Audience :: Developers
15+
License :: OSI Approved :: BSD License
16+
Operating System :: OS Independent
17+
Programming Language :: Python
18+
Programming Language :: Python :: 3
19+
Topic :: Utilities
20+
21+
[options]
22+
packages = netfields
23+
zip_safe = False
24+
install_requires =
25+
netaddr
26+
django>=1.11
27+
python_requires = >=3.5

setup.py

Lines changed: 2 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,5 @@
11
#!/usr/bin/env python
2-
# -*- coding: utf-8 -*-
3-
from distutils.core import setup
42

5-
import os
3+
from setuptools import setup
64

7-
def get_long_description():
8-
path = os.path.join(os.path.dirname(__file__), 'README.rst')
9-
with open(path) as f:
10-
return f.read()
11-
12-
13-
requirements = [
14-
'netaddr',
15-
'django>=1.8',
16-
]
17-
18-
setup(
19-
name='django-netfields',
20-
version='1.3.2',
21-
license='BSD',
22-
description='Django PostgreSQL netfields implementation',
23-
long_description=get_long_description(),
24-
url='https://github.com/jimfunk/django-postgresql-netfields',
25-
26-
author=u'James Oakley',
27-
author_email='jfunk@funktronics.ca',
28-
29-
packages=["netfields"],
30-
zip_safe=False,
31-
install_requires=requirements,
32-
33-
classifiers=[
34-
'Development Status :: 4 - Beta',
35-
'Environment :: Web Environment',
36-
'Framework :: Django',
37-
'Intended Audience :: Developers',
38-
'License :: OSI Approved :: BSD License',
39-
'Operating System :: OS Independent',
40-
'Programming Language :: Python',
41-
'Programming Language :: Python :: 3',
42-
'Topic :: Utilities',
43-
],
44-
)
5+
setup()

0 commit comments

Comments
 (0)