class RouteWiseMOEParametersDataAccess(DataAccess):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.da_base = DataAccessBase(model.RouteWiseMOEParameters,
                                      RouteWiseMOEParametersInfo, **kwargs)

    def list(self):
        """
        :rtype: list[pyticas_tetres.ttypes.WorkZoneGroupInfo]
        """
        data_list = self.da_base.list(
            order_by=[('reference_tt_route_id',
                       'asc'), ('start_time', 'asc'), ('end_time',
                                                       'asc'), ('status',
                                                                'asc')])
        for data in data_list:
            data.start_time = str(data.start_time) if data.start_time else ""
            data.end_time = str(data.end_time) if data.end_time else ""
            data.update_time = str(
                data.update_time) if data.update_time else ""
        return data_list

    def get_by_id(self, moe_param_id):
        """
        :type wz_id: int
        :rtype: pyticas_tetres.ttypes.WorkZoneGroupInfo
        """
        return self.da_base.get_data_by_id(moe_param_id)

    def insert(self, route_wise_moe_param_info, **kwargs):
        """
        :type wzi: pyticas_tetres.ttypes.WorkZoneGroupInfo
        :rtype: model.WorkZoneGroup
        """
        return self.da_base.insert(route_wise_moe_param_info, **kwargs)

    def search_by_route_id(self, route_id, *args, **kwargs):
        return self.da_base.search([('reference_tt_route_id', route_id)])

    def get_latest_moe_param_for_a_route(self, route_id, *args, **kwargs):
        latest_object = None
        data_list = self.search_by_route_id(route_id, *args, **kwargs)
        if not data_list:
            return latest_object
        latest_update_time = data_list[0].update_time
        latest_object = data_list[0]
        for data in data_list:
            if data.update_time > latest_update_time:
                latest_update_time = data.update_time
                latest_object = data
        return latest_object
Example #2
0
class ConfigDataAccess(DataAccess):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.da_base = DataAccessBase(model.Config, ConfigInfo, **kwargs)

    def list(self, **kwargs):
        """
        :rtype: list[pyticas_tetres.ttypes.ConfigInfo]
        """
        return self.da_base.list(**kwargs)

    def get_by_id(self, pkey):
        """
        :type pkey: int
        :rtype: ConfigInfo
        """
        return self.da_base.get_data_by_id(pkey)

    def get_by_name(self, name, **kwargs):
        """
        :type name: str
        :rtype: pyticas_tetres.ttypes.ConfigInfo
        """
        if kwargs.get('as_model', False):
            return self.da_base.get_model_by_name(name, **kwargs)
        else:
            return self.da_base.get_data_by_name(name, **kwargs)

    def insert(self, name, content, **kwargs):
        """
        :type name: str
        :type content: str
        :rtype: model.Config
        """
        c = ConfigInfo()
        c.name = name
        c.content = content
        return self.da_base.insert(c, **kwargs)

    def insert_or_update(self, name, content):
        """

        :type name: str
        :type content: str
        :rtype: Union(model.Config, bool)
        """
        item = self.get_by_name(name, as_model=True)
        if not item:
            return self.insert(name, content)
        else:
            if self.update(item.id, {'content': content}):
                item.content = content
                return item
            return False

    def delete_by_name(self, name, **kwargs):
        """
        :type name: str
        :rtype: bool
        """
        print_exception = kwargs.get('print_exception',
                                     DEFAULT_PRINT_EXCEPTION)
        sess = self.get_session()
        try:
            sess.query(model.Config).filter(model.Config.name == name).delete()
            sess.commit()
            return True
        except Exception as ex:
            if print_exception:
                tb.traceback(ex)
            sess.rollback()
            return False
class SpecialEventDataAccess(DataAccess):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)
        self.da_base = DataAccessBase(model.Specialevent, SpecialEventInfo,
                                      **kwargs)

    def exist(self, name, start_time, end_time):
        """
        :type name: str
        :type start_time: str
        :type end_time: str
        :rtype: pyticas_tetres.db.model.SpecialEvent
        """

        if isinstance(start_time, str):
            start_time = datetime.datetime.strptime(start_time,
                                                    '%Y-%m-%d %H:%M:%S')
        if isinstance(end_time, str):
            end_time = datetime.datetime.strptime(end_time,
                                                  '%Y-%m-%d %H:%M:%S')
        exs = self.da_base.search([('name', name), ('start_time', start_time),
                                   ('end_time', end_time)],
                                  op='and',
                                  cond='match')
        return exs

    def list(self):
        """
        :rtype: list[SpecialEventInfo]
        """
        return self.da_base.list()

    def list_by_year(self, years):
        """
        :type years: list[int]
        :rtype: list[SpecialEventInfo]
        """
        wheres = [('years', y) for y in years]
        return self.da_base.search(wheres, op='or', cond='like')

    def search_date_range(self, sdt, edt, **kwargs):
        """ search data overlapped with the given data range

        :type sdt: datetime.datetime
        :type sdt: datetime.datetime
        :rtype: list[pyticas_tetres.ttypes.SpecialEventInfo]
        """
        as_model = kwargs.get('as_model', False)
        clauses = or_()
        clauses = or_(clauses, self.da_base.dbModel.end_time < sdt)
        clauses = or_(clauses, self.da_base.dbModel.start_time > edt)
        clauses = not_(clauses)

        data_list = []
        for model_data in self.da_base.session.query(
                self.da_base.dbModel).filter(clauses):
            if as_model:
                data_list.append(model_data)
            else:
                data_list.append(self.da_base.to_info(model_data))
        return data_list

    def years(self):
        """
        :rtype: list[int]:
        """
        ys = []
        for sei in self.da_base.list():
            for y in sei.years.split(','):
                iy = int(y)
                if iy not in ys:
                    ys.append(iy)
        return sorted(ys)

    def get_by_id(self, se_id):
        """
        :type se_id: int
        :rtype: SpecialEventInfo
        """
        return self.da_base.get_data_by_id(se_id)

    def insert(self, sei, **kwargs):
        """
        :type sei: SpecialEventInfo
        """
        return self.da_base.insert(sei, **kwargs)