Skip to content

Commit

Permalink
Fix Python 3 and Django 4.x compatibility issues
Browse files Browse the repository at this point in the history
  • Loading branch information
vdboor committed Feb 2, 2024
1 parent 59887dc commit 8488090
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
1 change: 0 additions & 1 deletion example/example_standalone/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
SITE_ID = 1

USE_I18N = True
USE_L10N = True
USE_TZ = True

MEDIA_ROOT = join(dirname(__file__), "media")
Expand Down
35 changes: 15 additions & 20 deletions fluent_blogs/models/query.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""
A query interface to retrieve blog models and tags.
"""
import sys
from calendar import monthrange
from datetime import datetime, timedelta

Expand All @@ -10,26 +9,24 @@
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.db.models.aggregates import Count
from django.utils.timezone import utc
from django.utils.timezone import get_current_timezone
from parler.models import TranslatableModel

from fluent_blogs import appsettings
from fluent_blogs.models.db import get_category_model, get_entry_model

if sys.version_info[0] >= 3:
basestring = str


__all__ = (
"query_entries",
"query_tags",
)

User = get_user_model()
ENTRY_ORDER_BY_FIELDS = {
"slug": "slug",
"title": "title",
"author": ("author__first_name", "author__last_name"),
"author_slug": ("author__username",),
"author_slug": (f"author__{User.USERNAME_FIELD}",),
"category": ("categories__name",),
"category_slug": ("categories__slug",),
"tag": ("tags__name",),
Expand All @@ -38,10 +35,6 @@
"year": ("publication_date",),
}

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

TAG_ORDER_BY_FIELDS = {
"slug": ("slug",),
Expand Down Expand Up @@ -123,29 +116,29 @@ def query_entries(

# The main category/tag/author filters
if category:
if isinstance(category, basestring):
if isinstance(category, str):
queryset = queryset.categories(category)
elif isinstance(category, (int, long)):
elif isinstance(category, int):
queryset = queryset.filter(categories=category)
else:
raise ValueError("Expected slug or ID for the 'category' parameter")
if category_slug:
queryset = queryset.categories(category)

if tag:
if isinstance(tag, basestring):
if isinstance(tag, str):
queryset = queryset.tagged(tag)
elif isinstance(tag, (int, long)):
elif isinstance(tag, int):
queryset = queryset.filter(tags=tag)
else:
raise ValueError("Expected slug or ID for 'tag' parameter.")
if tag_slug:
queryset = queryset.tagged(tag)

if author:
if isinstance(author, basestring):
if isinstance(author, str):
queryset = queryset.authors(author)
elif isinstance(author, (int, long)):
elif isinstance(author, int):
queryset = queryset.filter(author=author)
else:
raise ValueError("Expected slug or ID for 'author' parameter.")
Expand Down Expand Up @@ -218,19 +211,21 @@ def get_date_range(year=None, month=None, day=None):
if year is None:
return None

timezone = get_current_timezone()

if month is None:
# year only
start = datetime(year, 1, 1, 0, 0, 0, tzinfo=utc)
end = datetime(year, 12, 31, 23, 59, 59, 999, tzinfo=utc)
start = datetime(year, 1, 1, 0, 0, 0, tzinfo=timezone)
end = datetime(year, 12, 31, 23, 59, 59, 999, tzinfo=timezone)
return (start, end)

if day is None:
# year + month only
start = datetime(year, month, 1, 0, 0, 0, tzinfo=utc)
start = datetime(year, month, 1, 0, 0, 0, tzinfo=timezone)
end = start + timedelta(days=monthrange(year, month)[1], microseconds=-1)
return (start, end)
else:
# Exact day
start = datetime(year, month, day, 0, 0, 0, tzinfo=utc)
start = datetime(year, month, day, 0, 0, 0, tzinfo=timezone)
end = start + timedelta(days=1, microseconds=-1)
return (start, end)

0 comments on commit 8488090

Please sign in to comment.