|
1 | 1 | """ |
2 | 2 | A query interface to retrieve blog models and tags. |
3 | 3 | """ |
4 | | -import sys |
5 | 4 | from calendar import monthrange |
6 | 5 | from datetime import datetime, timedelta |
7 | 6 |
|
|
10 | 9 | from django.contrib.auth import get_user_model |
11 | 10 | from django.contrib.contenttypes.models import ContentType |
12 | 11 | from django.db.models.aggregates import Count |
13 | | -from django.utils.timezone import utc |
| 12 | +from django.utils.timezone import get_current_timezone |
14 | 13 | from parler.models import TranslatableModel |
15 | 14 |
|
16 | 15 | from fluent_blogs import appsettings |
17 | 16 | from fluent_blogs.models.db import get_category_model, get_entry_model |
18 | 17 |
|
19 | | -if sys.version_info[0] >= 3: |
20 | | - basestring = str |
21 | | - |
22 | 18 |
|
23 | 19 | __all__ = ( |
24 | 20 | "query_entries", |
25 | 21 | "query_tags", |
26 | 22 | ) |
27 | 23 |
|
| 24 | +User = get_user_model() |
28 | 25 | ENTRY_ORDER_BY_FIELDS = { |
29 | 26 | "slug": "slug", |
30 | 27 | "title": "title", |
31 | 28 | "author": ("author__first_name", "author__last_name"), |
32 | | - "author_slug": ("author__username",), |
| 29 | + "author_slug": (f"author__{User.USERNAME_FIELD}",), |
33 | 30 | "category": ("categories__name",), |
34 | 31 | "category_slug": ("categories__slug",), |
35 | 32 | "tag": ("tags__name",), |
|
38 | 35 | "year": ("publication_date",), |
39 | 36 | } |
40 | 37 |
|
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}" |
45 | 38 |
|
46 | 39 | TAG_ORDER_BY_FIELDS = { |
47 | 40 | "slug": ("slug",), |
@@ -123,29 +116,29 @@ def query_entries( |
123 | 116 |
|
124 | 117 | # The main category/tag/author filters |
125 | 118 | if category: |
126 | | - if isinstance(category, basestring): |
| 119 | + if isinstance(category, str): |
127 | 120 | queryset = queryset.categories(category) |
128 | | - elif isinstance(category, (int, long)): |
| 121 | + elif isinstance(category, int): |
129 | 122 | queryset = queryset.filter(categories=category) |
130 | 123 | else: |
131 | 124 | raise ValueError("Expected slug or ID for the 'category' parameter") |
132 | 125 | if category_slug: |
133 | 126 | queryset = queryset.categories(category) |
134 | 127 |
|
135 | 128 | if tag: |
136 | | - if isinstance(tag, basestring): |
| 129 | + if isinstance(tag, str): |
137 | 130 | queryset = queryset.tagged(tag) |
138 | | - elif isinstance(tag, (int, long)): |
| 131 | + elif isinstance(tag, int): |
139 | 132 | queryset = queryset.filter(tags=tag) |
140 | 133 | else: |
141 | 134 | raise ValueError("Expected slug or ID for 'tag' parameter.") |
142 | 135 | if tag_slug: |
143 | 136 | queryset = queryset.tagged(tag) |
144 | 137 |
|
145 | 138 | if author: |
146 | | - if isinstance(author, basestring): |
| 139 | + if isinstance(author, str): |
147 | 140 | queryset = queryset.authors(author) |
148 | | - elif isinstance(author, (int, long)): |
| 141 | + elif isinstance(author, int): |
149 | 142 | queryset = queryset.filter(author=author) |
150 | 143 | else: |
151 | 144 | raise ValueError("Expected slug or ID for 'author' parameter.") |
@@ -218,19 +211,21 @@ def get_date_range(year=None, month=None, day=None): |
218 | 211 | if year is None: |
219 | 212 | return None |
220 | 213 |
|
| 214 | + timezone = get_current_timezone() |
| 215 | + |
221 | 216 | if month is None: |
222 | 217 | # 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) |
225 | 220 | return (start, end) |
226 | 221 |
|
227 | 222 | if day is None: |
228 | 223 | # 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) |
230 | 225 | end = start + timedelta(days=monthrange(year, month)[1], microseconds=-1) |
231 | 226 | return (start, end) |
232 | 227 | else: |
233 | 228 | # Exact day |
234 | | - start = datetime(year, month, day, 0, 0, 0, tzinfo=utc) |
| 229 | + start = datetime(year, month, day, 0, 0, 0, tzinfo=timezone) |
235 | 230 | end = start + timedelta(days=1, microseconds=-1) |
236 | 231 | return (start, end) |
0 commit comments