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 --- online_test/settings.py | 3 ++- requirements.txt | 1 + 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 +- 7 files changed, 35 insertions(+), 9 deletions(-) create mode 100644 yaksh/middleware/user_time_zone.py 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/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 d3976427988d08490e813918d800bb29f551f9dd Mon Sep 17 00:00:00 2001 From: adityacp Date: Sat, 7 May 2016 00:21:22 +0530 Subject: added pytz in setup.py --- setup.py | 1 + 1 file changed, 1 insertion(+) 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', -- 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 --- .travis.yml | 1 + yaksh/middleware/user_time_zone.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 8f48a88..bf77d5a 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 before_install: - sudo apt-get update -qq 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 --- .travis.yml | 2 +- yaksh/forms.py | 3 ++- yaksh/models.py | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index bf77d5a..38db811 100644 --- a/.travis.yml +++ b/.travis.yml @@ -11,7 +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 + - pip install -q pytz==2016.4 before_install: - sudo apt-get update -qq 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(-) 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