diff options
Diffstat (limited to 'yaksh')
-rw-r--r-- | yaksh/forms.py | 5 | ||||
-rw-r--r-- | yaksh/middleware/user_time_zone.py | 15 | ||||
-rw-r--r-- | yaksh/models.py | 14 | ||||
-rw-r--r-- | yaksh/templates/yaksh/intro.html | 4 | ||||
-rw-r--r-- | yaksh/templates/yaksh/quizzes_user.html | 2 |
5 files changed, 32 insertions, 8 deletions
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 @@ </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 %} |