
from datetime import datetime

from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect

from adzone.models import AdBase, AdClick


def ad_view(request, id):
    """ Record the click in the database, then redirect to ad url """
    ad = get_object_or_404(AdBase, id=id)

    click = AdClick.objects.create(
        ad=ad,
        click_date=datetime.now(),
        source_ip=request.META.get('REMOTE_ADDR', '')
    )
    click.save()

    redirect_url = ad.url
    if not redirect_url.startswith('http://'):
        # Add http:// to the url so that the browser redirects correctly
        redirect_url = 'http://' + redirect_url

    return HttpResponseRedirect(redirect_url)


import re

from datetime import datetime

from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect

from adzone.models import AdBase, AdClick


http_re = re.compile(r'^https?://')


def ad_view(request, id):
    """ Record the click in the database, then redirect to ad url """
    ad = get_object_or_404(AdBase, id=id)

    click = AdClick.objects.create(
        ad=ad,
        click_date=datetime.now(),
        source_ip=request.META.get('REMOTE_ADDR', '')
    )
    click.save()

    redirect_url = ad.url
    if not http_re.match(redirect_url):
        # Add http:// to the url so that the browser redirects correctly
        redirect_url = 'http://' + redirect_url

    return HttpResponseRedirect(redirect_url)

Improve http check to allow https as well
Improve http check to allow https as well

We switch from 'startswith' to a regex check which allows both as we
tested with https facebook urls and it failed to handle them properly.

+ 
+ import re
  
  from datetime import datetime
  
  from django.shortcuts import get_object_or_404
  from django.http import HttpResponseRedirect
  
  from adzone.models import AdBase, AdClick
+ 
+ 
+ http_re = re.compile(r'^https?://')
  
  
  def ad_view(request, id):
      """ Record the click in the database, then redirect to ad url """
      ad = get_object_or_404(AdBase, id=id)
  
      click = AdClick.objects.create(
          ad=ad,
          click_date=datetime.now(),
          source_ip=request.META.get('REMOTE_ADDR', '')
      )
      click.save()
  
      redirect_url = ad.url
-     if not redirect_url.startswith('http://'):
+     if not http_re.match(redirect_url):
          # Add http:// to the url so that the browser redirects correctly
          redirect_url = 'http://' + redirect_url
  
      return HttpResponseRedirect(redirect_url)
  
Improve http check to allow https as well
## Code Before:

from datetime import datetime

from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect

from adzone.models import AdBase, AdClick


def ad_view(request, id):
    """ Record the click in the database, then redirect to ad url """
    ad = get_object_or_404(AdBase, id=id)

    click = AdClick.objects.create(
        ad=ad,
        click_date=datetime.now(),
        source_ip=request.META.get('REMOTE_ADDR', '')
    )
    click.save()

    redirect_url = ad.url
    if not redirect_url.startswith('http://'):
        # Add http:// to the url so that the browser redirects correctly
        redirect_url = 'http://' + redirect_url

    return HttpResponseRedirect(redirect_url)

## Instruction:
Improve http check to allow https as well
## Code After:

import re

from datetime import datetime

from django.shortcuts import get_object_or_404
from django.http import HttpResponseRedirect

from adzone.models import AdBase, AdClick


http_re = re.compile(r'^https?://')


def ad_view(request, id):
    """ Record the click in the database, then redirect to ad url """
    ad = get_object_or_404(AdBase, id=id)

    click = AdClick.objects.create(
        ad=ad,
        click_date=datetime.now(),
        source_ip=request.META.get('REMOTE_ADDR', '')
    )
    click.save()

    redirect_url = ad.url
    if not http_re.match(redirect_url):
        # Add http:// to the url so that the browser redirects correctly
        redirect_url = 'http://' + redirect_url

    return HttpResponseRedirect(redirect_url)
