Fix created/submitted dates to use timezone and call function when needed instead of at initialization.

This commit is contained in:
kououken 2019-03-08 14:31:29 -08:00
parent a05a952960
commit 977c5dd175
6 changed files with 99 additions and 4 deletions

View file

@ -0,0 +1,25 @@
# Generated by Django 2.1.7 on 2019-03-08 22:16
import datetime
from django.db import migrations, models
from django.utils.timezone import utc
class Migration(migrations.Migration):
dependencies = [
('backend', '0002_auto_20190303_0106'),
]
operations = [
migrations.AlterField(
model_name='report',
name='date_created',
field=models.DateTimeField(default=datetime.datetime(2019, 3, 8, 22, 16, 32, 697634, tzinfo=utc), verbose_name='date created'),
),
migrations.AlterField(
model_name='report',
name='date_submitted',
field=models.DateTimeField(default=datetime.datetime(2019, 3, 8, 22, 16, 32, 697634, tzinfo=utc), verbose_name='date submitted'),
),
]

View file

@ -0,0 +1,24 @@
# Generated by Django 2.1.7 on 2019-03-08 22:23
from django.db import migrations, models
import django.utils.timezone
class Migration(migrations.Migration):
dependencies = [
('backend', '0003_auto_20190308_1416'),
]
operations = [
migrations.AlterField(
model_name='report',
name='date_created',
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='date created'),
),
migrations.AlterField(
model_name='report',
name='date_submitted',
field=models.DateTimeField(default=django.utils.timezone.now, verbose_name='date submitted'),
),
]

View file

@ -1,6 +1,6 @@
from django.db import models
from django.conf import settings
import datetime
from django.utils import timezone
import ntpath
class Report(models.Model):
@ -10,8 +10,8 @@ class Report(models.Model):
"""
user_id = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
title = models.CharField(max_length=128)
date_created = models.DateTimeField('date created', default=datetime.date.today)
date_submitted = models.DateTimeField('date submitted', default=datetime.date.today)
date_created = models.DateTimeField('date created', default=timezone.now)
date_submitted = models.DateTimeField('date submitted', default=timezone.now)
submitted = models.BooleanField(default=False)
reference_number = models.CharField(max_length=32, default='')

View file

@ -116,3 +116,48 @@ class ReportTests(TestCase):
force_authenticate(review_request, user=user)
response = report_detail(review_request, 1)
self.assertEqual(response.status_code, 409)
def test_report_finalize_logged_out(self):
"""
Test for when an unauthenticated user tries to finalize a report.
"""
factory = APIRequestFactory()
request = factory.put('/api/v1/report/1/final')
response = create_report(request)
self.assertEqual(response.status_code, 401)
def test_report_finalize_logged_in_not_finalized(self):
"""
Test for when an authenticated user tries to finalize a report
that has not been finalized yet.
"""
factory = APIRequestFactory()
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
user = CustomUser.objects.get(email='one@one.com')
force_authenticate(add_report_request, user=user)
create_report(add_report_request)
review_request = factory.put('/api/v1/report/1/final')
force_authenticate(review_request, user=user)
response = report_detail(review_request, 1)
self.assertEqual(response.status_code, 200)
reports = Report.objects.filter(user_id=user)
print("reports=",len(reports))
self.assertTrue(report.submitted)
def test_report_finalize_logged_in_already_finalized(self):
"""
Test for when an authenticated user tries to finalize a report
that has already been finalized.
"""
factory = APIRequestFactory()
add_report_request = factory.post('/api/v1/report', {'title':'Test Report', 'reference':'12345'})
user = CustomUser.objects.get(email='one@one.com')
force_authenticate(add_report_request, user=user)
create_report(add_report_request)
report = Report.objects.get(user_id=user)
report.submitted = True
report.save()
review_request = factory.put('/api/v1/report/1/final')
force_authenticate(review_request, user=user)
response = report_detail(review_request, 1)
self.assertEqual(response.status_code, 409)

View file

@ -6,6 +6,7 @@ import os
from django.core.mail import EmailMultiAlternatives
from django.template.loader import render_to_string
from decouple import config
from django.utils import timezone
def get_report(report_pk):
"""
@ -125,7 +126,7 @@ def create_report(request):
report = Report.objects.create(
user_id=request.user,
title=request.data['title'],
date_created=datetime.date.today(),
date_created=timezone.now(),
reference_number=request.data['reference']
)
report.save()

Binary file not shown.