Beispiel #1
0
def index():
    if request.method == 'POST':
        thing = request.form.get('url')
        if thing:
            if '://' not in thing:
                thing = 'http://' + thing

            # Verify the URL
            parsed = urlparse(thing)
            if parsed.scheme not in ('http', 'https'):
                return "I only take HTTP or HTTPS URLs, dummy"

            urlhash = hashlib.sha1(thing).hexdigest()
            try:
                url = Url.get(Url.url_hash == urlhash)
            except:
                url = Url()
                url.url = thing
                url.url_hash = urlhash
                url.created = datetime.datetime.now()
                url.save()

                # hokay. got us an ID, let's make a key.
                url.key = base36_encode(url.id)
                url.save()

            return render_template('added.html', short_url="http://{0}/{1}".format(request.headers['host'], url.key))
        else:
            return "You didn't give me shit"
    else:
        return render_template('index.html')
Beispiel #2
0
def make_url_model(url, site): 
	""" This should on occur once per newly created URL, the linked count is set to zero if it
		is a new site added to database
	"""
	now = datetime.now()
	base_url = 'http://links.ep.io/'
	url_model = Url()
	url_model.url = url
	url_short  = url
	try: 
		domain = Domain.objects.get(site=site)
		domain.linked_count += 1
		domain.date_updated = now
		domain.save()
	except:
		domain = Domain(site=site, linked_count=1, date_updated= now)
		domain.save()
	url_model.site = domain
	url_model.date_time_created = datetime.now()
	url_model.linked_count = 1
	url_model.save()
	url_model.url_shortened = base_url + encode_62(url_model.pk)
	print url_model.url_shortened
	url_model.save()
	return url_model