def post(self, request, *args, **kwargs): print('from class based view') context_dict = {} context_dict['user_type'] = 'school_admin' context_dict['school_name'] = request.session['school_name'] context_dict['header'] = 'Setup Activity Group' # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) school_id = request.session['school_id'] school = School.objects.get(id=school_id) # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print( 'now starting to process the uploaded file Activity Group members setup...' ) fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook( filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if sheet: print('Successfully got hold of sheet!') for row in range(sheet.nrows): if row == 0: print('checking for existence of group...') group_name = sheet.cell(row, 0).value print('group_name = %s' % group_name) try: activity_group = ActivityGroup.objects.get( school=school, group_name=group_name) print( 'activity group %s in school %s already exists' % (group_name, school.school_name)) except Exception as e: print('activity group %s does not exist at %s', (group_name, school.school_name)) print( 'exception 241117-A from activity_group views.py %s %s', (e.message, type(e))) error = 'Activity Group does not exist.' print(error) form.errors['__all__'] = form.error_class([error]) return render(request, 'classup/setup_data.html', context_dict) else: print('Processing a new row') erp_id = str(sheet.cell(row, 1).value) print(erp_id) decimal = '.' if decimal in erp_id: print( 'student id contains a decimal followed by zero. This has to be removed' ) erp_id = erp_id[:-2] print( 'decimal and following zero removed. Now student_id = %s' % erp_id) print( 'erp_id = %s. Will now try to add to Activity Group %s' % (erp_id, group_name)) try: student = Student.objects.get( school=school, student_erp_id=erp_id) try: entry, created = ActivityMembers.objects.get_or_create( group=activity_group, student=student) if entry: print( 'student %s %s is already a member of %s group' % (student.fist_name, student.last_name, group_name)) if created: print( 'made student %s %s a member of %s group' % (student.fist_name, student.last_name, group_name)) except Exception as e: print( 'exception 241117-C from activity_group views.py %s %s' % (e.message, type(e))) print( 'could not create an Activity Group entry for student with erp_id %s in %s' % (erp_id, activity_group)) except Student.DoesNotExist as e: print('no student is associated with erp_id %s' % erp_id) print( 'exception 241117-B from activity_group views.py %s %s' % (e.message, type(e))) # file upload and saving to db was successful. Hence go back to the main menu messages.success(request._request, 'Activity Group entries created.') return render(request, 'classup/setup_index.html', context_dict) except Exception as e: error = 'invalid excel file uploaded.' print(error) print('exception 171117-C from time_table views.py %s %s ' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) return render(request, 'classup/setup_data.html', context_dict)
def student_bus_rout(request): context_dict = { } context_dict['user_type'] = 'school_admin' # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) # now start processing the file upload context_dict['header'] = 'Upload Student Bus Rout Data' if request.method == 'POST': # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: # determine the school for which this processing is done school_id = request.session['school_id'] school = School.objects.get(id=school_id) print ('now starting to process the students bus rout mapping...') file_to_process_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(file_to_process_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it file_to_process = xlrd.open_workbook(filename=None, file_contents=file_to_process_handle.read()) sheet = file_to_process.sheet_by_index(0) if sheet: print ('Successfully got hold of sheet!') for row in range(sheet.nrows): # skip the header row if row == 0: continue print ('Processing a new row') # first, capture student data # we need to explicitly cast student id to string. Else update will not function properly student_id = str(sheet.cell(row, 0).value) decimal = '.' if decimal in student_id: print('student id contains a decimal followed by zero. This has to be removed') student_id = student_id[:-2] print('decimal and following zero removed. Now student_id = %s' % student_id) student_first_name = sheet.cell(row, 1).value student_last_name = sheet.cell(row, 2).value bus_rout = sheet.cell(row, 3).value print('bus_rout = %s' % bus_rout) stop_name = sheet.cell(row, 4).value print('bus stop = %s' % stop_name) # start assigning student to bus routs. Both bus_rout and student are foreign objects try: the_rout = Bus_Rout.objects.get(school=school, bus_root=bus_rout) except Exception as e: error = 'Unable to retrieve the Bus Rout: ' + bus_rout print ('Exception7 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) print (error) # todo - we should skip this student but report # this and move on to the next student <provide code> continue try: the_stop = BusStop.objects.get(stop_name=stop_name, bus_rout=the_rout) except Exception as e: error = 'Unable to retrieve the Bus stop: ' + stop_name print ('Exception8 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) print (error) # todo - we should skip this student but report this and move on to the next student <provide code> continue try: the_student = Student.objects.get(student_erp_id=student_id) except Exception as e: print ('Exception9 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) error = 'Unable to retrieve the student: ' + student_id + ' ' + \ student_first_name + ' ' + student_last_name form.errors['__all__'] = form.error_class([error]) print (error) # todo - we should skip this student but report this and move on to the next student <provide code> continue # process student first. If this is an existing student, this is an update operations. try: r = Student_Rout.objects.get(student=the_student) if r: print ('Student with ID: ' + student_id + ' & name: ' + student_first_name + ' already exist. This will be updated!') r.student = the_student r.bus_root = the_rout r.bus_stop = the_stop except Exception as e: print ('Exception = %s (%s)' % (e.message, type(e))) print ('Student with ID: ' + student_id + ' Name: ' + student_first_name + ' ' + student_last_name + ' is a new entry. Hence inserting...') try: r = Student_Rout(student=the_student, bus_root=the_rout, bus_stop=the_stop) r.save() print ('saving successful!') except Exception as e: error = 'Unable to create the new student-bus rout mapping in the database' print (error) print ('Exception10 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) # todo - we should skip this student but report this and move on to the next student <provide code> try: r.save() print ('updated Student with ID: ' + student_id + ' & name: ' + student_first_name) except Exception as e: print ('Exception11 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) error = 'Unable to save the bus rout mapping for Student with ID: ' + student_id + \ ' Name: ' + student_first_name + ' ' + student_last_name + ' in Table Student_Rout' form.errors['__all__'] = form.error_class([error]) print (error) # todo - we should skip this student but report this and move on to the next student <provide code> # file upload and saving to db was successful. Hence go back to the main menu messages.success(request, 'Bus Routs for ' + school.school_name + ' uploaded. Please login from device to verify') return render(request, 'classup/setup_index.html', context_dict) except Exception as e: print ('Exception12 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) error = 'Invalid file uploaded. Please try again.' form.errors['__all__'] = form.error_class([error]) print (error) return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def setup_routs(request): context_dict = { } context_dict['user_type'] = 'school_admin' # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) # now start processing the file upload context_dict['header'] = 'Upload Bus Routs' if request.method == 'POST': # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: # determine the school for which this processing is done school_id = request.session['school_id'] school = School.objects.get(id=school_id) print ('now starting to process the uploaded file for setting up Bus Routs...') fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook(filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if sheet: print ('Successfully got hold of sheet!') for row in range(sheet.nrows): # skip the header rows if row == 0: continue print ('Processing a new row') bus_rout = sheet.cell(row, 0).value print (bus_rout) # Now we are ready to insert into db. But, we need to be sure that we are not trying # to insert a duplicate try: c = Bus_Rout.objects.get(school=school, bus_root=bus_rout) if c: print ('Bus Rout ' + bus_rout + ' for school ' + school.school_name + ' already exist. Hence skipping...') except Exception as e: print ('Exception1 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) print ('Bus Rout ' + bus_rout + ' is a new rout. Hence inserting...') try: c = Bus_Rout(school=school, bus_root=bus_rout) c.save() except Exception as e: print ('Exception2 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) error = 'Unable to save the Bus Rout ' + bus_rout + ' for school ' \ + school.school_name + ' in table Bus_Rout' form.errors['__all__'] = form.error_class([error]) print (error) return render(request, 'classup/setup_data.html', context_dict) # file upload and saving to db was successful. Hence go back to the main menu messages.success(request, 'Bus Routs Uploaded. Please login from device to verify') return render(request, 'classup/setup_index.html', context_dict) except Exception as e: print ('Exception3 in bus_attendance views.py = %s (%s)' % (e.message, type(e))) error = 'Invalid file uploaded. Please try again.' form.errors['__all__'] = form.error_class([error]) print (error) return render(request, 'classup/setup_data.html', context_dict) else: # we are arriving at this page for the first time, hence show an empty form form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def setup_third_lang(request): context_dict = {} context_dict['user_type'] = 'school_admin' context_dict['school_name'] = request.session['school_name'] # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) # now start processing the file upload context_dict['header'] = 'Setup Third Language' if request.method == 'POST': school_id = request.session['school_id'] school = School.objects.get(id=school_id) # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print( 'now starting to process the uploaded file for Third Language mapping...' ) fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook( filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if sheet: print('Successfully got hold of sheet!') for row in range(sheet.nrows): if row == 0: continue print('Processing a new row') erp_id = sheet.cell(row, 1).value print(erp_id) try: student = Student.objects.get(school=school, student_erp_id=erp_id) print('now dealing with %s %s' % (student.fist_name, student.last_name)) except Exception as e: print('student with erp id %s does not exist' % erp_id) print('exception 231017-G from exam views.py %s %s ' % (e.message, type(e))) continue # 31/10/2017 - get the third language t_l = sheet.cell(row, 5).value print( 'third language specified for %s %s in the sheet is %s' % (student.fist_name, student.last_name, t_l)) try: third_lang = Subject.objects.get(school=school, subject_name=t_l) except Exception as e: print('Exception 311017-A from exam views.py %s %s' % (e.message, type(e))) print('%s is not a third languate!' % t_l) try: record = ThirdLang.objects.get(student=student) print( 'third language for %s %s is already set as %s. This will be updated' % (student.fist_name, student.last_name, record.third_lang.subject_name)) print('third language ' + student.fist_name + ' ' + student.last_name + ' already exists. This will be updated') try: record.third_lang = third_lang record.save() print( 'successfully updated third language for %s %s as %s' % (student.fist_name, student.last_name, t_l)) print( 'successfully updated the third language for ' + student.fist_name, ' ' + student.last_name) except Exception as e: print( 'failed to update the third language for ' + student.fist_name, ' ' + student.last_name) print( '(exception 311017-B from exam views.py %s %s) ' % (e.message, type(e))) except Exception as e: print( '(exception 311017-C from exam views.py %s %s) ' % (e.message, type(e))) print('third language for ' + student.fist_name + ' ' + student.last_name + ' is not in record. This will be created') try: new_record = ThirdLang(student=student, third_lang=third_lang) new_record.save() print(('created third language entry for %s %s)' % (student.fist_name, student.last_name))) except Exception as e: print( 'failed to create third language entry for %s %s)' % (student.fist_name, student.last_name)) print( '(exception 311017-D from exam views.py %s %s) ' % (e.message, type(e))) # file upload and saving to db was successful. Hence go back to the main menu messages.success(request, 'Third Language uploaded') return render(request, 'classup/setup_index.html', context_dict) except Exception as e: error = 'invalid excel file uploaded.' print(error) print('exception 311017-E from exam views.py %s %s ' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def post(self, request, *args, **kwargs): print('from class based view') context_dict = {} context_dict['user_type'] = 'school_admin' context_dict['school_name'] = request.session['school_name'] context_dict['header'] = 'Setup Additional Details' # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) school_id = request.session['school_id'] school = School.objects.get(id=school_id) # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print( 'now starting to process the uploaded file for additional details...' ) fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook( filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if sheet: print('Successfully got hold of sheet!') for row in range(sheet.nrows): # first two rows are header rows if row < 1: continue print('Processing a new row') try: erp_id = str(sheet.cell(row, 1).value) decimal = '.' if decimal in erp_id: print( 'student id contains a decimal followed by zero. This has to be removed' ) erp_id = erp_id[:-2] print( 'decimal and following zero removed. Now student_id = %s' % erp_id) mother_name = (sheet.cell(row, 4).value).title() address = sheet.cell(row, 5).value house = (sheet.cell(row, 6).value).title() except Exception as e: print('exception 19032018-E from erp views. %s %s' % (e.message, type(e))) try: student = Student.objects.get(school=school, student_erp_id=erp_id) student_name = '%s %s of class %s-%s' % ( student.fist_name, student.last_name, student.current_class.standard, student.current_section.section) print( 'setting the additional details for %s with erp_id %s' % (student_name, erp_id)) try: ad = AdditionalDetails.objects.get(student=student) print( 'additional details for %s with erp_id %s are already set. Those will be updated' % (erp_id, student_name)) ad.mother_name = mother_name ad.address = address ad.save() print( 'successfully updated the additional details for %s' % student_name) except Exception as e: print( 'exception 19032018-A from erp views.py %s %s' % (e.message, type(e))) print( 'additional details for %s with erp_id %s were not set. Setting now...' % (student_name, erp_id)) try: ad = AdditionalDetails(student=student, mother_name=mother_name, address=address) ad.save() print( 'successfully created additional details for %s with erp_id %s' % (student_name, erp_id)) except Exception as e: print( 'exception 30052019-A from erp views.py %s %s' % (e.message, type(e))) print( 'failed to set up additional details for %s' % student) print( 'now setting the house details for %s with erp_id %s' % (student_name, erp_id)) try: h = House.objects.get(student=student) print( 'house details for %s with erp_id %s are already set. Those will be updated' % (erp_id, student_name)) h.house = house h.save() print( 'successfully updated the house details for %s' % student_name) except Exception as e: print( 'exception 28032018-A from erp views.py %s %s' % (e.message, type(e))) print( 'house details for %s with erp_id %s were not set. Setting now...' % (student_name, erp_id)) h = House(student=student, house=house) h.save() print( 'successfully set house %s for student %s with erp_id %s' % (house, student_name, erp_id)) except Exception as e: print('exception 19032018-B from erp views.py %s %s' % (e.message, type(e))) print('no student associated with erp_id %s' % erp_id) row += 1 # file upload and saving to db was successful. Hence go back to the main menu messages.success(request._request, 'Additional Details Successfully uploaded') return render(request, 'classup/setup_index.html', context_dict) except Exception as e: error = 'invalid excel file uploaded.' print(error) print('exception 19032018-D from erp views.py %s %s ' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) return render(request, 'classup/setup_data.html', context_dict)
def setup_scheme(request): context_dict = { 'user_type': 'school_admin', 'school_name': request.session['school_name'] } # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) # now start processing the file upload context_dict['header'] = 'Setup Scheme' if request.method == 'POST': school_id = request.session['school_id'] school = School.objects.get(id=school_id) # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print( 'now starting to process the uploaded file for scheme setup...' ) fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook( filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if sheet: print('Successfully got hold of sheet!') for row in range(sheet.nrows): if row == 0: continue print('Processing a new row') standard = sheet.cell(row, 0).value print(standard) try: the_class = Class.objects.get(school=school, standard=standard) print('now dealing with class %s of %s' % (standard, school.school_name)) except Exception as e: print('exception 011117-A from exam views.py %s %s ' % (e.message, type(e))) continue for col in range(1, 11): sub = sheet.cell(row, col).value print(sub) if sub == 'N/A': continue try: subject = Subject.objects.get(school=school, subject_name=sub) except Exception as e: print( 'exception 011117-B from exam views.py %s %s ' % (e.message, type(e))) continue sequence = col try: scheme = Scheme.objects.get(school=school, the_class=the_class, subject=subject) print( 'subject %s in the scheme of class %s of school %s already exist. ' 'This will be updated' % (sub, standard, school.school_name)) scheme.sequence = sequence scheme.subject = subject try: scheme.save() print( 'successfully updated the scheme of subject %s of class %s of school %s' % (sub, standard, school.school_name)) except Exception as e: print( 'failed to updated the scheme of subject %s of class %s of school %s' % (sub, standard, school.school_name)) print( 'exception 021117-B exam views.py %s %s ' % (e.message, type(e))) except Exception as e: print( 'scheme of subject %s of class %s of school %s is not set yet. This will be created' % (sub, standard, school.school_name)) print( 'exception 201117-C from exam views.py %s %s' % (e.message, type(e))) try: scheme = Scheme(school=school, the_class=the_class, sequence=sequence, subject=subject) scheme.save() print( 'successfully created the scheme of subject %s of class %s of school %s' % (sub, standard, school.school_name)) except Exception as e: print( 'failed in creating the scheme of subject %s of class %s of school %s' % (sub, standard, school.school_name)) print( 'exception 021117-D from exam views.py %s %s' % (e.message, type(e))) # file upload and saving to db was successful. Hence go back to the main menu messages.success(request, 'Scheme successfully uploaded') return render(request, 'classup/setup_index.html', context_dict) except Exception as e: error = 'invalid excel file uploaded.' print(error) print('exception 021117-E from exam views.py %s %s ' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def setup_higher_class_subject_mapping(request): context_dict = { 'user_type': 'school_admin', 'school_name': request.session['school_name'] } maths_stream = ['English', 'Mathematics', 'Physics', 'Chemistry'] biology_stream = ['English', 'Biology', 'Physics', 'Chemistry'] commerce_stream = [ 'English', 'Economics', 'Accountancy', 'Business Studies' ] humanities_stream = ['English', 'Socialogy', 'History', 'Economics'] maths = 'Mathematics' bio = 'Biology' commerce = 'Commerce' humanities = 'Humanities' # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) context_dict['header'] = 'Subject Mapping for Higher Classes' if request.method == 'POST': school_id = request.session['school_id'] school = School.objects.get(id=school_id) print(school) # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print( 'now starting to process the uploaded file for Higher classes subject mapping...' ) fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook( filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if sheet: print('Successfully got hold of sheet! the chako') for row in range(sheet.nrows): # get the subject name if row != 0: erp = str(sheet.cell(row, 1).value) print(erp) try: student = Student.objects.get(school=school, student_erp_id=erp) student_name = '%s %s' % (student.fist_name, student.last_name) stream = (sheet.cell(row, 5).value).title() print('stream chosen by %s is %s' % (student_name, stream)) if stream == maths: print( 'going to set the chosen_stream to be %s' % stream) chosen_stream = list(maths_stream) print('chosen_stream = Mathematics') try: m_stream = Stream.objects.get( stream='Mathematics') stream_mapping = StreamMapping.objects.get_or_create( student=student, stream=m_stream) print('set Mathematics for %s' % student) print(stream_mapping) except Exception as e: print( 'failed to create %s stream mapping for %s' % (stream, student)) print( 'exception 08062019-A from exam views.py %s %s' % (e.message, type(e))) if stream == bio: print( 'going to set the chosen_stream to be %s' % stream) chosen_stream = list(biology_stream) print('chosen_stream = Biology') try: b_stream = Stream.objects.get( stream='Biology') stream_mapping = StreamMapping.objects.get_or_create( student=student, stream=b_stream) print('set biology for %s' % student) print(stream_mapping) except Exception as e: print( 'failed to create %s stream mapping for %s' % (stream, student)) print( 'exception 08062019-B from exam views.py %s %s' % (e.message, type(e))) if stream == commerce: print( 'going to set the chosen_stream to be %s' % stream) chosen_stream = list(commerce_stream) print('chosen_stream = Commerce') try: c_stream = Stream.objects.get( stream='Commerce') stream_mapping = StreamMapping.objects.get_or_create( student=student, stream=c_stream) print('set Commerce for %s' % student) print(stream_mapping) except Exception as e: print( 'failed to create %s stream mapping for %s' % (stream, student)) print( 'exception 08062019-C from exam views.py %s %s' % (e.message, type(e))) if stream == humanities: print( 'going to set the chosen_stream to be %s' % stream) chosen_stream = list(humanities_stream) print('chosen_stream = Humanities') try: h_stream = Stream.objects.get( stream='Humanities') stream_mapping = StreamMapping.objects.get_or_create( student=student, stream=h_stream) print('set Humanities for %s' % student) print(stream_mapping) except Exception as e: print( 'failed to create %s stream mapping for %s' % (stream, student)) print( 'exception 08062019-D from exam views.py %s %s' % (e.message, type(e))) elective = (sheet.cell(row, 6).value).title() print('elective chosen by %s is %s' % (student_name, elective)) try: chosen_stream.append(str(elective)) print( 'complete list of subjects to be mapped for %s' % student_name) print(chosen_stream) except Exception as e: print( 'failed to add elective %s to chosen stream' % elective) print( 'exception 28122018-A from exam views.py %s %s' % (e.message, type(e))) print('setting stream %s & elective %s for %s' % (stream, elective, student_name)) for sub in chosen_stream: try: subject = Subject.objects.get( school=school, subject_name=sub) print( 'retrieved the subject object for %s' % sub) mapping = HigherClassMapping.objects.get( student=student, subject=subject) print( 'subject %s mapping for %s already exist. Not doing again.' % (sub, student_name)) except Exception as e: print( 'exception 141117-C from exam views.py %s %s' % (e.message, type(e))) print( 'subject %s mapping for %s does not exist. Hence creating...' % (sub, student_name)) try: mapping = HigherClassMapping( student=student, subject=subject) mapping.save() print( 'created %s subject mapping for % s' % (sub, student_name)) except Exception as e: print( 'exception 141117-D from exam views.py %s %s' % (e.message, type(e))) print( 'failed to create %s subject mapping for %s' % (sub, student_name)) except Exception as e: print('failed to create subject mapping') print( 'exception 141117-A from exam views.py %s %s' % (e.message, type(e))) except Exception as e: error = 'invalid excel file uploaded.' print(error) print('exception 141117-B from exam views.py %s %s' % (e.message, type(e))) form.errors['__all__'] = form.error_class([error]) return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def send_health_record(request): context_dict = { } context_dict['user_type'] = 'school_admin' context_dict['school_name'] = request.session['school_name'] # first see whether the cancel button was pressed if "cancel" in request.POST: return render(request, 'classup/setup_index.html', context_dict) # now start processing the file upload context_dict['header'] = 'Upload Health Data' if request.method == 'POST': # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: # determine the school for which this processing is done school_id = request.session['school_id'] school = School.objects.get(id=school_id) sender = request.session['user'] print('school=' + school.school_name) print ('now starting to process the uploaded file for sending Health data...') fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook(filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if sheet: print('Successfully got hold of sheet!') for row in range(sheet.nrows): # skip the header rows if row == 0: continue print('Processing a new row') erp_id = sheet.cell(row, 1).value try: student = Student.objects.get(school=school, student_erp_id=erp_id) print ('dealing with % s %s' % (student.fist_name, student.last_name)) parent_name = student.parent.parent_name mobile = student.parent.parent_mobile1 weight = sheet.cell(row, 3).value if weight == '': weight = 'N/A' message = 'Dear %s, weight of your ward %s during last health checkup is %s KG' % \ (parent_name, student.fist_name, weight) message_type = 'Health Data Communication (Web)' print(message) sms.send_sms1(school, sender, mobile, message, message_type) except Exception as e: print ('failed to send health data for %s' % erp_id) print ('Exception 041117-A from parents views.py %s %s ' % (e.message, type(e))) messages.success(request, 'health data sent to parents') except Exception as e: print ('invalid excel file uploaded. Please fix and upload again') print ('Exception 041117-B from parents views.py %s %s ' % (e.message, type(e))) else: # we are arriving at this page for the first time, hence show an empty form form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def setup_subjects(request): # first see whether the cancel button was pressed if "cancel" in request.POST: return HttpResponseRedirect(reverse('setup_index')) # now start processing the file upload context_dict = { } context_dict['header'] = 'Upload Subjects Data' if request.method == 'POST': # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print 'now starting to process the uploaded file for setting up Subjects...' fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook(filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if (sheet): print 'Successfully got hold of sheet!' for row in range(sheet.nrows): # skip the header row if row == 0: continue print 'Processing a new row' # we need to explicitly cast employee id to string. Else update will not function properly sub_name = str(sheet.cell(row, 0).value) sub_code = sheet.cell(row, 1).value # if this is an existing employee/teacher, this is an update operations. try: s = Subject.objects.get(subject_code=sub_code) if s: print 'Subject: ' + sub_name + \ ' already exist. This will be updated!' s.subject_name = sub_name s.subject_code = sub_code except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) print 'Subject ' + 'is a new entry. Hence inserting...' s = Subject(subject_name=sub_name, subject_code=sub_code) try: s.save() except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Unable to save the data for ' + sub_name + ' ' + sub_code + ' in Table Subject' form.errors['__all__'] = form.error_class([error]) print error # file upload and saving to db was successful. Hence go back to the main menu return HttpResponseRedirect(reverse('setup_index')) except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Invalid file uploaded. Please try again.' form.errors['__all__'] = form.error_class([error]) print error return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def setup_students(request): # first see whether the cancel button was pressed if "cancel" in request.POST: return HttpResponseRedirect(reverse('setup_index')) # now start processing the file upload context_dict = { } context_dict['header'] = 'Upload Student Data' if request.method == 'POST': # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print 'now starting to process the uploaded file for setting up Student data...' fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook(filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if (sheet): print 'Successfully got hold of sheet!' for row in range(sheet.nrows): # skip the header row if (row == 0): continue print 'Processing a new row' # first, capture student data # we need to explicitly cast student id to string. Else update will not function properly student_id = str(sheet.cell(row, 0).value) student_first_name = sheet.cell(row, 1).value student_last_name = sheet.cell(row, 2).value current_class = sheet.cell(row, 3).value current_section = sheet.cell(row, 4).value current_roll_no = sheet.cell(row, 5).value # now, capture the parent data parent_name = sheet.cell(row, 6).value parent_email = sheet.cell(row, 7).value # we need to explicitly case mobile number to string. Else update will not function properly parent_mobile1 = str(sheet.cell(row, 8).value) parent_mobile2 = str(sheet.cell(row, 9).value) # class and section are foreign keys. Get hold of the relevant objects print 'Class = ' + current_class the_class = None the_section = None try: the_class = Class.objects.get(standard=current_class) except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Unable to retrieve the relevant class: ' + current_class form.errors['__all__'] = form.error_class([error]) print error # todo - we should skip this student but report this and move on to the next student <provide code> continue try: the_section = Section.objects.get(section=current_section) except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Unable to retrieve the relevant object for section: ' + current_section form.errors['__all__'] = form.error_class([error]) print error # todo - we should skip this student but report this and move on to the next student <provide code> continue # process student first. If this is an existing student, this is an update operations. try: s = Student.objects.get(student_erp_id=student_id) if (s): print 'Student with ID: ' + student_id + \ ' & name: ' + student_first_name + ' already exist. This will be updated!' s.student_erp_id = student_id s.first_name = student_first_name s.last_name = student_last_name s.current_class = the_class s.current_section = the_section s.roll_no = current_roll_no except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) print 'Student with ID: ' + student_id + ' Name: ' \ + student_first_name + ' ' + student_last_name + ' is a new entry. Hence inserting...' try: s = Student(student_erp_id=student_id, fist_name=student_first_name, last_name=student_last_name, current_class=the_class, current_section=the_section, roll_number=current_roll_no) s.save() print 'saving successful!' except Exception as e: error = 'Unable to save the new student to the database' print error print 'Exception = %s (%s)' % (e.message, type(e)) form.errors['__all__'] = form.error_class([error]) # todo - we should skip this student but report this and move on to the next student <provide code> try: s.save() except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Unable to save the data for Student with ID: ' + student_id + \ ' Name: ' + student_first_name + ' ' + student_last_name + ' in Table Student' form.errors['__all__'] = form.error_class([error]) print error # todo - we should skip this student but report this and move on to the next student <provide code> # now, process the parent for the student created/updated above try: p = Parent.objects.get(parent_mobile1=parent_mobile1) if (p): print 'Parent for Student with ID: ' + student_id + \ ' & name: ' + student_first_name + ' already exist. This will be updated!' p.student_id = s p.parent_name = parent_name p.parent_mobile1 = parent_mobile1 p.parent_mobile2 = parent_mobile2 p.parent_email = parent_email except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) print 'Student with ID: ' + student_id + ' Name: ' \ + student_first_name + ' ' + student_last_name + ' is a new entry. Hence inserting...' p = Parent(student_id=s, parent_name=parent_name, parent_mobile1=parent_mobile1, parent_mobile2=parent_mobile2, parent_email=parent_email) try: p.save() except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Unable to save the parent data for Student with ID: ' + student_id + \ ' Name: ' + student_first_name + ' ' + student_last_name + ' in Table Parent' form.errors['__all__'] = form.error_class([error]) print error # file upload and saving to db was successful. Hence go back to the main menu return HttpResponseRedirect(reverse('setup_index')) except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Invalid file uploaded. Please try again.' form.errors['__all__'] = form.error_class([error]) print error return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def setup_teachers(request): # first see whether the cancel button was pressed if "cancel" in request.POST: return HttpResponseRedirect(reverse('setup_index')) # now start processing the file upload context_dict = { } context_dict['header'] = 'Upload Teachers Data' if request.method == 'POST': # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print 'now starting to process the uploaded file for setting up Teachers...' fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook(filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if (sheet): print 'Successfully got hold of sheet!' for row in range(sheet.nrows): # skip the header row if (row == 0): continue print 'Processing a new row' # we need to explicitly cast employee id to string. Else update will not function properly employee_id = str(sheet.cell(row, 0).value) f_name = sheet.cell(row, 1).value l_name = sheet.cell(row, 2).value email = sheet.cell(row, 3).value # we need to explicitly case mobile number to string. Else update will not function properly mobile = str(sheet.cell(row, 4).value) # if this is an existing employee/teacher, this is an update operations. try: t = Teacher.objects.get(teacher_erp_id=employee_id) if (t): print 'Teacher with Employee ID: ' + employee_id + \ ' & name: ' + f_name + ' already exist. This will be updated!' t.first_name = f_name t.last_name = l_name t.email = email t.mobile = mobile except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) print 'Teacher ' + f_name + ' ' + l_name + ' is a new entry. Hence inserting...' t = Teacher(teacher_erp_id=employee_id, first_name=f_name, last_name=l_name, email=email, mobile=mobile) try: t.save() print 'Successfully created Teacher ' + f_name + ' ' + l_name # now, create a user for this teacher # the user name would be the email, and password would be a random string password = User.objects.make_random_password(length=5) print 'Initial password = '******'Successfully created user for ' + f_name + ' ' + l_name except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) print 'Unable to create user for ' + f_name + ' ' + l_name # todo implement the code to send password to the user through an sms and email # make this user part of the Teachers group try: group = Group.objects.get(name='teachers') user.groups.add(group) print 'Successfully added ' + f_name + ' ' + l_name + ' to the Teachers group' except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) print 'Unable to add ' + f_name + ' ' + l_name + ' to the Teachers group' except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Unable to save the data for ' + f_name + ' ' + l_name + ' in Table Teacher' form.errors['__all__'] = form.error_class([error]) print error # todo - we should skip this teacher but report this and move on to the next treacher <provide code> continue # file upload and saving to db was successful. Hence go back to the main menu return HttpResponseRedirect(reverse('setup_index')) except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Invalid file uploaded. Please try again.' form.errors['__all__'] = form.error_class([error]) print error return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)
def setup_sections(request): # first see whether the cancel button was pressed if "cancel" in request.POST: return HttpResponseRedirect(reverse('setup_index')) # now start processing the file upload context_dict = { } context_dict['header'] = 'Upload Section Data' if request.method == 'POST': # get the file uploaded by the user form = ExcelFileUploadForm(request.POST, request.FILES) context_dict['form'] = form if form.is_valid(): try: print 'now starting to process the uploaded file for setting up Sections...' fileToProcess_handle = request.FILES['excelFile'] # check that the file uploaded should be a valid excel # file with .xls or .xlsx if not validate_excel_extension(fileToProcess_handle, form, context_dict): return render(request, 'classup/setup_data.html', context_dict) # if this is a valid excel file - start processing it fileToProcess = xlrd.open_workbook(filename=None, file_contents=fileToProcess_handle.read()) sheet = fileToProcess.sheet_by_index(0) if (sheet): print 'Successfully got hold of sheet!' for row in range(sheet.nrows): if (row == 0): continue print 'Processing a new row' section = sheet.cell(row, 0).value print section # Now we are ready to insert into db. But, we need to be sure that we are not trying # to insert a duplicate try: s = Section.objects.get(section=section) if (s): print 'Section ' + section + ' already exist. Hence skipping...' except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) print 'Section ' + section + ' is a new section. Hence inserting...' try: s = Section(section=section) s.save() except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Unable to save the data in Table Section' form.errors['__all__'] = form.error_class([error]) print error return render(request, 'classup/setup_data.html', context_dict) # file upload and saving to db was successful. Hence go back to the main menu return HttpResponseRedirect(reverse('setup_index')) except Exception as e: print 'Exception = %s (%s)' % (e.message, type(e)) error = 'Invalid file uploaded. Please try again.' form.errors['__all__'] = form.error_class([error]) print error return render(request, 'classup/setup_data.html', context_dict) else: form = ExcelFileUploadForm() context_dict['form'] = form return render(request, 'classup/setup_data.html', context_dict)