def create_task(task_name, user: User) -> Task: if task_name in (task.name() for task in user.completed_tasks): raise AlreadyCompletedTaskException(ALREADY_COMPLETED_MSG % task_name) available_tasks = TaskFactory.available_tasks(user) if task_name not in available_tasks: raise NotAvailableTaskException(NOT_AVAILABLE_TASK_MSG % task_name) # Find task class and create instance task = next(filter(lambda x: x.name() == task_name, TASK_TYPES))() # For find user task we need to find another random user with image if type(task) == FindUserTask: if not user.image: raise NotAvailableTaskException( 'Please complete send selfie task first') session = Session.object_session(user) # ToDo: we need to have another user with selfie # available_users = session.query(User).filter(User.chat_id != self.chat_id).all() chat_id = user.chat_id available_users = [ user for user in session.query(User).all() if user.image and user.chat_id != chat_id ] if len(available_users) == 0: raise NotEnoughUsersException(NOT_AVAILABLE_TASK_MSG % task_name) user_to_find = random.choice(available_users) task.user_to_find = user_to_find return task
def _merge_local_accounts(long_acc, short_acc): """remove short_code account""" s = Session.object_session(short_acc) incoming = _query_incoming_transactions(short_acc) for trx in incoming: trx.beneficiary_id = long_acc outgoing = _query_outgoing_transactions(short_acc) for trx in outgoing: trx.payee_id = long_acc s.delete(short_acc) s.commit()
def _query_iban_account_candidate(local_acc): """Not checking if has more than one result, code is unique in model""" s = Session.object_session(local_acc) return s.query(Account).filter(Account.acc_type == "IBAN", Account.code.like('%' + local_acc.code)).first()
def _query_outgoing_transactions(acc): s = Session.object_session(acc) return s.query(Transaction).filter(Transaction.payee_id == acc.id)
def _query_incoming_transactions(acc): s = Session.object_session(acc) return s.query(Transaction).filter(Transaction.beneficiary_id == acc.id)