def retrieve_student_subjects(request): subject_list = { } response_array = [] if request.method == 'GET': student = request.GET.get('student') print (student) try: s = Student.objects.get(id=student) c = s.current_class sect = s.current_section test_list = ClassTest.objects.filter(the_class=c, section=sect, is_completed=True) for test in test_list: subject = test.subject.subject_name subject_list['subject'] = subject d = dict(subject_list) response_array.append(d) try: parent_mobile = s.parent.parent_mobile1 action = 'Retrieved Subject List for ' + s.fist_name + ' ' + s.last_name log_entry(parent_mobile, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 506 from parents views.py %s %s' % (e.message, type(e))) return JSONResponse(response_array, status=200) except Exception as e: print ('unable to retrieve list of subjects for ' + s.fist_name + ' ' + s.last_name) print ('Exception11 from parents views.py = %s (%s)' % (e.message, type(e))) return JSONResponse(response_array, status=201)
def set_subjects(request, teacher): print ('request.body(set_subjects)=') if request.method == 'POST': t = Teacher.objects.get(email=teacher) print (t) print ('request.body(set_subjects)=') print (request.body) school = t.school data = json.loads(request.body) print ('data(set_subjects)=') print (data) for key in data: print('key(set_subjects)=' + key) subject = data[key] print('subject(set_subjects)=' + subject) print ('now trying to extract subject(set_subjects)') try: s = Subject.objects.get(school=school, subject_code=subject) except Exception as e: print('unable to retrieve subject(set_subjects)') print ('Exception1 from teacher views.py = %s (%s)' % (e.message, type(e))) print ('now trying to set teacher subject') try: ts = TeacherSubjects.objects.get(teacher=t, subject=s) if ts: print('subject ' + s.subject_name + ' has already been selected by teacher ' + t.first_name + ' ' + t.last_name) pass except Exception as e: print('now setting subject ' + s.subject_name + ' for teacher ' + t.first_name + ' ' + t.last_name) ts = TeacherSubjects(teacher=t, subject=s) try: ts.save() print('successfully set subject ' + s.subject_name + ' for teacher ' + t.first_name + ' ' + t.last_name) except Exception as e: print('unable to set subject ' + s.subject_name + ' for teacher ' + t.first_name + ' ' + t.last_name) print ('Exception 2 from teacher views.py = %s (%s)' % (e.message, type(e))) try: action = 'Subjects Set' log_entry(teacher, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 500 from teachers views.py %s %s' % (e.message, type(e))) return HttpResponse('OK')
def delete_attendance2(request, school_id, the_class, section, subject, d, m, y): response_data = {} if request.method == 'POST': school = School.objects.get(id=school_id) # all of the above except date are foreign key in Attendance model. Hence we need to get the actual object c = Class.objects.get(school=school, standard=the_class) s = Section.objects.get(school=school, section=section) sub = Subject.objects.get(school=school, subject_name=subject) the_date = date(int(y), int(m), int(d)) print(the_date) data = json.loads(request.body) print('correction list=') print(data) for key in data: student_id = data[key] student = Student.objects.get(id=student_id) # check to see if absence for this student for this date, class, section and subject has already been marked try: q = Attendance.objects.filter(date=the_date, the_class=c, section=s, subject=sub, student=student) print(q.count()) # make an entry to database only it is a fresh entry if q.count() > 0: try: q1 = q[:1].get() teacher = q1.taken_by.email q.delete() action = 'Deleted a previously taken attendance for ' +\ student.fist_name + ' ' + student.last_name log_entry(teacher, action, "Normal", True) except Exception as e: print( 'Exception 7 from attendance views.py = %s (%s)' % (e.message, type(e))) except Exception as e: print('Exception 9 from attendance views.py = %s (%s)' % (e.message, type(e))) # this view is being called from mobile. We use dummy template so that we dont' run into exception # return render(request, 'classup/dummy.html') response_data['status'] = 'success' return JSONResponse(response_data, status=200)
def retrieve_stu_sub_marks_history(request, subject): marks_history = { } response_array = [] if request.method == 'GET': student = request.GET.get('student') try: s = Student.objects.get(id=student) sub = Subject.objects.get(school=s.school, subject_name=subject) c = s.current_class sect = s.current_section test_list = ClassTest.objects.filter(the_class=c, section=sect, subject=sub, is_completed=True). \ order_by('date_conducted') for test in test_list: marks_history['date'] = test.date_conducted test_result = TestResults.objects.get(class_test=test, student=s) if test.grade_based: marks_history['max_marks'] = 'Grade Based' marks_history['marks'] = test_result.grade else: marks_history['max_marks'] = test.max_marks marks_history['marks'] = test_result.marks_obtained d = dict(marks_history) response_array.append(d) try: parent_mobile = s.parent.parent_mobile1 action = 'Retrieved ' + sub.subject_name + ' marks history for ' + s.fist_name + ' ' + s.last_name log_entry(parent_mobile, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 507 from parents views.py %s %s' % (e.message, type(e))) return JSONResponse(response_array, status=200) except Exception as e: print ('Exception 12 from parents views.py = %s (%s)' % (e.message, type(e))) print ('unable to retrieve ' + sub.subject_name + ' marks history for ' + s.fist_name + ' ' + s.last_name) return JSONResponse(response_array, status=201)
def unset_subjects(request, teacher): print ('request.body(unset_subjects)=') if request.method == 'POST': t = Teacher.objects.get(email=teacher) print (t) print ('request.body(unset_subjects)=') print (request.body) school = t.school data = json.loads(request.body) print ('data(unset_subjects)=') print (data) for key in data: print('key(unset_subjects)=' + key) subject = data[key] print('subject(unset_subjects)=' + subject) print ('now trying to extract subject(unset_subjects)') try: s = Subject.objects.get(school=school, subject_code=subject) except Exception as e: print('unable to retrieve subject(unset_subjects)') print ('Exception3 from teacher views.py = %s (%s)' % (e.message, type(e))) print ('now trying to unset teacher subject') try: ts = TeacherSubjects.objects.get(teacher=t, subject=s) if ts: print('subject ' + s.subject_name + ' was set for this teacher ' + t.first_name + ' ' + t.last_name + '. This will now be deleted') ts.delete() except Exception as e: print('subject ' + s.subject_name + ' was not set for teacher ' + t.first_name + ' ' + t.last_name) print ('Exception4 from teacher views.py = %s (%s)' % (e.message, type(e))) pass try: action = 'Subjects unSet' log_entry(teacher, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 501 from teachers views.py %s %s' % (e.message, type(e))) return HttpResponse('OK')
def get_queryset(self): user = self.kwargs['teacher'] try: teacher = Teacher.objects.get(email=user) print('will now try to retrieve the Images Video created by %s' % teacher) q = ImageVideo.objects.filter( teacher=teacher, active_status=True).order_by('-creation_date') print( 'query retrieved successfully for Image Video list of %s = ' % teacher) print(q) try: action = 'Retrieving Image Video list for %s' % teacher log_entry(teacher.email, action, 'Normal', True) except Exception as e: print('unable to crete logbook entry') print('Exception 504 from pic_share views.py %s %s' % (e.message, type(e))) print( 'now returning the query retrieved successfully for Image/Video list of %s ' % teacher) return q except Exception as e: print('Exception 12082019-B from pic_share view.py %s %s' % (e.message, type(e))) print('We need to retrieve the Image/Video list for student') self.serializer_class = SharedWithSerializer try: student = Student.objects.get(pk=user) q = ShareWithStudents.objects.filter( student=student).order_by('image_video__creation_date') return q except Exception as e: print('Exception 12082019-A from pic_share views.py %s %s' % (e.message, type(e))) print('could not retrieve student with id %s' % user)
def post(self, request, *args, **kwargs): context_dict = {} context_dict['header'] = 'Share Image' try: data = json.loads(request.body) print('Image sharing process started') teacher = data['teacher'] t = Teacher.objects.get(email=teacher) print(t) print(teacher) the_class = data['class'] print(the_class) c = Class.objects.get(school=t.school, standard=the_class) print(c) section = data['section'] print(section) s = Section.objects.get(school=t.school, section=section) print(s) whole_class = data['whole_class'] print('whole class = %s' % whole_class) if whole_class == 'false': student_list = data['student_list'] print(student_list) print(type(student_list)) # 29/08/2019 - when comes from Android, the student id list comes as an array. So we need to break it # into list. But from iOS, it comes as a proper list. So need not break it try: students = ast.literal_eval(student_list) print(students) except Exception as e: print( 'exception 29082019-A from pic_share views.py %s %s' % (e.message, type(e))) print( 'looks the request has come from iOS, hence no need for ast.literal_eval' ) students = student_list image_name = data['image_name'] print(image_name) image = data['image'] image_file = ContentFile(base64.b64decode(image), name=image_name.replace('@', '')) description = data['description'] print(description) # save the image image_video = ImageVideo() image_video.location = image_file image_video.descrition = description image_video.teacher = t image_video.the_class = c image_video.section = s # long_link = 'https://storage.cloud.google.com/classup/classup2/media/prod/image_video/%s' % \ # image_name.replace('@', '') long_link = 'https://classup2.s3.us-east-2.amazonaws.com/media/prod/image_video/%s' % \ image_name.replace('@', '') print('long_link = %s' % long_link) short_link = long_link # prepare short link global_conf = GlobalConf.objects.get(pk=1) key = global_conf.short_link_api url = 'https://cutt.ly/api/api.php?' url += 'key=%s&short=%s' % (key, long_link) print('url for generating short link = %s' % url) try: req = urllib2.Request(url, headers={'User-Agent': "Magic Browser"}) response = urllib2.urlopen(req) print('response for generating short link = %s' % response) outcome = json.loads(response.read()) print('ouctome = ') print(outcome) status = outcome['url']['status'] print('status = %i' % status) if status == 7: short_link = outcome['url']['shortLink'] print('short_lint = %s' % short_link) image_video.short_link = short_link except Exception as e: print('exception 15082019-A from pic_share views.py %s %s' % (e.message, type(e))) print( 'failed to generate short link for the image/video uploaded by %s' % t) image_video.short_link = 'not available' try: image_video.save() print('saved the image uploaded by %s' % t) # now update with the SharedWith table if whole_class == 'true': print('this image/video is shared with whole class %s-%s' % (the_class, section)) students = Student.objects.filter(current_class=c, current_section=s) for student in students: try: shared = ShareWithStudents(image_video=image_video, student=student, the_class=c, section=s) shared.save() print( 'saved the SharedWithStudent for %s of %s-%s' % (student, the_class, section)) parent = student.parent.parent_name message = 'Dear %s new pic uploaded. %s. Click %s' % ( parent, description, short_link) print(message) sms.send_sms1(student.school, teacher, student.parent.parent_mobile1, message, 'Share Pic') except Exception as e: print( 'exception 01082019-A from pic_share views.py %s %s' % (e.message, type(e))) print('failed to save SharedWithStudent object') else: for an_item in students: try: student = Student.objects.get(pk=an_item) shared = ShareWithStudents(image_video=image_video, student=student, the_class=c, section=s) shared.save() print( 'saved the SharedWithStudent for %s of %s-%s. Now sending sms' % (student, c, s)) parent = student.parent.parent_name message = 'Dear %s new pic uploaded. %s. Click %s' % ( parent, description, short_link) print(message) sms.send_sms1(student.school, teacher, student.parent.parent_mobile1, message, 'Share Pic') except Exception as e: print( 'exception 02082019-A from pic_share views.py %s %s' % (e.message, type(e))) print('failed to save SharedWithStudent object') context_dict['status'] = 'success' context_dict['message'] = 'Media upload successful' print(image_video.location) try: action = 'uploading image for %s-%s teacher %s' % ( the_class, section, t) print(action) log_entry(teacher, action, 'Normal', True) except Exception as e: print('unable to crete logbook entry') print( 'Exception 31072019-A from pic_share views.py %s %s' % (e.message, type(e))) return JSONResponse(context_dict, status=200) except Exception as e: print( 'Exception 31072019-B from pic_share views.py = %s (%s)' % (e.message, type(e))) print( 'error while trying to save the image/video uploaded by %s' % t) context_dict['status'] = 'failed' context_dict[ 'message'] = 'error while trying to save the image/video uploaded' return JSONResponse(context_dict, status=201) except Exception as e: print('failed to save image/video') print('Exception 02082019-B from pic_share views.py = %s (%s)' % (e.message, type(e))) context_dict['status'] = 'failed' context_dict[ 'message'] = 'error while trying to save the image/video uploaded' return JSONResponse(context_dict, status=201)
def submit_parents_communication(request): if request.method == 'POST': data = json.loads(request.body) student_id = data['student_id'] cat = data['category'] communication_text = data['communication_text'] student = Student.objects.get(id=student_id) school = student.school configuration = Configurations.objects.get(school=school) # student and category are foreign keys category = ParentCommunicationCategories.objects.get(category=cat) # now, create the ParentCommunicationObject try: pc = ParentCommunication(student=student, category=category, date_sent=datetime.datetime.now(), communication_text=communication_text) pc.save() print ('successfully saved the message "' + communication_text + '" in the database') try: parent_mobile = student.parent.parent_mobile1 action = 'Submitted Parent Communication' log_entry(parent_mobile, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 500 from parents views.py %s %s' % (e.message, type(e))) # if the message was for Class Teacher/Principal's immediate attention an sms need to be sent to them if cat == 'Class Teacher/Principal Attention' or cat == 'Leave Application' \ or configuration.send_all_parent_sms_to_principal: try: parent = student.parent parent_name = parent.parent_name parent_mobile = parent.parent_mobile1 the_class = student.current_class section = student.current_section # compose the message message_type = 'Parent Communication' message = communication_text + '. Regards, ' + parent_name + ' (' + parent_mobile + ')' message += ', Parent of ' message += student.fist_name + ' ' + student.last_name + ' (class ' message += the_class.standard + '-' + section.section + ')' print (message) # sometimes class teacher may not be set try: ct = ClassTeacher.objects.get(standard=the_class, section=section) if ct: teacher = ct.class_teacher teacher_mobile = teacher.mobile sms.send_sms1(school, parent_name, teacher_mobile, message, message_type) try: parent_mobile = student.parent.parent_mobile1 action = 'Parent Communication SMS sent to Class Teacher: ' action += teacher.first_name + ' ' + teacher.last_name log_entry(parent_mobile, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 501 from parents views.py %s %s' % (e.message, type(e))) except Exception as e: print('Class Teacher not set for ' + the_class.standard + '-' + section.section) print ('Exception 1 from parents views.py = %s (%s)' % (e.message, type(e))) # 23/07/2019 leave applications need not be sent to principal if cat != 'Leave Application': try: principal_mobile = configuration.principal_mobile sms.send_sms1(school, parent_name, principal_mobile, message, message_type) except Exception as e: print('unable to send Parent communication to Principal') print ('Exception 502-A from parents views.py %s %s' % (e.message, type(e))) # 21/09/2017 - added so that apart from Principal, some other responsible staff can also receive # the message try: admin1_mobile = configuration.admin1_mobile # sms.send_sms1(school, parent_name, admin1_mobile, message, message_type) except Exception as e: print('unable to send Parent communication to Admin 1') print ('Exception 502-B from parents views.py %s %s' % (e.message, type(e))) try: parent_mobile = student.parent.parent_mobile1 action = 'Parent Communication SMS sent to Principal' log_entry(parent_mobile, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 502 from parents views.py %s %s' % (e.message, type(e))) except Exception as e: print ('failed to send message ' + communication_text + ' to Class Teacher of class ' + the_class.standard + '-' + section.section) print ('Exception 2 from parents views.py = %s (%s)' % (e.message, type(e))) return HttpResponse('Success') except Exception as e: print ('Error occured while trying to save comments from parents of ' + student.fist_name + ' ' + student.last_name) print ('Exception 3 from parents views.py = %s (%s)' % (e.message, type(e))) return HttpResponse('Failed') return HttpResponse('OK')
def retrieve_stu_att_summary(request): dict_attendance_summary = { } response_array = [] if request.method == 'GET': student_id = request.GET.get('student_id') student = Student.objects.get(id=student_id) school = student.school c = Configurations.objects.get(school=school) session_start_month = c.session_start_month print (session_start_month) the_class = student.current_class section = student.current_section # 22/08/2016 logic - Coaching classes and Colleges prefer to conduct attendance subject wise and hence # Main subject may not exist for them. In this case we will be presenting the aggregate of attendance for all # the subjects. This will be done by first checking the existence of subject Main. If it is present the # calculations are based on attendance in Main subjects, else aggregate of all subjects main_exist = True try: main = Subject.objects.get(school=school, subject_name='Main') except Exception as e: print ('Main subject does not exist for this school/Coaching Institute') print ('Exception4 from parents views.py = %s (%s)' % (e.message, type(e))) main_exist = False now = datetime.datetime.now() work_days = 0 if now.month < session_start_month: print ('current month is less than session start month. Hence starting from last year') for m in range(session_start_month, 12 + 1): # 12+1, because loop executes for 1 time less than max index month_year = calendar.month_abbr[m] + '/' + str(now.year - 1) dict_attendance_summary["month_year"] = month_year try: if main_exist: query = AttendanceTaken.objects.filter(date__month=m, date__year=now.year - 1, subject=main, the_class=the_class, section=section) else: query = AttendanceTaken.objects.filter(date__month=m, date__year=now.year - 1, the_class=the_class, section=section) work_days = query.count() dict_attendance_summary["work_days"] = work_days print ('days in ' + str(m) + '/' + str(now.year - 1) + '=' + str(work_days)) except Exception as e: print ('unable to fetch the number of days for ' + month_year) print ('Exception5 from parents views.py = %s (%s)' % (e.message, type(e))) try: if main_exist: query = Attendance.objects.filter(student=student, subject=main, date__month=m, date__year=now.year - 1) else: query = Attendance.objects.filter(student=student, date__month=m, date__year=now.year - 1) absent_days = query.count() dict_attendance_summary["absent_days"] = absent_days present_days = work_days - absent_days dict_attendance_summary["present_days"] = present_days if work_days != 0: present_perc = round((float(present_days) / float(work_days)) * 100, 2) dict_attendance_summary['percentage'] = str(present_perc) + '%' else: dict_attendance_summary['percentage'] = 'N/A' print ('absent days for ' + str(m) + '/' + str(now.year - 1) + '=' + str(query.count())) except Exception as e: print ('unable to fetch absent days for ' + str(m) + '/' + str(now.year - 1)) print ('Exception6 from parents views.py = %s (%s)' % (e.message, type(e))) d = dict(dict_attendance_summary) response_array.append(d) for m in range(1, now.month + 1): month_year = calendar.month_abbr[m] + '/' + str(now.year) dict_attendance_summary["month_year"] = month_year try: if main_exist: query = AttendanceTaken.objects.filter(date__month=m, date__year=now.year, subject=main, the_class=the_class, section=section) else: query = AttendanceTaken.objects.filter(date__month=m, date__year=now.year, the_class=the_class, section=section) work_days = query.count() dict_attendance_summary["work_days"] = work_days print ('days in ' + str(m) + '/' + str(now.year) + '=' + str(work_days)) except Exception as e: print ('unable to fetch the number of days for ' + str(m) + '/' + str(now.year)) print ('Exception7 from parents views.py = %s (%s)' % (e.message, type(e))) try: if main_exist: query = Attendance.objects.filter(student=student, subject=main, date__month=m, date__year=now.year) else: query = Attendance.objects.filter(student=student, date__month=m, date__year=now.year) absent_days = query.count() dict_attendance_summary["absent_days"] = absent_days present_days = work_days - absent_days dict_attendance_summary["present_days"] = present_days if work_days != 0: present_perc = round((float(present_days) / float(work_days)) * 100, 2) dict_attendance_summary['percentage'] = str(present_perc) + '%' else: dict_attendance_summary['percentage'] = 'N/A' print ('absent days for ' + str(m) + '/' + str(now.year) + '=' + str(query.count())) except Exception as e: print ('unable to fetch absent days for ' + str(m) + '/' + str(now.year)) print ('Exception8 from parents views.py = %s (%s)' % (e.message, type(e))) d = dict(dict_attendance_summary) response_array.append(d) # if current month is higher than the session_start_month then we need to add the working days # session start month till current-1 month else: for m in range(session_start_month, now.month + 1): month_year = calendar.month_abbr[m] + '/' + str(now.year) dict_attendance_summary["month_year"] = month_year try: if main_exist: query = AttendanceTaken.objects.filter(date__month=m, date__year=now.year, subject=main, the_class=the_class, section=section) else: query = AttendanceTaken.objects.filter(date__month=m, date__year=now.year, the_class=the_class, section=section) work_days = query.count() dict_attendance_summary["work_days"] = work_days print ('days in ' + str(m) + '/' + str(now.year) + '=' + str(work_days)) except Exception as e: print ('unable to fetch the number of days for ' + str(m) + '/' + str(now.year)) print ('Exception9 from parents views.py = %s (%s)' % (e.message, type(e))) try: if main_exist: query = Attendance.objects.filter(student=student, subject=main, date__month=m, date__year=now.year) else: query = Attendance.objects.filter(student=student, date__month=m, date__year=now.year) absent_days = query.count() dict_attendance_summary["absent_days"] = absent_days present_days = work_days - absent_days dict_attendance_summary["present_days"] = present_days if work_days != 0: present_perc = round((float(present_days) / float(work_days)) * 100, 2) dict_attendance_summary['percentage'] = str(present_perc) + '%' else: dict_attendance_summary['percentage'] = 'N/A' print ('absent days for ' + str(m) + '/' + str(now.year) + '=' + str(query.count())) except Exception as e: print ('unable to fetch absent days for ' + str(m) + '/' + str(now.year)) print ('Exception10 from parents views.py = %s (%s)' % (e.message, type(e))) d = dict(dict_attendance_summary) response_array.append(d) print (response_array.__len__()) try: parent_mobile = student.parent.parent_mobile1 action = 'Retrieved ' + student.fist_name + ' ' + student.last_name + ' Attendance History' log_entry(parent_mobile, action, 'Normal', True) except Exception as e: print('unable to create logbook entry') print ('Exception 505 from parents views.py %s %s' % (e.message, type(e))) return JSONResponse(response_array, status=200)
def attendance_taken(request, school_id, the_class, section, subject, d, m, y, teacher): if request.method == 'POST': school = School.objects.get(id=school_id) # all of the above except date are foreign key in Attendance model. Hence we need to get the actual object c = Class.objects.get(school=school, standard=the_class) print(c) s = Section.objects.get(school=school, section=section) print(s) sub = Subject.objects.get(school=school, subject_name=subject) print(sub) t = Teacher.objects.get(email=teacher) log_entry(teacher, "Attendance Taken Started", "Normal", True) print(t) the_date = date(int(y), int(m), int(d)) print(the_date) # verify if the attendance for this class, section, subject on this date has already been taken try: q = AttendanceTaken.objects.filter(date=the_date, the_class=c, section=s, subject=sub) if 0 == q.count(): log_entry(teacher, "Attendance not taken earlier", "Normal", True) a = AttendanceTaken(date=the_date) a.the_class = c a.section = s a.subject = sub a.taken_by = t a.save() log_entry(teacher, "Attendance Taken Recorded", "Normal", True) except Exception as e: print('failed to recored AttendanceTaken') print('Exception1 from attendance views.py = %s (%s)' % (e.message, type(e))) log_entry( teacher, "failed to recored AttendanceTaken. Exception 1 from attendance views.py", "Normal", False) # for the purpose of audit, make an entry in AttendanceUpdated table. try: au = AttendanceUpdated(date=the_date, the_class=c, section=s, subject=sub, updated_by=t) au.save() log_entry(teacher, "Attendance Updated recorded", "Normal", True) except Exception as e: print('failed to record AttendanceUpdate') print('Exception2 from attendance views.py = %s (%s)' % (e.message, type(e))) log_entry( teacher, "failed to record AttendanceUpdate. Exepction 2 from attendance views.py", "Normal", False) return HttpResponse('OK')
def process_attendance1(request, school_id, the_class, section, subject, d, m, y, teacher): response_data = {} message_type = 'Attendance' if request.method == 'POST': log_entry(teacher, "Attendance Processing Started", "Normal", True) school = School.objects.get(id=school_id) # all of the above except date are foreign key in Attendance model. Hence we need to get the actual object c = Class.objects.get(school=school, standard=the_class) s = Section.objects.get(school=school, section=section) sub = Subject.objects.get(school=school, subject_name=subject) the_date = date(int(y), int(m), int(d)) t = Teacher.objects.get(email=teacher) teacher_name = '%s %s' % (t.first_name, t.last_name) today = date.today() time_delta = today - the_date print( 'this attendance taken by %s for class %s-%s for subject %s is %i days old' % (teacher_name, the_class, section, subject, time_delta.days)) data = json.loads(request.body) print(data) absent = 0 for key in data: student_id = data[key] student = Student.objects.get(id=student_id) student_name = '%s %s' % (student.fist_name, student.last_name) absent += 1 # check to see if absence for this student for this date, class, section and subject has already # been marked try: q = Attendance.objects.filter(date=the_date, the_class=c, section=s, subject=sub, student=student) # make an entry to database only it is a fresh entry #if q.count() == 0: if not Attendance.objects.filter(date=the_date, the_class=c, section=s, subject=sub, student=student).exists(): action = 'Absence marked for %s %s' % (student.fist_name, student.last_name) print(action) attendance = Attendance(date=the_date) attendance.the_class = c attendance.section = s attendance.subject = sub attendance.student = student attendance.taken_by = t attendance.save() log_entry(teacher, action, "Normal", True) action = 'Starting to send sms to parents of ' + student.fist_name + ' ' + student.last_name log_entry(teacher, action, "Normal", True) # send sms to the parents of absent students m1 = '' m2 = '' try: p = student.parent m1 = p.parent_mobile1 m2 = p.parent_mobile2 except Exception as e: print('Exception occured during processing of: ') print(student) print('Exception3 from attendance views.py = %s (%s)' % (e.message, type(e))) log_entry( teacher, "Attendance Processing Error. Exception 3 from attendance views.py", "Normal", True) print("mobile1=" + m1) print("mobile2=" + m2) message = '' configuration = Configurations.objects.get(school=school) school_name = school.school_name school_short_name = configuration.school_short_name # 14/04/2018 - for collage students, SMS will be sent to students institute_type = configuration.type try: parent_name = student.parent.parent_name # prepare the message action = 'Absence SMS Drafting for ' + parent_name + ' started' log_entry(teacher, action, "Normal", True) # 17/02/17 we are looking to use student first name in messages. # However, some schools store entire name as first name. T # his need to be checke and split first name if entire name is stored as first name the_name = student.fist_name if ' ' in student.fist_name: (f_name, l_name) = the_name.split(' ') else: f_name = the_name if institute_type == 'Collage': message = 'Dear %s, you' % the_name else: message = 'Dear ' + parent_name + ', your ward ' + f_name if institute_type == 'Collage': if time_delta.days == 0: message += ' are' else: message += ' were' else: if time_delta.days == 0: message += ' is' else: message += ' was' # if subject is main then we need to tell that student was absent if subject == 'Main' or subject == 'main' or subject == 'MAIN': message += ' absent on ' + str(d) + '/' + str( m) + '/' + str(y) else: message += ' absent on ' + str(d) + '/' + str(m) + '/' + str(y) + \ ' in the attendance of ' + subject # 04/05/2017 - coaching classes does not require application for absence if configuration.type != 'school': message += '. Regards %s ' % school_short_name else: message += '. Please send an application (Ignore if already done). Regards, ' + school_short_name except Exception as e: print('Exception4 from attendance views.py = %s (%s)' % (e.message, type(e))) action = 'Error in drafting SMS for ' + parent_name + '. Exception 4 from attendance views.py' log_entry(teacher, action, "Normal", True) print(message) # 10/02/2018 - absence SMS will not be sent if attendance is for a date older than 7 days if time_delta.days < 7: print( 'this attendance is recent. Hence SMS will be sent' ) # for coaching classes and colleges we need to send sms for any kind of absence if configuration.send_period_bunk_sms: sms.send_sms1(school, teacher, m1, message, message_type) action = 'Absence SMS sent to ' + parent_name log_entry(teacher, action, "Normal", True) if m2 != '': if configuration.send_absence_sms_both_to_parent: sms.send_sms1(school, teacher, m2, message, message_type) log_entry(teacher, action, "Normal", True) else: # for schools # if this subject is NOT the main subject, then we will # send sms only if the student was present # in main attendance (means the student has BUNKED this class :) if subject != 'Main' and subject != 'main' and subject != 'MAIN': try: main = Subject.objects.get( school=school, subject_name='Main') q = Attendance.objects.filter( date=the_date, the_class=c, section=s, subject=main, student=student) if q.count() == 0: print( student.fist_name + ' ' + student.last_name + ' was not absent in Main attendance. ' 'Looks he has bunked this class...' ) action = student.fist_name + ' ' + student.last_name action += ' found to be bunking the Class!!!' log_entry(teacher, action, "Normal", True) if configuration.send_period_bunk_sms: sms.send_sms1( school, teacher, m1, message, message_type) action = 'Absence SMS sent to ' + student.parent.parent_name log_entry(teacher, action, "Normal", True) if m2 != '': if configuration.send_absence_sms_both_to_parent: sms.send_sms1( school, teacher, m2, message, message_type) action = 'Absence SMS sent to Mrs. ' + student.parent.parent_name log_entry( teacher, action, "Normal", True) except Exception as e: action = 'Unable to send SMS to ' + student.parent.parent_name log_entry(teacher, action, "Normal", True) print('unable to send sms for ' + f_name) print( 'Exception5 from attendance views.py = %s (%s)' % (e.message, type(e))) else: if configuration.send_absence_sms: sms.send_sms1(school, teacher, m1, message, message_type) action = 'Absence SMS sent to ' + student.parent.parent_name log_entry(teacher, action, "Normal", True) if m2 != '': if configuration.send_absence_sms_both_to_parent: sms.send_sms1( school, teacher, m2, message, message_type) action = 'Absence SMS sent to Mrs. ' + student.parent.parent_name log_entry(teacher, action, "Normal", True) else: print( 'this attendance is more than 7 days old. Hence not sending SMS' ) else: print( 'absence for %s for %s in %s has already been marked.' % (student_name, the_date, subject)) except Exception as e: print('Exception6 from attendance views.py = %s (%s)' % (e.message, type(e))) log_entry( teacher, "Absence was already marked. Exception 6 from attendance views.py", "Normal", True) # 16/07/2019 we are implementing table to store daily attendance summaries. # So that download attendance summary is fast print('now storing the attendance summary for %s-%s of %s date %s' % (the_class, section, school, the_date)) try: total = Student.objects.filter(current_class=c, current_section=s, active_status=True).count() print('total students in %s-%s of %s = %i' % (the_class, section, school, total)) present = total - absent percentage = int(round((float(present) / float(total)) * 100, 0)) print(percentage) print('students present on %s = %i' % (the_date, percentage)) try: summary = DailyAttendanceSummary.objects.get(date=the_date, the_class=c, section=s, subject=sub) summary.total = total summary.present = present summary.absent = absent summary.percentage = percentage summary.save() print( 'attendance summary for class %s-%s of %s date %s is updated' % (the_class, section, school, the_date)) except Exception as ex: print('exception 16072019-X from attendance views.py %s %s' % (ex.message, type(ex))) summary = DailyAttendanceSummary(date=the_date, the_class=c, section=s, subject=sub) summary.total = total summary.present = present summary.absent = absent summary.percentage = percentage summary.save() print( 'attendance summary for class %s-%s of %s date %s is stored' % (the_class, section, school, the_date)) except Exception as ex: print('exception 16072019-B from attendance views.py %s (%s)' % (ex.message, type(ex))) print('failed in storing the attendance summary for %s-%s of %s' % (the_class, section, school)) response_data['status'] = 'success' log_entry(teacher, "Attendance Processing Complete", "Normal", True) return JSONResponse(response_data, status=200)