Search code examples
python-3.xdjangodjango-rest-frameworkpython-decorators

how to check a condition before exectuing any apis - python


My project has many APIs. but now I have to put a check in all of these APIs before start executing the codes in it. I have to make sure that API only returns data if that particular form's deactivation date is grater than datetime.date.today(). Queryset would be -

available_for_entry = Forms.objects.filter(form_id=form_id, deactivate_date__gte=today_date).exists()

One way to do this to write that query set in each of APIs and then use if available_for_entry: condition. But, I was wondering if there is any other way to achieve this without writing similar conditions so many times.

I tried to achieve this with writing decorator but I could not able to pass the form_id value from function based API to that decorator. snapshot of the code is...

def is_form_available(func):
    def checking():
        today_date = datetime.date.today()
        available_for_entry = Forms.objects.filter(form_id=form_id, deactivate_date__gte=today_date).exists()
        if available_for_entry:
            func()
        else:
            print('form is not available now')
    return checking
    

@api_view(['POST'])
@is_form_available
def question_list(request):
  form_id = request.POST.get('form_id')
  parameter_list = (FormParameters.objects.filter(mappings__form__formparametermappings=   form_id).all())
  serializer = FormParametersQuizSerializer(parameter_list, many=True)

  return serializer.data

Solution

  • Decorator is a good idea, you can get some inspiration from native Django decorators like require_http_methods

    from functools import wraps
    
    def is_form_available(func):
        @wraps(func)
        def checking(request, *args, **kwargs):
            form_id = <extract from request>
            today_date = datetime.date.today()
            available_for_entry = Forms.objects.filter(form_id=form_id, deactivate_date__gte=today_date).exists()
            if available_for_entry:
                return func(args, kwargs)
            else:
                print('form is not available now')
        return checking