def attend_class(): """ Function to render form for class attendance :return: redirection to student homepage, else attendance template """ return_403('lecturer_id') form, message, status = SignInForm(), '', 0 reg_num, url, verified = Student.query.filter_by( id=session['student_id']).first().reg_num, "", 0 # get chosen running class course = request.args.get("course") course_title = Course.query.filter( Course.id == course).first().name # course title class_ = Class.query.filter(Class.is_active).filter(LecturersTeaching.courses_id == course) \ .filter(Class.lec_course_id == LecturersTeaching.id).first().id # check if student has already signed into a class if Attendance.query.filter((Attendance.student == reg_num) & (Attendance.class_ == class_)).first(): flash("Class already attended") return redirect(url_for('student.web_', pid=session['student_id'])) if form.validate_on_submit(): image = request.files['photo'] # check if allowed if allowed_file(image.filename): # get name of the source file + Make the filename safe, remove unsupported chars filename = str(secure_filename(image.filename)) # get name of student student_name = Student.query.filter_by( reg_num=reg_num).first().name # if allowed, process image to get url and verification status of image url, verified = determine_picture(reg_num, student_name.replace(" ", "_"), image, filename, attendance=True) # add to db message, status = atten_dance(reg_num, url, verified, class_, course_title) if not status: flash(message) return redirect(url_for('student.home')) else: form.photo.errors.append(message) if not allowed_file(image.filename): # if not allowed, raise error form.photo.errors.append("Files should only be pictures") return render_template("student/attend.html", form=form, title="Attend Class", is_student=True, pid=session['student_id'])
def web(): """ Function to render form to student on the website and subsequently register student :return: Student login page if successful, else registration page """ form, message, status = RegistrationForm(), '', 0 if 'student_id' in session: flash("Logout out first") return redirect(url_for('student.home')) return_403('lecturer_id') if form.validate_on_submit(): reg_num, name = form.reg_num.data, form.first_name.data + " " + form.last_name.data images, pic_url = request.files.getlist("photo"), [] if images: # if len(images) < 10: form.photo.errors.append("Please upload at least 10 images of yourself") for image in images: # get name of the source file + Make the filename safe, remove unsupported chars filename = str(secure_filename(image.filename)) # check if allowed if not allowed_file(image.filename): # if not allowed, raise error form.photo.errors.append("Files should only be pictures") return render_template("student/register.html", form=form, title="Student Registration", is_student=True) else: url, verified = determine_picture(reg_num, name.replace(" ", "_"), image, filename) pic_url.append(url) # check if student already registered message, status = add_student(form.reg_num.data, name, form.year_of_study.data, courses.get(form.programme.data), form.email.data, pic_url) if not status: flash(message) return redirect(url_for('student.login')) else: form.reg_num.errors.append(message) return render_template("student/register.html", form=form, title="Student Registration")
def phone(): """ Function to register student from the mobile app Zipping functionality is courtesy of http://www.geeksforgeeks.org/working-zip-files-python/ :return: JSON Object containing error code and accompanying message """ json, message, status, pic_url, filename, unzip, files = request.form, '', 0, [], None, False, [] name = json['name'] reg_no = json['regno'] email = json['email'] # check that correct format of student reg. num is followed if not re.search("/[\S]+/", reg_no): return jsonify({'message': 'Invalid registration number', 'status': 4}) # check if student is already registered if Student.query.filter_by(reg_num=reg_no).first(): return jsonify({ 'message': 'Registration number already registered', 'status': 2 }) if Student.query.filter_by(email=email).first(): return jsonify({'message': 'Email already registered', 'status': 5}) department = json['departments'] year = json['year'] # receive and save zip file zip_file = request.files['FILE'] if zip_file: filename = secure_filename(zip_file.filename) zip_file.save(os.path.join(create_path(reg_no), filename)) path = create_path(reg_no) + "/" + os.path.basename(filename).replace( ".zip", "") # confirm that file is a zip file if is_zipfile(zip_file): # open the zip file in READ mode with ZipFile(os.path.join(create_path(reg_no), filename), 'r') as zip_file: # print all the contents of the zip file in table format zip_file.printdir() # extract all the files in zip file print('Extracting all the files now...') zip_file.extractall(path=path) # remember unzipping has happened unzip = True # delete redundant zip file print('Deleting file {}...'.format(filename)) filename = os.path.join(create_path(reg_no), filename) os.remove(filename) print('Done!') # if successful unzipping if unzip: # read the files in the resultant directory files = os.listdir(path) # change working directory in resultant directory os.chdir(path) for image in files: # move files into the parent directory filename = secure_filename(image) move(filename, os.path.join(os.pardir, filename)) # move to parent directory os.chdir(os.pardir) # delete resultant directory rmtree(path) # process the files for image in files: if os.path.isfile(os.path.join(os.curdir, image)): filename = secure_filename(image) if not allowed_file(filename): # noinspection PyUnusedLocal message, status = "File format not supported", 3 return jsonify({'message': message, "status": status}) else: url, verified = determine_picture(reg_no, name.replace(" ", "_"), image, filename, phone=True) pic_url.append(url) # return to original location os.chdir(home_folder) print(u"Current path is", os.path.abspath(os.curdir), sep=' ') message, status = add_student(reg_no, name, year, department, email, pic_url) # print(reg_no, name, department, year, email, sep='\n') return jsonify({'message': message, "status": status})