Beispiel #1
0
 async def get_user_id(self, code):
     code_map = await self.code_map
     result = ''
     for char in code:
         for key in code_map:
             if char == code_map[key]:
                 result += key
     try:
         return int(result)
     except ValueError:
         pass
     except Exception as e:
         print_tb(e)
Beispiel #2
0
 async def update_rates_history():
     count_query = RateHistory.select()
     count = await objects.count(count_query)
     await objects.create(
         RateHistory, btc_usd=bitmex_btc_usd, coinbase=coinbase_btc_usd, kraken=kraken_btc_usd
     )
     if count >= 11:
         query = RateHistory.select().order_by(RateHistory.id.asc())
         histories = await objects.execute(query)
         try:
             for hist in histories[0: count-10]:
                 await objects.delete(hist)
         except Exception as e:
             print_tb(e)
Beispiel #3
0
async def global_scheduler(argv):
    config = get_config(argv=[])
    app = create_app(config=config)
    bot = app['bot']
    all_users = await objects.execute(query=User.select())
    for user in all_users:
        try:
            await objects.update(user)
            profile = await get_user_profile(user=user)
            _ = await get_translator(profile)
            profile.steps += 2
            await objects.update(profile)
            message_text = "%s <i>%s</i> %s: <b>+2</b> 🥾" % (_('You have been '), _('recovered'), _('a bit and can move on'))
            print("%s\n" % user)
            await bot.send_message(chat_id=user.id, text=message_text, parse_mode="HTML")
        except Exception as e:
            print_tb(e)
Beispiel #4
0
 async def get_redis_value(self, key):
     try:
         redis_record = REDIS.get(self.profile.user.id).decode()
     except AttributeError:
         REDIS.set(self.profile.user.id,
                   json.dumps({STATE.basic_state: 'REST'}))
         redis_record = REDIS.get(self.profile.user.id).decode()
     try:
         json_redis_record = json.loads(redis_record)
     except Exception as e:
         print_tb(e)
         json_redis_record = None
     try:
         value = json_redis_record[key]
     except KeyError:
         value = None
     # logging.info("REDIS value={value}".format(value=value))
     return value
Beispiel #5
0
def migrate():
    # create tables migration:
    migrations_pack = 'migrations'
    # importlib.import_module('%s.create_tables' % migrations_pack)

    # other migrations:
    all_modules = os.listdir(
        os.path.join(
            os.path.dirname(__file__).replace('application', ''),
            'migrations'))
    for mdl in sorted(all_modules):
        if 'migration' in mdl:
            print(mdl)
            try:
                importlib.import_module(
                    '%s.%s' % (migrations_pack, mdl.replace('.py', '')))
            except Exception as e:
                print_tb(e)
        else:
            pass
 async def _get_obj(self):
     cq_data = self.update.callback_query.data
     model_name = cq_data.split(':')[1].split('|')[0]
     klass = None
     uri = '%s/models/' % ROOT_DIR
     for mod_file in get_all_python_files(uri):
         mod_name = mod_file.rsplit('/', 1)[1].replace('.py', '')
         flag = True
         while flag:
             try:
                 mod = __import__('%s.models.%s' %
                                  (MAIN_APP_NAME, mod_name),
                                  fromlist=[model_name])
                 flag = False
                 klass = getattr(mod, model_name)
             except AttributeError:
                 pass
     try:
         instance = klass.get(klass.id == self.model_id)
         return instance
     except klass.DoesNotExist as kde:
         print_tb(kde)
         return None
    async def process_transaction(self, transaction: Transaction,
                                  bot: Bot) -> Transaction:
        document = await Billing.get_document(transaction=transaction)
        user = document.user
        if not transaction.processed_at:
            transaction.processed_at = datetime.datetime.now()
            try:
                document.status = BaseDocument.Status.Processed
                await Billing._change_user_balance(user=user,
                                                   amount=transaction.amount,
                                                   tr_type=transaction.tr_type)
                # make blockcypher payment
                if transaction.tr_type == Transaction.Types.credit:
                    try:
                        btc_worker = BlockcypherWorker(config=self.config)
                        tr_hash = await btc_worker.send_money(
                            to_address=transaction.address_to,
                            amount=await
                            get_satoshis_from_btc(btc_count=transaction.amount
                                                  ))
                        transaction.tr_hash = tr_hash
                        await objects.update(transaction)
                        link = 'https://live.blockcypher.com/btc/tx/{tr_hash}'.format(
                            tr_hash=tr_hash)
                        text = 'User <b>{user}</b> withdraw <code>{amount}</code> <b>BTC</b>\n{link}'.format(
                            user=user, amount=transaction.amount, link=link)
                        await bot.send_message(chat_id=await get_root_user(),
                                               text=text,
                                               parse_mode='HTML',
                                               disable_web_page_preview=True)
                    except Exception as send_money_error:
                        print_tb(send_money_error)

            except NotEnoughFunds as nef:
                print_tb(nef)
                raise NotEnoughFunds
            except Exception as e:
                print_tb(e)
                raise TrxProcessFailed(message=e)
        else:
            raise TrxProcessFailed(message='Transaction was already processed')
        await objects.update(user)
        await objects.update(transaction)
        await objects.update(document)
        return transaction
Beispiel #8
0
from peewee import ProgrammingError, BooleanField
from playhouse.migrate import migrate, PostgresqlMigrator

from application.models import database
from application.utils import print_tb

migrator = PostgresqlMigrator(database)

try:
    sex = BooleanField(default=True)
    migrate(migrator.add_column('bot_person', 'sex', sex))
except ProgrammingError as pe:
    print_tb(pe)
    database.rollback()
except Exception as e:
    database.rollback()
from application.models import database, Person, Skill, PersonSkill
from application.utils import print_tb

try:
    database.create_tables(
        [
            Person, Skill, PersonSkill
        ]
    )
except Exception as e:
    print_tb(e)
    database.rollback()