Пример #1
0
def new_review(course_id):
    course = Course.query.get(course_id)
    if not course:
        abort(404)
    user = current_user
    review = Review.query.filter_by(course=course, author=user).first()
    if not review:
        review = Review()
        review.course = course
        review.author = user
        is_new = True
    else:
        is_new = False

    message = ''
    form = ReviewForm(request.form)
    if request.method == 'POST':
        if form.validate_on_submit():
            # check validity of term
            if str(form.term.data) not in course.term_ids:
                abort(404)

            if form.is_mobile.data:
                form.content.data = markdown.markdown(form.content.data)
            form.content.data, mentioned_users = editor_parse_at(form.content.data)
            form.content.data = sanitize(form.content.data)
            form.populate_obj(review)

            if is_new:
                review.add()
                for user in set(current_user.followers + course.followers + course.joined_users):
                    user.notify('review', review, ref_display_class='Course')
                # users can only receive @ notifications for new reviews
                for user in mentioned_users:
                    user.notify('mention', review)
            else:
                review.save()
            return redirect(url_for('course.view_course',course_id=course_id))
        else: # invalid submission, try again
            if form.content.data:
                review.content = sanitize(form.content.data)
            if form.difficulty.data:
                review.difficulty = form.difficulty.data
            if form.homework.data:
                review.homework = form.homework.data
            if form.gain.data:
                review.gain = form.gain.data
            if form.rate.data:
                review.rate = form.rate.data
            message = '提交失败,请编辑后重新提交!'

    polls = [
        {'name': 'difficulty', 'display': '课程难度', 'options': ['简单', '中等', '困难'] },
        {'name': 'homework', 'display': '作业多少', 'options': ['不多', '中等', '超多'] },
        {'name': 'grading', 'display': '给分好坏', 'options': ['超好', '厚道', '杀手'] },
        {'name': 'gain', 'display': '收获多少', 'options': ['很多', '一般', '没有'] },
    ]
    return render_template('new-review.html', form=form, course=course, review=review, polls=polls, message=message, is_new=is_new)
Пример #2
0
 def has_service(self):
     from app.services.models.service import Service
     from app.utils import sanitize
     services = Service.query.all()
     for service in services:
         if service.category_items:
             for cgItem in service.category_items:
                 if sanitize(cgItem) == self.sanitized():
                     return service.published
     return False
Пример #3
0
def form():
    form = CreateSessionForm()
    if form.validate_on_submit():
        tournament = form.tournament.data
        team1 = form.team1.data
        team2 = form.team2.data
        starter = form.starter.data
        time = form.time.data
        url = sanitize(form.tournament.data)
        session['tournament'] = tournament
        session['team1'] = team1
        session['team2'] = team2
        if not db.session.query(Tournament).filter(Tournament.url == url).count():
            curr = Tournament(url, tournament, team1, team2, starter, time)
            db.session.add(curr)
            db.session.commit()
        return redirect(url_for('branch', page=url))
    return render_template("form.html", title="Create New Session", form=form)
Пример #4
0
def edit_course(course_id=None):
    if course_id:
        course = Course.query.get(course_id)
    else:
        course = Course()
    if not course:
        abort(404)
    latest_term = course.latest_term
    if not latest_term:
        abort(404)
    course_form = CourseForm(formdata=request.form, obj=course)
    if course_form.validate_on_submit():
        course_form.introduction.data = sanitize(course_form.introduction.data)
        course_form.populate_obj(latest_term)
        if not latest_term.homepage.startswith('http'):
            latest_term.homepage = 'http://' + course.homepage
        latest_term.save()
        db.session.commit()
        return redirect(url_for('course.view_course', course_id=course.id))
    return render_template('course-edit.html', form=course_form, course=course)
Пример #5
0
def edit_course(course_id=None):
    if not current_user.is_admin:
        abort(403)
    if course_id:
        course = Course.query.get(course_id)
    else:
        course = Course()
    if not course:
        abort(404)
    latest_term = course.latest_term
    if not latest_term:
        abort(404)
    course_form = CourseForm(request.form, course)
    if course_form.validate_on_submit():
        course_form.introduction.data = sanitize(course_form.introduction.data)
        course_form.populate_obj(latest_term)
        if not latest_term.homepage.startswith("http"):
            latest_term.homepage = "http://" + course.homepage
        latest_term.save()
        db.session.commit()
        return redirect(url_for("course.view_course", course_id=course.id))
    return render_template("course-edit.html", form=course_form, course=course)