Package: request_casting
request_castingPython module that allows to cast django request data for POST and GET methods easylyAllows you to capture errors in parameters and give them default values, to protect your application with little code and easy to read.InstallationYou can use pip to install this modulepipinstallrequest_castingCastsRequestBoolGets a parameter from a djangorestframework or django view and cast it to a bool object. For examplefromrequest_castingimportrequest_castingfromrest_framework.decoratorsimportapi_viewfromrest_framework.responseimportResponse@api_view(['GET','POST'])defmyview(request):a=request_casting.RequestBool(request,"a")returnResponse({"a":a,"class":a.__class__.__name__},status=status.HTTP_200_OK)You can call this view with a GET method with requests, curl, axios...curlhttp://localhost:8000/myview/?a=truecurl-XPOSThttp://localhost:8000/myview/-d"a=false"You'll get this answer in both cases{"a":true,"class":"bool"}All request_casting methods allow to set a default value. By default this value is None in all Request methods.  This value is returned when cast fails.curlhttp://localhost:8000/myview/?a=BADBOOLYou'll get this answer in both cases{"a":null,"class":"NoneType"}RequestDateUse this method inside a view to get a casted date. Use dates in Iso format# ... The same as RequestBool examplea=request_casting.RequestDate(request,"a")You'll get this answerscurl http://localhost:8000/myview/?a=BADDATE   => a will be None
curl http://localhost:8000/myview/?a=2021-1-1   => a will be date(2023,1,1)RequestDecimalUse this method inside a view to get a casted Decimal# ... The same as RequestBool examplea=request_casting.RequestDecimal(request,"a",Decimal(0))You'll get this answerscurl http://localhost:8000/myview/?a=12.1212  => a will be Decimal(12.1212)
curl http://localhost:8000/myview/?a=2021-1-1   => a will be Decimal(0)RequestDtawareUse this method inside a view to get a datetime with timezone. Use dates in Iso format# ... The same as RequestBool examplea=request_casting.RequestDtaware(request,"a")You'll get this answerscurl http://localhost:8000/myview/?a=2011-10-05T14:48:00.000Z   => a will be a datetime with timezone
curl http://localhost:8000/myview/?a=2021-1-1   => a will be NoneRequestIntegerUse this method inside a view to get a casted Integer# ... The same as RequestBool examplea=request_casting.RequestInteger(request,"a")You'll get this answerscurl http://localhost:8000/myview/?a=12 => a will be 12
curl http://localhost:8000/myview/?a=BADINTEGER  => a will be NoneRequestListOfBoolsUse this method inside a view to get a list of Booleans# ... The same as RequestBool examplea=request_casting.RequesListOfBools(request,"a")You'll get this answerscurl"http://localhost:8000/myview/?a[]=true&a[]=false"=>awillbealist[True,False]RequestListOfIntegersUse this method inside a view to get a list of Integers# ... The same as RequestBool examplea=request_casting.RequestListOfIntegers(request,"a")You'll get this answerscurl"http://localhost:8000/myview/?a[]=1&a[]=2"=>awillbealist[1,2]RequestListOfStringsUse this method inside a view to get a list of strings# ... The same as RequestBool examplea=request_casting.RequestListOfStrings(request,"a")You'll get this answerscurl"http://localhost:8000/myview/?a[]=a&a[]=b"=>awillbealist["a","b"]RequestStringUse this method inside a view to get a casted String# ... The same as RequestBool examplea=request_casting.RequestString(request,"a")You'll get this answerscurl http://localhost:8000/myview/?a=12 => a will be "12"
curl http://localhost:8000/myview/?a=BADINTEGER  => a will be "BADINTEGER"RequestUrlUse this method inside a view to get a django model object using its hyperlinked url# ... The same as RequestBool examplea=request_casting.RequestUrl(request,"a",models.Record,model_url="records")You'll get this answerscurl"http://localhost:8000/myview/?a=http://localhost:8000/api/records/1/"=>awillbeaRecordobjectwithpk=1RequestListOfUrlsUse this method inside a view to get a list of django model object using its hyperlinked url# ... The same as RequestBool examplea=request_casting.RequestListOfUrls(request,"a",models.Record,model_url="records")You'll get this answerscurl"http://localhost:8000/myview/?a[]=http://localhost:8000/api/records/1/&a[]=http://localhost:8000/api/records/2/"=>awillbealistwithRecordobjectswithpk=1andpk=2Other usefull functionsall_args_are_not_emptyReturns True if all function arguments are different to None and ""It's very usefull to compare view parameters fast.request_casting.all_args_are_not_empty(None,"",None)#Returns Falserequest_casting.all_args_are_not_empty("","","")# Returns Falserequest_casting.all_args_are_not_empty(1,1,1)#Return Trueall_args_are_not_noneReturns True if all function arguments are different to NoneIt's very usefull to compare view parameters fast.request_casting.all_args_are_not_none(None,"",None)#Returns Falserequest_casting.all_args_are_not_none("","","")# Returns Truerequest_casting.all_args_are_not_none(1,1,1)#Return TrueTest moduleRunpoe coverageto test module.Changelog0.5.0 (2023-12-05)Added support to request_casting with json formats (APICLIENT)0.4.0 (2023-12-05)Migrated to pydicts-0.9.00.3.0 (2023-12-04)Improved parse_from_url and object_from_url validationsAdded validate_object to RequestUrl and RequestListOfUrls to validate returned objectsAdded gettext support0.2.0 (2023-11-18)Improving documentationAll default values are set to None, including RequestList methodsstring2dtaware now uses ZoneInfo instead of pytz0.1.0 (2023-11-15)Converted reusingcode module to an independent module