Created
May 2, 2013 23:47
-
-
Save xmonader/5506325 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def shortlink_exists(s): | |
| ls=select(p for p in Link if p.short_link==s)[:] | |
| if ls: | |
| return ls[0] | |
| def longlink_exists(link): | |
| ls= select(p for p in Link if p.long_link==link)[:] | |
| if ls: | |
| return ls[0] | |
| def get_unique_shortlink(): | |
| gen_shortlink= lambda length: ''.join(random.choice(string.ascii_uppercase + string.digits) for x in range(length)) | |
| shortlink=gen_shortlink(5) | |
| if shortlink_exists(shortlink): | |
| return get_unique_shortlink() | |
| return shortlink | |
| @with_transaction | |
| def create_link(longlink, shortlink): | |
| return Link(long_link=longlink, short_link=shortlink) | |
| @route("/shorturl_ajax", method="POST") | |
| def shorturl_ajax(): | |
| appurl=request.url.replace("/shorturl_ajax", "/") | |
| form=ShortURLForm(request.POST) | |
| longlink=form.long_link.data | |
| link=longlink_exists(longlink) | |
| if link: | |
| #print "ALREADY EXISTS...." | |
| return dict(data='<a href="/%s">%s</a>'%(link.short_link,appurl+link.short_link), success=True) | |
| else: | |
| #print "CREATING NEW LINK..." | |
| shortlink=get_unique_shortlink() | |
| l=create_link(longlink, shortlink) | |
| return dict(data='<a href="/%s">%s</a>'%(l.short_link, appurl+l.short_link), success=True) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment