예제 #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)
예제 #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)
예제 #3
0
def add_category():
    # Start with no errors or fields
    errors = None
    fields = None
    # Store user id from session to associate category with user
    user_id = session['user_id']

    if request.method == 'POST':
        fields = {'name': request.form['name']}
        # Validate form submission by checking no empty fields
        errors = check_no_blanks(fields=fields)
        if not errors:
            # Check that category name does not already exist
            # TODO: this check needs to check by case insensitive
            if Category.get_by_name(fields['name']):
                errors['name_exists'] = True
            else:
                category = Category.create(name=fields['name'],
                                           user_id=user_id)
                flash('Category created')
                return redirect(url_for('view_category', id=category.id))
    return render_template('add_category.html', fields=fields, errors=errors)
예제 #4
0
def edit_category(id):
    # Retrieve school from db or 404
    category = Category.get_or_404(id)
    # Start with no errors
    errors = None
    if request.method == 'POST':
        fields = {'name': request.form['name']}
        # Validations that check that no fields are empty
        errors = check_no_blanks(fields=fields)
        if not errors:
            # Check that category name does not match other category names,
            # except if it is the same instance
            if (Category.get_by_name(fields['name']) and
                    Category.get_by_name(fields['name']).id != category.id):
                errors['name_exists'] = True
            else:
                category.edit(name=fields['name'])
                flash('Category edited')
                return redirect(url_for('view_category', id=category.id))
    # If it is not a POST rquest, populate field values from database
    else:
        fields = {'name': category.name}
    return render_template('edit_category.html', fields=fields, errors=errors)
예제 #5
0
def edit_school(id):
    # Get school from db or 404
    school = School.get_or_404(id)
    # Start with no errors
    errors = None
    if request.method == 'POST':
        fields = {'name': request.form['name'], 'url': request.form['url']}
        # Validations that check that no fields are empty
        errors = check_no_blanks(fields=fields)
        if not errors:
            # Check that school name does not match other school names,
            # except if it is the same instance
            if (School.get_by_name(fields['name'])
                    and School.get_by_name(fields['name']).id != school.id):
                errors['name_exists'] = True
            if not errors:
                school.edit(name=fields['name'], url=fields['url'])
                flash('School edited')
                return redirect(url_for('view_school', id=school.id))
    # If it is not a POST rquest, populate field values from database
    else:
        fields = {'name': school.name, 'url': school.url}
    return render_template('edit_school.html', fields=fields, errors=errors)
예제 #6
0
def add_school():
    # Start with no errors and no fields
    errors = None
    fields = None
    # Store user id from session to associate school with user
    user_id = session['user_id']

    if request.method == 'POST':
        fields = {'name': request.form['name'], 'url': request.form['url']}
        # Validate form submission by checking no empty fields
        errors = check_no_blanks(fields=fields)
        if not errors:
            # Check that school name does not already exist
            # TODO: this check needs to check by case insensitive
            if School.get_by_name(fields['name']):
                errors['name_exists'] = True
            else:
                school = School.create(name=fields['name'],
                                       url=fields['url'],
                                       user_id=user_id)
                flash('School created')
                return redirect(url_for('view_school', id=school.id))
    return render_template('add_school.html', fields=fields, errors=errors)