class CouponSchema(Schema): id = fields.Integer() name = fields.String(attribute='revised_coupon_name') link = fields.String() status = fields.String() expiry_time = fields.DateTime(attribute='end_date', format='%Y-%m-%d %H:%M:%S') description = fields.String() published = fields.Boolean() merchant = fields.String(attribute='mechant.name') categories = fields.Method('get_categories') type = fields.Method('get_type') def get_categories(self, instance, **kwargs): categories = [] for cat in instance.category.all(): categories.append(cat.name) return categories def get_type(self, instance, **kwargs): coup_type = [] for item in instance.type.all(): coup_type.append(item.name) return coup_type
class OTPSchema(Schema): """ OTP Verification API Schema """ mobile = fields.Number(required=True) otp = fields.Number(required=True) gender = fields.String(required=False, allow_none=True) email = fields.String(required=False, allow_none=True)
class WebHookPlaidApiSchema(Schema): """ Webhook api for plaid Schema """ webhook_code = fields.String() webhook_type = fields.String() item_id = fields.String() error = fields.String(allow_none=True) new_transaction = fields.String() @post_load def get_item_obj(self, data): try: data['item'] = Item.objects.get(item_id=data['item_id']) except Exception as e: InvalidDataException('Item doesnt exist') return data
class TransactionSchema(Schema): """ Account Schema category = models.ManyToManyField(TransactionCategory, blank=True, null=True) """ name = fields.String() transaction_id = fields.String() pending_transaction_id = fields.String() transaction_type = fields.String() currency = fields.String() date = fields.Date() authorized_date = fields.Date() pending = fields.Boolean() amount = fields.Number() categories = fields.Method('get_categories') def get_categories(self, instance, **kwargs): categories = [x['name'] for x in list(instance.category.all().values('name'))] return categories
class LoginSchema(Schema): """ User API Schema """ name = fields.String(required=True) mobile = fields.Number(required=True) @validates("mobile") def validates_mobile(self, value): if not valid_mobile_number(value): raise InvalidDataException("Not a valid mobile Number") @post_load def post_processing(self, data, **kwargs): data['otp'] = create_otp() data['existing_user'] = not User.objects.filter( mobile=data['mobile']).exists() return data
class AccountSchema(Schema): """ AccountSchema """ item_id = fields.String(attribute='item.item_id') updated = fields.String(attribute='item.updated') current_balance = fields.Number() available_balance = fields.Number() account_id = fields.String(required=False, allow_none=True) official_name = fields.String(required=False, allow_none=True) currency = fields.String(required=False, allow_none=True) type = fields.String(required=False, allow_none=True) subtype = fields.String(required=False, allow_none=True) transactions = fields.Method('get_transaction') def get_transaction(self, instance, **kwargs): return TransactionSchema().dump(instance.transaction_account.all(),many=True)
class CategorySchema(Schema): id = fields.Integer() name = fields.String()
class PublicTokenApiSchema(Schema): """ User API Schema """ public_token = fields.String(required=True)