Esempio n. 1
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)
Esempio n. 2
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)