def insere_disciplina(nome, periodo, prof, creditos, carga, site): subject = Subjects(name=nome, period=periodo, professor=prof, credits=creditos, workload=carga, url=site) subject.save()
def update_records(request, lms_id): if request.method == 'POST': student = Student.objects.get(lmsId=lms_id) status = "" # Logging In s = requests.session() login_url = 'http://lms.bml.edu.in/login/index.php' dash_url = 'http://lms.bml.edu.in/local/dashboard/index.php' year = student.lmsId.split('.')[2][:2] print '-' * 103 print 'Name : ' + student.lmsId.split( '.')[0].title() + ' ' + student.lmsId.split('.')[1].title() print 'Year : ' + str(year) print 'Batch: ' + student.lmsId.split('.')[2][2:].upper() sem = 'Sem I' login = s.get(login_url) login_html = lxml.html.fromstring(login.content) hidden_inputs = login_html.xpath(r'//form//input[@type="hidden"]') form = {x.attrib["name"]: x.attrib["value"] for x in hidden_inputs} form['username'] = student.lmsId form['password'] = decode(student.password) response = s.post(login_url, data=form) print '-' * 103 if response.url == dash_url: status = "Login Succees!" print status else: status = 'Could Not Login! Check Details!' print status return render(request, "attendance/error.html", {}) # Searching for attendance link res_html = lxml.html.fromstring(response.content) res_links = res_html.xpath('//a[@class="btn-block "]/@href') attendance_link = '' for i in range(len(res_links)): if 'http://lms.bml.edu.in/mod/attendance/myattendance.php?studentid' in res_links[ i]: attendance_link = res_links[i] # Extracting info from Attendance Page attendance_page = s.get(attendance_link) attendance_html = lxml.html.fromstring(attendance_page.content) subjects = attendance_html.xpath('//a[@class="panel-title"]/text()') total_classes = attendance_html.xpath( '//div[@class="attendance-report-wrapper"]/div[2]/table[1]/tbody[1]/tr[1]/td[1]/text()' ) present = attendance_html.xpath( '//div[@class="attendance-report-wrapper"]/div[2]/table[1]/tbody[1]/tr[1]/td[' '2]/text()') absent = attendance_html.xpath( '//div[@class="attendance-report-wrapper"]/div[2]/table[1]/tbody[1]/tr[1]/td[' '3]/text()') sem_subjects = [] sub_array = [] # Formatting Subjects for i in range(len(subjects)): sem_subjects += [i] num = -1 if "SEM VI" in subjects[i]: num = 6 print "Found 6 sem" + subjects[i] elif "SEM V" in subjects[i]: num = 5 elif "SEM IV" in subjects[i]: num = 4 elif "SEM III" in subjects[i]: num = 3 elif "SEM II" in subjects[i]: num = 2 elif "Sem II" in subjects[i]: num = 2 elif "SEM I" in subjects[i]: num = 1 elif "Sem I" in subjects[i]: num = 1 temp = subjects[i].split(':') if len(temp) > 1: try: subjects[i] = temp[2].strip()[:-12] except IndexError: subjects[i] = subjects[i].strip() else: subjects[i] = temp[0].split('-')[0] sub_array.append({'name': subjects[i], 'sem': num}) k = 0 subjects_array = [] for subject in sub_array: try: test = Subjects.objects.get(name=subject['name']) except Exception: test = Subjects(name=subject['name'], sem=subject['sem']) test.save() pass k += 1 subjects_array.append(test) # Printing Details print '-' * 103 print '{0:50}||{1:5}|{2:7}|{3:6}|{4:7}|{5:6}|{6:6}| Set ah? '.format( 'Course', 'Total', 'Present', 'Absent', ' %', 'For 80', 'For 75') print '-' * 103 for i in range(len(sem_subjects)): k = sem_subjects[i] if total_classes[k] != '0': perc = (float(present[k])) / float(total_classes[k]) * 100 else: perc = -1 comment = '' if perc >= 80: comment = '{0:6}|{1:6}| SET!'.format(' SET', ' SET') elif perc == -1: perc = 0 comment = '{0:6}|{1:6}| SET!'.format(' SET', ' SET') elif perc >= 75: classes80 = int(absent[k]) * 5 - int(total_classes[k]) comment = '{0:4d} |{1:6}| uh?'.format(classes80, ' SET') else: classes80 = int(absent[k]) * 5 - int(total_classes[k]) classes75 = int(absent[k]) * 4 - int(total_classes[k]) comment = '{0:4d} |{1:4d} | Dhadel!'.format( classes80, classes75) print u'{0:50}||{1:4d} |{2:5d} |{3:4d} |{4:6.2f}%|{5:15}'.format( subjects[k], int(total_classes[k]), int(present[k]), int(absent[k]), float(perc), comment) try: temp = Attendance.objects.filter( student__lmsId=student.lmsId).get( subject__name=subjects[k]) # print temp temp.present_class = present[k] temp.absent_class = absent[k] temp.total_class = total_classes[k] temp.for75 = int(absent[k]) * 4 - int(total_classes[k]) temp.for80 = int(absent[k]) * 5 - int(total_classes[k]) temp.save() except Exception: temp = Attendance( student=student, subject=subjects_array[k], present_class=present[k], absent_class=absent[k], total_class=total_classes[k], for75=int(absent[k]) * 4 - int(total_classes[k]), for80=int(absent[k]) * 5 - int(total_classes[k])) temp.save() print '-' * 103 student.last_updated = timezone.now() student.cur_sem = max(list_of_sems(student)) print student.name + " --------- " + str(student.cur_sem) student.save() print student.last_updated # Loggin Out page_html = lxml.html.fromstring(response.content) logout = page_html.xpath(r'//a[@title = "Log out"]/@href') response = s.post(logout[0]) if response.url == 'http://lms.bml.edu.in/': status = 'Logout Success!' print status else: status = 'Logout Failed!' print status print '-' * 103 return redirect('/attendance/' + student.lmsId + "/" + str(student.cur_sem) + "/#focus") return HttpResponse("Failure!")