Skip to content

Instantly share code, notes, and snippets.

@Barolina
Last active November 28, 2019 09:28
Show Gist options
  • Save Barolina/13bcdb9942302744dd88f1107b5154d2 to your computer and use it in GitHub Desktop.
Save Barolina/13bcdb9942302744dd88f1107b5154d2 to your computer and use it in GitHub Desktop.
multilayer (buisness logic)
'''
So my layers would be like this:
1. Presentation Layer: Used for translation from HTTP to Python and Python to HTTP
2. Authentication and Authorization Layer: for identifying the user and his information and permissions
3. Service Layer: For executing the Business Logic
4. Validation Layer: For validating the integrity and constraint of the data that is gonna be store
5. Data Layer: Orm layer
1. Represented by a URL of the router function of DRF and a ViewSet of DRF as well
2 Django Rest makes a really good proposal using his permissions classes on a ViewSet level
3 For executing the Business Logic we choose to create separated services.py where you can arrange and combine business logic as you want.
4. For validating the integrity and constraint of the data that is gonna be stored. I tried before DRF serializers and Django Forms, both too heavy to only do validation, so I tested Cerberus library
http://docs.python-cerberus.org/en/stable/
'''
class PaymentHistoryVIPValidator:
"""
Validator Class for VIP Payment
"""
schema = {
'buyer_id': {'type': 'integer', 'empty': False, 'nullable': False},
'status': {'type': 'string', 'allowed': [i[0] for i in STATUS_PAYMENT]},
'commission_id': {'type': 'integer', 'empty': False, 'nullable': False},
'payerID': {'type': 'string', 'empty': True, 'nullable': True},
'paymentId': {'type': 'string', 'empty': True, 'nullable': True},
'paymentMethod': {'type': 'string', 'allowed': [i[0] for i in PAYMENT_METHOD], 'nullable': False},
'typeTransaction': {'type': 'string', 'allowed': [i[0] for i in TYPE_TRANSACTION], 'nullable': False},
'amount': {'type': 'number', 'empty': False, 'nullable': False},
'liable': {'type': 'string', 'empty': True, 'nullable': True},
'code': {'type': 'string', 'empty': False, 'nullable': False},
}
def __init__(self, data):
self.validator = Validator()
self.data = data
self.schema = self.__class__.schema
def validate(self):
return self.validator.validate(self.data, self.schema)
def errors(self):
return self.validator.errors
'''
4. Orm layer. Just plain old Django ORM (which i love)
'''
# from https://cobuildlab.com/development-blog/2018-07-02-slim-multilayer-architecture-for-rest-api-using-python-with-django-cerberus-serpy-and-django-rest/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment