Exemplo n.º 1
0
def ModifyDepartment(dep_id):
    form = DepartmentForm()
    if form.validate_on_submit():
        department_name = form.department_name.data.strip()
        if dep_id is None:
            department = Department.query.filter(
                Department.name == department_name).first()
            if form.create.data is True:
                if department is not None:
                    flash('Отдел с таким названием уже существует.')
                else:
                    department = Department(name=department_name)
                    db.session.add(department)
                    db.session.commit()
                    flash('Отдел успешно создан.')
            elif form.delete.data is True:
                if department is None:
                    flash('Такого отдела не существует.')
                else:
                    db.session.delete(department)
                    db.session.commit()
                    flash('Отдел успешно удалён.')
        else:
            department = Department.query.filter(
                Department.id == dep_id).first()
            if department is None:
                flash('Такого отдела не существует.')
            else:
                department.name = department_name
                db.session.commit()
                flash('Отдел успешно изменён.')
    else:
        for error in form.department_name.errors:
            flash(error)
    return redirect(url_for('main.ShowUsers'))
Exemplo n.º 2
0
def import_data(request):
    rb = xlrd.open_workbook('D:\pydata\prepod_goda.xlsx')
    sheet = rb.sheet_by_index(0)
    ins = Institute.objects.all()[0]
    n = sheet.nrows
    s = 'sdads'
    for rownum in range(sheet.nrows):
        row = sheet.row_values(rownum)
        d = row[0].strip()
        t = row[1].strip()
        g = row[2].strip()
        dep = Department()
        tech = Teacher()
        group = Group()

        db_d = Department.objects.filter(name=d)
        if (db_d.count() == 0):
            dep.name = d
            dep.institute = ins
            dep.save()
        else:
            dep = db_d[0]

        db_t = Teacher.objects.filter(last_name=t)
        if (db_t.count() == 0):
            tech.last_name = t
            tech.department = dep
            tech.save()
        else:
            tech = db_t[0]

        db_g = Group.objects.filter(name=g)
        if (db_g.count() == 0):
            group.name = g
            group.department = dep
            group.save()
        else:
            group = db_g[0]

        group.teachers.add(tech)
        group.save()

    return HttpResponse("OK " + s)