summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPrabhu Ramachandran2016-05-27 12:32:21 +0530
committerPrabhu Ramachandran2016-05-27 12:32:21 +0530
commit033d116bc803fe165cbe6f7e228c29d8f0b019a9 (patch)
tree6553297f4855810eb349916a4a5947a1f88c4fb2
parent5c74697b00ea08a2b78615637d8b322410fca4b0 (diff)
parent7b762b4550f08b95c1ebf8196bd79c3f77a306ca (diff)
downloadonline_test-033d116bc803fe165cbe6f7e228c29d8f0b019a9.tar.gz
online_test-033d116bc803fe165cbe6f7e228c29d8f0b019a9.tar.bz2
online_test-033d116bc803fe165cbe6f7e228c29d8f0b019a9.zip
Merge pull request #99 from adityacp/fix_timezone
Fix timezone
-rw-r--r--.travis.yml1
-rw-r--r--online_test/settings.py3
-rw-r--r--requirements.txt1
-rw-r--r--setup.py1
-rw-r--r--yaksh/forms.py6
-rw-r--r--yaksh/middleware/user_time_zone.py15
-rw-r--r--yaksh/models.py15
-rw-r--r--yaksh/templates/yaksh/intro.html4
-rw-r--r--yaksh/templates/yaksh/quizzes_user.html2
9 files changed, 39 insertions, 9 deletions
diff --git a/.travis.yml b/.travis.yml
index 8f48a88..38db811 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -11,6 +11,7 @@ env:
install:
- pip install git+https://github.com/FOSSEE/online_test.git#egg=yaksh-0.1
- pip install -q Django==$DJANGO --use-mirrors
+ - pip install -q pytz==2016.4
before_install:
- sudo apt-get update -qq
diff --git a/online_test/settings.py b/online_test/settings.py
index adc3dc3..7a89217 100644
--- a/online_test/settings.py
+++ b/online_test/settings.py
@@ -46,6 +46,7 @@ MIDDLEWARE_CLASSES = (
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'yaksh.middleware.one_session_per_user.OneSessionPerUserMiddleware',
+ 'yaksh.middleware.user_time_zone.TimezoneMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
)
@@ -77,7 +78,7 @@ USE_I18N = True
USE_L10N = True
-USE_TZ = False
+USE_TZ = True
# Static files (CSS, JavaScript, Images)
diff --git a/requirements.txt b/requirements.txt
index f717e09..2ace8a9 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -1,3 +1,4 @@
django==1.9.5,
mysql-python==1.2.5,
django-taggit==0.18.1,
+pytz==2016.4
diff --git a/setup.py b/setup.py
index 18d0d23..6bc0168 100644
--- a/setup.py
+++ b/setup.py
@@ -26,6 +26,7 @@ setup(
'django==1.9.5',
'mysql-python==1.2.5',
'django-taggit==0.18.1',
+ 'pytz==2016.4'
],
classifiers=[
'Development Status :: 4 - Beta',
diff --git a/yaksh/forms.py b/yaksh/forms.py
index 808262b..26e0967 100644
--- a/yaksh/forms.py
+++ b/yaksh/forms.py
@@ -11,6 +11,7 @@ from django.forms.models import inlineformset_factory
from django.db.models import Q
from string import letters, punctuation, digits
import datetime
+import pytz
languages = (
("select", "Select Language"),
@@ -73,6 +74,8 @@ class UserRegisterForm(forms.Form):
(max_length=64, help_text='Department you work/study at')
position = forms.CharField\
(max_length=64, help_text='Student/Faculty/Researcher/Industry/etc.')
+ timezone = forms.ChoiceField(choices=[(tz, tz) for tz in pytz.common_timezones],
+ initial=pytz.utc)
def clean_username(self):
u_name = self.cleaned_data["username"]
@@ -118,6 +121,7 @@ class UserRegisterForm(forms.Form):
new_profile.institute = cleaned_data["institute"]
new_profile.department = cleaned_data["department"]
new_profile.position = cleaned_data["position"]
+ new_profile.timezone = cleaned_data["timezone"]
new_profile.save()
return u_name, pwd
@@ -208,7 +212,7 @@ class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['first_name', 'last_name', 'institute',
- 'department', 'roll_number', 'position']
+ 'department', 'roll_number', 'position', 'timezone']
first_name = forms.CharField(max_length=30)
last_name = forms.CharField(max_length=30)
diff --git a/yaksh/middleware/user_time_zone.py b/yaksh/middleware/user_time_zone.py
new file mode 100644
index 0000000..f1aace9
--- /dev/null
+++ b/yaksh/middleware/user_time_zone.py
@@ -0,0 +1,15 @@
+import pytz
+
+from django.utils import timezone
+
+
+class TimezoneMiddleware(object):
+ """ Middleware to get user's timezone and activate timezone
+ if user timezone is not available default value 'UTC' is activated """
+ def process_request(self, request):
+ user = request.user
+ if hasattr(user, 'profile'):
+ user_tz = user.profile.timezone
+ timezone.activate(pytz.timezone(user_tz))
+ else:
+ timezone.activate(pytz.timezone('UTC'))
diff --git a/yaksh/models.py b/yaksh/models.py
index 32fb0d0..6ee02e1 100644
--- a/yaksh/models.py
+++ b/yaksh/models.py
@@ -8,7 +8,8 @@ from django.contrib.auth.models import User
from django.forms.models import model_to_dict
from django.contrib.contenttypes.models import ContentType
from taggit.managers import TaggableManager
-
+from django.utils import timezone
+import pytz
languages = (
("python", "Python"),
@@ -144,6 +145,8 @@ class Profile(models.Model):
institute = models.CharField(max_length=128)
department = models.CharField(max_length=64)
position = models.CharField(max_length=64)
+ timezone = models.CharField(max_length=64,
+ choices=[(tz, tz) for tz in pytz.common_timezones])
###############################################################################
@@ -291,12 +294,14 @@ class Quiz(models.Model):
# The start date of the quiz.
start_date_time = models.DateTimeField("Start Date and Time of the quiz",
- default=datetime.now(),
+ default=timezone.now(),
null=True)
# The end date and time of the quiz
end_date_time = models.DateTimeField("End Date and Time of the quiz",
- default=datetime(2199, 1, 1, 0, 0, 0, 0),
+ default=datetime(2199, 1, 1,
+ tzinfo=pytz.timezone\
+ (timezone.get_current_timezone_name())),
null=True)
# This is always in minutes.
@@ -331,7 +336,7 @@ class Quiz(models.Model):
def is_expired(self):
- return not self.start_date_time <= datetime.now() < self.end_date_time
+ return not self.start_date_time <= timezone.now() < self.end_date_time
def has_prerequisite(self):
return True if self.prerequisite else False
@@ -409,7 +414,7 @@ class QuestionPaper(models.Model):
last_attempt = AnswerPaper.objects.get_user_last_attempt(user=user,
questionpaper=self)
if last_attempt:
- time_lag = (datetime.today() - last_attempt.start_time).days
+ time_lag = (datetime.today() - last_attempt.start_time.replace(tzinfo=None)).days
return time_lag >= self.quiz.time_between_attempts
else:
return True
diff --git a/yaksh/templates/yaksh/intro.html b/yaksh/templates/yaksh/intro.html
index 1ed82e2..f05e9a1 100644
--- a/yaksh/templates/yaksh/intro.html
+++ b/yaksh/templates/yaksh/intro.html
@@ -12,7 +12,9 @@
</div>
{% else %}
<div class="alert">
- You can attempt this Quiz at any time between {{ questionpaper.quiz.start_date_time }} GMT and {{ questionpaper.quiz.end_date_time }} GMT
+ {% load tz %}
+ {% get_current_timezone as TIME_ZONE %}
+ You can attempt this Quiz at any time between {{ questionpaper.quiz.start_date_time }} {{ TIME_ZONE }} and {{ questionpaper.quiz.end_date_time }} {{ TIME_ZONE }}
<br/>
You are not allowed to attempt the Quiz before or after this duration
<br/>
diff --git a/yaksh/templates/yaksh/quizzes_user.html b/yaksh/templates/yaksh/quizzes_user.html
index 223952e..6403a21 100644
--- a/yaksh/templates/yaksh/quizzes_user.html
+++ b/yaksh/templates/yaksh/quizzes_user.html
@@ -42,7 +42,7 @@
{% if cannot_attempt %}
<p>You have not passed the prerequisite & hence you cannot take the quiz.</p>
{% endif %}
- <h4>List of quizzes availbale for you</h4>
+ <h4>List of quizzes available for you</h4>
{% if not quizzes %}
<h5>No active quizzes for you</h5>
{% endif %}