Esempio n. 1
0
def edit_course(id):
    # Get course from db or 404
    course = Course.get_or_404(id)
    # Start with no errors
    errors = None
    # Get categories and schools to populate dropdowns
    categories = Category.get_all()
    schools = School.get_all()

    if request.method == 'POST':
        fields = {
            'name': request.form['name'],
            'url': request.form['url'],
            'school': request.form.get('school', ''),
            'category': request.form.get('category', '')
        }
        # Validations that checks that no fields are blank
        errors = check_no_blanks(fields=fields)
        if not errors:
            # Check that new course name does not exist already for this school
            school_courses = School.get_by_id(fields['school']).courses
            for school_course in school_courses:
                # If name isn't changed, will exist so allow this
                if school_course.name == fields['name'] and \
                        school_course.id != course.id:
                    errors['name_exists'] = True
            if not errors:
                course.edit(name=fields['name'],
                            url=fields['url'],
                            school_id=fields['school'],
                            category_id=fields['category'])
                flash('Course edited')
                return redirect(url_for('view_course', id=course.id))
    # If it is not a POST request, populate input values from database
    else:
        fields = {
            'name': course.name,
            'url': course.url,
            'school': course.school.id,
            'category': course.category.id
        }
    return render_template('edit_course.html',
                           fields=fields,
                           errors=errors,
                           categories=categories,
                           schools=schools)
Esempio n. 2
0
def add_course():
    # Start with no errors and no fields
    errors = None
    fields = None
    # Grab user id. Associates course with user.
    user_id = session['user_id']
    # Get all categories and schools to populate dropdowns
    categories = Category.get_all()
    schools = School.get_all()

    if request.method == 'POST':
        # Grab all form data
        fields = {
            'name': request.form['name'],
            'url': request.form['url'],
            'school': request.form.get('school', ''),
            'category': request.form.get('category', '')
        }
        # Form validation that makes sure no fields are blank
        errors = check_no_blanks(fields=fields)
        if not errors:
            # Check if school already has course by same name
            school_courses = School.get_by_id(fields['school']).courses
            for school_course in school_courses:
                if school_course.name == fields['name']:
                    errors['name_exists'] = True
            # If there are no errors, then create course
            if not errors:
                course = Course.create(name=fields['name'],
                                       url=fields['url'],
                                       school_id=fields['school'],
                                       category_id=fields['category'],
                                       user_id=user_id)
                flash('Course created')
                return redirect(url_for('view_course', id=course.id))
    # If this is a GET request, or there are errors, show form
    return render_template('add_course.html',
                           fields=fields,
                           categories=categories,
                           schools=schools,
                           errors=errors)
Esempio n. 3
0
def schools_json():
    # Get all schools from db
    schools = School.get_all()
    # Create list to store JSON data
    schools_json = []
    for school in schools:
        schools_json.append({
            'id':
            school.id,
            'name':
            school.name,
            'url':
            school.url,
            'courses': [{
                'id': course.id,
                'name': course.name,
                'url': course.url,
                'school': course.school.name,
                'category': course.category.name
            } for course in school.courses]
        })
    return jsonify(schools=schools_json)
Esempio n. 4
0
def view_all_schools():
    # Get all schools
    schools = School.get_all()
    return render_template('view_all_schools.html', schools=schools)