Exemplo n.º 1
0
def shortener(request):
    real_url = request.POST.get('long_url', '')

    long_url = validate_url(real_url)
    if not long_url:
        return render(request, 'url_validation_error.html', {'bad_url': real_url})

    elif Master.objects.filter(long_url=long_url).exists():
        temp_short = Master.objects.get(long_url=long_url)
        return render(request, 'exists.html', {'shorten': temp_short.short_url})

    else:

        record = Master(short_url='', long_url=long_url, creation_time=datetime.now(), clicks=0)
        record.save()

        # Unique Code Generator Function for SHORT_URL
        short_url = encode(record.id)

        Master.objects.filter(id=record.id).update(short_url=short_url)

        return render(request, 'shortener.html', {'long_url': long_url, 'short_url': short_url})
Exemplo n.º 2
0
 def test_encode_decode(self):
     for i in range(0, 1234567, randint(20, 100)):
         self.assertEqual(decode(encode(i)),i)