def target(request, URLid): al = algo() urlid = al.decode(URLid) + 1 target = get_object_or_404(models.Link, pk=urlid) targetURL = target.targetURL print('targetURL:', targetURL) return HttpResponseRedirect(targetURL)
def shorten(request): form = forms.LinkForm() al = algo() shortenURL = '' if request.method == 'POST': form = forms.LinkForm(request.POST) if form.is_valid(): targetURL = form.cleaned_data['targetURL'] if targetURL not in cache: p = PhishTank() result = p.check(targetURL) print((result.in_database)) # for debug purpose if not result.in_database: link = check_and_create(targetURL) shortenURL = link.shortenURL cache.set(targetURL, result.in_database) else: shortenURL = "Sorry we cannot provide service to phish sites." else: response = cache.get(targetURL) link = check_and_create(targetURL) shortenURL = link.shortenURL return render(request, 'index.html', { 'form': form, 'shortenurl': shortenURL })
def shorten(request): form = forms.LinkForm() al = algo() shortenURL = '' if request.method == 'POST': form = forms.LinkForm(request.POST) if form.is_valid(): targetURL = form.cleaned_data['targetURL'] link = check_and_create(targetURL) shortenURL = link.shortenURL return render(request, 'index.html', { 'form': form, 'shortenurl': shortenURL })
def check_and_create(targetURL): al = algo() shortenURL, created_date = '', '' targetURL = targetURL.lower() if urlparse(targetURL).scheme == '': targetURL = 'http://' + targetURL link = models.Link.objects.filter(targetURL=targetURL) if link.count() == 0: link = models.Link() urlid = models.Link.objects.count() shortenURL = BASE_URL + al.encode(urlid) created_date = timezone.now() link.targetURL = targetURL link.shortenURL = shortenURL link.created_date = created_date link.save() else: link = link[0] return link
def populate(N): fake = Faker() al = algo() urls = set() while len(urls) < N: url = fake.url() urls.add(url) for url in urls: try: check = Link.objects.get(targetURL=url) # retrieve the object except: check = None if check is None: urlid = Link.objects.count() shortenURL = BASE_URL + al.encode(urlid) created_date = timezone.now() link = Link.objects.get_or_create(created_date=created_date, shortenURL=shortenURL, targetURL=url)[0] link.save() print(len(urls))