Package: django-sa-request
Django-SARequestSimple, compact and useful library to read request parameters in django framework.InstallThere are 2 methods to install this:1- Install with pip (Recommended and the easiest way)pip install sa_request2- Install from source code:git clone https://github.com/sauditore/SARequest.git
python setup.py installUsage:To use this library just create a class and inherit from sa_request main classfromsa_request.helperimportSARequestfromsa_request.exceptionsimportParamNotFoundError,AuthNeedErrorclassViewName(SARequest):defget(self,request):# Get int_name from query stringuser_int=self.get_int("int_name",False,100)# Get str_name from query string. If data was not found then "default_data" will returnuser_str=self.get_string("str_name",False,"default_data")# Get float_name from query string. If data was not found then 1.1 will returnuser_float=self.get_float("float_name",False,1.1)try:# If raise_error is set True, then default value ignored# and and exception will raise.str_data=self.get_string("parameter_name",True)# Checks for user permission. If permission not granted then# AuthError will raiseself.validate_request(perm="user.add_user")exceptParamNotFoundErrorase:# A response will return to userreturne.get_response()exceptAuthNeedErrorase:# redirect user to auth_login viewreturne.get_response()# and other logicsBasic Structure:Most of the methods have a similar signature:method(name, raise_error, default)name: Name of the parameter to read from query string or post dataraise_error: If data is not provided, then raise ParamNotFoundError error.default: If raise_error is set to False, then this value will return as result.Methods:get_int:Get "name" as int from request:defget_int(name:str,raise_error:bool=False,default:int=0,min_value:int=None,max_value:int=None)->int:get_string:Get "name" from request as str with min / max length validationdefget_string(name:str,raise_error:bool=False,default:str='',min_len:int=0,max_len:int=0)->str:paginate:Paginate query set or list and returns it to use in a template. Queryset or data key name in a dictionary is
the samedata_nameprovided. Per Page parameter is read fromppquerystring. Current Page is also
read fromcpquerystring.
Result contains these keys:1-data_name: Contains paginated data2-next: Next number to pass tocpparameter to view next page.3-back: Back number to pass tocpparameter in querystring to view previous page4-current_page: Current page number5-pages: Total number of pages6-last_page: Number of last page to pass tocpparameter to view7-request: request object to build URL8-is_last_page: True if reached last page9-is_first_page: True if cp is on the first page10-total_result: Number of records to view11-next_pages: List of pages to view for the next pages. Each item must pass to cp param to view the page.12-back_pages: List of pages to view for previous pages. Each item must pass to cp param to view the pate.defpaginate(query_set:Union[List,QuerySet],data_name:str,extra:Dict=None,default_per_page:int=10)get_decrypted_list:Process request and find objects by name, decrypt and return in a listdefget_decrypted_list(name:str,raise_error:bool=False,default:List=None)->Listget_int_list:Process the request and get a list of intdefget_int_list(name:str,raise_error:bool=False,default:List=())->Listget_file_size:Convert user input data to file size. e.g. User enters : 1024 MB, you will receive : 1024 * 1024 bytesdefget_file_size(name:str,raise_error:bool=False,default:float=0)->floatget_float:Get "name" as floatdefget_float(name:str,raise_error:bool=False,default:float=0.0)->floatBugs:I would be happy if you help me find them.TODO:Add more useful functions