Skip to content

Commit

Permalink
Fixed jDateTimeField problem with auto_now_add when tz enable.(#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
hramezani committed Dec 16, 2020
1 parent e7a3bbf commit fbeef70
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Unreleased
### Fixed
- Fixed `jDateTimeField` problem with `auto_now_add` when tz enable (#124)

## [4.1.0] - 2020-12-11
### Fixed
- Fix Django Jalali javascipt files loading when Django Jquery file loaded first
Expand Down
5 changes: 4 additions & 1 deletion django_jalali/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,10 @@ def to_python(self, value):

def pre_save(self, model_instance, add):
if self.auto_now or (self.auto_now_add and add):
value = jdatetime.datetime.fromgregorian(datetime=timezone.now())
value = jdatetime.datetime.now()
if value is not None and settings.USE_TZ and timezone.is_naive(value):
default_timezone = timezone.get_default_timezone()
value = timezone.make_aware(value, default_timezone)
setattr(model_instance, self.attname, value)
return value
else:
Expand Down
7 changes: 7 additions & 0 deletions jalali_test/foo/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,11 @@ class Migration(migrations.Migration):
('datetime2', django_jalali.db.models.jDateTimeField(default=datetime.datetime(2011, 9, 22, 10, 22, 23, 240000))),
],
),
migrations.CreateModel(
name='ModelWithAutoNowAdd',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('datetimefield', django_jalali.db.models.jDateTimeField(auto_now_add=True)),
],
),
]
5 changes: 5 additions & 0 deletions jalali_test/foo/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,8 @@ class DateTimeWithDefault(models.Model):
objects = jmodels.jManager()
datetime1 = jmodels.jDateTimeField(default=jdatetime.datetime(1390, 6, 31, 10, 22, 23, 240000))
datetime2 = jmodels.jDateTimeField(default=datetime.datetime(2011, 9, 22, 10, 22, 23, 240000))


class ModelWithAutoNowAdd(models.Model):
objects = jmodels.jManager()
datetimefield = jmodels.jDateTimeField(auto_now_add=True)
22 changes: 21 additions & 1 deletion jalali_test/foo/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@
from django import get_version
from django.utils import timezone

from foo.models import Bar, BarTime, DateWithDefault, DateTimeWithDefault
from foo.models import (
Bar,
BarTime,
DateWithDefault,
DateTimeWithDefault,
ModelWithAutoNowAdd,
)
import jdatetime
from urllib.parse import unquote

Expand Down Expand Up @@ -133,6 +139,20 @@ def test_lookup_date_with_no_tz(self):
self.assertEqual(str(k[0].datetime), '1392-03-12 10:22:23.240000')
self.assertEqual(k[0].datetime.strftime('%z'), '')

def test_lookup_auto_now_add(self):
ModelWithAutoNowAdd.objects.create()
dt = ModelWithAutoNowAdd.objects.all()[0].datetimefield
objects = ModelWithAutoNowAdd.objects.filter(datetimefield=dt)
self.assertEqual(objects[0].datetimefield, dt)

@requires_tz_support
@override_settings(USE_TZ=True, TIME_ZONE='Asia/Tehran')
def test_lookup_auto_now_add_datetime_with_tz(self):
ModelWithAutoNowAdd.objects.create()
dt = ModelWithAutoNowAdd.objects.all()[0].datetimefield
objects = ModelWithAutoNowAdd.objects.filter(datetimefield=dt)
self.assertEqual(objects[0].datetimefield, dt)

def test_serialize_default_jdatetime_value(self):
jdt1 = jdatetime.datetime(1390, 6, 31, 10, 22, 23, 240000)
field = jmodels.jDateTimeField(default=jdt1)
Expand Down

0 comments on commit fbeef70

Please sign in to comment.