def commit_and_get_report_back(self, money, hours): new_ticketsman_report = TicketsmanReport(Ticketsman_id=self.user.id, Money=money, WorkTime=hours) session.add(new_ticketsman_report) ##to get id of added report session.flush() session.commit() return new_ticketsman_report
async def update_resource(db_object: Base) -> bool: try: session.add(db_object) session.commit() session.flush() return True except exc.SQLAlchemyError as err: logging.log('info', str(err)) print(err) return False
async def update_user_balance(user: User, balance: any): user.balance = balance try: session.add(user) session.commit() session.flush() return True except exc.SQLAlchemyError as err: logger.debug('Error updating user balance for user %s' % user.name + ': \n' + json.dumps(err)) return False
async def parse_price_data(data): logger.debug('Parsing data:' + str(data)) for k, v in data.items(): logger.debug('Iterating key: ' + str(k)) if currency_index[k] is not None: # result = session.query(CXPrice).filter(CXPrice.id == currency_index[k]).one() cur_time = time.time() # if result is None: # result = CXPrice(currency=k, sell=v['sell'], last=v['last'], buy=v['buy'], modified=cur_time) # else: # result.currency = k # result.sell = v['sell'] # result.last = v['last'] # result.buy = v['buy'] # result.modified = cur_time # # try: # session.add(result) # session.commit() # session.flush() # # except exc.SQLAlchemyError as e: # logger.error(e) # logger.debug('Rolling back after failed CXPrice update') # session.rollback() # return False result2 = session.query(CXPriceRevision).filter( CXPriceRevision.currency == k).group_by( CXPriceRevision.id).order_by( desc(func.max(CXPriceRevision.rid))).first() rid = find_rid(result2) rid = rid + 1 if rid is not None else 1 revision_insert = CXPriceRevision(rid=rid, currency=k, sell=v['sell'], last=v['last'], buy=v['buy'], modified=cur_time, currency_id=currency_index[k]) try: session.add(revision_insert) session.commit() session.flush() except exc.SQLAlchemyError as e: logger.error(e) logger.debug( 'Rolling back after failed CXPriceRevision update') return False return True
def trc_insert_price(date, value): new_price = TRCHistory(time=date, value=value) try: session.add(new_price) session.commit() session.flush() return True except exc.SQLAlchemyError as err: print('This should be logged: \n' + str(err)) return False
async def disable_key(kid: int) -> bool: key = session.query(TrxKey).filter(TrxKey.id == kid).one_or_none() if key is not None: key.status = false() session.add(key) try: session.commit() session.flush() return True except exc.SQLAlchemyError as err: print(err) return False
async def update_key(kid: int, label: str) -> dict: key = session.query(TrxKey).filter(TrxKey.id == kid).one_or_none() if key is not None: if key.label is not None: key.label.text = label else: key.label = KeyLabel(text=label, kid=kid) session.add(key) try: session.commit() session.flush() return True except exc.SQLAlchemyError as err: print(err) return err
async def addMultiSigAddress(pub_addr: str, keys: list, uid: int): if pub_addr is not None and len(keys) > 0: for key in keys: trx_key = TrxKey(value=key, uid=uid) session.add(trx_key) session.flush() multi_sig_key = MKey(pub=pub_addr, uid=uid, kid=trx_key.id) session.add(multi_sig_key) try: session.commit() return multi_sig_key.id except exc.SQLAlchemyError as err: print(err.args) session.rollback()
async def create_bid(uid, rate, amount, date, currency, metric='satoshi'): if metric is not 'satoshi': amount = Decimal(amount) * COIN new_bid = Bid(uid=uid, rate=rate, amount=amount, end_date=date, currency=currency) try: session.add(new_bid) session.commit() session.flush() return True except exc.SQLAlchemyError as e: logger.info(e) return False
def create_driverreport_and_add_to_existing_dayreport( self, day_report, data): new_driverreport = None if data["type"] == "bus": new_driverreport = DriverReport(Problems=data["problems"], Mileage=data["mileage"], Bus_id=data["vehicle"].id, Route_id=data["route"].id, Driver_id=self.user.id) if data["type"] == "tram": new_driverreport = DriverReport(Problems=data["problems"], Mileage=data["mileage"], Train_id=data["vehicle"].id, Route_id=data["route"].id, Driver_id=self.user.id) session.add(new_driverreport) session.flush() day_report.DriverReport_id = new_driverreport.id session.commit()
def create_day_report_with_driverreport_inside(self, data): new_driverreport = None if data["type"] == "bus": new_driverreport = DriverReport(Problems=data["problems"], Mileage=data["mileage"], Bus_id=data["vehicle"].id, Route_id=data["route"].id, Driver_id=self.user.id) if data["type"] == "tram": new_driverreport = DriverReport(Problems=data["problems"], Mileage=data["mileage"], Train_id=data["vehicle"].id, Route_id=data["route"].id, Driver_id=self.user.id) session.add(new_driverreport) session.flush() new_dayreport = DayReport(Organisation_id=self.organisation.id, Date=data["date"], DriverReport_id=new_driverreport.id) session.add(new_dayreport) session.commit()
async def trade_finish(offer: Offer, bid: Bid, trade_type: str): # TODO -> Create a trade here trade = Trade(bid=bid.id, offer=offer.id, pending=true(), time=func.now()) accounts = session.query(Account).filter( Account.uid.in_([bid.uid, offer.uid])).all() if len(accounts) == 2: # Handle account balances transfer_between_accounts(accounts[1], accounts[0], offer.rate * offer.amount / COIN) trade.pending = false() offer.completed = true() bid.completed = true() try: session.add(offer) session.add(bid) session.add(trade) session.commit() session.flush() return True except exc.SQLAlchemyError as e: logger.info(e) return False
async def parse_eth_price_data(data, currency='cad'): result = session.query(ETHPrice).filter( ETHPrice.currency == currency).one() cur_time = datetime.datetime.now() if result is None: result = ETHPrice(currency=currency, last=data[0], modified=cur_time) try: session.add(result) session.commit() session.flush() except exc.SQLAlchemyError as e: print(e) return False result2 = session.query(ETHPriceRevision).filter( ETHPriceRevision.currency == currency).group_by( ETHPriceRevision.id).order_by(desc(func.max( ETHPriceRevision.rid))).first() rid = find_rid(result2) rid = rid + 1 if rid is not None else 1 revision_insert = ETHPriceRevision(rid=rid, currency=currency, last=data[0], date=cur_time, currency_id=result.id) try: session.add(revision_insert) session.commit() session.flush() except exc.SQLAlchemyError as e: print(e) return False return True
async def addSingleKey(key: str, uid: int): """ :param key: :param uid: :return: """ if key is not None and uid is not None: new_trx_key = TrxKey(value=key, uid=uid, multi=False, status=True) # TODO we should be checking if a key exists with the same hash # find_key = await find_key_for_uid(uid) session.add(new_trx_key) session.flush() single_key = SKey(value=key, uid=uid, kid=new_trx_key.id) session.add(single_key) try: session.commit() return single_key except exc.SQLAlchemyError as err: print(err.args) session.rollback() return err