def wtf(mapping): glml_id = mapping['glml_id'] from glml.web.models import ANSWER_STUDENT_ID, ANSWER_STUDENT_NAME, District, GRADES, SchoolID, Year year = Year.get_current_year() try: district = District.objects.get(glml_id=glml_id[0], year=year) try: school = SchoolID.objects.get(glml_id=glml_id[1:3], district=district).school.name if school == ANSWER_STUDENT_NAME: assert False except SchoolID.DoesNotExist: school = u'?' district = district.glml_id if district == ANSWER_STUDENT_ID[0]: assert False except District.DoesNotExist: district = u'?' try: grade = dict(zip(GRADES.values(), GRADES.keys()))[int(glml_id[3])] except: grade = u'?' if grade == 11: grade = u'n %s' % grade else: grade = u' %s' % grade return u'%s, row %s (seems like a%sth grader at %s in district %s)' % (glml_id, mapping['row'], grade, school, district)
def year_context_processor(request): current_year = Year.get_current_year() working_year = get_working_year(request) year_selector_form = YearSelectorForm(initial={'selected_year': working_year.id}) context = {'year_selector_form': year_selector_form, 'current_year': current_year, 'working_year': working_year} return context
def clean_excel_file(self): errors = [] try: file_contents = self.cleaned_data['excel_file'].read() book = xlrd.open_workbook(file_contents=file_contents) sheet = book.sheet_by_index(0) glml_ids = sheet.col_values(0) if ANSWER_STUDENT_ID not in glml_ids: errors.append(u'The key (%s) was not found.' % ANSWER_STUDENT_ID) used_glml_ids = {} used_glml_ids_keys = set() missing_students = [] i = 0 while i < len(glml_ids): glml_id = unicode(glml_ids[i])[:6] if re.match(r'\d{6}', glml_id): try: student_id = StudentID.objects.get(glml_id=glml_id, school_id__district__year__id=Year.get_current_year().id) except StudentID.DoesNotExist: missing_students.append({u'glml_id': glml_id, u'row': i+1}) elif re.match(r'\d', glml_id): errors.append(u'The student ID %s on row %s is an invalid ID.' % (glml_id, i+1)) if glml_id: if glml_id in used_glml_ids: used_glml_ids[glml_id].append(unicode(i+1)) used_glml_ids_keys.add(glml_id) else: used_glml_ids[glml_id] = [unicode(i+1)] i += 1 used_glml_ids_keys = list(used_glml_ids_keys) for used_glml_id in used_glml_ids_keys: rows = used_glml_ids[used_glml_id] rows.insert(-1, u'and') if len(rows) == 3: rows = u' '.join(rows) else: rows = u', '.join(rows) rows = rows.replace(u'and,', u'and') errors.append(u'The student ID %s was found on rows %s.' % (used_glml_id, rows)) if missing_students: if len(missing_students) == 1: verb = u' was' noun = u'the student' else: verb = u's were' noun = u'them' errors.insert(0, u'The above student ID%s not found in the database.' % verb) errors.insert(1, u'Please add %(noun)s in the database or correct %(noun)s in the spreadsheet.' % {'noun': noun}) self.missing_students = missing_students except xlrd.XLRDError: errors = [u"That's an invalid spreadsheet. Please upload the correct file."] if errors: raise forms.ValidationError(errors) else: return sheet
def __init__(self, *args, **kwargs): super(UploadForm, self).__init__(*args, **kwargs) year = Year.get_current_year() self.years = (year.start, year.end) self.fields['date'].widget = SelectDateWidget(years=self.years)
def get_working_year(request): if 'working_year' not in request.session.keys(): return Year.get_current_year() return request.session['working_year']