async def execute(self, conn, redis=None): async with conn.transaction() as tx: if self.sender_user_id == self.recipient_user_id: raise ValidationError('sender can not be equal recipient') sender_user = await User.query.where( User.id == self.sender_user_id).with_for_update().gino.first() if not sender_user: raise ValidationError('sender user not found') recipient_user = await User.query.where( User.id == self.recipient_user_id ).with_for_update().gino.first() if not recipient_user: raise ValidationError('recipient user not found') currency_converter = CurrencyConverter(redis=redis) send_value_in_user_currency = await currency_converter.convert( self.sender_value, self.sender_currency, sender_user.currency) if send_value_in_user_currency > sender_user.balance: raise ValidationError('you have not cash for transfer') recipient_user_currency_amount = await currency_converter.convert( self.sender_value, self.sender_currency, recipient_user.currency) await Transaction.create( transaction_type=TRANSACTION_TYPE_TRANSFER, sender_user_id=self.sender_user_id, sender_currency=sender_user.currency, sender_value=send_value_in_user_currency, recipient_user_id=self.recipient_user_id, recipient_currency=recipient_user.currency, recipient_value=recipient_user_currency_amount, ) new_sender_balance = await Transaction.calculate_user_balance_by_transactions( self.sender_user_id) await User.update.values(balance=new_sender_balance ).where(User.id == self.sender_user_id ).gino.status() if new_sender_balance < 0: await tx.rollback() raise ValidationError('you have not cash for transfer') new_recipient_balance = await Transaction.calculate_user_balance_by_transactions( self.recipient_user_id) await User.update.values(balance=new_recipient_balance ).where(User.id == self.recipient_user_id ).gino.status()
def check_required_args(data_dict, cls): sig = inspect.signature(cls.__init__) req_params = set(p.name for p in sig.parameters.values() if p.name != "self" and p.default == inspect._empty) not_found_params = req_params.difference(set(data_dict.keys())) if not_found_params: raise ValidationError(f'please provide required parametres: {not_found_params}')
async def execute(self, conn): try: await User.create(name=self.name, country=self.country, city=self.city, currency=self.currency) except UniqueViolationError: raise ValidationError("user name already exists")
def __post_init__(self): for field in fields(self): attr = getattr(self, field.name) if not attr or isinstance(attr, field.type): continue elif hasattr(field.type, '_origin_type') and isinstance( attr, field.type._origin_type): setattr(self, field.name, field.type(attr)) else: raise ValidationError(f'wrong type of field {field}')
def post(self): req = request.get_json(force=True) # logger.info("request: {}".format(req)) # Validate and deserialize input data, errors = request_schema.load(req) logger.info("data: {}".format(data)) if errors: raise ValidationError(errors=errors) response = DelayModelHelpers().get_response(data) code = 200 return make_response(jsonify({"delayed": response}), code)
async def execute(self, conn): user = await User.query.where(User.name == self.user_name).gino.first() if not user: raise ValidationError('user not found') query = Transaction.query.where( or_(Transaction.recipient_user_id == user.id, Transaction.sender_user_id == user.id)) if self.start_date: query = query.where(Transaction.timestamp >= self.start_date. replace(hour=0, minute=0, second=0)) if self.end_date: query = query.where(Transaction.timestamp <= self.end_date.replace( hour=23, minute=59, second=59)) transactions = await query.order_by('timestamp').gino.all() return transactions
async def execute(self, conn, redis=None): async with conn.transaction(): user = await User.query.where(User.id == self.user_id ).with_for_update().gino.first() if not user: raise ValidationError('user not found') currency_converter = CurrencyConverter(redis=redis) user_currency_amount = await currency_converter.convert( self.value, self.currency, user.currency) await Transaction.create( transaction_type=TRANSACTION_TYPE_DEPOSIT, sender_currency=self.currency, sender_value=self.value, recipient_user_id=self.user_id, recipient_currency=user.currency, recipient_value=user_currency_amount, ) new_user_balance = await Transaction.calculate_user_balance_by_transactions( self.user_id) await User.update.values(balance=new_user_balance ).where(User.id == self.user_id ).gino.status()
def choices_validator(val, choices=None): if val not in choices: raise ValidationError(f'use one of: {choices}')
def length_validator(val, max_length=None): if len(val) > max_length: raise ValidationError('value too long')
def scale_validator(val, scale=None): scale_part_len = len(str(val - int(val))[2:]) if scale_part_len != scale: raise ValidationError(f'scale part must be {scale_part_len} digits')