Commit 6e0fb24d by sigberto

Display correct timezone on timestamp for question submission

The view for submission history used to grab the timezone from the database (UTC) and
appeded the TIME_ZONE variable after (could be any time zone). Adjusted displayed
timedate to the TIME_ZONE variable. Added unittest checks the localized datetime
object used by submission history for the expected hour difference.
parent 632c38f6
...@@ -269,3 +269,4 @@ Florian Haas <florian@hastexo.com> ...@@ -269,3 +269,4 @@ Florian Haas <florian@hastexo.com>
Leonardo Quiñonez <leonardo.quinonez@edunext.co> Leonardo Quiñonez <leonardo.quinonez@edunext.co>
Dmitry Viskov <dmitry.viskov@webenterprise.ru> Dmitry Viskov <dmitry.viskov@webenterprise.ru>
Brian Jacobel <bjacobel@edx.org> Brian Jacobel <bjacobel@edx.org>
Sigberto Alarcon <salarcon@stanford.edu>
...@@ -11,6 +11,7 @@ import unittest ...@@ -11,6 +11,7 @@ import unittest
from datetime import datetime, timedelta from datetime import datetime, timedelta
from HTMLParser import HTMLParser from HTMLParser import HTMLParser
from nose.plugins.attrib import attr from nose.plugins.attrib import attr
from freezegun import freeze_time
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import AnonymousUser from django.contrib.auth.models import AnonymousUser
...@@ -18,6 +19,7 @@ from django.core.urlresolvers import reverse ...@@ -18,6 +19,7 @@ from django.core.urlresolvers import reverse
from django.http import Http404, HttpResponseBadRequest from django.http import Http404, HttpResponseBadRequest
from django.test import TestCase from django.test import TestCase
from django.test.client import RequestFactory from django.test.client import RequestFactory
from django.test.client import Client
from django.test.utils import override_settings from django.test.utils import override_settings
from mock import MagicMock, patch, create_autospec, Mock from mock import MagicMock, patch, create_autospec, Mock
from opaque_keys.edx.locations import Location, SlashSeparatedCourseKey from opaque_keys.edx.locations import Location, SlashSeparatedCourseKey
...@@ -614,6 +616,39 @@ class ViewsTestCase(ModuleStoreTestCase): ...@@ -614,6 +616,39 @@ class ViewsTestCase(ModuleStoreTestCase):
self.assertIn("Score: 3.0 / 3.0", response_content) self.assertIn("Score: 3.0 / 3.0", response_content)
self.assertIn('#4', response_content) self.assertIn('#4', response_content)
@ddt.data(('America/New_York', -5), # UTC - 5
('Asia/Pyongyang', 9), # UTC + 9
('Europe/London', 0), # UTC
('Canada/Yukon', -8), # UTC - 8
('Europe/Moscow', 4)) # UTC + 3 + 1 for daylight savings
@ddt.unpack
@freeze_time('2012-01-01')
def test_submission_history_timezone(self, timezone, hour_diff):
with (override_settings(TIME_ZONE=timezone)):
course = CourseFactory.create()
course_key = course.id
client = Client()
admin = AdminFactory.create()
client.login(username=admin.username, password='test')
state_client = DjangoXBlockUserStateClient(admin)
usage_key = course_key.make_usage_key('problem', 'test-history')
state_client.set(
username=admin.username,
block_key=usage_key,
state={'field_a': 'x', 'field_b': 'y'}
)
url = reverse('submission_history', kwargs={
'course_id': unicode(course_key),
'student_username': admin.username,
'location': unicode(usage_key),
})
response = client.get(url)
response_content = HTMLParser().unescape(response.content)
expected_time = datetime.now() + timedelta(hours=hour_diff)
expected_tz = expected_time.strftime('%Z')
self.assertIn(expected_tz, response_content)
self.assertIn(str(expected_time), response_content)
def _email_opt_in_checkbox(self, response, org_name_string=None): def _email_opt_in_checkbox(self, response, org_name_string=None):
"""Check if the email opt-in checkbox appears in the response content.""" """Check if the email opt-in checkbox appears in the response content."""
checkbox_html = '<input id="email-opt-in" type="checkbox" name="opt-in" class="email-opt-in" value="true" checked>' checkbox_html = '<input id="email-opt-in" type="checkbox" name="opt-in" class="email-opt-in" value="true" checked>'
......
<%page expression_filter="h"/> <%page expression_filter="h"/>
<% import json %> <% import json, pytz %>
<h3>${username} > ${course_id} > ${location}</h3> <h3>${username} > ${course_id} > ${location}</h3>
% for i, (entry, score) in enumerate(zip(history_entries, scores)): % for i, (entry, score) in enumerate(zip(history_entries, scores)):
<hr/> <hr/>
<div> <div>
<b>#${len(history_entries) - i}</b>: ${entry.updated} UTC</br> <% timedate = entry.updated.astimezone(pytz.timezone(settings.TIME_ZONE))%>
<% timedate_str = timedate.strftime('%Y-%m-%d %H:%M:%S %Z') %>
<b>#${len(history_entries) - i}</b>: ${timedate_str}</br>
Score: ${score.grade} / ${score.max_grade} Score: ${score.grade} / ${score.max_grade}
<pre> <pre>
${json.dumps(entry.state, indent=2, sort_keys=True)} ${json.dumps(entry.state, indent=2, sort_keys=True)}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment