class Task(object): db, cursor = db_connect() redis = redis_conn() logger = get_logger() def run(self): pass
def list_targets(): """ Shows the page which lists all of the available target platforms. """ db = db_connect() targets = db.getAllTargets().all() devices = db.getAllDevices().all() cores = db.getAllCores().all() boards = db.getAllBoards().all() targets.sort(key = lambda x : x.core.name) cores.sort(key = lambda x : x.name) template = render_template( "targets/list.html", targets = targets, devices = devices, cores = cores, boards = boards ) db_close() return template
def render_selection(tids): """ Plot a set of traces over one-another. """ sel = [int(i) for i in tids.split(",")] db = db_connect() traces = [] labels = [] for tid in sel: stat_trace = db.getStatisticTraceById(tid) traces.append(stat_trace.getValuesAsNdArray()) labels.append(stat_trace.name) figure = makePlotFigure(traces, slabels=labels, xlabel="Sample", width_in=15, height_in=8) rsp = makePlotResponse(figure) db_close() return rsp
def plot_statistic_trace(eid, tid, stid): """ Render a the plot.html template for viewing a single statistic trace. """ db = db_connect() pbin = None disasm = None target = db.getTargetById(tid) experiment = db.getExperimentById(eid) pbin = db.getProgramBinaryByTargetAndExperiment(target.id, experiment.id) disasm = getExperimentPayloadFromProgramBinary(pbin) trace = db.getStatisticTraceById(stid) template = render_template("plot.html", plotType=str(trace.stat_type), target=target, experiment=experiment, stid=trace.id, strace=trace, pbin=pbin, disasm=disasm) db_close() return template
def show_results(eid, tid): """ Render page showing experiment results for a particular target and experiment combination. """ db = db_connect() experiment = db.getExperimentById(eid) target = db.getTargetById(tid) ttests = db.getTTraceSetsByTargetAndExperiment(target.id, experiment.id).all() blobs = db.getTraceSetBlobByTargetAndExperiment( target.id, experiment.id, ).all() corrs = db.getCorrolationTraceByTargetAndExperiment( target.id, experiment.id).all() pbin = db.getProgramBinaryByTargetAndExperiment(target.id, experiment.id) template = render_template("experiments/results.html", target=target, experiment=experiment, blobs=blobs, ttests=ttests, corrs=corrs, pbin=pbin) db_close() return template
def plot_view_corrolation_statistic(tid): """ Render a the plot.html template for viewing a single corrolation-statistic trace. """ db = db_connect() corr = db.getCorrolationTraceById(tid) target = corr.target experiment = corr.experiment stid = corr.statisticTraceid pbin = db.getProgramBinaryByTargetAndExperiment(target.id, experiment.id) disasm = getExperimentPayloadFromProgramBinary(pbin) template = render_template("plot.html", plotType="Corrolation", corr=corr, target=target, experiment=experiment, stid=stid, pbin=pbin, disasm=disasm) db_close() return template
def plot_view_tstatistic(tid): """ Render a the plot.html template for viewing a single t-statistic trace. """ db = db_connect() ttest = db.getTTraceSetsById(tid) target = ttest.target experiment = ttest.experiment stid = ttest.ttraceId pbin = db.getProgramBinaryByTargetAndExperiment(target.id, experiment.id) disasm = getExperimentPayloadFromProgramBinary(pbin) template = render_template("plot.html", plotType="TTest", ttest=ttest, target=target, experiment=experiment, stid=stid, pbin=pbin, disasm=disasm) db_close() return template
def add_film_info(rj): result = None ext_oid = None if 'externalOrderId' in rj: conv = MySQLConverter() ext_oid = rj['externalOrderId'] success_url = conv.escape(rj.get('successUrl', None)) cancel_url = conv.escape(rj.get('cancelUrl', None)) db = db_connect() try: if not ext_oid: result = db_query(db, 'insert into vidimax (film_id, type, name, price) values ({film_id}, "{type}", "{name}", {price});'\ .format(film_id=rj['id'], type=rj['type'], name=rj['name'], price=int(int(rj['price'])/100)), commit=True, lastrow=True) else: query = 'insert into vidimax values (null, null, "{external_oid}", {film_id}, "{type}", "{name}", {price}, '.format( external_oid=ext_oid, film_id=rj['id'], type=rj['type'], name=rj['name'], price=int(int(rj['price'])/100) ) if success_url: query += '"{0}", '.format(success_url) else: query += 'null, ' if cancel_url: query += '"{0}");'.format(cancel_url) else: query += 'null);' result = db_query(db, query, commit=True, lastrow=True) except Exception as e: print e pass finally: db_disconnect(db) return result
def get_user_subscriptions(ip, ua): def get_subs_list(db, client_id): subs = [] res = db_query(db, 'select order_id, client_films_id, first_ip, direction \ from orders where client_id=%s and new_model=1 and state_id=0;'%(client_id), full=True) if res: for [ order_id, film_id, first_ip, direction ] in res: subs_info = get_subs_info(db, film_id, (first_ip, direction)) if subs_info not in subs: subs.append(subs_info) return subs pprint('get_user_subscriptions:') pprint(ip) pprint(ua) result = {} mac = get_mac(ip) db = db_connect() vip_client = is_vip_client(db, ip, mac) if vip_client: return { "VIP" : True } client_ids = db_query(db, 'select client_id as id from client_info where ip="%s" and mac="%s" group by client_id'%(ip, mac), full=True) if client_ids: print 'client found', client_ids result['UserID'] = client_ids[0][0] # in every way result['Subscriptions'] = [] if len(client_ids) > 1: print "f*****g f**k! this should not have happened but happened" for client in client_ids: result['Subscriptions'] += get_subs_list(db, client) db_disconnect(db) pprint(result) return result
def get_code_by_billnumber(direction, ip, order, billnumber): result = {} res = None try: mac = get_mac(ip).replace(':','') if mac == '0'*12: raise ValueError('No MAC address for %s'%(ip)) db = db_connect() res = db_query(db, "select o.code from orders o " "where o.payment_time is not null and o.code <> '' and o.end_time is null and o.state_id <> 3 " # hardcode "and o.dev_count < 2 and o.direction='%s' and o.billnumber='%s' and o.first_mac=x'%s' and o.order_id='%s'" %(direction, billnumber, mac, order), ) except Exception as e: print e if not res: r = False code = None else: r = True code = res[0] result = { 'Code': code, 'Result': r } return result
def get_stats(begin, end): db = db_connect() now = datetime.datetime.now() datefmt = '%Y-%m-%d %H:%M:%S' begin_str = get_time(begin) or now.strftime('%Y-%m-%d 00:00:00') # hardcode end_str = get_time(end) or now.strftime(datefmt) query = 'select order_id, phone, code, price, price*0.10, payment_time, refund_time \ from orders o left join clients c on o.client_id=c.id \ left join tariffs.tariffs_tariff t on o.tariff_id=t.id \ where partner_id=1 and payment_time between "%s" and "%s"'%(begin_str, end_str) # hardcode res = db_query(db, query, full=True) db_disconnect(db) result = { 'Statistics' : [] } if res: for (oid, phone, code, price, fee, p_time, r_time) in res: tmp = { 'OrderID' : oid, 'Phone' : phone, 'Code' : code, 'Price' : int(price), 'Fee' : float(fee), 'PaymentTime' : p_time.strftime(datefmt) } if r_time: tmp['RefundTime'] = r_time.strftime(datefmt) result['Statistics'].append(tmp) return result
def start_session(order_id): db = db_connect() db_query(db, 'update orders set stop_time=null, start_time=now(), ' 'state_id=0 ' 'where id=%d'%(order_id), fetch=False, commit=True ) db_disconnect(db)
def get_active_sessions(): db = db_connect() lines = db_query(db, 'select ords.id, ords.direction, tariff_id, ords.start_time, ords.session_time, ' 'ords.client_films_id, tar.duration, ords.state_id from orders ords ' 'left join tariffs tar on tariff_id = tar.id ' 'where start_time is not null and state_id=0;', full=True ) result = {} if lines != []: for ( order_id, direction, tariff_id, start_time, session_time, is_film, dur, state_id ) in lines: print order_id, direction, dur duration = dur if direction and not is_film: duration = tariffs.get_duration(direction, tariff_id) # else: # duration = tariffs.get_direction_trip_duration(direction) result[str(order_id)] = { 'start_time' : start_time, 'session_time' : session_time, 'duration' : duration, 'state' : state_id } db_disconnect(db) return result
def verify_user(usrname, passwd): db = db_connect() result = True if not db_query(db, 'select id from users where user="******" and passwd=password("%s");'%(usrname, passwd)): result = False db_disconnect(db) return result
def get_code(order_id): result = None db = db_connect() res = db_query(db, 'select code from orders where order_id="%s"'%(order_id)) db_disconnect(db) if res: [ result ] = res return result
def get_shopid_by_orderid(order_id): result = {} db = db_connect() res = db_query(db, 'select s.shop from orders o left join shops s on s.id = o.shop_id where o.order_id = "%s"'%(order_id)) if res: result['ShopID'] = res[0] db_disconnect(db) return result
def get_phones_to_sms(): db = db_connect() result = db_query(db, 'select ords.id, cl.phone, ords.code from orders ords left join clients cl on cl.id = client_id ' 'where (sms_sent = 0 or (sms_sent = 1 and unix_timestamp(now()) - unix_timestamp(payment_time) > 600)) ' 'and code <> "" order by payment_time;', full=True, quiet=True ) db_disconnect(db) return result
def curs(self): from db import db_connect conn, curs = db_connect(self.hostname, self.dbname, self.schema, user=self.db_user, password=self.db_passwd) return curs
def route_jinja(path): t_wordcloud = [] db_connection, db_cursor = db.db_connect() if db_cursor: t_wordcloud = db.db_select_word_cloud(db_cursor) db.db_close(db_connection, db_cursor) return render_template('jinja.html', t_wordcloud=t_wordcloud)
def get_filmid_by_orderid(order_id): result = {} if order_id[:8] == 'VIDEOSVC': db = db_connect() res = db_query(db, 'select client_films_id from orders where order_id="%s"'%(order_id)) if res: result['FilmID'] = res[0] db.close() return result
def get_train_direction(train): db = db_connect() query = 'select abbr from trains where number="%s"'%train res = db_query(db, query) db_disconnect(db) result = {} if res: result['Direction'] = res[0] return result
def end_session(order_id): db = db_connect() db_query(db, 'update orders set stop_time=null, end_time=now(), state_id=3, ' 'session_time = sec_to_time(unix_timestamp() - unix_timestamp(start_time) + time_to_sec(session_time)), ' 'start_time=null ' 'where id=%d'%(order_id), fetch=False, commit=True ) unauth_user(db, order_id) db_disconnect(db)
def route_device(p_address=None, p_address_type=None, p_limit=100): time_now = time.strftime("%H:%M", time.localtime()) t_device = [] db_connection, db_cursor = db.db_connect() if db_cursor: t_device = db.db_select_device(db_cursor, p_address, p_address_type, p_limit) db.db_close(db_connection, db_cursor) return render_template('device.html', date=time_now, t_device=t_device)
def view_login_page(): if request.method == 'POST': user_id = request.form['user_id'] password = request.form['password'] if user.login(db_connect(), user_id, password): return redirect('/') else: return redirect('/login') return render_template('login.html')
def __init__(self, root, fallbackPath, username, secret): dbms = db.db_connect(username=username, password=secret) tokens = dbms.get_all_tokens() keys, paths = dbms.get_all_keys() if not keys: key = crypt.gen_sym_key_from_password(secret) print(key) print(key.decode()) dbms.insert_key(key.decode(), '/') keys.append(key.decode()) paths.append('/') self.root = root self.fallbackPath = fallbackPath self.token = tokens self.keys = keys self.paths = paths print('KEYS AND PATHS: ' + str(self.keys) + str(self.paths)) url = variables['server'] + '/v1/listFolders' header = {'Authorization': 'Token ' + self.token} r = requests.get(url, headers=header).content req = json.loads(r.decode('utf-8')) print(req) for folders in req: os.makedirs('/tmp/.tmp_fuse/' + folders) ############################################################## url = variables['server'] + '/v1/listFiles' header = {'Authorization': 'Token ' + self.token} r = requests.get(url, headers=header).content req = json.loads(r.decode('utf-8')) print(req) for files in req: print('creating file: ' + root + files) try: f = open(root + files, "w+") f.close() except Exception as e: print(e) ############################################################### url = variables['server'] + '/v1/downloadFile' values = {'name': files} header = {'Authorization': 'Token ' + self.token} r = requests.get(url, headers=header, data=values) #print(str(r.content)) decoded_string = crypt.decode_with_password( r.content.decode(), self.keys[0]) # ROOT KEY f = open(self.root + files, 'w') f.write(decoded_string.decode('utf-8')) # (decode("utf-8")) f.close()
def route_scan_data_car(): time_now = time.strftime("%H:%M", time.localtime()) t_scan_data_car = [] db_connection, db_cursor = db.db_connect() if db_cursor: t_scan_data_car = db.db_select_scan_data_car(db_cursor) db.db_close(db_connection, db_cursor) return render_template('scan_data_car.html', date=time_now, t_scan_data_car=t_scan_data_car)
def stop_session(order_id, by_user=False): if by_user: state_id = 1 else: state_id = 2 db = db_connect() db_query(db, 'update orders set stop_time = now(), ' 'state_id = %d, ' 'session_time = sec_to_time(unix_timestamp() - unix_timestamp(start_time) + time_to_sec(session_time)),' 'start_time = null' 'where id=%d'%(state_id, order_id), fetch=False, commit=True ) db_disconnect(db)
def new_code(): db = db_connect() generated = False while not generated: try: code = gen_code() db_query(db, 'insert into codes (key_value, used) values ("%s", 1);'%(code), commit=True, fetch=False) generated = True except: pass db_disconnect(db) return code
def save_taxi_order(data): print 'save_taxi_order:' print data db = db_connect() query = '''insert into taxi values ( 0, now(), "%(direction)s", "%(ip)s", "%(mac)s", "%(vgt_name)s", "%(vgt_phone)s", "%(vgt_email)s", "%(train)s", "%(vgt_from)s", "%(vgt_dest)s", "%(vgt_ctype)s", %(vgt_cprice)s, "%(vgt_data)s", "%(vgt_add)s", "%(vgt_add2)s", "%(vgt_tab)s", "%(vgt_comment)s", %(accept_offer)s );'''%data print query db_query(db, query, commit=True, fetch=False) db_disconnect(db) pass
class Parser(object): db, cursor = db_connect() logger = get_logger() def run(self): pass def handle_db_timeout(self, sql, params, e): """Reconnect db when db connection timeout.""" self.logger.info("database connection timeout, reconnect") self.logger.exception(e) self.db.ping(reconnect=True) self.cursor.execute(sql, params) self.db.commit()
def list_experiments(): """ Shows the page which lists all of the available target platforms. """ db = db_connect() experiments = db.getAllExperiments() template = render_template("experiments/list.html", experiments=experiments) db_close() return template
def refund_payment(payment_info): db = db_connect() result = False pprint(payment_info) try: db_query(db, 'update orders set refund_id="%s", end_time=now(), refund_time="%s", state_id=4 where order_id="%s";' %(payment_info['refund_id'], payment_info['date'], payment_info['order_id']), commit=True, fetch=False) order_id=int(payment_info['order_id'][8:]) unauth_user(db, order_id) result = True except Exception as e: print e db_disconnect(db) return result
def render_statistic_trace(tid): """ Render a single statistic trace using matplotlib. """ db = db_connect() strace = db.getStatisticTraceById(tid) nptrace = strace.getValuesAsNdArray() tgtId = request.args.get("tgtid", None) expId = request.args.get("eid", None) ttestId = request.args.get("ttid", None) corrId = request.args.get("corrId", None) title = "" xlabel = "Trace Sample" ylabel = "" ylines = [] if (ttestId): ttest = db.getTTraceSetsById(ttestId) title += "TTest: " + str(ttest.parameterDict) + " " ylabel = "T-Statistic Value" ylines.append(4.5) ylines.append(-4.5) if (corrId): corr = db.getCorrolationTraceById(corrId) title += corr.corrType + ": " + ttest.randomTraceSet.parameters + " " if (expId): title += db.getExperimentById(expId).fullname + " " if (tgtId): title += db.getTargetById(tgtId).name + " " figure = makePlotFigure([nptrace], [], title, xlabel=xlabel, ylabel=ylabel, ylines=ylines) rsp = makePlotResponse(figure) db_close() return rsp
def parse_file_infite(): filename = FILE while True: timeBefore = time.time() newName = filename + 'stat' + str(int(time.time())) if path.isfile(filename): conn,curr = db.db_connect() rename(filename, newName) mainlog = open(newName,'a+') mainlog.write('\nFrom MAILER-DAEMON'); mainlog.close() mainlog = open(newName, 'a+') process_file(mainlog, conn, curr) db.db_close(conn, curr) if time.time() - timeBefore < 60: time.sleep(60)
def parse_file_infite(): filename = FILE while True: timeBefore = time.time() newName = filename + 'stat' + str(int(time.time())) if path.isfile(filename): conn, curr = db.db_connect() rename(filename, newName) mainlog = open(newName, 'a+') mainlog.write('\nFrom MAILER-DAEMON') mainlog.close() mainlog = open(newName, 'a+') process_file(mainlog, conn, curr) db.db_close(conn, curr) if time.time() - timeBefore < 60: time.sleep(60)
def get_film_price(filmid): db = db_connect() [ name, price ] = db_query(db, 'select name, price from films where id=%s'%(filmid)) db_disconnect(db) result = { '0' : { 'Button' : '%s руб/24 часа'%(price), 'Button_EN' : '%s rub/24 hours'%(price), 'Tariff' : 'FILM', 'Sum' : price, 'Description' : 'Стоимость доступа к фильму "%s" в течение 24 часов составляет %s рублей (включая НДС)'%(name, price), 'Description_EN' : 'The cost of access to "%s" film during 24 hours shall be %s rubles (VAT included)'%(name, price) }, 'URL' : settings.vidimax_base + '/#movie/' + filmid } pprint(result) return result
def add_film_watch(request_json): result = False pprint(request_json) db = db_connect() ip = request_json['IPAddress'] mac = get_mac(ip) film_id = request_json['FilmID'] name = request_json['Name'] user_id = request_json.get('UserID', '0') user_agent = request_json.get('UserAgent', '') db_query(db, 'insert into watches (ip, mac, film_id, name, ua, user_id) values ("%s", "%s", %s, "%s", "%s", %s);' %(ip, mac, film_id, name, user_agent, user_id), commit=True, fetch=False ) result = True db_disconnect(db) return { "Result" : result }
def get_tariffs(service): db = db_connect() tariffs = db_query(db, 'select type, button_name, price, button_name_en, description, description_en from tariffs ' 'where service="%s" order by price;'%(service.upper()), full=True ) db_disconnect(db) result = {} for i in range(len(tariffs)): result[str(i)] = { 'Button' : tariffs[i][1] , 'Button_EN' : tariffs[i][3], 'Tariff' : tariffs[i][0], 'Sum' : tariffs[i][2], 'Description' : tariffs[i][4], 'Description_EN' : tariffs[i][5] } return result
def main(): try: with open(config_file(), "r") as file: #Code here # print("yahoo file opened successfully") db_details = json.load(file) # print(db_details['_comment']) hostname=db_details['database_details']['hostname'] username=db_details['database_details']['username'] password=db_details['database_details']['password'] database=db_details['database_details']['db'] db_connect_obj = db.db_connect(hostname,username,password,database) print("Connected successfully") except IOError as e: print("IO Error : {error}".format(error=e))
def show_board(id): """ Renders a page which lists all the information on a particular board. """ db = db_connect() board = db.getBoardById(id) targets = db.getTargetsByBoard(board.id) template = render_template( "targets/board.html", board = board, targets = targets ) db_close() return template
def show_core(id): """ Renders a page which lists all the information on a particular core. """ db = db_connect() core = db.getCoreById(id) targets = db.getTargetsByCore(core.id) template = render_template( "targets/core.html", core = core, targets = targets ) db_close() return template
def show_experiment(id): """ Render page showing all information on a particular experiment. """ db = db_connect() experiment = db.getExperimentById(id) targets = db.getTargetsByExperiment(experiment.id).all() targets.sort(key=lambda x: x.core.name) template = render_template("experiments/show.html", targets=targets, experiment=experiment) db_close() return template
def get_latest_summary_stats(ticker=None, limit=0): client = db_connect() collection = client["stockAnalyzer"]["TSX-Stock"] option_dict = {"_id": False, "Date": False, "Root Ticker": False} if ticker is None: sum_cursor = collection.find({}, {"_id": False}, limit=limit) sum_list = { k: i[-1]["Value"] for row in sum_cursor for k, i in row.items() } else: base_dict = collection.find_one({"Root Ticker": ticker}, option_dict) sum_list = { title: valList[-1]["Value"] for title, valList in base_dict.items() } return sum_list
def get_historical_list(ticker=None, limit=0): client = db_connect() collection = client["stockAnalyzer"]["TSX-History"] if ticker is None: his_cursor = collection.find({}, { "History": True, "_id": False }, limit=limit) his_list = [row["History"] for row in his_cursor] else: his_list = collection.find_one({"Root Ticker": ticker}, { "History": True, "_id": False })["History"] return his_list
def show_device(id): """ Renders a page which lists all the information on a particular device. """ db = db_connect() device = db.getDeviceById(id) targets = db.getTargetsByDevice(device.id) template = render_template( "targets/device.html", device = device, targets = targets ) db_close() return template
def show_target(id): """ Renders a page which lists all the information on a particular target. """ db = db_connect() target = db.getTargetById(id) experiments = db.getExperimentsByTarget(target.id) template = render_template( "targets/target.html", target = target, experiments = experiments ) db_close() return template
def get_client_codes(direction, phone, ip): result = {} try: mac = get_mac(ip).replace(':','') if settings.check_mac and mac == '0'*12: raise ValueError('No MAC address for %s'%(ip)) db = db_connect() res = db_query(db, "select o.code, s.state, " "substr(o.order_id, 1, 8) as service, " "if(substr(o.order_id, 1, 8)='INTERNET', " "if(o.direction is null, t.button_name, tt.button_ru), " "if(o.new_model=1,v.name,f.name)) as tariff_ru, " "if(substr(o.order_id, 1, 8)='INTERNET', " "if(o.direction is null, t.button_name_en, tt.button_en), " "if(o.new_model=1,v.name,f.name)) as tariff_en, " "time(if(o.begin_time is not null, now() - o.begin_time, '00:00:00')) as elapsed, " "if(o.dev_count > 1, 0, 2-o.dev_count) as devices " "from orders o " "left outer join tariffs t on t.id = o.tariff_id " "left outer join tariffs.tariffs_description tt on tt.tariff_id = o.tariff_id " "left join clients c on c.id = o.client_id " "left join states s on s.id = o.state_id " "left outer join films f on f.id = o.client_films_id " "left outer join vidimax v on v.id = o.client_films_id " "where o.payment_time is not null and o.code <> '' and o.end_time is null and o.state_id <> 3 " # hardcode "and o.dev_count < 2 and o.direction='%s' and c.phone='%s' and o.first_mac=x'%s'"%(direction, phone, mac), full=True) count = 0 for code, state, service, tariff_name_ru, tariff_name_en, elapsed, dev_remained in res: code_info = { 'Code': code, 'Status': state, 'Service': service, 'TariffName_RU': unicode(tariff_name_ru), 'TariffName_EN': unicode(tariff_name_en), 'DevicesRemained': dev_remained } if elapsed: code_info['Elapsed'] = str(elapsed) result[str(count)] = code_info count += 1 except Exception as e: print e return result
def download_binary(pid): """ Download a program binary. """ db = db_connect() pbin = db.getProgramBinaryById(pid) rsp = make_response(pbin.binary) rsp.headers.set("Content-Type", "application/octet-stream") rsp.headers.set("Content-Disposition", "attachment", filename="%s-%s-program.elf" % (pbin.target.name, pbin.experiment.name)) db_close() return rsp
def download_disassembly(pid): """ Download a program disassembly. """ db = db_connect() pbin = db.getProgramBinaryById(pid) rsp = make_response(pbin.disasm) rsp.headers.set("Content-Type", "text/plain") rsp.headers.set("Content-Disposition", "attachment", filename="%s-%s-program.dis" % (pbin.target.name, pbin.experiment.name)) db_close() return rsp
def get_trains_list(after=None): db = db_connect() result = { 'Trains' : [] } datefmt = '%Y-%m-%d %H:%M:%S' query = 'select number, start, end, inactive, added, updated from trains' if get_time(after): query += ' where updated >= "%s"'%after res = db_query(db, query, full=True) db_disconnect(db) for number, start, end, inactive, added, updated in res: el = { 'TrainNumber' : number, 'StartStation' : start, 'EndStation' : end, 'State' : inactive, 'Added' : added.strftime(datefmt), 'Updated' : updated.strftime(datefmt) } result['Trains'].append(el) return result
def get_film_session(request_json): pprint('get_film_session') result = { 'Result' : False } mac = get_mac(request_json['IPAddress']) db = db_connect() try: # checking user code print "checking" res = db_query(db, 'select o.id from orders o cross join ' '(select client_orders_id from client_info where mac = "%s" and ip = "%s" order by update_time desc) s on s.client_orders_id = o.id ' 'where client_films_id=%s and begin_time is not null and end_time is null limit 1;' %(mac, request_json['IPAddress'], request_json['FilmID']) ) if res: result['Result'] = True except Exception as e: print e pass db_disconnect(db) return result
def update_order(payment_info): db = db_connect() result = False pprint(payment_info) client_id = get_client_by_phone(db, payment_info['phone']) if not client_id: client_id = add_client_phone(db, payment_info['phone']) code = payment_info['approval_code'] if check_order(db, payment_info): if not code: code = new_code() client_order_id = add_client_order(db, client_id, payment_info['order_id']) db_query(db, 'update orders set billnumber="%s", client_id=%d, payment_time="%s", code="%s" where order_id="%s";' %(payment_info['uni_billnumber'], client_id, payment_info['date'], code, payment_info['order_id']), commit=True, fetch=False) result = True if not settings.testing: send_sms(payment_info['phone'], code) sms_sent(payment_info['order_id']) db_disconnect(db) return result
def route_ajax_db(): time_now = time.strftime("%H:%M:%S", time.localtime()) if request.method == "GET": restrict = request.args.getlist('restrict') query = request.args.get('query') elif request.method == "POST": restrict = request.form.getlist('restrict') query = request.form.get('query') else: restrict = [] query = "" t_result = [] db_connection, db_cursor = db.db_connect() if db_cursor: if query == "db_select_scan_bucket": t_result = db.db_select_scan_bucket(db_cursor) elif query == "db_select_recap": t_result = db.db_select_recap(db_cursor, restrict) db.db_close(db_connection, db_cursor) return jsonify(time=time_now, result=t_result)
import re import json import time from flask_cors import CORS import sys reload(sys) sys.setdefaultencoding('utf-8') from workflow import generateContent from db import db_connect, db_counterup, db_counterdown app = Flask(__name__) CORS(app) db_connect() @app.route("/") def ping(): return "OK" @app.route("/input", methods=['POST']) def input(): # FOR LIVE PRESENTATION # time.sleep( 5 ) # with open('mockResponse.json') as json_data: # return jsonify(json.load(json_data)) content = request.get_json(silent=True)['content'] # content = re.sub('[^a-zA-Z. 0-9]', '', content) originalContent = content
def get_ticker_names(): client = db_connect() collection = client["stockAnalyzer"]["TSX-History"] tickers_cursor = collection.find({}, {"Root Ticker": True}) return [ticker["Root Ticker"] for ticker in tickers_cursor]
def sms_sent(order_id, status=2): db = db_connect() db_query(db, 'update orders set sms_sent=%d where id=%d;'%(status, order_id), fetch=False, commit=True) db_disconnect(db)
def get_session(request_json, update=False): pprint('get_session') pprint(request_json) is_film = False if 'FilmID' in request_json: is_film = True result = { 'Result' : False, 'IPAddress' : '', 'UserAgent' : '', 'Logout' : 0 } if is_film: result['URL'] = settings.vidimax_base + '/#movie/' + request_json['FilmID'] if int(request_json['FilmID']) == 694: # hardcode result['URL'] = settings.vidimax_base print result['URL'] db = db_connect() mac = get_mac(request_json['IPAddress']) vip_client = is_vip_client(db, request_json['IPAddress'], mac) if vip_client: # vip client print "is vip client" if is_film: result['URL'] = settings.vidimax_base + '/#movie/' + request_json['FilmID'] if int(request_json['FilmID']) == 694: # hardcode result['URL'] = settings.vidimax_base # result['URL'] = settings.vidimax_base + '/#play/' + request_json['FilmID'] result['Result'] = True return result if 'Code' in request_json: if is_vip_code(db, request_json['Code']): print "is vip code" if not vip_client: add_vip_client(db, request_json['Code'], request_json['IPAddress'], mac) auth_client(request_json['IPAddress'], mac) result['Result'] = True return result else: tar = is_scratch_code(db, request_json['Code']) if tar and not is_film: # i don't accept scratch card payment for films for a while fd = get_first_data(tar['service'], tar['tariff'], None, 'SCRATCH') order_id = get_order_id(fd['OrderID']) sms_sent(order_id) update_order(generate_scratch_payment(fd['ShopID'], fd['OrderID'], fd['Sum'], request_json['Code'])) scratch_set_used(db, request_json['Code']) if is_film and not update: return get_film_session(request_json) try: pprint('client_info = get_client_info:') client_info = get_client_info(db, request_json) pprint(client_info) if client_info: result = { 'Result' : True, 'IPAddress' : client_info['ip'], 'UserAgent' : client_info['user_agent'], 'Logout' : client_info['state'] } if is_film: if 'external_order_id' in client_info: # not vidimax result['URL'] = client_info.get('url', settings.vidimax_base) else: result['URL'] = settings.vidimax_base + '/#movie/' + request_json['FilmID'] if int(request_json['FilmID']) == 694: # hardcode result['URL'] = settings.vidimax_base print result['URL'] else: auth_client(client_info['ip'], client_info['mac']) if client_info['changed']: print 'if update:' if update and update_client_info(db, client_info, False): auth_client(client_info['ip'], mac) else: result['Result'] = False except KeyError as e: print 'KeyError: ' + str(e) result = None db_disconnect(db) return result
#!/usr/bin/python # Migrate from imagestore1 to imagestore2 import db import gc import MySQLdb import sha, md5 import mx.DateTime from sqlobject import * db.db_connect(); #conn = MySQLConnection(host='127.0.0.1', db='imagestore', user='******', passwd='zwarp', debug=0) #conn = MySQLConnection(host='lurch', db='imagestore', user='******', passwd='im_zwarp', debug=0) conn = connectionForURI('mysql://[email protected]/imagestore?cache=0') #conn = connectionForURI('mysql://*****:*****@lurch/imagestore') conn.debug=0 __connection__ = conn class MigImages(SQLObject): _cacheValues = False _table = 'images' _idName = 'img_id' image = StringCol(dbName='image', sqlType='MEDIUMBLOB', notNone=True) thumbnail = StringCol(dbName='thumbnail', sqlType='blob') mime_type = StringCol(length=32, dbName='mime_type')
def get_first_data(service, tariff, film_id=None, payment_system=None, new_model=False, direction=None, ip=None, partner=None): def get_partner_id(db, partner): result = None res = db_query(db, 'select id from partners where name="%s"'%partner) if res: result = res[0] return result def create_order(db, shop, tariff, film, direction=None, ip=None, partner_id=None): mac = get_mac(ip).replace(':','') or '000000000000' print mac if not film: oid = db_query(db, "insert into orders (shop_id, tariff_id, first_mac, first_ip) values ( %s, %s , x'%s', '%s');"%(shop, tariff, mac, ip), fetch=False, commit=True, lastrow=True) else: query = "insert into orders (shop_id, tariff_id, client_films_id, first_mac, first_ip) values (%s, %s, %s, x'%s', '%s');" if new_model: query = "insert into orders (shop_id, tariff_id, client_films_id, new_model, first_mac, first_ip) values (%s, %s, %s, 1, x'%s', '%s');" # hardcode oid = db_query(db, query%(shop, tariff, film, mac, ip), fetch=False, commit=True, lastrow=True) if new_model: update_order_id(db, film, oid) if direction: print 'adding direction "%s" to order %s'%(direction, oid) db_query(db, 'update orders set direction="%s" where id=%s;'%(direction, oid), fetch=False, commit=True) if partner_id: print 'adding partner id = %d to order %s'%(partner_id, oid) db_query(db, 'update orders set partner_id=%d where id=%s;'%(partner_id, oid), fetch=False, commit=True) return oid db = db_connect() tariff_id, tariff_sum = get_tariff(db, service, tariff, film_id, new_model) shop = get_shop(payment_system) shop_id = get_shop_id(db, payment_system) if partner: partner_id = get_partner_id(db, partner) else: partner_id = None if not (tariff_id and tariff_sum and shop_id): return None order_num = create_order(db, shop_id, tariff_id, film_id, direction, ip, partner_id) if not order_num: return None order_id = '%s%020d'%(service.upper(), order_num) db_query(db, 'update orders set order_id="%s" where id=%d'%(order_id, order_num), fetch=False, commit=True) if not film_id: if not direction: [ desc, desc_en ] = db_query(db, 'select t.button_name, t.button_name_en from orders o left join tariffs t on t.id = o.tariff_id where o.id = %d;' %(order_num) ) else: [ desc, desc_en ] = tariffs.get_descriptions(tariff_id) else: # is film if new_model: price = get_price(db, film_id) desc = '%s руб / 24 часа'%(price) # hardcode desc_en = '%s rub / 24 hours'%(price) else: [ price ] = db_query(db, 'select price from films where id = %s;'%(film_id)) desc = '%s руб / 24 часа'%(price) desc_en = '%s rub / 24 hours'%(price) db_disconnect(db) result = { 'ShopID' : shop, 'OrderID' : order_id, 'Sum' : tariff_sum, 'Description' : desc, 'Description_EN' : desc_en } return result
def create_app(): config = os.getenv('CONFIG', default_config).title() if config not in [default_config, 'Prod']: config = default_config app = Flask(__name__) app.config.from_object('app.' + config) db_connect(app) @app.errorhandler(Unprocessable) def unprocessable(e): return make_response(jsonify({ 'message': e.message, 'data': e.data }), 422) @app.errorhandler(Unauthorized) def unauthorized(e): return make_response(jsonify({ 'message': e.message, 'data': e.data }), 401) @app.route('/') def review(): return render_template('index.html') @app.route('/get-reviews/') def get_reviews(): name = request.args.get('name') if not name: print('No email') return jsonify({}) reviews = mongo.db.SummaryReview.find({}) return jsonify({ 'reviews': list(reviews) }) @app.route('/newsfetch-summarize/') def process_newsfetch_articles(): results = process_article_summaries(mongo.db) return jsonify(results) @app.route('/article/<int:article_id>/summary/', methods=['GET', 'POST']) def add_summary(article_id): name = request.values.get('name') summary_sentences = request.values.get('summary') flagged_sentences = request.values.get('flagged_sentences') if not name: raise Unprocessable('Email not found') elif not summary_sentences: raise Unprocessable('Summary not found') elif not flagged_sentences: raise Unprocessable('Flagged sentences not found') summary_sentences = json.loads(summary_sentences) flagged_sentences = json.loads(flagged_sentences) article = mongo.db.SummaryReview.find({ 'article_id': article_id }).limit(1)[0] if 'summary' not in article: article['summary'] = {} summary = article['summary'] summary[name] = summary_sentences if 'invalid' not in article: article['invalid'] = [] invalids = flagged_sentences tokens_valid = False if 'tokens_valid' not in article or not article['tokens_valid'] else True if flagged_sentences: tokens_valid = False article = mongo.db.SummaryReview.update({ 'article_id': article_id }, { '$set': { 'summary': summary, 'invalid': invalids, 'tokens_valid': tokens_valid } }) return jsonify({ 'success': True, 'article': mongo.db.SummaryReview.find({ 'article_id': article_id }).limit(1)[0] }) @app.route('/article/<int:article_id>/vote/') def vote(): votes = request.values.get('votes') if not votes: raise Unprocessable('"votes" not found') user = request.values.get('user') if not user: raise Unauthorized() article = db.SummaryReview.find({ 'article_id': article_id }).limit(1) votes = article['votes'][user] db.SummaryReview.update({ 'article_id': article_id }, { "$set": { "votes." + user: votes } }) return jsonify({ 'success': True }) @app.route('/article/<int:article_id>/tokensValid/') def set_tokens_valid(article_id): tokens_valid = request.args.get('tokens_valid', None) if tokens_valid is None: raise Unprocessable('Argument tokens_valid not found') tokens_valid = True if tokens_valid.lower() == 'true' else False article = mongo.db.SummaryReview.find({ 'article_id': article_id }).limit(1)[0] if 'invalid' in article and article['invalid']: return jsonify({ 'success': False, 'article': article }) mongo.db.SummaryReview.update({ 'article_id': article_id }, { "$set": { "tokens_valid": tokens_valid } }) return jsonify({ 'success': True, 'article': mongo.db.SummaryReview.find({ 'article_id': article_id }).limit(1)[0] }) @app.route('/articles/valid-tokens/') def get_articles_with_valid_tokens(): articles = mongo.db.SummaryReview.find({ 'tokens_valid': True }) return jsonify({ 'success': True, 'articles': list(articles) }) return app