def collect_data(request):
    if request.user.is_authenticated:
        if request.user.is_teacher:
            if request.method == 'POST':
                studentToClassMap = MACObj.getStudenToClassMap()
                plist = request.POST.getlist('present')
                data = request.POST.getlist('check_1')
                classid = request.POST.get('classid')
                userid = request.POST.get('userid')
                print(data)
                print("present list-----")
                print(classid, userid)
                print(plist)
                print(data)
                print(studentToClassMap)
                final_result = set(plist + data)
                print(final_result)
                classToStudent = {}
                for i in final_result:
                    try:
                        classToStudent[studentToClassMap[i]].append(i)
                    except:
                        classToStudent[studentToClassMap[i]] = [i]
                for classid, final_result in classToStudent.items():
                    MarkAttendanceCode.updateInDB(classid, userid,
                                                  final_result)
            return render(request, 'classroom/collect.html')
        else:
            return render(request, 'classroom/NotAuthenticated.html')
 def code(self, classId, UsrName):
     var1 = classId
     print(UsrName)
     print(classId)
     global MACObj
     MACObj = MarkAttendanceCode.AttendanceTake()
     classIds = MarkAttendanceCode.getClassesFromElective(classId, UsrName)
     print("ClassIDs = ", classIds)
     return MACObj.takeAttendance(classIds, UsrName) + [classId, UsrName]
def AllStudents(request):
    now = timezone.now()
    if request.method == 'GET':
        form = PostForm()
    else:
        form = PostForm(request.POST)
        classID = form['classID'].value()
        py_obj = test_code()
        py_obj.code2(classID)
    print("Cid is " + cid1)
    #users = ClassToStudent_Mapping.objects.filter(ClassId = cid1)
    try:
        users = MarkAttendanceCode.getCumulativeAttendanceOfStudents(cid1)
        try:
            print(users[1])
        except:
            return render(request, 'classroom/None_cid.html',
                          {'message': 'Enter Valid ClassId'})
    except IndexError:
        return render(request, 'classroom/None_cid.html',
                      {'message': 'Enter Valid ClassId'})
    if (bool(users)):
        return render(request, 'classroom/AllStudentDetails.html', {
            'users': users,
            'classId': cid1
        })
    else:
        return render(request, 'classroom/None_cid.html', {
            'users': users,
            'message': 'Enter Valid ClassId'
        })
def Tpost_form_upload(request):
    if request.user.is_authenticated:
        if request.user.is_teacher:
            if request.method == 'GET':
                form = PostForm()
            else:
                form = PostForm(request.POST)
                usrname = request.user.username

                classID = form['classID'].value()
                py_obj = test_code()
                data = py_obj.code(classID, usrname)
                print(data[0], data[1], data[2], data[3])
                MarkAttendanceCode.callAbsenteesListAloud(list(data[0]))
            return render(
                request, 'classroom/absentees.html', {
                    'data': list(data[0]),
                    'data1': list(data[1]),
                    'classId': data[2],
                    'usrname': data[3]
                })
        else:
            return render(request, 'classroom/NotAuthenticated.html')
def download(request):
    response = HttpResponse(content_type='text/csv')
    response[
        'Content-Disposition'] = 'attachment;filename="attendence_list.xlsx"'
    if request.method == 'POST':
        cid = request.POST.get('classID')
        cid1 = cid.lower()
        print(cid1)
        usrname = request.user.username
        usrname1 = usrname.lower()
        x = usrname1.split('_')
        cid2 = cid1[1:len(cid1) - 1]
        if (cid2 == x[1]):
            try:
                users = MarkAttendanceCode.getCumulativeAttendanceOfStudents(
                    cid)
                print(users)
                if (len(users) == 0):
                    return render(request, 'classroom/None.html')
                else:
                    workbook = xlsxwriter.Workbook(response)
                    worksheet = workbook.add_worksheet()
                    cell_format = workbook.add_format()
                    cell_format.set_bold()
                    cell_format.set_font_color('red')
                    row = 0
                    col = 0
                    count = 0
                    for user in users:
                        for data in user:
                            if count == 0:
                                worksheet.write(row, col, data, cell_format)
                            worksheet.write(row, col, data)
                            col += 1
                            if count != 0 and int(user[-1]) < 75:
                                cc = 0
                        row += 1
                        col = 0
                        count += 1
                    workbook.close()

            except IndexError:
                return render(request, 'classroom/None.html')
            return response
        else:
            return render(request, 'classroom/NotAuthenticated.html')
            return response
def IndividualStudentForStudentLogin(request):
    if request.user.is_authenticated:
        if request.user.is_student:
            now = timezone.now()
            #users = User.objects.all()
            try:
                users = MarkAttendanceCode.getCumulativeAttendanceOfAStudent(
                    stuUsrname)
            except IndexError:
                return render(request, 'classroom/None.html')
            print(users)
            return render(
                request,
                'classroom/IndividualStudentDetailsForStudentLogin.html',
                {'users': users})
        else:
            return render(request, 'classroom/NotAuthenticated.html')
def DailyAttendance(request):
    now = timezone.now()
    if request.method == 'GET':
        form = PostForm()
    else:
        form = PostForm(request.POST)
        if request.user.is_teacher:
            if form.is_valid():
                studentID = form.cleaned_data['classID']
                print(studentID)
                date1 = form.cleaned_data['Date']
                py_obj = test_code()
                py_obj.code3(studentID, date1)
        else:
            form = PostForm(request.POST)
            studentID = request.user.username
            print(studentID)
            date1 = form['classID'].value()
            print(date1)
            py_obj = test_code()
            py_obj.code3(studentID, date1)
    #users = ClassToStudent_Mapping.objects.filter(ClassId = cid1)
    try:
        users = MarkAttendanceCode.getDailyAttendance(studentID, date1)
    except IndexError:
        return render(request, 'classroom/None_date.html',
                      {'message': 'Enter Valid Details'})
    if (bool(users)):
        paramList = [studentID + " on ", date1]
        print(paramList)
        return render(request, 'classroom/IndividualStudentDetails.html', {
            'users': users,
            'param': paramList
        })
    else:
        return render(request, 'classroom/None_date.html', {
            'users': users,
            'message': 'Enter Valid Details'
        })
def IndividualStudent(request):
    now = timezone.now()
    if request.method == 'GET':
        form = PostForm()
    else:
        form = PostForm(request.POST)
        studentID = form['classID'].value()
        py_obj = test_code()
        py_obj.code1(studentID)
    try:
        users = MarkAttendanceCode.getCumulativeAttendanceOfAStudent(studentID)
    except IndexError:
        return render(request, 'classroom/None.html',
                      {'message': 'Enter Valid StudentId'})
    print(users)
    #users = User.objects.filter(username = sid1)
    if (bool(users)):
        return render(request, 'classroom/IndividualStudentDetails.html', {
            'users': users,
            'sid': studentID
        })
    else:
        return render(request, 'classroom/None.html', {'users': users})