0

I have 3 functions. Each of them has the same line of code:

if request.method == "POST" and 'url' in request.POST:

Here are the functions:

def checkurl(request):
    if request.method == "POST" and 'url' in request.POST:
        url = request.POST.get('url', '')
        url_response = ""
        if not url.startswith('http://') and not url.startswith('https://'):
            url_get = "http://" + url
            url_response = requests.get(url_get, headers=headers, allow_redirects=False)
            url_status_code = url_response.status_code
            if url_status_code == 200:
                url = url_get
                return url
            else:
                url = "https://" + url
                return url
        return url


def netloc(request):
    url = checkurl(request)
    global url_scheme
    global url_port
    global url_netloc
    global url_path

    if request.method == "POST" and 'url' in request.POST:

        url_parsed = urlparse(url)
        url_scheme = url_parsed.scheme
        url_port = url_parsed.port
        url_netloc = url_parsed.netloc
        if url_netloc.startswith('www.'):
            url_netloc = url_netloc.replace('www.', '')
        if url_netloc.endswith('/'):
            url_netloc = url_netloc.replace('/', '')
        return url_scheme, url_port, url_netloc


def tests(request):
    if request.method == "POST" and 'url' in request.POST:
        url = checkurl(request)
        netloc(request)
        var = {
            'url':url,
            'url_scheme':url_scheme,
            'url_port':url_port,
            'url_netloc':url_netloc,
            }
        return render(request, 'apptests/shots.html', var)
    else:
        return render(request, 'apptests/shots.html')

I do not want to repeat the same line of code in each function and want to remove it and put it aside before these 3 functions. But I cannot do it

Please help

2
  • you have multiple options, first would be to write a decorator (docs.djangoproject.com/en/3.0/topics/http/decorators for your problem there are even exsisting decorators) or you could write a function that you call and takes the request as a parameter and then checks if method is right and so on. Commented Apr 28, 2020 at 10:40
  • I just saw, you just use the function "tests" the other two are just calles from the test function right? in that case just check it once in the "tests" function and later dont do it again. if the request is POST it wont change when you pass it to another function... Commented Apr 28, 2020 at 10:42

1 Answer 1

1

You could just create a function which does only check your if-statement:

def check_post(request):
    return request.method == "POST" and 'url' in request.POST

And your views.py could look like this:

# ...

def check_post(request):
    # ....


def checkurl(request):
    if check_post(request):
        # do stuff
    else:
        # do other stuff


def netloc(request):
    # ...

# ...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.