def assign_course(id): """ Assign a student to a course for a given year """ check_admin() course = Course.query.get_or_404(id) # prevent admin from being assigned a course if course.is_admin: abort(403) form = CourseForm(obj=course) if form.validate_on_submit(): course.department = form.department.data course.role = form.role.data course.student = form.student.data course.enrolment = form.enrolment.data db.session.add(course) db.session.commit() flash('You have successfully assigned a student, department and role.') # redirect to the roles page return redirect(url_for('admin.list_courses')) return render_template('admin/courses/course.html', course=course, form=form, title='Assign Course')
def home(): form = CourseForm() if form.validate_on_submit(): course_name = form.cname.data.upper() course_id = form.cid.data course_sec = form.sec.data.upper() email = form.email.data.lower() reserved = form.reserved.data result = checkCourse(course_name, course_id, course_sec) if result is not None: flash('Unable to find course') else: known = None course = Course.query.filter_by(cname=course_name, cid=course_id, sec=course_sec).first() if course is None: course = Course(cname=course_name, cid=course_id, sec=course_sec) db.session.add(course) db.session.flush() user = User(email=email, course_id=course.id, reserved=reserved) db.session.add(user) else: ckey = course.id result = checkUser(email, ckey, reserved) if result != None: known = True else: user = User(email=email, course_id=ckey, reserved=reserved) db.session.add(user) db.session.commit() return redirect(url_for('complete', known=known)) return render_template('home.html', form=form)
def course_details(id): course = Course.query.get(id) form = CourseForm() #:如果当前登录的教师没有教授这门课程,则禁止访问 if current_user.id != course.teacher.id: abort(403) if form.validate_on_submit(): course.time = form.time.data course.location = form.location.data flash(u'修改信息成功') db.session.commit() return redirect(url_for('course_details', id=id)) return render_template('courseDetails.html', form=form, course=course)
def addCourse(): ''' Add a new course to the course dashboard. ''' if 'username' not in login_session: return redirect(url_for('welcome')) form = CourseForm() if form.validate_on_submit(): title = form.title.data user_id = login_session['user_id'] description = form.description.data provider = form.provider.data website = form.website.data #If user uploaded an image file, ensure the filename is secure, #then save the file to the filesystem and create a path #to the file in the database. #http://flask.pocoo.org/docs/0.10/patterns/fileuploads/ file = form.image.data if file: filename = secure_filename(file.filename) if file and allowed_file(filename): newCourse = Course(title = request.form['title'], user_id = login_session['user_id'], description = request.form['description'], provider = request.form['provider'], website = request.form['website']) session.add(newCourse) session.commit() #The purpose of this line is to ensure that newCourse.id is #accessable - need it to name the image file on filesystem #http://stackoverflow.com/questions/1316952/sqlalchemy-flush-and-get-inserted-id session.refresh(newCourse) image_destination = 'static/course_images/%s_%s.%s' % (user_id, newCourse.id, get_file_extension(filename)) file.save(dst = image_destination) newCourse.image = image_destination session.commit() else: newCourse = Course(title = request.form['title'], user_id = login_session['user_id'], description = request.form['description'], provider = request.form['provider'], website = request.form['website'], image = None) session.add(newCourse) session.commit() flash('New course %s added!' % newCourse.title) return redirect(url_for('fullCourseList')) else: return render_template('newcourse.html', form = form)
def create_course(): course_form = CourseForm() course_form.semester_id.choices = [(row['id'], row['name']) for row in db.read_all_semesters()] if course_form.validate_on_submit(): rowcount = db.create_course({ 'designation': course_form.designation.data, 'name': course_form.name.data, 'semester_id': course_form.semester_id.data }) if rowcount == 1: flash('Course {} created'.format(course_form.name.data)) return redirect(url_for('all_courses')) return render_template('courses/add.html', form=course_form)
def editCourse(course_id): ''' Edit a course on the user's dashboard. ''' if 'username' not in login_session: return redirect(url_for('welcome')) editedCourse = session.query(Course).filter_by(id = course_id).one() if editedCourse.user_id != login_session['user_id']: return "<script>function myFunction() {alert('You are not authorized to edit this course. Please create your own course in order to edit.');}</script><body onload='myFunction()''>" form = CourseForm() if form.validate_on_submit(): editedCourse.title = form.title.data editedCourse.user_id = login_session['user_id'] editedCourse.description = form.description.data editedCourse.provider = form.provider.data editedCourse.website = form.website.data #http://flask.pocoo.org/docs/0.10/patterns/fileuploads/ file = form.image.data if file and allowed_file(file.filename): filename = secure_filename(file.filename) session.add(editedCourse) session.commit() #The purpose of this line is to ensure that editedCourse.id is #accessable - need it to name the image file on filesystem #http://stackoverflow.com/questions/1316952/sqlalchemy-flush-and-get-inserted-id session.refresh(editedCourse) image_destination = 'static/course_images/%s_%s.%s' % (editedCourse.user_id, editedCourse.id, get_file_extension(filename)) #If there existed a previous image for the course, delete it from filesystem. if editedCourse.image: os.remove(editedCourse.image) file.save(dst = image_destination) editedCourse.image = image_destination session.add(editedCourse) session.commit() session.add(editedCourse) session.commit() flash('Course successfully edited %s' % editedCourse.title) return redirect(url_for('fullCourseList')) else: #Since course description is TextArea input type, need to #prepopulate it with statement below. form.description.data = editedCourse.description return render_template('editcourse.html', course=editedCourse, time = time.time(), form = form)
def edit_course(name): """ Edit a course """ if not current_user.isTeacher: abort(403) add_course = False course = Course.query.filter_by(courseNums=name).first() form = CourseForm(obj=course) if form.validate_on_submit(): course.name = form.name.data course.description = form.description.data db.session.commit() flash(u'课程信息修改成功') return redirect(url_for('admin.list_courses')) form.description.data = course.description form.name.data = course.name course.courseNums = form.courseNums.data return render_template('admin/courses/course.html', action="Edit", add_course=add_course, form=form, course=course, title="Edit Course", code=course.courseNums)
def add_course(): add_course = True courseNums = ''.join(random.sample(string.ascii_letters+string.digits, 8)) form = CourseForm() if form.validate_on_submit(): course = Course(name=form.name.data, description=form.description.data,teacherName=current_user.name, courseNums=form.courseNums.data) try: # add course to the database db.session.add(course) db.session.commit() flash(u'成功创建一门课程') except: # in case course name already exists flash(u'创建课程失败,可能是选课口令与已存在的课程相同') # redirect to courses page return redirect(url_for('admin.list_courses')) # load course template return render_template('admin/courses/course.html', add_course=add_course, form=form, title="Add Course", code=courseNums)
def enroll(): if not current_user.verified: return redirect(url_for('register',_external=True)) form = CourseForm() if not form.validate_on_submit(): flash("Invalid Course name provided", "warning") return redirect(url_for('home',_external=True)) print(form.data['course']) course = Course.query.filter_by(name=form.data['course']).first() print(course) notif = Exam.query.filter_by(course = course, user= current_user).first() if course and not notif: exam = Exam.make_unique(course,DEFAULT_NOTIF,current_user) db.session.add(exam) db.session.commit() flash("You have successfully registered for this course.", "success") elif not course: flash("Course not found. Please enter the full course code as it appears on ACORN.", "warning") elif notif and course: flash("You are already registered in %s" % course.name, "warning") return redirect(url_for('home',_external=True))
def calendar(): conf = "" #cookies cookies = request.cookies if "courses" in cookies: cs = json.loads(cookies.get("courses")) else: cs = [] if "credits" in cookies: credits = json.loads(cookies.get("credits")) else: credits = 0 for i in cs: credits += data.getCredits(i["crn"]) if "modal" in cookies: m = json.loads(cookies.get("modal")) else: m = "first" modalform = StartForm() form = CourseForm() if form.department.data == None: form.course.choices = data.getDeptCourses('ALL DEPTS') else: form.course.choices = data.getDeptCourses(form.department.data) form_clear = ClearForm() form_remove = RemoveForm() ch = [] #for c in courses: for c in cs: ch.append((c["crn"], (c["title"] + " " + c["sect"]))) form_remove.selcourses.choices = ch error = None #if the form is submitted validly if request.method == 'POST': if form.validate_on_submit(): #reformat the form data into a dictionary of course info course = fmat(form.course.data) #if the course does not conflict with any pre-selected classes, add it to the class schedule conflicts = conflict(course, cs) if len(conflicts) == 0: #courses.append(course) cs.append(course) form_remove.selcourses.choices.append( (course["crn"], (course["title"] + " " + course["sect"]))) credits += data.getCredits(course["crn"]) #if there are one or more conflicts with the current class schedule, do not add it and report conflict else: conlist = ", ".join(conflicts) conf = course["dept"] + " " + course["number"] error = 'The selected class, ' + course["title"] + ' (' + ''.join( course["days"] ) + ', ' + ''.join( course["times"] ) + '),' + ' conflicts with the following courses on your schedule: ' + conlist if form_remove.submit.data: print(form_remove.selcourses.data) for s in form_remove.selcourses.data: print(cs) for i in cs: if i["crn"] == int(s): print("yes") cs.remove(i) form_remove.selcourses.choices.remove( (int(s), (i["title"] + " " + i["sect"]))) credits -= data.getCredits(i["crn"]) if modalform.start.data: if m == "first": m = "yes" else: m = "no" attr = [] for i in cs: attr.extend(data.getAttr(i["crn"])) attr = ", ".join(list(set(attr))) res = make_response( render_template('calendar.html', title='Calendar', modalform=modalform, form=form, cform=form_clear, rform=form_remove, error=error, courses=cs, creds=credits, attributes=attr, conf=conf, m=m)) res.set_cookie("courses", json.dumps(cs)) res.set_cookie("credits", json.dumps(credits)) res.set_cookie("modal", json.dumps(m)) return res