A general utility for your Django Rest Framework project.
Install or add general-methods.
pip install general-methods
Import utils.
from general_methods import utils
To get headers (Dictionary type) from request.
headers = utils.get_headers(request)
To get client IP Address.
client_ip_address = utils.get_client_ip_address(request)
To generate random alpha-numeric string. Default string length is 20.
token = utils.generate_token() // Default token length is 20 characters.
token = utils.generate_token(length=50) // Token length is 50 characters.
To get UUID string.
uuid = utils.get_uuid()
The available algorithms are : 'sha256', 'sha384', 'sha224', 'sha512', 'sha1', 'md5'
hashed_string = utils.sha1('string_to_hash')
hashed_string = utils.sha224('string_to_hash')
hashed_string = utils.sha256('string_to_hash')
hashed_string = utils.sha384('string_to_hash')
hashed_string = utils.sha512('string_to_hash')
hashed_string = utils.md5('string_to_hash')
To encode string data using django SECRET_KEY (available in settings.py file) as key to encode.
Note: If you changed the secret key of your django application then it won't decode the string back. Use only in run time use-cases for example encode user id before sending it to client side so client will never get to know the actual user id.
encoded_data = utils.encode('MY_DATA_TO_ENCODE')
To decode string data using django SECRET_KEY (available in settings.py file) as key to decode.
decoded_data = utils.decode('MY_DATA_TO_DECODE')
To identify JSON data is valid or not. If valid, returns True else False.
is_valid_json = utils.is_valid_json({'key': 'value'})
To identify email is valid or not. If valid, returns True else False.
is_valid_email = utils.is_valid_email('[email protected]')
Note: Returns application/json response.
Import response.
from general_methods import response
Returns dictionary { status: boolean, message: str, result: any }
Response code: 200
Function: response.success('Any Type of Data')
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.success('Any Type of Data') // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 400
Function: response.bad_request('Username is required.')
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.bad_request('Username is required.') // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 500
Function: response.server_error('Optional Message')
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.server_error() // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 200
Function: response.no_data_found()
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.no_data_found() // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 400
Function: response.param_missing(key, message)
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.param_missing('username', 'This field is required.') // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 401
Function: response.unauthorized()
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.unauthorized() // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 403
Function: response.forbidden()
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.forbidden() // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 405
Function: response.method_not_allowed()
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.method_not_allowed() // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: 406
Function: response.not_acceptable()
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.not_acceptable() // returning response to client.
Returns dictionary { status: boolean, message: str, result: any }
Response code: User Specified
Function: response.custom_error(status_code: int, result)
class MyView(APIView):
def get(self, request, *args, **kwargs):
return response.custom_error(status_code=200, result='Login Successful.') // returning response to client.
Note: Returns application/json response. Can be used in API exceptions. for example in API authorization or permission classes.
Import exceptions.
from general_methods import exceptions
Returns dictionary { status: boolean, message: str, result: any }
Response code: 500
raise exceptions.ServerException()
Returns dictionary { status: boolean, message: str, result: any }
Response code: 401
raise exceptions.UnauthorizedException()
Returns dictionary { status: boolean, message: str, result: any }
Response code: 401
raise exceptions.SessionExpiredException()
Returns dictionary { status: boolean, message: str, result: any }
Response code: 400
raise exceptions.MissingHeaderException()
Returns dictionary { status: boolean, message: str, result: any }
Response code: 401
raise exceptions.BlockedClientException()
Returns dictionary { status: boolean, message: str, result: any }
Response code: 401
raise exceptions.AuthenticationFailedException()
Can be used in defining Models.
Import validators.
from general_methods import validators
Function: email_validator
class MyModel(models.Model):
email = models.CharField(max_length=100, validators=[validators.email_validator])
Function: mobile_number_validator
class MyModel(models.Model):
mobile = models.CharField(max_length=15, validators=[validators.mobile_number_validator])
Function: country_code_validator
class MyModel(models.Model):
mobile = models.CharField(max_length=15, validators=[validators.country_code_validator])
Function: string_with_space_validator
class MyModel(models.Model):
mobile = models.CharField(max_length=15, validators=[validators.string_with_space_validator])
All country codes are available.
Array of Objects like:
{ 'country_code': "IN", 'calling_code': "+91", 'name': 'India (+91)' }
Import country_codes.
from general_methods import country_codes