def edit(): """Shows the form for creating or editing a link.""" if request.host != 'go.wave.com': return redirect('http://go.wave.com/.edit?' + request.query_string) name = request.args.get('name', '').lstrip('.') url = data.get_url(name) if not name: return redirect('/') if not url: if not data.get_url(normalize(name)): name = normalize(name) # default to normalized when creating original_name = message = '' if url: title = 'Edit go/' + name message = ' is an existing link. You can change it below.' original_name = name else: title = 'Create go/' + name message = " isn't an existing link. You can create it below." return Response(format_html(''' <!doctype html> <link rel="stylesheet" href=".style.css"> <div class="corner"><a href="/">ALL THE LINKS!</a></div> <h1>{title}</h1> <div><a href="/{name_param}">go/{name_param}</a> {message}</div> <form action="/.save" method="post"> <input type="hidden" name="original_name" value="{original_name}"> <table class="form" cellpadding=0 cellspacing=0><tr valign="center"> <td>go/<input name="name" value="{name}" placeholder="shortcut" size=12> <td><span class="arrow">\u2192</span> <td><input id="url" name="url" value="{url}" placeholder="URL" size=60> </tr></table> <div> <input type=submit name="save" value="Save"> <input type=submit name="delete" value="Delete" onclick="return confirm('Really delete this link?')"> </div> <script>document.getElementById("url").focus()</script> </form> <div class="tip"> Fancy tricks: <ul> <li>If go/foo is defined, then go/foo?a=b will expand go/foo and append "a=b" as a form variable. <li>If go/foo is defined but not go/foo/bar, then go/foo/bar will expand go/foo and append "/bar". <li>If go/foo is defined to be a URL that contains "%s", then go/foo/bar will expand go/foo and substitute "bar" for "%s". </ul> </div> ''', title=title, message=message, url=url or '', name=name, name_param=urlquote(name), original_name=original_name))
def edit(): """Shows the form for creating or editing a link.""" name = request.args.get('name', '').lstrip('.') url = data.get_url(name) if not name: return redirect('/') if not url: if not data.get_url(normalize(name)): name = normalize(name) # default to normalized when creating original_name = message = '' if url: title = 'Edit go.wave.com/' + name message = ' is an existing link. You can change it below.' original_name = name else: title = 'Create go.wave.com/' + name message = " isn't an existing link. You can create it below." return make_page_response( title, format_html(''' <div><a href="/{name_param}">go.wave.com/{name_param}</a> {message}</div> <form action="/.save" method="post"> <input type="hidden" name="original_name" value="{original_name}"> <table class="form" cellpadding=0 cellspacing=0><tr valign="center"> <td>go.wave.com/<input name="name" value="{name}" placeholder="shortcut" size=12> <td><span class="arrow">\u2192</span> <td><input id="url" name="url" value="{url}" placeholder="URL" size=60> </tr></table> <div> <input type=submit name="save" value="Save"> <input type=submit name="delete" value="Delete" onclick="return confirm('Really delete this link?')"> </div> <script>document.getElementById("url").focus()</script> </form> <div class="tip"> Fancy tricks: <ul> <li>If go.wave.com/foo is defined, then go.wave.com/foo?a=b will expand go.wave.com/foo and append "a=b" as a form variable. <li>If go.wave.com/foo is defined but not go.wave.com/foo/bar, then go.wave.com/foo/bar will expand go.wave.com/foo and append "/bar". <li>If go.wave.com/foo is defined to be a URL that contains "%s", then go.wave.com/foo/bar will expand go.wave.com/foo and substitute "bar" for "%s". </ul> </div> ''', title=title, message=message, url=url or '', name=name, name_param=urlquote(name), original_name=original_name))
def redirect_url(link): if get_url(link) is None: return '404' else: url = get_url(link) print(link) if url.find('http://') != -1: return redirect(get_url(link), code=302) elif url.find('https://') != -1: return redirect(get_url(link), code=302) else: return redirect('http://' + url, code=302)
def go(name): """Redirects to a link.""" url = data.get_url(name) if not url: return redirect('/.edit?name=' + urlquote(name)) qs = (request.query_string or '').encode('utf-8') if qs: url += ('&' if '?' in url else '?') + qs data.log('redirect', name, url) data.update_count(name) return redirect(url)
def go(name): """Redirects to a link.""" url = data.get_url(name) or data.get_url(normalize(name)) # If "foo/bar/baz" is not found, try "foo/bar" and append "/baz"; # if that's not found, try "foo" and append "/bar/baz". suffix = '' while not url and '/' in name: name, part = name.rsplit('/', 1) suffix = '/' + part + suffix url = data.get_url(name) or data.get_url(normalize(name)) if not url: return redirect('/.edit?name=' + urlquote(name + suffix)) if '%s' in url: url = url.replace('%s', urlquote(suffix.lstrip('/'))) else: url += suffix qs = (request.query_string or '').encode('utf-8') if qs: url += ('&' if '?' in url else '?') + qs data.log('redirect', name, url) data.update_count(name) return redirect(url)
def edit(): """Shows the form for creating or editing a link.""" name = request.args.get('name', '').lstrip('.') url = data.get_url(name) if not name: return redirect('/') if not url: if not data.get_url(normalize(name)): name = normalize(name) # default to normalized when creating original_name = message = '' if url: title = 'Edit go/' + name message = ' is an existing link. You can change it below.' original_name = name else: title = 'Create go/' + name message = " isn't an existing link. You can create it below." return Response(format_html(''' <!doctype html> <link rel="stylesheet" href=".style.css"> <div class="corner"><a href="/">ALL THE LINKS!</a></div> <h1>{title}</h1> <div><a href="/{name_param}">go/{name_param}</a> {message}</div> <form action="/.save" method="post"> <input type="hidden" name="original_name" value="{original_name}"> <table class="form"><tr valign="center"> <td>go/<input name="name" value="{name}" placeholder="shortcut" size=10> <td><span class="arrow">\u2192</span> <td><input id="url" name="url" value="{url}" placeholder="URL" size=60> </tr></table> <div><input type=submit value="Save"></div> <script>document.getElementById("url").focus()</script> </form> ''', title=title, message=message, url=url or '', name=name, name_param=urlquote(name), original_name=original_name))
def go(short_url): url = get_url(short_url) return redirect(url)