def from_to(ses, fs, ts, pc, pn): es = [] has_next_p = False if fs != '' and ts == '': es, has_next_p = full(ses, fs.replace(' ', '%').replace('-', '%'), pc, pn) elif fs == '' and ts != '': es, has_next_p = full(ses, ts.replace(' ', '%').replace('-', '%'), pc, pn) else: prepared_fs = fs.replace(' ', '%').replace('-', '%') prepared_ts = ts.replace(' ', '%').replace('-', '%') prepared_ph = prepared_fs + '%' + prepared_ts try: q = ses.query(orm.uatrains.E).\ filter(orm.and_( orm.or_(orm.uatrains.E.ua_graph.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.ru_graph.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.en_graph.ilike('%' + prepared_ph.lower() + '%')), orm.uatrains.E.ref_id == None)).\ order_by(orm.uatrains.E.etype.desc(), orm.uatrains.E.vc.desc(), orm.uatrains.E.ua_title) es = q.limit(pc).offset(pn * pc).all() next_p_es = q.limit(pc).offset((pn + 1) * pc).all() if len(next_p_es) > 0: has_next_p = True except Exception: nlog.info( 'Uatrains error', 'Can\'t find entities by fs and ts\n' + traceback.format_exc()) return es, has_next_p
def ts(self, ph='', pn=0): lng = get_lng() conn = orm.q_engine.connect() ses = orm.sescls(bind=conn) pn = int(pn) pc = 9 ts = [] has_next_p = False try: q = None if lng == lngs.UA: q = ses.query(orm.uatrains.E).\ filter(orm.and_(orm.or_(orm.uatrains.E.etype == etype.train, orm.uatrains.E.etype == etype.etrain, orm.uatrains.E.etype == etype.ptrain), orm.uatrains.E.ua_graph.ilike('%' + ph.lower().replace(' ', '%') + '%'), orm.uatrains.E.ua_title.ilike('%' + ph.lower() + '%'), orm.uatrains.E.ref_id == None)).\ order_by(orm.uatrains.E.vc.desc(), orm.uatrains.E.ua_title) if lng == lngs.RU: q = ses.query(orm.uatrains.E).\ filter(orm.and_(orm.or_(orm.uatrains.E.etype == etype.train, orm.uatrains.E.etype == etype.etrain, orm.uatrains.E.etype == etype.ptrain), orm.uatrains.E.ru_graph.ilike('%' + ph.lower().replace(' ', '%') + '%'), orm.uatrains.E.ru_title.ilike('%' + ph.lower() + '%'), orm.uatrains.E.ref_id == None)).\ order_by(orm.uatrains.E.vc.desc(), orm.uatrains.E.ru_title) if lng == lngs.EN: q = ses.query(orm.uatrains.E).\ filter(orm.and_(orm.or_(orm.uatrains.E.etype == etype.train, orm.uatrains.E.etype == etype.etrain, orm.uatrains.E.etype == etype.ptrain), orm.uatrains.E.en_graph.ilike('%' + ph.lower().replace(' ', '%') + '%'), orm.uatrains.E.en_title.ilike('%' + ph.lower() + '%'), orm.uatrains.E.ref_id == None)).\ order_by(orm.uatrains.E.vc.desc(), orm.uatrains.E.en_title) ts = q.limit(pc).offset(pn * pc).all() next_p_ts = q.limit(pc).offset((pn + 1) * pc).all() if len(next_p_ts) > 0: has_next_p = True except Exception: nlog.info('Uatrains error', 'Can\'t find trains\n' + traceback.format_exc()) ses.close() conn.close() return layout.getTrains(ts, ph, pn, has_next_p, lng)
def full(ses, ph, pc, pn): es = [] has_next_p = False prepared_ph = ph.replace(' ', '%').replace('-', '%') try: q = ses.query(orm.uatrains.E).\ filter(orm.and_(orm.or_(orm.uatrains.E.ua_title.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.ru_title.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.en_title.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.ua_graph.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.ru_graph.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.en_graph.ilike('%' + prepared_ph.lower() + '%'), orm.uatrains.E.value.op('similar to')('([0-9А-Яа-я]*/)?' + prepared_ph.lower() + \ '([А-Яа-я]*)?(/[0-9А-Яа-я]*)?(/[0-9А-Яа-я]*)?')), orm.uatrains.E.ref_id == None)).\ order_by(orm.uatrains.E.etype.desc(), orm.uatrains.E.vc.desc(), orm.uatrains.E.ua_title) es = q.limit(pc).offset(pn * pc).all() next_p_es = q.limit(pc).offset((pn + 1) * pc).all() if len(next_p_es) > 0: has_next_p = True except Exception: nlog.info('Uatrains error', 'Can\'t find entities\n' + traceback.format_exc()) return es, has_next_p
def ss(self, ph='', pn=0): lng = get_lng() conn = orm.q_engine.connect() ses = orm.sescls(bind=conn) pn = int(pn) pc = 9 ss = [] has_next_p = False try: q = ses.query(orm.uatrains.E).filter(orm.uatrains.E.etype == etype.station).\ filter(orm.or_(orm.uatrains.E.ua_title.ilike('%' + ph.lower() + '%'), orm.uatrains.E.ru_title.ilike('%' + ph.lower() + '%'), orm.uatrains.E.en_title.ilike('%' + ph.lower() + '%'))).\ order_by(orm.uatrains.E.vc.desc(), orm.uatrains.E.ua_title) ss = q.limit(pc).offset(pn * pc).all() next_p_ss = q.limit(pc).offset((pn + 1) * pc).all() if len(next_p_ss) > 0: has_next_p = True except Exception: nlog.info('Uatrains error', 'Can\'t find stations\n' + traceback.format_exc()) ses.close() conn.close() return layout.getStations(ss, ph, pn, has_next_p, lng)
import traceback import time import datetime from werp import orm, nlog, exec_log conn = None ses = None try: start_dt = datetime.datetime.now() start_time = time.time() conn = orm.null_engine.connect() ses = orm.sescls(bind=conn) trains = ses.query(orm.uatrains.E).\ filter(orm.or_(orm.uatrains.E.etype == 1, orm.uatrains.E.etype == 4, orm.uatrains.E.etype == 5)).all() for train in trains: # Clear halts of train for the same s_id halt_ids = ses.query(orm.distinct(orm.uatrains.TrainStation.s_id)).\ filter(orm.uatrains.TrainStation.t_id == train.id).all() for halt_id in halt_ids: halts = ses.query(orm.uatrains.TrainStation).\ filter(orm.and_(orm.uatrains.TrainStation.t_id == train.id, orm.uatrains.TrainStation.s_id == halt_id[0])).all() if len(halts) > 1: newest_halt = halts[0] for halt in halts: if halt.c_date > newest_halt.c_date: newest_halt = halt for halt in halts: if halt.id != newest_halt.id:
import traceback import time import datetime from werp import orm, nlog, exec_log conn = None ses = None try: start_dt = datetime.datetime.now() start_time = time.time() conn = orm.null_engine.connect() ses = orm.sescls(bind=conn) etrains = ses.query(orm.uatrains.E).filter( orm.or_(orm.uatrains.E.etype == 1, orm.uatrains.E.etype == 4)).all() for et in etrains: if et.ref_id is None: if (et.from_date is None and et.to_date is None) or \ (et.from_date is not None and et.to_date is not None and not (\ et.from_date <= datetime.datetime.now() and \ et.to_date >= datetime.datetime.now())): last_etrain = et similar_etrains = ses.query(orm.uatrains.E).\ filter(orm.and_(orm.or_(orm.uatrains.E.value == et.value, orm.uatrains.E.oid == et.oid), orm.uatrains.E.ua_title == et.ua_title, orm.or_(orm.uatrains.E.etype == 1, orm.uatrains.E.etype == 4))).all() if len(similar_etrains) > 1: for similar_et in similar_etrains: if (similar_et.from_date is not None and similar_et.to_date is not None and \ similar_et.from_date <= datetime.datetime.now() and \ similar_et.to_date >= datetime.datetime.now()) and \