Skip to content

Commit 8488090

Browse files
committed
Fix Python 3 and Django 4.x compatibility issues
1 parent 59887dc commit 8488090

File tree

2 files changed

+15
-21
lines changed

2 files changed

+15
-21
lines changed

Diff for: example/example_standalone/settings.py

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
SITE_ID = 1
2727

2828
USE_I18N = True
29-
USE_L10N = True
3029
USE_TZ = True
3130

3231
MEDIA_ROOT = join(dirname(__file__), "media")

Diff for: fluent_blogs/models/query.py

+15-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
"""
22
A query interface to retrieve blog models and tags.
33
"""
4-
import sys
54
from calendar import monthrange
65
from datetime import datetime, timedelta
76

@@ -10,26 +9,24 @@
109
from django.contrib.auth import get_user_model
1110
from django.contrib.contenttypes.models import ContentType
1211
from django.db.models.aggregates import Count
13-
from django.utils.timezone import utc
12+
from django.utils.timezone import get_current_timezone
1413
from parler.models import TranslatableModel
1514

1615
from fluent_blogs import appsettings
1716
from fluent_blogs.models.db import get_category_model, get_entry_model
1817

19-
if sys.version_info[0] >= 3:
20-
basestring = str
21-
2218

2319
__all__ = (
2420
"query_entries",
2521
"query_tags",
2622
)
2723

24+
User = get_user_model()
2825
ENTRY_ORDER_BY_FIELDS = {
2926
"slug": "slug",
3027
"title": "title",
3128
"author": ("author__first_name", "author__last_name"),
32-
"author_slug": ("author__username",),
29+
"author_slug": (f"author__{User.USERNAME_FIELD}",),
3330
"category": ("categories__name",),
3431
"category_slug": ("categories__slug",),
3532
"tag": ("tags__name",),
@@ -38,10 +35,6 @@
3835
"year": ("publication_date",),
3936
}
4037

41-
if django.VERSION >= (1, 11):
42-
# Django 1.10 doesn't support early importing.
43-
User = get_user_model()
44-
ENTRY_ORDER_BY_FIELDS["author_slug"] = f"author__{User.USERNAME_FIELD}"
4538

4639
TAG_ORDER_BY_FIELDS = {
4740
"slug": ("slug",),
@@ -123,29 +116,29 @@ def query_entries(
123116

124117
# The main category/tag/author filters
125118
if category:
126-
if isinstance(category, basestring):
119+
if isinstance(category, str):
127120
queryset = queryset.categories(category)
128-
elif isinstance(category, (int, long)):
121+
elif isinstance(category, int):
129122
queryset = queryset.filter(categories=category)
130123
else:
131124
raise ValueError("Expected slug or ID for the 'category' parameter")
132125
if category_slug:
133126
queryset = queryset.categories(category)
134127

135128
if tag:
136-
if isinstance(tag, basestring):
129+
if isinstance(tag, str):
137130
queryset = queryset.tagged(tag)
138-
elif isinstance(tag, (int, long)):
131+
elif isinstance(tag, int):
139132
queryset = queryset.filter(tags=tag)
140133
else:
141134
raise ValueError("Expected slug or ID for 'tag' parameter.")
142135
if tag_slug:
143136
queryset = queryset.tagged(tag)
144137

145138
if author:
146-
if isinstance(author, basestring):
139+
if isinstance(author, str):
147140
queryset = queryset.authors(author)
148-
elif isinstance(author, (int, long)):
141+
elif isinstance(author, int):
149142
queryset = queryset.filter(author=author)
150143
else:
151144
raise ValueError("Expected slug or ID for 'author' parameter.")
@@ -218,19 +211,21 @@ def get_date_range(year=None, month=None, day=None):
218211
if year is None:
219212
return None
220213

214+
timezone = get_current_timezone()
215+
221216
if month is None:
222217
# year only
223-
start = datetime(year, 1, 1, 0, 0, 0, tzinfo=utc)
224-
end = datetime(year, 12, 31, 23, 59, 59, 999, tzinfo=utc)
218+
start = datetime(year, 1, 1, 0, 0, 0, tzinfo=timezone)
219+
end = datetime(year, 12, 31, 23, 59, 59, 999, tzinfo=timezone)
225220
return (start, end)
226221

227222
if day is None:
228223
# year + month only
229-
start = datetime(year, month, 1, 0, 0, 0, tzinfo=utc)
224+
start = datetime(year, month, 1, 0, 0, 0, tzinfo=timezone)
230225
end = start + timedelta(days=monthrange(year, month)[1], microseconds=-1)
231226
return (start, end)
232227
else:
233228
# Exact day
234-
start = datetime(year, month, day, 0, 0, 0, tzinfo=utc)
229+
start = datetime(year, month, day, 0, 0, 0, tzinfo=timezone)
235230
end = start + timedelta(days=1, microseconds=-1)
236231
return (start, end)

0 commit comments

Comments
 (0)