Esempio n. 1
0
def shortenUrl(request):
	if request.method == "GET":
		t = get_template("index.html")
		return HttpResponse(t.render())
	if request.method == "POST":
		url = request.POST.get("urlToShorten","")
		if url == "":
			return HttpResponseRedirect("/")
		try:
			if not ("http://" in url) or not ("https://" in url):
				url="http://"+url
			val(url)
		except ValidationError,e:
			t = get_template("invalid.html")
			return HttpResponse(t.render())

		url = url.replace("http://","")
		url = url.replace("https://","")
		QS = Url.objects.all().filter(actualUrl=url)
		if(len(QS)>0):
			UrlObject = QS[0]
			t = get_template("shortened.html")
			return HttpResponse(t.render(Context({"actual_url":url, "shortened_url":dehydrate(UrlObject.id)})))	

		mUrl = Url()
		
		mUrl.actualUrl = url
		mUrl.save()
		# mUrl.shortenedUrl = shorten(url)
		t = get_template("shortened.html")

		return HttpResponse(t.render(Context({"actual_url":url, "shortened_url":dehydrate(mUrl.id)})))
Esempio n. 2
0
def shortenUrl(request):
    if request.method == "GET":
        t = get_template("index.html")
        return HttpResponse(t.render())
    if request.method == "POST":
        url = request.POST.get("urlToShorten", "")
        if url == "":
            return HttpResponseRedirect("/")
        try:
            if not ("http://" in url) or not ("https://" in url):
                url = "http://" + url
            val(url)
        except ValidationError, e:
            t = get_template("invalid.html")
            return HttpResponse(t.render())

        url = url.replace("http://", "")
        url = url.replace("https://", "")
        QS = Url.objects.all().filter(actualUrl=url)
        if (len(QS) > 0):
            UrlObject = QS[0]
            t = get_template("shortened.html")
            return HttpResponse(
                t.render(
                    Context({
                        "actual_url": url,
                        "shortened_url": dehydrate(UrlObject.id)
                    })))

        mUrl = Url()

        mUrl.actualUrl = url
        mUrl.save()
        # mUrl.shortenedUrl = shorten(url)
        t = get_template("shortened.html")

        return HttpResponse(
            t.render(
                Context({
                    "actual_url": url,
                    "shortened_url": dehydrate(mUrl.id)
                })))