0

Let say I want to test several data before proceeding and if it fails return an error response directly to the person who made the request.

I have this:

def gerUserInfo(request):
    if request.user.is_authenticated:
        data = json.loads(request.body.decode('utf-8'))
    
        info1 = data.get("info1") if data.get("info1") else ""
        if info1.strip() == "": 
            return JsonResponse({"status":"fail"})
        else: 
            #proceed...
        
        info2 = data.get("info2") if data.get("info2") else ""
        if info2.strip() == "": 
            return JsonResponse({"status":"fail"})
        else: 
            #proceed...
        
        ...

I want this:

def gerUserInfo(request):
  def secureGetData(data):
    try:
        data = data if data else ""
        if data.strip() == "": return JsonResponse({"status":"fail"})
        else: return data
    except:
        return JsonResponse({"status":"fail"})
        
        
  if request.user.is_authenticated:
    data = json.loads(request.body.decode('utf-8'))
    info1 = secureGetData(data.get("info1"))
    info2 = secureGetData(data.get("info2"))
    ...
    # i can proceed without worring...

I want the server response the request from secureGetData(), but never do.

EDIT:

Basically what I'm trying to do is to make a return from a nested function like here in JS for a loop, I want to do it with a return, I'm even not sure it's possible in JS...

Zorro
  • 1,085
  • 12
  • 19
  • `return JsonResponse({"status":"fail"})` – Andrey Maslov Jul 13 '20 at 10:07
  • it's what i have in my code, it was juste a mistake. i think secureGetData() trying to store the server response on the variable. – Zorro Jul 13 '20 at 10:16
  • you can use `return secureGetData(data.get("info2"))`, but you need to change line `return data` to something like that `return JsonResponse(data)` – Andrey Maslov Jul 13 '20 at 10:22
  • Thanks, but what I actually want the function to do for me is either answer the query or return me some data that I can use before responding to the request myself later, hence the line ```return data```. – Zorro Jul 13 '20 at 10:39
  • The link is not working – Madalosso Jul 13 '20 at 21:19

1 Answers1

0

Looks like you could use some refactor on your approach.

You should separate the logic of extract/validate data and the response that you'll create out of it.

Doesn't make sense for a function named secureGetData to return a JsonResponse instead of a data-like structure. It looks like you're trying to not repeat yourself by changing your first implementation, and that's right, but you might want to call a function to fetch the valid data or none data at all and use it to decide which response the function returns.

def gerUserInfo(request):
  def validate_data(data):
    # validate all your data. and return a dict ({} if data is not valid)
        
        
  if request.user.is_authenticated:
    data = json.loads(request.body.decode('utf-8'))
    data = validate_data(data)
    if data:
      # proceed without worring...
    else:
      # return JsonResponse({"status":"fail"})
Madalosso
  • 108
  • 2
  • 5
  • so if i undestand this line ```return JsonResponse({"status":"fail"})``` can not be executed by validate_data? – Zorro Jul 13 '20 at 20:06
  • Well.. it definitely can, but this approach would force you to perform a check for the type of the returned value from the nested function. What I am suggesting is that you use the nested function to validate your data and return the values that you're expecting on your request, and if the values aren't available, return a empty dictionary, which you can later evaluate to bool (at `gerUserInfo`) and then make the decision if you want to return a JsonResponse with a fail status or proceed with your routine knowing that the data is good. – Madalosso Jul 13 '20 at 20:26
  • Thank you but in fact this approach I had already thought about it and this is probably what I will end up doing. But the reason I ask this question is that I want the ```make the decision``` you mentioned to be made by ```validate_data``` without going back to ```gerUserInfo```. – Zorro Jul 13 '20 at 21:13