Example #1
0
def logout():
    if current_user.role == 'driver':
        Drivers.get_by_identity(current_user.name).stop_work('stop')

    logout_user()
    flash('You have been logged out.', 'success')
    return redirect(url_for('user.login'))
Example #2
0
def login():
    form = LoginForm(next_page=request.args.get('next'))

    if form.validate_on_submit():
        u = User.find_by_identity(request.form.get('identity'))
        if u and u.authenticate(request.form.get('password')):
            if login_user(u, remember=True):
                next_url = request.form.get('next')
                role = u.role.value

                if next_url:
                    return _redirect(role, next_url)

                if role == 'Manager':
                    return redirect(url_for('manager.index'))
                else:
                    Drivers.get_by_identity(u.name).ready('ready')
                    return redirect(url_for('driver.index'))
        else:
            flash('Username or password does not match', 'error')

    payload = {
        'title': 'Login',
        'page_title': 'Welcome',
        'form': form,
        'page_color': 'teal darken-2'
    }

    return render_template('user/login.html', **payload)
Example #3
0
    def test_drivers_get_working(self):
        url = api.url_for(DriversCtrl)

        d1 = Drivers.get_by_identity('Stan Mohan')
        d2 = Drivers.get_by_identity('John Smith')
        d1.ready()
        d2.ready()

        response = self.client.get(url)
        data = dict(json.loads(response.data.decode('utf-8')))

        assert 'drivers' in data.keys()
        assert len(data['drivers']) == 2
Example #4
0
def db(app):
    """
    Setup database. This only gets executed once per session
    :param app: PyTest Fixture
    :return: SQLAlchemy database session
    """

    _db.drop_all()
    _db.create_all()

    # Add users
    for u in U_all:
        _db.session.add(User(**u))

    # Add driver
    for d in D_drivers:
        _db.session.add(Drivers(**d))

    # Add tasks
    for t in T_tasks:
        _db.session.add(Tasks(**t))

    for f in F_flights:
        _db.session.add(Flights(**f))

    _db.session.commit()

    return _db
Example #5
0
def drivers(db):
    yield db

    db.session.query(Drivers).delete()

    for d in D_drivers:
        db.session.add(Drivers(**d))

    try:
        db.session.commit()
    except:
        db.session.rollback()
Example #6
0
def _seed_users_and_workers(_db, employees: list):
    count = 0
    for e in ProgressEnumerate(employees):
        if not User.find_by_identity(e['username']):
            _db.session.add(User(**e))

        if e['role'] == 'driver' and not Drivers.get_by_identity(e['name']):
            _db.session.add(Drivers(name_=e['name']))

        if count % 250 == 0:
            _db.session.commit()
        count += 1

    try:
        print("Committing data. This may take a while..")
        _db.session.commit()
    except SQLAlchemyError as e:
        _db.session.rollback()
        print("ERROR: Mass commit failed!!!! ",
              e,
              sep='\n',
              end='\n',
              file=sys.stderr)
Example #7
0
def clear_route():
    tasks = (Tasks.query
             .filter((Tasks.flight_time <= now()) &
                     (Tasks.status != Choice('done', 'Done')))
             .all())

    drivers = Drivers.get_all_drivers_names()
    for task in tasks:
        if task.driver is None:
            task.driver = rng.choice(drivers)
        if task.task_start_time is None:
            task.task_start_time = now()
        task.completed_time = now()
        task.status = Choice('done', 'Done')
        task.task_time_taken = -1
        db.session.add(task)

    tasks = (Tasks.query
             .filter(
        (
            (Tasks.ready_time >= now()) &
            (Tasks.status != Choice('ready', 'Ready'))
        ) |
        (
            (Tasks.flight_time >= now()) &
            (Tasks.ready_time <= now())
        )
    )
             .all())

    for task in tasks:
        task.status = Choice('ready', 'Ready')
        task.driver = None
        task.task_start_time = None
        db.session.add(task)

    db.session.commit()

    flash("Clearing is successful", "success")

    return redirect(url_for('.login'))
Example #8
0
def clear_before_now():
    """
    Set all tasks whose flight time <= now as done"
    """
    print("Setting all tasks whose flight time <= now as done")
    tasks = (
        Tasks.query.filter((Tasks.flight_time <= now())
                           & (Tasks.status != Choice('done', 'Done'))).all())
    print("Total tasks: ", len(tasks))

    drivers = Drivers.get_all_drivers_names()
    for task in ProgressEnumerate(tasks):
        if task.driver is None:
            task.driver = rng.choice(drivers)
        if task.task_start_time is None:
            task.task_start_time = now()
        task.completed_time = now()
        task.status = Choice('done', 'Done')
        task.task_time_taken = -1
        db.session.add(task)

    db.session.commit()
 def test_get_all_drivers_names(self):
     drivers = Drivers.get_all_drivers_names()
     assert len(drivers) == len(D_drivers)