Esempio n. 1
0
 def test_hash(self):
     """
     Tests that hash function is a bijection
     """
     for i in xrange(len(PRIMES)-1):
         num = 56 ** i + random.randint(1, 50)
         self.assertEqual(mmi_unhash(mmi_hash(num)), num)
Esempio n. 2
0
def shorten_url(request):
    if request.POST:
        form = UrlForm(request.POST)
        if form.is_valid():
            origin = form.cleaned_data['origin']
            url_hash = hashlib.sha1(origin).hexdigest()
            try:
                url = Url.objects.get(hash=url_hash)
                new = False
            except Url.DoesNotExist:
                new = True
                url = Url.objects.create(origin=origin, hash=url_hash,
                    created_by=get_client_ip(request))
                url.short= mmi_hash(url.id)
                url.save()
            return HttpResponse(json.dumps({'is_new': new, 'shorten': url.short}),
                mimetype="application/json")
        return HttpResponseBadRequest(form.errors['origin'])
    raise Http404