def tetres_route_opposite_route(): route_id = request.form.get('id') da = TTRouteDataAccess() ttri = da.get_by_id(route_id) da.close_session() route_setup(ttri.route) opposite_route = route.opposite_route(ttri.route) if not isinstance(opposite_route, Route): return prot.response_fail( 'fail to load_data route configuration file') return prot.response_success(opposite_route)
def _wz_insert_from_route(wzi, route_json): """ :type wzi: WorkZoneInfo :type route_json: str """ route1 = json2route(route_json) route2 = route.opposite_route(route1) if not isinstance(route2, Route): return prot.response_fail( 'fail to load_data route configuration file') cfg2 = route1.cfg.clone() route2.cfg = rc.route_config.reverse(cfg2) wzi.route1 = route1 wzi.route2 = route2 return _wz_insert_from_wz(wzi)
def tetres_snowroute_insert(): route_json = request.form.get('data') info = json2snri(route_json) snowRouteDA = SnowRouteDataAccess() ex = snowRouteDA.get_by_name(info.name) if ex: snowRouteDA.close_session() return prot.response_fail('already exist') route2 = route.opposite_route(info.route1) cfg2 = info.route1.cfg.clone() rc.route_config.reverse(cfg2) route2.cfg = cfg2 info.route2 = route2 info.route1.name = 'route1 - %s' % info.route1.rnodes[0].corridor.name info.route1.desc = '' info.route2.name = 'route2 - %s' % info.route2.rnodes[0].corridor.name info.route2.desc = '' snrm = snowRouteDA.insert(info) if not snrm: snowRouteDA.close_session() return prot.response_fail('Fail to insert data') snowRouteDA.commit() tetresApi.add_actionlog(ActionLogDataAccess.INSERT, snowRouteDA.get_tablename(), snrm.id, ActionLogDataAccess.data_description( ActionLogDataAccess.DT_SNOWROUTE, snrm), handled=False, dbsession=snowRouteDA.get_session()) inserted_id = snrm.id snowRouteDA.close_session() return prot.response_success(obj=inserted_id)
def json2snri(json_str): """ convert json to `pyticas_tetres.types.SnowRouteInfo` object from TICAS client :type json_str: str :rtype: SnowRouteInfo """ info = json.loads(json_str) if info.route1 and not hasattr(info.route1, 'cfg'): info.route1.cfg = None route_setup(info.route1) route2 = route.opposite_route(info.route1) cfg2 = info.route1.cfg.clone() rc.route_config.reverse(cfg2) route2.cfg = cfg2 info.route2 = route2 info.route1.name = 'route1 - %s' % info.route1.rnodes[0].corridor.name info.route1.desc = '' info.route2.name = 'route2 - %s' % info.route2.rnodes[0].corridor.name info.route2.desc = '' return info
def tetres_workzone_update(): wz_id = request.form.get('id') wz_json = request.form.get('data') wzDA = WorkZoneDataAccess() exWZObj = wzDA.get_by_id(wz_id) if not exWZObj: wzDA.close_session() return prot.response_invalid_request() info = json2wzi(wz_json) route2 = route.opposite_route(info.route1) cfg2 = info.route1.cfg.clone() rc.route_config.reverse(cfg2) route2.cfg = cfg2 info.route2 = route2 info.route1.name = 'route1 - %s' % info.route1.rnodes[0].corridor.name info.route1.desc = '' info.route2.name = 'route2 - %s' % info.route2.rnodes[0].corridor.name info.route2.desc = '' if not isinstance(info.route2, Route): wzDA.close_session() return prot.response_fail( 'fail to load_data route configuration file') wzgDA = WZGroupDataAccess(session=wzDA.get_session()) is_updated = wzDA.update(wz_id, info.get_dict()) if not is_updated or not wzDA.commit(): wzDA.rollback() wzDA.close_session() return prot.response_fail('fail to update database (1)') is_updated = wzgDA.update_years(exWZObj.wz_group_id) if not is_updated or not wzgDA.commit(): wzgDA.rollback() wzgDA.close_session() return prot.response_fail('fail to update database (2)') updatedWZObj = wzDA.get_by_id(wz_id) inserted = _wz_insert_feature(updatedWZObj) if not inserted: wzDA.close_session() return prot.response_fail('fail to update database (3)') # commit here # if not wzDA.commit(): # return prot.response_fail('fail to update database (4)') tetres_api.add_actionlog(ActionLogDataAccess.UPDATE, wzDA.get_tablename(), wz_id, ActionLogDataAccess.data_description( ActionLogDataAccess.DT_WORKZONE, updatedWZObj), handled=_should_be_set_as_handled( exWZObj, updatedWZObj), dbsession=wzDA.get_session()) wzDA.close_session() return prot.response_success(wz_id)
def test_data(): import datetime from pyticas import route from pyticas_tetres.ttypes import WorkZoneInfo, WorkZoneGroupInfo, TTRouteInfo, SpecialEventInfo from pyticas_tetres.da.wz import WorkZoneDataAccess from pyticas_tetres.da.wz_group import WZGroupDataAccess from pyticas_tetres.da.specialevent import SpecialEventDataAccess from pyticas_tetres.da.route import TTRouteDataAccess # route data r1 = route.create_route('S38', 'S40', name='Route I-35W NB') # I-35W NB r2 = route.create_route('S186', 'S188', name='Route I-494 WB') # I-494 WB r3 = route.create_route('S428', 'S430', name='Route US-169 NB') # US169 NB ri1 = TTRouteInfo(r1) ri2 = TTRouteInfo(r2) ri3 = TTRouteInfo(r3) dsRoute = TTRouteDataAccess() dsWZ = WorkZoneDataAccess(session=dsRoute.da_base.session) dsWZGroup = WZGroupDataAccess(session=dsRoute.da_base.session) dsSE = SpecialEventDataAccess(session=dsRoute.da_base.session) rm = dsRoute.insert(ri1) rm = dsRoute.insert(ri2) rm = dsRoute.insert(ri3) # workzone data def _wzg(idx, r1, r2, y1, y2): wgi = WorkZoneGroupInfo() wgi.name = 'test wz group %d' % idx wgi.desc = 'test is test wz group %d' % idx wgi.years = WorkZoneGroupInfo.years_string(y1, y2) wgi.corridors = WorkZoneGroupInfo.corridor_string([r1, r2]) return wgi def _wzi(idx, r1, r2, y1, y2, wgid): wi = WorkZoneInfo() wi.wz_group_id = wgid wi.route1 = r1 wi.route2 = r2 wi.memo = 'memo of test wz %d' % idx sdt = datetime.datetime(y1, 3, 2, 1, 0, 0) edt = datetime.datetime(y2, 7, 2, 1, 0, 0) wi.start_time = sdt.strftime('%Y-%m-%d %H:%M:%S') wi.end_time = edt.strftime('%Y-%m-%d %H:%M:%S') wi.years = WorkZoneInfo.years_string(y1, y2) return wi r1 = route.create_route('S38', 'S40', name='Route I-35W NB') # I-35W NB r2 = route.opposite_route(r1) r3 = route.create_route('S186', 'S188', name='Route I-494 WB') # I-494 WB r4 = route.opposite_route(r3) wgi1 = _wzg(1, r1, r2, 2012, 2012) wgi2 = _wzg(2, r3, r4, 2012, 2013) wgi3 = _wzg(3, r1, r2, 2014, 2016) ac = True wgm1 = dsWZGroup.insert(wgi1) wgm2 = dsWZGroup.insert(wgi2) wgm3 = dsWZGroup.insert(wgi3) wi1 = _wzi(1, r1, r2, 2012, 2012, wgm1.id) wi2 = _wzi(2, r3, r4, 2012, 2013, wgm2.id) wi3 = _wzi(3, r1, r2, 2014, 2016, wgm3.id) wm1 = dsWZ.insert(wi1) wm2 = dsWZ.insert(wi2) wm3 = dsWZ.insert(wi3) # special event data def _sei(idx, y1, m1, d1, y2, m2, d2, att): sei = SpecialEventInfo() sei.name = 'test se %d' % idx sei.description = 'test is test se %d' % idx sdt = datetime.datetime(y1, m1, d1, 1, 0, 0) edt = datetime.datetime(y2, m2, d2, 1, 0, 0) sei.start_time = sei.datetime2str(sdt) sei.end_time = sei.datetime2str(edt) sei.set_years() sei.attendance = att sei.lon = -93.331893 sei.lat = 44.970797 return sei se1 = _sei(1, 2014, 3, 1, 2014, 3, 2, 1000) se2 = _sei(2, 2012, 4, 1, 2012, 4, 2, 20000) se3 = _sei(3, 2016, 5, 1, 2016, 5, 2, 300) sem1 = dsSE.insert(se1) sem2 = dsSE.insert(se2) sem3 = dsSE.insert(se3) dsWZ.close_session() dsSE.close_session() dsRoute.close_session()