Example #1
0
def save_station(session, data):
    """
    Add or update a station in the DB.
    """
    # If this station is set to "start", we must remove that flag from all other
    # stations. We'll just set them all to False, then update the given station
    # with True. This ensures that we only have one starting station.
    if data.get('is_start'):
        session.execute(update(Station).values(is_start=False))

    if data.get('is_end'):
        session.execute(update(Station).values(is_end=False))

    # Ensure we don't have duplicate values for the "order" field
    same_order = session.query(Station).filter(and_(
        Station.order == data['order'],
        Station.id != data['id'])).first()
    while same_order:  # As long as we have a matching entry, increment by 1
        data['order'] += 1
        same_order = session.query(Station).filter(and_(
            Station.order == data['order'],
            Station.id != data['id'])).first()

    station = Station(
        name=data['name'],
        contact=data['contact'],
        phone=data['phone'],
    )
    station.id = data.get('id')
    station.order = data['order']
    station.is_start = data['is_start']
    station.is_end = data['is_end']
    merged = session.merge(station)
    DB.session.commit()
    return merged
Example #2
0
def add_station(stat_name, contact, phone, order, session):
    """
    Creates a new :py:class:`Station` in the database.
    """
    new_station = Station(stat_name, contact, phone)
    new_station.order = order
    session.add(new_station)
    return u"Station {0} added. Contact: {1} / {2}".format(
        stat_name, contact, phone)
Example #3
0
def get_dashboard(station):
    """
    Retrieves dashboard information for a given station. The dashboard contains
    the states for each group at that station, and incoming groups from
    neighbouring stations.
    """
    station = Station.by_name_or_id(station)

    neighbours = station.neighbours
    main_states = GroupStation.by_station(station)

    # Add missing groups to the main states because the UI should list them all.
    # Even if no scores have been reported yet.
    missing_groups = Group.query.filter(~Group.id.in_([
        row.group_id for row in main_states]))
    main_states.extend([GroupStation(group.id, station.id)
                        for group in missing_groups])

    # remove states from cancelled/abandoned/invalid groups
    main_states = [state for state in main_states
                   if (state.group and
                       not state.group.cancelled and
                       state.group.start_time not in (None, 'None'))]

    time_threshold = datetime.now(
        timezone('Europe/Luxembourg')) - timedelta(minutes=45)
    before_states = sorted(
        GroupStation.by_station(neighbours['before']),
        key=_dashboard_order)
    before_states = [state for state in before_states
                     if state.updated > time_threshold]
    after_states = sorted(
        GroupStation.by_station(neighbours['after']),
        key=_dashboard_order)
    after_states = [state for state in after_states
                    if state.updated > time_threshold]
    main_states = sorted(main_states, key=_main_dashboard_order)
    finished_groups = {value.group_id for value in main_states
                       if value.state == STATE_FINISHED}
    before_states = [value for value in before_states
                     if value.group_id not in finished_groups]
    after_states = [value for value in after_states
                    if value.group_id not in finished_groups]
    output = dict(
        station=station,
        neighbours=neighbours,
        main_states=main_states,
        before_states=before_states,
        after_states=after_states,
    )
    return output