Example #1
0
def todo(request):
    salida = ""
    if request.method == "POST":
        nombre = request.POST["larga"]
        #pongo el http o https si hace falta
        if not nombre.startswith("http"):
            nombre = "http://" + nombre

        try:
            fila = Urls.objects.get(larga=nombre)
            salida += "<b>ERROR: la url '" + fila.larga + "' esta acortada ya en: " + str(fila.id) + "</b><br><br>"
        except Urls.DoesNotExist:
            salida += "Guardada correctamente la url '" + str(nombre) + "'<br><br>"
            #Guardo en la BD
            p = Urls(larga=nombre)
            p.save()
            
                           
    salida += "<form action='' method='POST'>\n"
    salida += "Url a acortar: <input type='text' name='larga'>"
    salida += '<input type="submit" value="Acortar"><br>'
    salida += "<br><hr>Estas son las urls acortadas:<ul>"
    lista = Urls.objects.all()
    for fila in lista:
        salida += "<li>" + str(fila.id) + "->" + fila.larga + " "
    salida += "</ul>"

    return HttpResponse(salida)
 def _insertUrl(self, url, hasher):
     urlinfo = Urls()
     urlinfo.urlhash = hasher
     urlinfo.url = url
     urlinfo.save()
     page = Pages()
     page.url = urlinfo
     page.save()
     return urlinfo
Example #3
0
def manejador(request):
    url_basica = "http://localhost:8000"
    metodo = request.method
    cuerpo = request.body

    if metodo == "GET":
        inicio = '<p><h1><center> Servidor acortador de URLs <center></h1><p>'
        formulario = '<center><FORM method="POST" action="">' + \
                    'URL: <input type="text" name="url"><br>' + \
                    '<input type="submit" value="Enviar"></form><center>'
        lista = '<h4><center><p> URLs reales y acortadas hasta el momento: <p><center></h4>'
        try:
            lista_urls = Urls.objects.all()
            pagina = ""
            for url in lista_urls:
                pagina += "<p><a href='/" + url.url_larga + "'>" + url.url_larga + "</a>" + \
                        " ==> " + "<a href='/" + url.url_corta + "'>" + url.url_corta + "</a></p>"
        except Urls.DoesNotExist:
            pagina = ("<p><h1>Ha ocurrido un error. No hay paginas almacenadas</h1></p>")
        respuesta = inicio + formulario + lista + pagina

    elif metodo == "POST":
        try:
            lista_urls = Urls.objects.all()
            contador = len(lista_urls)
        except Urls.DoesNotExist:
            contador = 0

        if len(cuerpo.split("=")) != 2 or cuerpo.split("=")[0] != "url":
                return HttpResponse("Error en el formulario")
        url = cuerpo.split("=")[1]
        url = urllib.unquote(url).decode('utf8')
        if url.split("://")[0] != "http" and url.split("://")[0] != "https":
            url_larga = "http://" + url
        else:
            url_larga = url

        try:
            page = Urls.objects.get(url_larga=url_larga)
            respuesta = ("La url que usted quiere añadir ya está en la lista de urls acortadas. Compruebe antes. ")
            respuesta = "<h4>" + respuesta + "La url acortada es: " + str(page.url_corta) + "</h4>"
        except Urls.DoesNotExist:
            url_acortada = "http://localhost:8000/" + str(contador)
            nueva = Urls(url_corta=url_acortada, url_larga=url_larga)
            nueva.save()
            respuesta= "<h3><a href=" + url_larga + ">" + url_larga + \
                       "</a> ==> " + "<a href=" + url_acortada + ">" + url_acortada + \
                       "<p></a> Vuelve a la pagina principal pinchando aqui: " + \
                       "<a href=" + url_basica + ">" + url_basica + "</p>"

    else:
        respuesta = "Método erróneo"

    return HttpResponse(respuesta)
Example #4
0
def index(request):
    if not request.POST:
        all_urls = Urls.objects.all()
        form = GifForm()
        return render(request, 'index.html', {"urls": all_urls, "form": form})
    else:
        form = GifForm(request.POST)
        if form.is_valid():
            formurl = form.cleaned_data['url']
            url = Urls(urls=formurl)
            url.save()
            formkeys = form.cleaned_data['keywords']
            keywords = formkeys.split(',')
            for keyz in keywords:
                key = Keywords(keywords=keyz)
                key.save()
                lookup = Lookup(keywords=key, urls=url)
            return HttpResponse(json.dumps({"success": True, "url": formurl}))
        else:
            return HttpResponse(json.dumps({"error": "Invalid form post"}))
Example #5
0
def acorta(request,resourceName):
    response = "welcome to urlshort" + "<br/>"
    urlsList = Urls.objects.all()
    response += getUrls(urlsList) + "<br/>"
    if request.method == 'POST':
        if request.POST['name'] == "":
            return HttpResponseBadRequest("EMPTY POST")
        elif not request.POST['name'].startswith("http://") and not request.POST['name'].startswith("https://"):
            page = "http://" + request.POST['name']
        newUrl = Urls(name=page)
        newUrl.save()
        response += "--Page: <a href=" + request.POST['name'] + ">" + request.POST['name'] + "</a></p>\n"
        response += "-- Url short: <a href=" + str(newUrl.id) + ">" +  str(newUrl.id) + "</a></p>" 
    elif request.method == 'GET':
        try:
            content = Urls.objects.get(name=resourceName)
            response += content.name
        except Urls.DoesNotExist:
            response += getform()
    return HttpResponse(response)
Example #6
0
def acorta(request):

    if request.method == "GET":
        template = get_template('formulario_inicio.html')
        return HttpResponse(template.render(Context({})))

    elif request.method == "POST":
        url = request.POST['url']
        print "URL REAL ES    " + str(url)
        if len(url) == 0:
            httpCode = "405 Method Not Allowed"
            htmlBody = "Go Away!"
        elif len(url) != 0:
            if url[0:7] == "http://":
                url_real = url
            elif url[0:8] == "https://":
                url_real = url
            else:
                url_real = "http://" + url
            print "URL REAL ES    " + str(url_real)
            lista = Urls.objects.all()
            ya_acortada = False
            for elemento in lista:
                if elemento.larga == url_real: # quiere decir que ya ha sido acortada, busco su valor en la lista
                    print url_real + "SI ESTA EN URLS_REALES!!!"
                    url_acortada = Urls.objects.get(larga = url_real)
                    url_acortada = url_acortada.id
                    ya_acortada = True
            if ya_acortada == False: # si no esta en la lista significa que es nueva, la tengo que acortar y meterla a la lista
                nueva_url = Urls(larga = url_real)
                nueva_url.save()
                url_acortada = nueva_url.id


            template = get_template('plantilla_enlaces.html')
            return HttpResponse(template.render(Context({'url_acortada':url_acortada, 'url_real': url_real})))
    else:
        httpCode = "405 Method Not Allowed"
        htmlBody = "Go Away!"

    return HttpResponse(htmlBody)
def formulario(request):
    salida = ""

    if request.method == "POST":
        if request.body == "":
            salida += "Error, debe escribir una url"     
        else:
            cuerpo = request.body.split('valor=')[1]
            if cuerpo.find("http") == -1:
                cuerpo = "http://" + cuerpo
            else:
                cuerpo = cuerpo.replace('%3A%2F%2F', '://')
        try: 
            fila=Urls.objects.get(larga=cuerpo)
        except Urls.DoesNotExist:
            fila = Urls(larga=cuerpo)
            fila.save()
            
        salida += "Url larga: " + "<a href='" + fila.larga
        salida += "'>" + fila.larga + "</a>" + "</br>"
        salida += "Url corta: " + "<a href='" + str(fila.id)
        salida += "'>" + str(fila.id) + "</a>" + "</br>"
         
    elif request.method == "GET":
        salida += '<form action="" method="POST">'
        salida += 'Introducir url : <input type="text" name="valor">'
        salida += '<input type="submit" value="Enviar">'
        salida += '</form>'
        
        lista = Urls.objects.all()
        salida += "</br></br>Las urls que hay son:<ul>"
        for fila in lista:
            salida += "<li> Url larga: " + fila.larga 
            salida += "  -->  Url Corta: " + str(fila.id)
        salida += "</ul>"
                      
        
    return HttpResponse(salida)
Example #8
0
def formulario(request):
    salida = ""

    if request.method == "POST":
        if request.body == "":
            salida += "Error, debe escribir una url"
        else:
            cuerpo = request.body.split('valor=')[1]
            if cuerpo.find("http") == -1:
                cuerpo = "http://" + cuerpo
            else:
                cuerpo = cuerpo.replace('%3A%2F%2F', '://')
        try:
            fila = Urls.objects.get(larga=cuerpo)
        except Urls.DoesNotExist:
            fila = Urls(larga=cuerpo)
            fila.save()

        salida += "Url larga: " + "<a href='" + fila.larga
        salida += "'>" + fila.larga + "</a>" + "</br>"
        salida += "Url corta: " + "<a href='" + str(fila.id)
        salida += "'>" + str(fila.id) + "</a>" + "</br>"

    elif request.method == "GET":
        salida += '<form action="" method="POST">'
        salida += 'Introducir url : <input type="text" name="valor">'
        salida += '<input type="submit" value="Enviar">'
        salida += '</form>'

        lista = Urls.objects.all()
        salida += "</br></br>Las urls que hay son:<ul>"
        for fila in lista:
            salida += "<li> Url larga: " + fila.larga
            salida += "  -->  Url Corta: " + str(fila.id)
        salida += "</ul>"

    return HttpResponse(salida)
def all(request):
    if request.method == "GET":
        list = Urls.objects.all()
        out = "<html><body><ul>\n"
        for i in list:
            out += "<li><a href=" + i.url + ">" + str(i.id) + "</a></li>\n"
        out += "</ul>" + formulario + "</body></html>"
    elif request.method == "POST":
        url = request.POST.get("url")
        url = post(url)
        try:
            out = "Old URL"
            new = Urls.objects.get(url=url)
        except Urls.DoesNotExist:
            new = Urls(url=url)
            new.save()
            out = "New URL"
        out += "<html><body><ul>\n"
        out += "<li><a href=" + new.url + ">Url Original</a></li>\n"
        out += "<li><a href=" + str(new.id) + ">Url corta</a></li>\n"
        out += "</ul>" + formulario + "</body></html>"
    else:
        out = "Request not found"
    return HttpResponse(out)