Exemple #1
0
def edit_subject():
    form = SubjectForm()

    if request.method == 'GET':

        subject_faculty, subject_name = request.args.get('subj_faculty'), \
                                                                     request.args.get('subj_name')
        db = PostgresDb()

        # -------------------------------------------------------------------- filter for "and" google
        subject = db.sqlalchemy_session.query(Subject).filter(
            Subject.subj_faculty == subject_faculty,
            Subject.subj_name == subject_name).one()

        # fill form and send to discipline
        form.subject_faculty.data = subject.subj_faculty
        form.subject_name.data = subject.subj_name
        form.subject_hours.data = subject.subj_hours

        return render_template('subject_form.html',
                               form=form,
                               form_name="Edit subject",
                               action="edit_subject")

    else:

        if not form.validate():
            return render_template('subject_form.html',
                                   form=form,
                                   form_name="Edit subject",
                                   action="edit_subject")
        else:
            db = PostgresDb()
            # find discipline
            subject = db.sqlalchemy_session.query(Subject).filter(
                Subject.subj_faculty == form.subject_faculty.data,
                Subject.subj_name == form.subject_name.data).one()

            # update fields from form data
            subject.subj_faculty = form.subject_faculty.data
            subject.subj_name = form.subject_name.data
            subject.subj_hours = form.subject_hours.data

            db.sqlalchemy_session.commit()

            return redirect(url_for('index_subject'))
Exemple #2
0
def new_subject():
    form = SubjectForm()

    if request.method == 'POST':
        if not form.validate():
            return render_template('subject_form.html', form=form, form_name="New subject",
                                   action="new_subject")
        else:
            subject_obj = Subject(
                subj_name=form.subject_name.data,
                subj_faculty=form.subject_faculty.data,
                subj_hours=form.subject_hours.data)

            db = PostgresDb()
            db.sqlalchemy_session.add(subject_obj)
            db.sqlalchemy_session.commit()

            return redirect(url_for('index_subject'))

    return render_template('subject_form.html', form=form, form_name="New subject", action="new_subject")