Skip to content

Commit c4864aa

Browse files
Merge branch 'lcd1232:master' into fix-default-values
2 parents d135330 + 06f79a1 commit c4864aa

File tree

9 files changed

+76
-11
lines changed

9 files changed

+76
-11
lines changed

.github/workflows/test.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ jobs:
3535
- "2.2"
3636
- "3.0"
3737
- "3.1"
38-
38+
- "3.2"
39+
- "4.0"
40+
exclude:
41+
# django 4.0 supports python >= 3.8
42+
- python-version: "3.6"
43+
django-version: "4.0"
44+
- python-version: "3.7"
45+
django-version: "4.0"
3946
steps:
4047
- uses: actions/checkout@v1
4148
- name: Set up Python ${{ matrix.python-version }}

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ pip-log.txt
2828

2929
# Sublime Codeintel
3030
.codeintel/
31+
32+
# virtualenv
33+
venv/

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
3+
## 2.0.5 - 2022-10-03
4+
### Fixed
5+
- Disable psycopg2 parsing JSONField value in Django 3.1+ ([#29](https://github.com/lcd1232/django-postgrespool2/pull/29))
6+
7+
## 2.0.3 - 2022-07-17
8+
### Fixed
9+
- Fixed timezone aware ([#27](https://github.com/lcd1232/django-postgrespool2/pull/27))
10+
311
## 2.0.1 - 2021-02-28
412
### Added
513
- Support django 2.2, 3.0 and 3.1

django_postgrespool2/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
__version__ = "2.0.1"
1+
__version__ = "2.0.5"
22
__author__ = 'lcd1232'

django_postgrespool2/base.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import datetime
12
import logging
23
from functools import partial
34
from importlib import import_module
@@ -10,14 +11,27 @@
1011
)
1112
from django.db.backends.postgresql.creation import DatabaseCreation as Psycopg2DatabaseCreation
1213
from django.dispatch import Signal
14+
from django.utils import version
1315
try:
1416
from django.utils.asyncio import async_unsafe
1517
except ImportError:
1618
# dummy decorator
1719
def async_unsafe(func):
1820
return func
1921
try:
22+
# django 2.2
2023
from django.db.backends.postgresql.utils import utc_tzinfo_factory
24+
from django.utils.timezone import utc
25+
26+
def utc_tzinfo_factory(offset):
27+
zero = 0
28+
# psycopg>=2.9 sends offset as timedelta
29+
if isinstance(offset, datetime.timedelta):
30+
zero = datetime.timedelta()
31+
if offset != zero:
32+
raise AssertionError("database connection isn't set to UTC")
33+
return utc
34+
2135
except ImportError:
2236
utc_tzinfo_factory = None
2337
from sqlalchemy import event
@@ -40,10 +54,11 @@ def async_unsafe(func):
4054
pool_args['poolclass'] = pool_cls
4155

4256
db_pool = manage(Database, **pool_args)
43-
pool_disposed = Signal(providing_args=["connection"])
57+
pool_disposed = Signal()
4458

4559
log = logging.getLogger('z.pool')
4660

61+
django_version = version.get_version_tuple(version.get_version())
4762

4863
def _log(message, *args):
4964
log.debug(message)
@@ -101,12 +116,13 @@ def create_cursor(self, name=None):
101116
name, scrollable=False, withhold=self.connection.autocommit)
102117
else:
103118
cursor = self._pool_connection.cursor()
104-
cursor.tzinfo_factory = utc_tzinfo_factory if settings.USE_TZ else None
119+
cursor.tzinfo_factory = self.tzinfo_factory if settings.USE_TZ else None
105120
return cursor
106121

107122
def tzinfo_factory(self, offset):
108-
if utc_tzinfo_factory is not None:
109-
return utc_tzinfo_factory
123+
if utc_tzinfo_factory:
124+
# for Django 2.2
125+
return utc_tzinfo_factory(offset)
110126
return self.timezone
111127

112128
def dispose(self):
@@ -141,6 +157,8 @@ def get_new_connection(self, conn_params):
141157
if self.isolation_level != c.isolation_level:
142158
c.set_session(isolation_level=self.isolation_level)
143159

160+
if django_version >= (3, 1, 1):
161+
psycopg2.extras.register_default_jsonb(conn_or_curs=c, loads=lambda x: x)
144162
return c
145163

146164
def is_usable(self):

docker-compose.dev.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
services:
2+
postgres_backend:
3+
image: postgres:alpine
4+
ports:
5+
- 5432:5432
6+
environment:
7+
POSTGRES_DB: pool
8+
POSTGRES_USER: pool
9+
POSTGRES_PASSWORD: pool

tests/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
from django.db import models
2+
from django.utils import timezone
3+
4+
try:
5+
from django.db.models import JSONField
6+
except ImportError:
7+
from django.contrib.postgres.fields import JSONField
28

39

410
class DogModel(models.Model):
511

612
name = models.CharField(max_length=200)
13+
created = models.DateTimeField(default=timezone.now)
14+
attrs = JSONField(default=dict)

tests/test_django.py

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1-
from django.test import TestCase
2-
from .models import DogModel
1+
from django.test import TestCase, override_settings
2+
from django.utils import timezone
3+
from tests.models import DogModel
34

45

56
class TestPool(TestCase):
67

7-
def tearUP(self):
8+
def test_simple_request(self):
89
DogModel.objects.create(name='wow')
910
DogModel.objects.create(name='gog')
1011

11-
def test_simple_request(self):
12-
pass
12+
@override_settings(USE_TZ=True, TIME_ZONE='Asia/Hong_Kong')
13+
def test_datetime_timezone(self):
14+
dog = DogModel.objects.create(name="wow", created=timezone.now())
15+
self.assertEqual(dog.created.tzname(), "UTC")
16+
dog = DogModel.objects.get(name="wow")
17+
self.assertEqual(dog.created.tzname(), "UTC")
18+
19+
def test_with_json(self):
20+
DogModel.objects.create(name="wow", attrs={"color": "pink"})
21+
dog = DogModel.objects.get(name="wow")
22+
self.assertEqual(dog.attrs, {"color": "pink"})

tox.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ deps =
2727
dj2.2: https://github.com/django/django/archive/stable/2.2.x.tar.gz#egg=django
2828
dj3.0: https://github.com/django/django/archive/stable/3.0.x.tar.gz#egg=django
2929
dj3.1: https://github.com/django/django/archive/stable/3.1.x.tar.gz#egg=django
30+
dj3.2: https://github.com/django/django/archive/stable/3.2.x.tar.gz#egg=django
31+
dj4.0: https://github.com/django/django/archive/stable/4.0.x.tar.gz#egg=django
3032
djmaster: https://github.com/django/django/archive/master.tar.gz#egg=django
3133

3234
commands =

0 commit comments

Comments
 (0)