Esempio n. 1
0
def add_department():
    check_admin()
    add_department = True
    form = DepartmentForm()
    if form.validate_on_submit():
        department = Department(name=form.name.data,
                                description=form.description.data)
        try:
            # add department to the database
            db.session.add(department)
            db.session.commit()
            flash('Đã thêm phòng ban thành công.')
        except:
            # in case department name already exists
            flash('Lỗi: Tên phòng ban đã tồn tại.')

        # redirect to departments page
        return redirect(url_for('admin.list_departments'))

    # load department template
    return render_template('admin/departments/department.html',
                           action="add",
                           add_department=add_department,
                           form=form,
                           title="Thêm phòng ban")
Esempio n. 2
0
def edit_department(id):
    """
    Edit a department
    """
    check_admin()

    add_department = False

    department = Department.query.get_or_404(id)
    form = DepartmentForm(obj=department)
    if form.validate_on_submit():
        department.name = form.name.data
        department.description = form.description.data
        db.session.commit()
        flash('Đã sửa phòng ban thành công.')

        # redirect to the departments page
        return redirect(url_for('admin.list_departments'))

    form.description.data = department.description
    form.name.data = department.name
    return render_template('admin/departments/department.html',
                           action="add",
                           add_department=add_department,
                           form=form,
                           department=department,
                           title="Sửa phòng ban")
Esempio n. 3
0
def add_department():
    """
    Add a department to the database
    """
    check_admin()
    add_department = True

    form = DepartmentForm()
    if form.validate_on_submit():
        department = Department(name=form.name.data,
                                description=form.description.data)
        try:
            # add department to the database
            db.session.add(department)
            db.session.commit()
            flash('You have successfully added a new department.')
        except:
            # in case department name already exists
            flash('Error: department name already exists.')

        # redirect to departments page
        return redirect(url_for('admin.list_departments'))

    # load department template
    return render_template('admin/departments/department.html',
                           action="Add",
                           add_department=add_department,
                           form=form,
                           title="Add Department")
Esempio n. 4
0
def edit_department(id):
    """
    Edit a department
    """

    check_admin()

    add_department = False

    department = Department.query.get_or_404(id)
    form = DepartmentForm(obj=department)
    if form.validate_on_submit():
        department.name = form.name.data
        department.description = form.description.data
        db.session.commit()
        flash('You have successfully edited the department.')

        # redirect to the departments page
        return redirect(url_for('admin.list_departments'))

    form.description.data = department.description
    form.name.data = department.name

    context = {
        'add_department': add_department,
        'form': form,
        'department': department,
    }
    return render_template('admin/departments/department.html', action='Edit', add_department=add_department, form=form,
                           department=department, title='Edit Department')
Esempio n. 5
0
def edit_department(department_id):
    '''
    Edit a department

    '''

    #  Throws a 403 Forbidden error if a non-admin user attempts to access these views.
    check_admin()

    add_department = False

    department = Department.query.get_or_404(department_id)
    logger.info(f'Id: {department.department_id} Department edited from {department.name}')
    form = DepartmentForm(obj=department)

    if form.validate_on_submit():
        department.name = form.name.data
        # department.description = form.description.data  # ToDo description later
        db.session.commit()
        logger.info(f'Id: {department.department_id} Department edited to {department.name}')
        flash('You have successfully edited the department.')

        return redirect(url_for('admin.list_departments'))

    form.name.data = department.name
    # form.description.data = department.description #Todo later

    return render_template('admin/departments/department.html', action="Edit",
                           add_department=add_department, form=form,
                           department=department, title="Edit Department")
Esempio n. 6
0
def add_department():
    """
    Add a department to the database
    """
    #  Throws a 403 Forbidden error if a non-admin user attempts to access these views.
    check_admin()

    add_department = True

    form = DepartmentForm()
    if form.validate_on_submit():

        department = Department(name=form.name.data)  # Todo add a  description parameter

        try:
            # add a department to the database
            db.session.add(department)
            db.session.commit()

            logger.info(f'Department {department.name} has been added')

            flash('The new department have been successfully added !')
        except exc.IntegrityError:
            logger.exception('Add department exc.IntegrityError')
            flash('Error: department name already exists.')

        return redirect(url_for('admin.list_departments'))

    return render_template('admin/departments/department.html', action='Add',
                           add_department=add_department,
                           form=form,
                           title='Add Department')
Esempio n. 7
0
def create_department():
    form = DepartmentForm()
    if form.validate_on_submit():
        department = Department(name=form.name.data,
                                description=form.description.data)

        db.session.add(department)
        db.session.commit()

        flash('Department created succesfully')

        return redirect(url_for('admin.departments'))

    return render_template('admin/create_department.html',
                           title='Create Departments',
                           form=form)
Esempio n. 8
0
 def test_validate_success_department_form(self):
     form = DepartmentForm(name='Test', csrf_enabled=False)
     self.assertTrue(form.validate())