Ejemplo n.º 1
0
def submit(request):
    """
    View for submitting a URL
    """
    if getattr(settings, 'REQUIRE_LOGIN', True) and not request.user.is_authenticated():
        # TODO redirect to an error page
        raise Http404
    url = None
    link_form = None
    if request.GET:
        link_form = LinkSubmitForm(request.GET)
    elif request.POST:
        link_form = LinkSubmitForm(request.POST)
    if link_form and link_form.is_valid():
        url = link_form.cleaned_data['u']
        link = None
        try:
            link = Link.objects.get(url = url)
        except Link.DoesNotExist:
            pass
        if link == None:
            new_link = Link(url = url)
            new_link.save()
            link = new_link
        values = default_values(request)
        values['link'] = link
        return render_to_response(
            'shortener/submit_success.html',
            values,
            context_instance=RequestContext(request))
    values = default_values(request, link_form=link_form)
    return render_to_response(
        'shortener/submit_failed.html',
        values,
        context_instance=RequestContext(request))
Ejemplo n.º 2
0
def submit(request):
    """
    View for submitting a URL to be shortened.  Modified to create a
    session and present that session information in the submit_success
    page so that we can carry the small link from here to the
    add_tag_to_link method.
    """
    lsform = LinkSubmitForm(request.POST)
    tagform = AddTagForm()
    tagurlform = TagURLForm()

    if lsform.is_valid():
        kwargs = {'url': lsform.cleaned_data['url']}
        custom = lsform.cleaned_data['custom']
        if custom:
            # specify an explicit id corresponding to the custom url
            kwargs.update({'id': base62.to_decimal(custom)})
        link = Link.objects.create(**kwargs)
        
        # JPK Need to put session info here, in the list that is
        # passed to the form

        request.session['link_id'] = link.id        
        
        return render(request, 'shortener/submit_success.html', {'link': link; 'tag_form': tagform; 'tag_select_form': tagurlform })
    else:
        return render(request, 'shortener/submit_failed.html', {'link_form': lsform})
Ejemplo n.º 3
0
def submit(request):
    """
    View for submitting a URL to be shortened
    """
    form = LinkSubmitForm(request.POST)
    if form.is_valid():
        kwargs = {'url': form.cleaned_data['url']}
        custom = form.cleaned_data['custom']
        if custom:
            # specify an explicit id corresponding to the custom url
            kwargs.update({'id': base62.to_decimal(custom)})
        link = Link.objects.create(**kwargs)
        return render(request, 'shortener/submit_success.html', {'link': link})
    else:
        return render(request, 'shortener/submit_failed.html', {'link_form': form})
Ejemplo n.º 4
0
def submit(request):
    """
    View for submitting a URL to be shortened
    """
    form = LinkSubmitForm(request.POST)
    if form.is_valid():
        kwargs = {'url': form.cleaned_data['url']}
        custom = form.cleaned_data['custom']
        if custom:
            kwargs.update({'is_custom': True, 'short_url': custom})
        link = Link(**kwargs)
        link.save()
        return render(request, 'submit_success.html', {'link': link})
    else:
        return render(request, 'submit_failed.html', {'link_form': form})
Ejemplo n.º 5
0
def submit(request):
    """
    View for submitting a URL to be shortened
    """
    form = LinkSubmitForm(request.POST)
    if form.is_valid():
        kwargs = {'url': form.cleaned_data['url']}
        custom = form.cleaned_data['custom']
        if custom:
            kwargs.update({'is_custom': True, 'short_url': custom})
        link = Link(**kwargs)
        link.save()
        return render(request, 'submit_success.html', {'link': link})
    else:
        return render(request, 'submit_failed.html', {'link_form': form})
Ejemplo n.º 6
0
def submit(request):
    """
    View for submitting a URL to be shortened
    """
    form = LinkSubmitForm(request.POST)
    if form.is_valid():
        kwargs = {'url': form.cleaned_data['url']}
        custom = form.cleaned_data['custom']
        if custom:
            # specify an explicit id corresponding to the custom url
            kwargs.update({'id': base62.to_decimal(custom)})
        link = Link.objects.create(**kwargs)
        return render(request, 'shortener/submit_success.html', {'link': link})
    else:
        return render(request, 'shortener/submit_failed.html',
                      {'link_form': form})
Ejemplo n.º 7
0
def index(request):
    """View for main page"""
    values = {
        'link_form': LinkSubmitForm(),
        'recent_links': Link.objects.all()[:5],
        'most_popular_links': Link.objects.all()[:5]
    }
    return render(request, 'index.html', values)
Ejemplo n.º 8
0
def index(request):
    """
    View for main page
    """
    values = {
        'link_form': LinkSubmitForm(),
        'recent_links': Link.objects.all().order_by('-date_submitted')[:5],
        'most_popular_links': Link.objects.all().order_by('-usage_count')[:5]}
    return render(request, 'shortener/index.html', values)
Ejemplo n.º 9
0
def submit(request):
    """
    View for submitting a URL to be shortened
    """
    form = LinkSubmitForm(request.POST)
    if form.is_valid():
        kwargs = {'url': form.cleaned_data['url']}
        custom = form.cleaned_data['custom']
        if custom:
            # specify an explicit id corresponding to the custom url
            kwargs.update({'id': base62.to_decimal(custom)})
        print(kwargs['url'])
        if kwargs['url'] in [item.url for item in Link.objects.all()]:
            return render(request, 'shortener/submit_failed.html', {'link_form': form})
        
        link = Link.objects.create(**kwargs)
        user = User.objects.order_by('?').first()
        link.submitter = user
        link.save(update_fields=['submitter'])
        return render(request, 'shortener/submit_success.html', {'link': link})
    else:
        return render(request, 'shortener/submit_failed.html', {'link_form': form})