Ejemplo n.º 1
0
def create_first_shorturl(sender, **kwargs):
    from models import ShortURL

    if not kwargs.get('interactive'):
        return

    ## check if the database contains some URL
    if ShortURL.objects.count() > 0:
        return

    ## check if the user wants to configure the site
    prompt = 'Would you like to create a first short URL pointing to this site? (yes/no): '
    option = ''
    while option.lower() != 'yes':
        option = raw_input(prompt)
        if option.lower() == 'no':
            return

    ## create the first url
    current_site = sender.Site.objects.get_current()
    scheme = getattr(settings, 'SHORTIM_SITE_SCHEME', 'http')
    url = scheme.lower() + '://' + current_site.domain \
            + reverse('shortim_create')

    shorturl = ShortURL(url=url, remote_user='******')
    shorturl.save()
    print 'First short URL created: %s\n' % shorturl.get_absolute_full_url()
Ejemplo n.º 2
0
def create_first_shorturl(sender, **kwargs):

    if not kwargs.get('interactive'):
        return

    ## check if the database contains some URL
    if ShortURL.objects.count() > 0:
        return

    ## check if the user wants to configure the site
    prompt = 'Would you like to create a first short URL pointing to this site? (yes/no): '
    option = ''
    while option.lower() != 'yes':
        option = raw_input(prompt)
        if option.lower() == 'no':
            return

    ## create the first url
    current_site = sender.Site.objects.get_current()
    scheme = getattr(settings, 'SHORTIM_SITE_SCHEME', 'http')
    url = scheme.lower() + '://' + current_site.domain \
            + reverse('shortim_create')

    shorturl = ShortURL(url=url, remote_user='******')
    shorturl.save()
    print 'First short URL created: %s\n' % shorturl.get_absolute_full_url()
Ejemplo n.º 3
0
    def test_encode_decode(self):
        base = ShortURL.base
        ids_to_test = (
            1, 2, 9, 10, 11, 15, 89, 1000, 999, 998, 8765431234567,
            base, base - 1, base + 1, base * 2, base * 2 - 1, base * base
        )
        for test_id in ids_to_test:
            print(test_id, 1)
            encoded = ShortURL(id=test_id, address='some_address').shorthand
            print(test_id, 2)
            decoded = ShortURL.shorthand_to_id(encoded)
            print(test_id, 3)
            assert decoded == test_id

        with pytest.raises(ValueError):
            ShortURL(id=0, address='some_address').shorthand
Ejemplo n.º 4
0
async def shorten_url(url: URL):
    code = generate_random_code()
    while get_url_record(code):
        print("Code already exists, generating again...")
        code = generate_random_code()
    short_url = prefix + "/" + code
    create_url_record(code, url.long_url)
    response = ShortURL(short_url=short_url)
    return response
Ejemplo n.º 5
0
def create_url2():
    # path = request.json['path']
    length = 4
    attempt = 0
    path = None
    while length<=MAX_URL_LENGTH:
        path = shortuuid.ShortUUID().random(length=length)
        if ShortURL.count(path):
            attempt = attempt + 1
            if attempt >= MAX_RETRY:
                length = length + 1
                attempt = 0
        else:
            break
    if length > MAX_URL_LENGTH:
        return jsonify(success=False), 500
    redirect_url = unquote(request.args.get('redirect_url'))
    # webhook = request.json.get('webhook', None)
    ShortURL(url=path, redirection_url=redirect_url, webhook=None).save()
    return jsonify(success=True,path=path), 200
Ejemplo n.º 6
0
def redirect_url(path):
    try:
        short_url = ShortURL.get(path)
        webhook = WEBHOOK
        if short_url.webhook:
            webhook = short_url.webhook
        webhook = get_hook(webhook, path)
        if webhook:
            call_url(webhook)
        return redirect(short_url.redirection_url, code=302)
    except ShortURL.DoesNotExist:
        return redirect(NOT_FOUND_URL, code=302)
Ejemplo n.º 7
0
def redirect_url(path):
    try:
        short_url = ShortURL.get(path)
        webhook = WEBHOOK
        if short_url.webhook:
            webhook = short_url.webhook
        webhook = get_hook(webhook, path)
        if webhook:
            call_url(webhook)
        return redirect(short_url.redirection_url, code=302)
    except ShortURL.DoesNotExist:
        # return render_template('./404/index.html'), 404
        return jsonify(error="URL Not Found. Make sure you entered the URL correctly."), 404
Ejemplo n.º 8
0
def index():
	""" Главная страница """

	form = URLForm()
	if form.validate_on_submit():
		url = db.session.query(ShortURL).filter_by(full_url=form.url_string.data).first()
		if url:
			return redirect(url_for('short_page', short_url=url.short_url))
		else:
			new_url = ShortURL(full_url=form.url_string.data, short_url=random_url())
			db.session.add(new_url)
			db.session.commit()
			return redirect(url_for('short_page', short_url=new_url.short_url))
	return render_template('index.html', form=form)
Ejemplo n.º 9
0
    def save(self, request, api):

        url = self.cleaned_data['url']
        remote_user = self.cleaned_data['remote_user']
        canonical = self.cleaned_data.get('canonical', False)

        instance = ShortURL.get_or_create_object(url, remote_user, canonical)

        if not api:
            message = _('Woow, your URL was successfully shortened.')
            messages.add_message(request, messages.SUCCESS, message)

        self.instance = instance
        return self.instance
Ejemplo n.º 10
0
    def save(self, request, api):
        
        url = self.cleaned_data['url']
        remote_user = self.cleaned_data['remote_user']
        exclusive = self.cleaned_data.get('exclusive', False)
        confirmation = self.cleaned_data.get('confirmation', False)
        canonical = self.cleaned_data.get('canonical', False)

        instance = ShortURL.get_or_create_object(url,
            remote_user, canonical, exclusive, confirmation)

        if not api:
            message = _('Woow, your URL was successfully shortened.')
            messages.add_message(request, messages.SUCCESS, message)

        self.instance = instance
        return self.instance
Ejemplo n.º 11
0
def create_short_url():
    url = request.form.get('url')
    custom_url = request.form.get('custom_url')
    print url, custom_url
    # Fetch the page title of the url
    # This function call can be made asynchronous to improve response time
    page_title = get_page_title(url)
    short_url = custom_url if custom_url != "" else generate_random_string()
    short_url_ = ShortURL.query.filter_by(short_url=short_url).first()
    if short_url_:
        return jsonify(
            {'error': 'Link already exists, try a different custom keyword!'})
    print "here"
    # Create the short url instance
    shorturl = ShortURL(url=url, short_url=short_url, url_title=page_title)
    db.session.add(shorturl)
    db.session.commit()

    return jsonify({
        'url_withad':
        url_for('short_url_ad', shorturl=shorturl.short_url, _external=True),
        'url_withre':
        url_for('short_url_re', shorturl=shorturl.short_url, _external=True)
    })
Ejemplo n.º 12
0
 def visit_shorthand(self, shorthand):
     short = ShortURL.query.get_or_404(ShortURL.shorthand_to_id(shorthand))
     return redirect(unquote(short.address))
Ejemplo n.º 13
0
def create_url():
    path = request.json['path']
    redirect_url = request.json['redirect_url']
    webhook = request.json.get('webhook', None)
    ShortURL(url=path, redirection_url=redirect_url, webhook=webhook).save()
    return jsonify(success=True), 200