from otree.api import Currency as c, currency_range from ._builtin import Page, WaitPage from otree.models import Session from .models import Constants from .forms import StudConfirmIdentityForm, TeachConfirmIdentityForm from django.db import transaction from django.http import HttpResponseRedirect from django.shortcuts import render, redirect from django.views.generic import View, FormView, TemplateView from django.urls import reverse_lazy from time import time class WelcomeStudent(View): url_name = 'bienvenue' url_pattern = r'^bienvenue/$' # uncomment below for landing page before launch # template_name = 'intro/WelcomeStudentBeforeLaunch.html' template_name = 'intro/Welcome.html' def get(self, request, *args, **kwargs): return render(request, self.template_name) def post(self, request): self.request.session['welcome'] = True return HttpResponseRedirect('/consent/') class WelcomeTeachers(View): url_name = 'bienvenue/enseignant' url_pattern = r'^bienvenue/enseignant/$' template_name = 'intro/Welcome.html' def get(self, request, *args, **kwargs): return render(request, self.template_name) def post(self, request): self.request.session['welcome'] = True return HttpResponseRedirect('/enseignant/consent/') class Welcome(View): url_name = 'welcome' url_pattern = r'^welcome/$' template_name = 'intro/Welcome.html' def get(self, request, *args, **kwargs): return render(request, self.template_name) def post(self, request): self.request.session['welcome'] = True return HttpResponseRedirect('/consent/') class ConsentStudents(TemplateView): url_name = 'consent' url_pattern = r'^consent/$' template_name = 'intro/Consent.html' def get(self, request, *args, **kwargs): if self.request.session.get('welcome'): return render(request, self.template_name) else: return HttpResponseRedirect('../bienvenue/') def post(self, request): self.request.session['consent'] = True return HttpResponseRedirect('/login/') class ConsentTeachers(TemplateView): url_name = 'enseignant/consent' url_pattern = r'^enseignant/consent/$' template_name = 'intro/Consent.html' def get(self, request, *args, **kwargs): if self.request.session.get('welcome'): return render(request, self.template_name) else: return HttpResponseRedirect('../bienvenue/enseignant/') def post(self, request): self.request.session['consent'] = True return HttpResponseRedirect('/enseignant/login/') # TODO: based on new design, use oTree page class for login class ConfirmIdentityStudents(FormView): url_name = 'login' url_pattern = r'^login/$' form_class = StudConfirmIdentityForm template_name = 'intro/StudLogin.html' def get(self, request, *args, **kwargs): if self.request.session.get('consent'): return self.render_to_response(self.get_context_data()) elif self.request.session.get('welcome'): return HttpResponseRedirect('../consent/') else: return HttpResponseRedirect('../bienvenue/') def _filter_sessions(self): return [s for s in Session.objects.all() if s.config.get('teachers_session') is False \ and not s.is_demo and s.config.get('name') == 'survey'] def form_valid(self, form): data = form.cleaned_data uuid = data['uuid'] sessions = self._filter_sessions() # TODO: with new login design, setting the login page outside of oTree page class is not needed anymore # If using old design, start atomic transaction here # with transaction.atomic(): session = next((s for s in sessions if any(p for p in s.get_participants() if p.vars.get('uuid') is None)), None) if session: participant_without_uuid = next((p for p in session.get_participants() if p.vars.get('uuid') is None), None) if participant_without_uuid: if data['not_in_db']: participant_without_uuid.vars['data'] = data else: participant_without_uuid.vars['data'] = next(r for r in Constants.students_db if r['uuid'] == uuid) if participant_without_uuid.vars['data']['region'] == 'Créteil': participant_without_uuid.vars['is_creteil'] = True else: participant_without_uuid.vars['is_creteil'] = False participant_without_uuid.vars['uuid'] = uuid participant_without_uuid.save() participant_url = self.request.build_absolute_uri(participant_without_uuid._start_url()) else: participant_url = reverse_lazy(SurveyClose.url_name) self.success_url = participant_url else: self.success_url = reverse_lazy(SurveyClose.url_name) return super().form_valid(form) # # # TODO: based on new design, use oTree page class for login # class ConfirmIdentityTeachers(FormView): # # url_name = 'enseignant/login' # url_pattern = r'^enseignant/login/$' # form_class = TeachConfirmIdentityForm # template_name = 'intro/TeachLogin.html' # # def get(self, request, *args, **kwargs): # if self.request.session.get('consent'): # return self.render_to_response(self.get_context_data()) # elif self.request.session.get('welcome'): # return HttpResponseRedirect('../enseignant/consent/') # else: # return HttpResponseRedirect('../bienvenue/enseignant/') # # def form_valid(self, form): # data = form.cleaned_data # uuid = data['uuid'] # sessions = [s for s in Session.objects.all() if s.config.get('teachers_session') is True] # # # TODO: with new login design, setting the login page outside of oTree page class is not needed anymore # # If using old design, start atomic transaction here # # with transaction.atomic(): # session = next((s for s in sessions if any(p for p in s.get_participants() if p.vars.get('uuid') is None)),None) # if session: # participant_without_uuid = next((p for p in session.get_participants() if p.vars.get('uuid') is None), None) # if participant_without_uuid: # if data['not_in_db']: # participant_without_uuid.vars['data'] = data # else: # participant_without_uuid.vars['data'] = next(r for r in Constants.teachers_db if r['uuid'] == uuid) # if participant_without_uuid.vars['data']['region'] == 'Créteil': # participant_without_uuid.vars['is_creteil'] = True # else: # participant_without_uuid.vars['is_creteil'] = False # if data['wave'] == 2: # participant_without_uuid.vars['second_wave'] = True # participant_without_uuid.vars['uuid'] = uuid # participant_without_uuid.vars['is_teacher'] = True # participant_without_uuid.save() # participant_url = self.request.build_absolute_uri(participant_without_uuid._start_url()) # else: # participant_url = reverse_lazy(SurveyClose.url_name) # self.success_url = participant_url # else: # self.success_url = reverse_lazy(SurveyClose.url_name) # # return super().form_valid(form) class SurveyClose(TemplateView): url_name = 'survey/close' url_pattern = r'^survey/close/$' template_name = 'intro/SurveyClose.html' class Introduction(Page): template_name = 'intro/Introduction.html' def is_displayed(self): self.participant.vars['time_started'] = time() if not self.player.is_creteil: self.player.is_creteil = self.participant.vars.get('is_creteil') return True def vars_for_template(self): return {'is_teacher': self.participant.vars.get('is_teacher')} def before_next_page(self): data = self.participant.vars.get('data') if data: self.player.uuid = str(data['uuid']) self.player.not_in_db = data.get('not_in_db') self.player.region = data['region'] self.player.school = data['school'] self.player.school_ID = data.get('school_ID') self.player.classroom = data.get('classroom') self.player.subject = data.get('subject') if data.get('year'): self.player.year = int(data['year']) self.participant.vars['year'] = int(data['year']) if not self.participant.vars.get('is_teacher'): classmates = [(p['uuid'], p['first_name'] + ' ' + p['last_name'][0] + '.') for p in Constants.students_db if p['school'] == self.participant.vars['data']['school'] and p['classroom'] == self.participant.vars['data']['classroom'] and p['uuid'] != self.participant.vars.get('uuid') ] self.participant.vars['classmates'] = classmates self.participant.vars['is_mobile'] = self.request.user_agent.is_mobile self.participant.vars['is_tablet'] = self.request.user_agent.is_tablet self.participant.vars['browser'] = self.request.user_agent.browser.family self.player.is_mobile = self.participant.vars['is_mobile'] self.player.is_tablet = self.participant.vars['is_tablet'] self.player.browser = self.participant.vars['browser'] class Email(Page): form_model = 'player' form_fields = ['email'] def vars_for_template(self): return { 'is_creteil': self.participant.vars.get('is_creteil'), 'is_teacher': self.participant.vars.get('is_teacher') } page_sequence = [Email,]