Repository: MAKENTNU/web

Issue:
TimePlace test fails periodically
Line 166 in news/tests.py fails:
```
def test_timeplace_duplicate(self):
    ...
    self.assertEqual(new.start_date, new_start_date)
```
Example of error message:
```
AssertionError: datetime.date(2018, 5, 10) != datetime.date(2018, 5, 9)
```

This is likely due to timezone mismatch. Can be reproduced by setting the system time between 00:00 and 01:59.


Discussion:
Can be fixed by replacing DateFields' default values with `datetime.date.today()` instead of `django.utils.timezone.now()`, like what happens when `DateField.auto_now_add` is set to True ([ref](https://docs.djangoproject.com/en/2.0/ref/models/fields/#django.db.models.DateField.auto_now_add)).

Resolving patch:
diff --git a/news/migrations/0009_auto_20180503_0141.py b/news/migrations/0009_auto_20180503_0141.py
new file mode 100644
--- /dev/null
+++ b/news/migrations/0009_auto_20180503_0141.py
@@ -0,0 +1,29 @@
+# Generated by Django 2.0.5 on 2018-05-02 23:41
+
+import datetime
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('news', '0008_auto_20180501_1240'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='article',
+            name='pub_date',
+            field=models.DateField(default=datetime.date.today, verbose_name='Publishing date'),
+        ),
+        migrations.AlterField(
+            model_name='timeplace',
+            name='pub_date',
+            field=models.DateField(default=datetime.date.today, verbose_name='Publishing date'),
+        ),
+        migrations.AlterField(
+            model_name='timeplace',
+            name='start_date',
+            field=models.DateField(default=datetime.date.today, verbose_name='Start date'),
+        ),
+    ]
diff --git a/news/models.py b/news/models.py
--- a/news/models.py
+++ b/news/models.py
@@ -1,10 +1,10 @@
-from datetime import time
+from datetime import date, time
 
-from django.utils.translation import gettext_lazy as _
-from django.db import models
 from ckeditor.fields import RichTextField
+from django.db import models
 from django.db.models import Q
 from django.utils import timezone
+from django.utils.translation import gettext_lazy as _
 
 
 class ArticleManager(models.Manager):
@@ -74,7 +74,7 @@ class Meta:
 class Article(NewsBase):
     objects = ArticleManager()
     pub_date = models.DateField(
-        default=timezone.now,
+        default=date.today,
         verbose_name=_('Publishing date'),
     )
     pub_time = models.TimeField(
@@ -101,7 +101,7 @@ class TimePlace(models.Model):
     objects = TimePlaceManager()
     event = models.ForeignKey(Event, on_delete=models.CASCADE)
     pub_date = models.DateField(
-        default=timezone.now,
+        default=date.today,
         verbose_name=_('Publishing date'),
     )
     pub_time = models.TimeField(
@@ -109,7 +109,7 @@ class TimePlace(models.Model):
         verbose_name=_('Publishing time'),
     )
     start_date = models.DateField(
-        default=timezone.now,
+        default=date.today,
         verbose_name=_('Start date'),
     )
     end_date = models.DateField(


Test patch:
diff --git a/news/tests.py b/news/tests.py
--- a/news/tests.py
+++ b/news/tests.py
@@ -1,5 +1,5 @@
-from datetime import timedelta
 import json
+from datetime import timedelta
 
 from django.contrib.auth.models import User, Permission
 from django.core.files.uploadedfile import SimpleUploadedFile
@@ -160,8 +160,8 @@ def test_timeplace_duplicate(self):
         new = TimePlace.objects.exclude(pk=tp.pk).latest('pk')
         self.assertRedirects(response, reverse('timeplace-edit', args=[new.pk]))
 
-        new_start_date = (tp.start_date + timedelta(weeks=1)).date()
-        new_end_date = (tp.end_date + timedelta(weeks=1)).date() if tp.end_date else None
+        new_start_date = tp.start_date + timedelta(weeks=1)
+        new_end_date = (tp.end_date + timedelta(weeks=1)) if tp.end_date else None
         self.assertTrue(new.hidden)
         self.assertEqual(new.start_date, new_start_date)
         self.assertEqual(new.end_date, new_end_date)