from django import forms from otree.api import ( models, widgets ) from .models import Constants from random import randint from uuid import uuid4 counter_for_teachers = 500 days_in_month = [('','Day')] + [(x, x) for x in range(1, 32)] months_in_year = [('', 'Month')] + [(n+1, x) for n, x in enumerate( ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'])] subjects = [('', 'Discipline')] + [(x, x) for x in ["French", "History-Geography", "Mathematics", "Philosophy", "Sciences", "Economic and Social Sciences"]] years = [('', '-----')] + [(f'0{n+1}', a) for n, a in enumerate(['Second', 'First', 'Terminale'])] regions = [('', '----')] + [(r, r) for r in ["Créteil", "Montpellier", "Nantes"]] class StudConfirmIdentityForm(forms.Form): first_name = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'placeholder': 'First name', 'autocomplete': 'off'})) last_name = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'placeholder': 'Last name', 'autocomplete': 'off'})) dob_day = forms.ChoiceField(choices=days_in_month) dob_month = forms.ChoiceField(choices=months_in_year) region = forms.ChoiceField(choices=regions, required=False) school = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'autocomplete': 'off'}), required=False) classroom = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'autocomplete': 'off'}), required=False) not_in_db = forms.BooleanField(required=False) year = forms.ChoiceField(choices=years, required=False) def clean(self): data = self.cleaned_data dob = data['dob_day'].zfill(2) + '/' + data['dob_month'].zfill(2) v = [data['first_name'], data['last_name'], dob] found = next((x for x in Constants.students_db if v == [x['first_name'], x['last_name'], x['dob']]), None) print(f"[===DEBUG===] found {found} {v}") if found: data['uuid'] = found['uuid'] data['not_in_db'] = False elif not data.get('not_in_db'): data['not_in_db'] = True raise forms.ValidationError( "Some of the information provided are not correct." ) elif data.get('not_in_db') is True: school_ID = next((r['school_ID'] for r in Constants.students_db if r['school'] == data['school']), "") data['uuid'] = school_ID + data['year'] + data['classroom'] + data['dob_day'].zfill(2) + \ data['dob_month'].zfill(2) + data['last_name'][:3] print(f"[===DEBUG===] {data}") return data class TeachConfirmIdentityForm(forms.Form): first_name = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'placeholder': 'Prénom', 'autocomplete': 'off'})) last_name = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'placeholder': 'Nom', 'autocomplete': 'off'})) dob_day = forms.ChoiceField(choices=days_in_month) dob_month = forms.ChoiceField(choices=months_in_year) region = forms.ChoiceField(choices=regions, required=False) school = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'autocomplete': 'off'}), required=False) classroom = forms.CharField(widget=forms.TextInput( attrs={'class': 'typeahead', 'autocomplete': 'off'}), required=False) not_in_db = forms.BooleanField(required=False) subject = forms.ChoiceField(choices=subjects, required=False) def clean(self): data = self.cleaned_data dob = data['dob_day'].zfill(2) + '/' + data['dob_month'].zfill(2) v = [data['first_name'], data['last_name'], dob] found = next((x for x in Constants.teachers_db if v == [x['first_name'], x['last_name'], x['dob']]), None) if found: data['uuid'] = found['uuid'] data['not_in_db'] = False elif not data.get('not_in_db'): data['not_in_db'] = True raise forms.ValidationError( "Some of the information provided are not correct." ) elif data.get('not_in_db') is True: school_ID = next((r['school_ID'] for r in Constants.teachers_db if r['school'] == data['school']), None) counter_for_teachers =+ 1 if school_ID: data['uuid'] = school_ID + str(counter_for_teachers) else: data['uuid'] = uuid4() return data