def resource_add_video_check(chapter_id): if request.method == 'POST': try: os.mkdir(current_app.config['UPLOAD_VIDEO_FOLDER']) except Exception as e: pass chapter = LightChapter.query.get(chapter_id) # check if the post request has the file part if 'file' not in request.files: flash(notify_warning('Video file empty!')) return redirect(request.url) file = request.files['file'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash(notify_warning('No selected file')) return redirect(request.url) if file and video_allowed_file(file.filename): filename = '{}_{}'.format(file_prefix(), secure_filename(file.filename)) resource = LightResource(type='video', filename=filename) chapter.resources.append(resource) chapter.update() file.save( os.path.join(current_app.config['UPLOAD_VIDEO_FOLDER'], filename)) flash(notify_success('Video uploaded!')) return redirect( url_for('lightcourse.view_chapter', chapter_id=chapter_id))
def add_check(): if request.method == 'POST': data = request.get_json() course_name = data['course_name'] course = LightCourse(name=course_name) grade_id = data['grade_id'] course.grade = Grade.query.get(grade_id) for quiz in data['quizes']: question = json.dumps(data['quizes'][quiz]['question']) current_quiz = LightQuiz(question=question) answers = data['quizes'][quiz]['answers'] for answer in answers: ans = json.dumps(answer['string']) current_quiz.answers.append( LightAnswer(string=ans, correct=answer['correct'])) course.quizzes.append(current_quiz) for chapter in data['chapters']: chapter_name = chapter['name'] chapter_text = chapter['text'] chapter = LightChapter(name=chapter_name) chapter.resources.append(LightResource(text=chapter_text)) course.chapters.append(chapter) course.teacher_id = current_user.id course.insert() flash(notify_success('course added!')) return jsonify({"submission": "ok"})
def toggle_subscribe(course_id): course = LightCourse.query.get(course_id) if course not in current_user.light_courses: current_user.light_courses.append(course) current_user.update() flash(notify_success('Subscribed to {}!'.format(course.name))) elif course in current_user.light_courses: current_user.light_courses.remove(course) current_user.update() flash(notify_warning('Unsubscribed from {}!'.format(course.name))) return redirect(url_for('course.list'))
def check_quiz(course_id): if request.method == 'POST': correct_answers = 0 # flash(notify_info(request.form)) course = LightCourse.query.get(course_id) submitted_quiz = [ name for name in request.form if name.startswith('quiz_') ] # flash(notify_info(submitted_quiz)) db_json = {} if len(submitted_quiz) == 0: flash(notify_warning("Can't be empty")) else: quiz_seen = {} for quiz_name in submitted_quiz: info = quiz_name.split('_') q_id = info[1] a_id = info[3] if q_id not in quiz_seen: quiz_seen[int(q_id)] = [] quiz_seen[int(q_id)].append(int(a_id)) # flash(notify_info(quiz_seen)) if len(quiz_seen) != current_app.config['LIGHTCOURSE_QUIZ_NUM']: flash(notify_warning('All questions must be answered!')) else: for quiz in course.quizzes: db_json[quiz.id] = [] for answer in quiz.answers: if answer.correct == True: db_json[quiz.id].append((answer.id)) # flash(notify_info(db_json)) for q in db_json: if quiz_seen[q] == db_json[q]: correct_answers += 1 if (correct_answers / current_app. config['LIGHTCOURSE_QUIZ_NUM']) * 100 >= 50: quiz_history = LightQuizHistory(person_id=current_user.id, light_course_id=course.id) quiz_history.insert() flash( notify_success('Great! correct answers: {}/{}'.format( correct_answers, current_app.config['LIGHTCOURSE_QUIZ_NUM']))) else: flash( notify_warning('Oh oh! correct answers: {}/{}'.format( correct_answers, current_app.config['LIGHTCOURSE_QUIZ_NUM']))) return redirect(url_for('lightcourse.take_quiz', course_id=course.id))
def resource_add_photos_check(chapter_id): form = AddPhotosForm() if request.method == 'POST': if form.validate_on_submit(): filename = photos.save(request.files[form.file_input.data.name]) resource = LightResource(type='photo', filename=filename) chapter = LightChapter.query.get(chapter_id) chapter.resources.append(resource) chapter.update() flash(notify_success('Photo file uploaded')) else: flash_errors(form) return redirect(url_for('lightcourse.view_chapter', chapter_id=chapter_id))
def add_homework_check(chapter_id): form = AddHomeworkForm() if request.method == 'POST': if form.validate_on_submit(): filename = docs.save(request.files[form.file_input.data.name]) homework = LightHomework(filename=filename) chapter = LightChapter.query.get(chapter_id) chapter.homeworks.append(homework) chapter.update() flash(notify_success('Homework file uploaded')) else: flash_errors(form) return redirect(url_for('lightcourse.view_chapter', chapter_id=chapter_id))
def resource_add_alldocs_check(chapter_id): form = AddDocsForm() if request.method == 'POST': if form.validate_on_submit(): filename = alldocs.save(request.files[form.file_input.data.name]) # filename = '{}_{}'.format(file_prefix(), filename) resource = LightResource(type='doc', filename=filename) chapter = LightChapter.query.get(chapter_id) chapter.resources.append(resource) chapter.update() flash(notify_success('Document file uploaded')) else: flash_errors(form) return redirect(url_for('lightcourse.view_chapter', chapter_id=chapter_id))
def submit_homework_check(chapter_id): form = SubmitHomeworkForm() if request.method == 'POST': if form.validate_on_submit(): filename = homeworksubmits.save( request.files[form.file_input.data.name]) homework_submit = LightHomeworkSubmission( filename=filename, course_taker_id=current_user.id) chapter = LightChapter.query.get(chapter_id) chapter.homework_submissions.append(homework_submit) chapter.update() flash(notify_success('Homework file submitted for evaluation')) else: flash_errors(form) return redirect(url_for('lightcourse.view_chapter', chapter_id=chapter_id))
def edit_course_name_check(course_id): if request.method == 'POST': course = LightCourse.query.get(course_id) if not (current_user.id == course.teacher_id or current_user.role == 'admin'): return "You don't have permission to edit" course_name = request.form['course_name'] if course_name.strip(): course.name = course_name course.update() flash(notify_success('Course name updated!')) return redirect(url_for('lightcourse.view', course_id=course_id)) else: flash(notify_warning('Course name cannot be empty!')) return redirect(url_for('lightcourse.view', course_id=course_id))
def add_logo_check(): form = AddLogoForm() if request.method == 'POST': if form.validate_on_submit(): filename = photos.save(request.files[form.file_input.data.name]) logo = Setting.query.filter(Setting.name == 'logo').first() os.remove( os.path.join(current_app.config['UPLOADED_PHOTOS_DEST'], logo.value)) logo.value = filename logo.update() flash(notify_success('Logo uploaded')) return redirect(url_for('school.index')) else: flash_errors(form) return redirect(url_for('school.index'))
def subsection_add_homework_check(subsection_id): form = AddHomeworkForm() if request.method == 'POST': if form.validate_on_submit(): subsection = SubSection.query.get(subsection_id) filename = docs.save(request.files[form.homework_doc.data.name]) subsection.homeworks.append(Homework(filename=filename)) subsection.update() # flash(notify_info(form.homework_doc.data.name)) flash(notify_success('Homework file uploaded')) else: flash_errors(form) flash('ERROR! Homework') return redirect( url_for('course.subsection_add_homework', subsection_id=subsection_id))
def add_grade_check(): if request.method == 'POST': context = base_context() form = AddGradeForm() # if form.validate_on_submit(): if not form.validate_on_submit(): flash_errors(form) return redirect(url_for('student.index')) grade = Grade.query.filter(Grade.name == form.name.data).first() if grade: flash(notify_danger('Grade already exists!')) return redirect(url_for('student.index')) grade = Grade(name=form.name.data) grade.insert() flash(notify_success('Added grade {}!'.format(form.name.data))) return redirect(url_for('student.index'))
def info_update_check(): if request.method == 'POST': school_name_input = request.form['school_name'] contact_mail_input = request.form['contact_mail'] school_name = Setting.query.filter( Setting.name == 'school_name').first() school_name.value = school_name_input school_name.update() contact_mail = Setting.query.filter( Setting.name == 'contact_mail').first() contact_mail.value = contact_mail_input contact_mail.update() flash(notify_success('Info updated!')) return redirect(url_for('school.index'))
def subsection_submit_homework_check(subsection_id): form = SubmitHomeworkForm() if request.method == 'POST': if form.validate_on_submit(): subsection = SubSection.query.get(subsection_id) filename = homeworksubmits.save( request.files[form.homework_submit.data.name]) subsection.homework_submissions.append( HomeworkSubmission(filename=filename, course_taker_id=current_user.id)) subsection.update() # flash(notify_info(form.homework_doc.data.name)) flash(notify_success('Homework file submitted!')) else: flash_errors(form) return redirect( url_for('course.view_subsection', subsection_id=subsection_id))
def certificate_request(course_id): if CertificateRequest.query.filter( (CertificateRequest.course_taker_id == current_user.id) & (CertificateRequest.course_id == course_id)).first(): flash(notify_warning('Certificate being processed!')) else: cert_req = CertificateRequest(course_taker_id=current_user.id, course_id=course_id) cert_req.insert() flash(notify_success('Certificate requested!')) course = Course.query.get(course_id) teacher = User.query.get(course.teacher_id) subject = '{}: {} requested certificate'.format( current_app.config['APP_NAME'], current_user.email) body = 'Greetings, <br> User {} ({}) requested certificate for course <br>{}'.format( current_user.name, current_user.email, course.name) send_mail(teacher.email, subject, body) return redirect(url_for('course.view', course_id=course_id))
def add_check(): if request.method == 'POST': context = base_context() form = AddTeacherForm() # if form.validate_on_submit(): if not form.validate_on_submit(): flash_errors(form) return redirect(url_for('teacher.index')) user = User.query.filter(User.email == form.email.data).first() if user: flash(notify_danger('Mail already exists!')) return redirect(url_for('teacher.index')) teacher = User(name=form.name.data, email=form.email.data, role='teacher') teacher.set_hash(current_app.config['DEFAULT_PASS_ALL']) teacher.insert() flash(notify_success('Added {}!'.format(teacher.name))) return redirect(url_for('teacher.index'))
def add_check(grade_id): if request.method == 'POST': context = base_context() form = AddStudentForm() # if form.validate_on_submit(): if not form.validate_on_submit(): flash_errors(form) return redirect(url_for('student.view', grade_id=grade_id)) user = User.query.filter(User.email == form.email.data).first() if user: flash(notify_danger('Mail already exists!')) return redirect(url_for('student.index')) student = User(name=form.name.data, email=form.email.data, role='student') grade = Grade.query.get(grade_id) student.grade = grade student.set_hash(current_app.config['DEFAULT_PASS_ALL']) student.insert() flash(notify_success('Added {}!'.format(form.email.data))) return redirect(url_for('student.view', grade_id=grade_id))