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 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 }}
You are not allowed to attempt the Quiz before or after this duration
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 %}

You have not passed the prerequisite & hence you cannot take the quiz.

{% endif %} -

List of quizzes availbale for you

+

List of quizzes available for you

{% if not quizzes %}
No active quizzes for you
{% endif %} -- cgit From cf244b48e9bebcaf48f5491e0a492b7788fd3709 Mon Sep 17 00:00:00 2001 From: adityacp Date: Sat, 7 May 2016 20:01:38 +0530 Subject: changes in travis.yml --- yaksh/middleware/user_time_zone.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'yaksh') diff --git a/yaksh/middleware/user_time_zone.py b/yaksh/middleware/user_time_zone.py index f48bb62..3555d1f 100644 --- a/yaksh/middleware/user_time_zone.py +++ b/yaksh/middleware/user_time_zone.py @@ -1,7 +1,7 @@ import pytz from django.utils import timezone -from yaksh.models import Profile + class TimezoneMiddleware(object): """ Middleware to get user's timezone and activate timezone -- cgit From c1cd00f34341e06dce7610fa5da42d68b531bd92 Mon Sep 17 00:00:00 2001 From: adityacp Date: Thu, 12 May 2016 11:27:57 +0530 Subject: rebase changes and comments changes --- yaksh/forms.py | 3 ++- yaksh/models.py | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'yaksh') diff --git a/yaksh/forms.py b/yaksh/forms.py index 34b87d4..26e0967 100644 --- a/yaksh/forms.py +++ b/yaksh/forms.py @@ -74,7 +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]) + 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"] diff --git a/yaksh/models.py b/yaksh/models.py index 23fd1c9..dbe7892 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -148,6 +148,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]) ############################################################################### -- cgit From 7b762b4550f08b95c1ebf8196bd79c3f77a306ca Mon Sep 17 00:00:00 2001 From: adityacp Date: Tue, 17 May 2016 12:59:24 +0530 Subject: comment changes --- yaksh/middleware/user_time_zone.py | 2 +- yaksh/models.py | 7 +++---- 2 files changed, 4 insertions(+), 5 deletions(-) (limited to 'yaksh') diff --git a/yaksh/middleware/user_time_zone.py b/yaksh/middleware/user_time_zone.py index 3555d1f..f1aace9 100644 --- a/yaksh/middleware/user_time_zone.py +++ b/yaksh/middleware/user_time_zone.py @@ -9,7 +9,7 @@ class TimezoneMiddleware(object): def process_request(self, request): user = request.user if hasattr(user, 'profile'): - user_tz = user.profile.timezone + 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 dbe7892..6ee02e1 100644 --- a/yaksh/models.py +++ b/yaksh/models.py @@ -48,9 +48,6 @@ 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) @@ -302,7 +299,9 @@ class Quiz(models.Model): # 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, tzinfo=tz), + default=datetime(2199, 1, 1, + tzinfo=pytz.timezone\ + (timezone.get_current_timezone_name())), null=True) # This is always in minutes. -- cgit