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 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 check_login (): if request.method == 'POST': form = LoginForm() #message = ' '.join([form.email.data, form.password.data]) #return render_template('auth/debug.html', message=message) #''' if form.validate_on_submit(): user = User.query.filter( User.email == form.email.data ).first() if user is None or not user.check_hash(form.password.data): flash(notify_warning("please check your user id and password")) return redirect(url_for("auth.login")) login_user(user) if user.role == 'admin': return redirect(url_for("teacher.index")) elif user.role == 'teacher': return redirect(url_for("course.index")) else: return redirect(url_for("course.list")) else: return render_template('auth/login.html', form=form) #''' else: return 'Non browsable mode'
def unsubscribe(course_id): course = LightCourse.query.get(course_id) flash(notify_warning('Unsubscribed from {}!'.format(course.name))) current_user.light_courses.remove(course) current_user.update() return redirect(url_for('course.mycourses'))
def flash_errors(form): """Flashes form errors""" for field, errors in form.errors.items(): for error in errors: error_msg = u"Error in the %s field - %s" % ( getattr(form, field).label.text, error ) flash(notify_warning(error_msg))
def approve_certif_req(certif_req_id): certif_request = CertificateRequest.query.get(certif_req_id) course = Course.query.get(certif_request.course_id) if not course: flash(notify_warning('Course no longer exist')) certif_request.delete() return redirect(url_for('course.view_certificate_request')) if not (current_user.id == course.teacher_id or current_user.role == 'admin'): return "You don't have permission to approve" course_taker_id = certif_request.course_taker_id course_id = certif_request.course_id certif_request.delete() certif = Certificate(course_taker_id=course_taker_id, course_id=course_id) person = User.query.get(course_taker_id) course = Course.query.get(course_id) person_name = person.name.replace(' ', '_').replace('-', '_') course_name = course.name.replace(' ', '_') #flash(notify_info('{}'.format(os.getcwd()))) try: dirname = current_app.config['UPLOAD_CERTIFICATES_FOLDER'] try: os.mkdir(dirname) except: pass filename = "{}_{}.pdf".format(person_name, course_name) fname = os.path.join(dirname, filename) # flash(notify_info(fname)) #with open(fname, 'w+') as f: # f.write('') c = canvas.Canvas(fname) c.setTitle("Certificate") init_y = 250 c.drawString(100, init_y, "Certificate Awarded To") init_y -= 20 c.drawString(100, init_y, person.name) init_y -= 20 c.drawString(100, init_y, "For") init_y -= 20 c.drawString(100, init_y, course.name) init_y -= 20 c.drawString(100, init_y, "On") init_y -= 20 datetime_now = datetime.datetime.now() datenow = '{} - {} - {}'.format(datetime_now.year, datetime_now.month, datetime_now.day) c.drawString(100, init_y, datenow) c.save() certif.filename = filename certif.insert() except Exception as e: flash(notify_danger('{}'.format(e))) return redirect(url_for('course.view_certificate_request'))
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 decline_certif_req(certif_req_id): certif_request = LightCertificateRequest.query.get(certif_req_id) course_taker_id = certif_request.course_taker_id course_id = certif_request.course_id course = lightCourse.query.get(course_id) if not course: flash(notify_warning('Course no longer exist')) certif_request.delete() return redirect(url_for('course.view_certificate_request')) if not (current_user.id == course.teacher_id or current_user.role == 'admin'): return "You don't have permission to approve" certif_request.delete() return redirect(url_for('course.view_certificate_request'))
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 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))
class Config: """Parent configuration class.""" APP_NAME = 'Maurilearn' DEBUG = False # "mysql://*****:*****@localhost/db_name" # pip install pymysqldb SQLALCHEMY_DATABASE_URI = '' # we override it below SQLALCHEMY_TRACK_MODIFICATIONS = False SECRET_KEY = "qow32ijjdkc756osk5dmck" # Need a generator LOGIN_VIEW = "auth.login" LOGIN_MESSAGE = notify_warning("Please login for access") MAX_CONTENT_LENGTH = 16 * 1024 * 1024 # 16MB BASE_DIR = base_path STATIC_DIR = os.path.join(BASE_DIR, 'static') UPLOAD_VIDEO_FOLDER = os.path.join(STATIC_DIR, 'uploads/video') UPLOAD_CERTIFICATES_FOLDER = os.path.join(STATIC_DIR, 'certificates') UPLOADED_PHOTOS_DEST = os.path.join(STATIC_DIR, 'uploads/img') UPLOADED_PHOTOS_ALLOW = ('png', 'jpg', 'jpeg') UPLOADED_DOCS_DEST = os.path.join( STATIC_DIR, 'uploads/homeworks') # must change to homework UPLOADED_DOCS_ALLOW = ('pdf', 'docx', 'odt') UPLOADED_ALLDOCS_DEST = os.path.join(STATIC_DIR, 'uploads/alldocs') UPLOADED_ALLDOCS_ALLOW = ('rtf', 'odf', 'ods', 'gnumeric', 'abw', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pdf') UPLOADED_HOMEWORKSUBMITS_DEST = os.path.join(STATIC_DIR, 'uploads/homework_submits') UPLOADED_HOMEWORKSUBMITS_ALLOW = ('pdf', 'docx', 'odt') SMTP_HOST = '' SMTP_PORT = 458 SMTP_MAIN_MAIL = '' SMTP_PASS = '' DEFAULT_PASS_ALL = 'pass' LIGHTCOURSE_QUIZ_NUM = 2 SCHOOL_DEFAULTS = os.path.join(STATIC_DIR, 'default')
def approve_certif_req(certif_req_id): certif_request = LightCertificateRequest.query.get(certif_req_id) course = LightCourse.query.get(certif_request.course_id) if not course: flash(notify_warning('Course no longer exist')) certif_request.delete() return redirect(url_for('course.view_certificate_request')) if not (current_user.id == course.teacher_id or current_user.role == 'admin'): return "You don't have permission to approve" course_taker_id = certif_request.course_taker_id course_id = certif_request.course_id certif_request.delete() certif = LightCertificate(course_taker_id=course_taker_id, course_id=course_id) person = User.query.get(course_taker_id) course = LightCourse.query.get(course_id) person_name = person.name.replace(' ', '_').replace('-', '_') course_name = course.name.replace(' ', '_') #flash(notify_info('{}'.format(os.getcwd()))) try: dirname = current_app.config['UPLOAD_CERTIFICATES_FOLDER'] try: os.mkdir(dirname) except: pass filename = "{}_{}.pdf".format(person_name, course_name) fname = os.path.join(dirname, filename) # flash(notify_info(fname)) #with open(fname, 'w+') as f: # f.write('') create_certificate(fname, person.name, course.name) certif.filename = filename certif.insert() except Exception as e: flash(notify_danger('{}'.format(e))) return redirect(url_for('course.view_certificate_request'))