def addeditstudentmarkss():
    addeditstudentmarksform = AddEditStudentMarksForm()
    print('addeditstudentmarks')
    idcourses = request.args.get('idcourses')
    # idcourse = request.args.get('idcourses')
    if(idcourses == None):
        idcourses = str(request.form.getlist('idcourses')[0])
    print(idcourses)
    idusers = session['idusers']
    resourcename = 'courses'
    permissions = session['allpermissionon']
    print(permissions)
    #DONE do all read write here on permission check
    # if (permissions['resourcename'] == resourcename):
        # read
    checkusercourse = checkUserCourse(idusers, idcourses)
    permupdate, constupdate = verifyPermissions(permissions, resourcename, 'update')
    # permwrite, constwrite = verifyPermissions(permissions, resourcename, 'write')
    print(permupdate, constupdate)
    # print(permwrite, constwrite)
    data = [['Permission Denied']]
    # data2 = [['Permission Denied']]
    if (checkusercourse and permupdate):
        if (constupdate.__contains__('self')):
            if (constupdate.__contains__('ownmarks')):
                data = [['Permission Denied']]
            # elif (constupdate.__contains__('marks')):
            #     data = [['Permission Denied']]
            else:
                if request.method == 'POST':
                    # print('Validate: ',addstudentform.validate())
                    # if addstudentform.validate_on_submit():
                    addeditstudent = request.form.getlist('addeditstudentmarks')
                    # data = []
                    # data.append(addeditstudentmarks)
                # DONE insert in database
                    if len(addeditstudent)!=0:
                        idusers = request.form.getlist('idusers')
                        print('check values: ',idusers," ",addeditstudent)
                        insertEditedMarksInCourse(idcourses,idusers,addeditstudent)
                tdata = getAllMarksFromCourse(idcourses)
                data=[]
                for i in tdata:
                    j = i[1]
                    if(i[1] == -1):
                        j = 'NA'
                    data.append([i[0],j])
                # data2 = getNotAssignedUsers(idcourses, 'student')
    return render_template('addeditstudentmarks.html',data=data, form = addeditstudentmarksform,
                           idcourses=idcourses)
def deletestudentmarks():
    deletestudentmarksform = DeleteStudentMarksForm()
    print('deletestudentmarks')
    idcourses = request.args.get('idcourses')
    # idcourse = request.args.get('idcourses')
    if(idcourses == None):
        idcourses = str(request.form.getlist('idcourses')[0])
    print(idcourses)
    idusers = session['idusers']
    resourcename = 'courses'
    permissions = session['allpermissionon']
    print(permissions)
    #TODO do all read write here on permission check
    # if (permissions['resourcename'] == resourcename):
        # read
    checkusercourse = checkUserCourse(idusers, idcourses)
    permupdate, constupdate = verifyPermissions(permissions, resourcename, 'update')
    # permwrite, constwrite = verifyPermissions(permissions, resourcename, 'write')
    print(permupdate, constupdate)
    # print(permwrite, constwrite)
    data = [['Permission Denied']]
    # data2 = [['Permission Denied']]
    if (checkusercourse and permupdate):
        if (constupdate.__contains__('self')):
            if (constupdate.__contains__('ownmarks')):
                data = [['Permission Denied']]
            # elif (constupdate.__contains__('marks')):
            #     data = [['Permission Denied']]
            else:
                if request.method == 'POST':
                    # print('Validate: ',addstudentform.validate())
                    # if addstudentform.validate_on_submit():
                    chkbox_values = request.form.getlist('deletestudentmarkschkbox')
                    # data = []
                    # data.append(chkbox_values)
                # DONE insert in database
                    if len(chkbox_values)!=0:
                        print('check values: ',chkbox_values)
                        deleteStudentMarks(idcourses,chkbox_values)
                tdata = getAllMarksFromCourse(idcourses)
                data = []
                for i in tdata:
                    if(i[1] != -1):
                        data.append(i)
                # data2 = getNotAssignedUsers(idcourses, 'student')
    return render_template('deletestudentmarks.html',data=data, deletestudentmarksform=deletestudentmarksform, idcourses=idcourses)
def removeta():
    removetaform = RemoveTAForm()
    print('removeta')
    idcourses = request.args.get('idcourses')
    # idcourse = request.args.get('idcourses')
    if(idcourses == None):
        idcourses = str(request.form.getlist('idcourses')[0])

    print(idcourses)
    idusers = session['idusers']
    resourcename = 'student'
    permissions = session['allpermissionon']
    print(permissions)
    #DONE do all read write here on permission check
    # if (permissions['resourcename'] == resourcename):
        # read
    checkusercourse = checkUserCourse(idusers, idcourses)
    permread, constread = verifyPermissions(permissions, resourcename, 'read')
    permwrite, constwrite = verifyPermissions(permissions, resourcename, 'write')
    print(permread, constread)
    print(permwrite, constwrite)
    data = [['Permission Denied']]
    # data2 = [['Permission Denied']]
    if (checkusercourse and permread and permwrite):
        if (constread.__contains__('self')):
            if (constread.__contains__('ownmarks')):
                data = [['Permission Denied']]
            elif (constread.__contains__('marks')):
                data = [['Permission Denied']]
            else:
                if request.method == 'POST':
                    # print('Validate: ',addstudentform.validate())
                    # if addstudentform.validate_on_submit():
                    chkbox_values = request.form.getlist('removetachkbox')
                    # data = []
                    # data.append(chkbox_values)
                # DONE insert in database
                    if len(chkbox_values)!=0:
                        print('check values: ',chkbox_values)
                        removeUserFromCourse(idcourses,chkbox_values)
                data = getTAInCourse(idcourses,'ta')
                # data2 = getNotAssignedUsers(idcourses, 'student')
    return render_template('removeta.html',data=data, removetaform=removetaform, idcourses=idcourses)