From 88db9b2451693ab832a9d8c0abcad0a452b3500d Mon Sep 17 00:00:00 2001 From: adityacp Date: Fri, 6 May 2016 23:45:16 +0530 Subject: display datetime as per user timezone --- yaksh/forms.py | 5 ++++- yaksh/middleware/user_time_zone.py | 15 +++++++++++++++ yaksh/models.py | 14 +++++++++----- yaksh/templates/yaksh/intro.html | 4 +++- yaksh/templates/yaksh/quizzes_user.html | 2 +- 5 files changed, 32 insertions(+), 8 deletions(-) create mode 100644 yaksh/middleware/user_time_zone.py (limited to 'yaksh') diff --git a/yaksh/forms.py b/yaksh/forms.py index 808262b..34b87d4 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,7 @@ 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]) def clean_username(self): u_name = self.cleaned_data["username"] @@ -118,6 +120,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 +211,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..f48bb62 --- /dev/null +++ b/yaksh/middleware/user_time_zone.py @@ -0,0 +1,15 @@ +import pytz + +from django.utils import timezone +from yaksh.models import Profile + +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..23fd1c9 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"), @@ -47,6 +48,9 @@ test_status = ( ('completed', 'Completed'), ) +# get current timezone info +tz = pytz.timezone(timezone.get_current_timezone_name()) + def get_assignment_dir(instance, filename): return '%s/%s' % (instance.user.roll_number, instance.assignmentQuestion.id) @@ -291,12 +295,12 @@ 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=tz), null=True) # This is always in minutes. @@ -331,7 +335,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 +413,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 @@ {% else %}
You have not passed the prerequisite & hence you cannot take the quiz.
{% endif %} -