예제 #1
0
def create(request, api=False, template_name=None):

    ## process or display the form based on the request method
    if request.method == "POST" or api is True:

        ## include the remote address int the submitted data
        data = getattr(request, request.method).copy()
        if api:
            data["exclusive"] = data.get("exclusive") == "1"
            data["confirmation"] = data.get("confirmation") == "1"
        data["remote_user"] = request.META["REMOTE_ADDR"]

        ## validate and process the form
        form = ShortURLForm(data)
        if form.is_valid():
            form.save(request, api)
            instance = form.instance

            ## if page is being accessed as API, return clean data
            if api is True:
                response = ""
                if request.GET.get("canonical") == "1":
                    response = instance.canonical_url
                if not response:
                    response = instance.get_short_full_url()
                return _api_response(response, data.get("type"))

            ## redirect to the preview page
            return HttpResponseRedirect(instance.get_absolute_url())

        ## form is not valid and the page is being accessed as API
        elif api is True:
            return _api_response("ERROR: Invalid data.")

    ## the page was not requested as POST neithe as API
    else:
        initial = {"url": request.GET.get("url")}
        if initial["url"] == "referer":
            initial["url"] = request.META.get("HTTP_REFERER")
        form = ShortURLForm(initial=initial)

    ## render the form template
    template_name = template_name or "base_shorturl_create.html"
    return render_to_response(template_name, locals(), context_instance=RequestContext(request))
예제 #2
0
def create(request, api=False, template_name=None):

    ## process or display the form based on the request method
    if request.method == 'POST' or api is True:

        ## include the remote address int the submitted data
        data = getattr(request, request.method).copy()
        data['remote_user'] = request.META['REMOTE_ADDR']

        ## validate and process the form
        form = ShortURLForm(data)
        if form.is_valid():
            form.save(request, api)
            instance = form.instance

            ## if page is being accessed as API, return clean data
            if api is True:
                response = ''
                if request.GET.get('canonical') == '1':
                    response = instance.canonical_url
                if not response:
                    response = instance.get_short_full_url()
                return _api_response(response, data.get('type'))

            ## redirect to the preview page
            return HttpResponseRedirect(instance.get_absolute_url())

        ## form is not valid and the page is being accessed as API
        elif api is True:
            return _api_response('ERROR: Invalid data.')

    ## the page was not requested as POST neithe as API
    else:
        initial = {'url': request.GET.get('url')}
        if initial['url'] == 'referer':
            initial['url'] = request.META.get('HTTP_REFERER')
        form = ShortURLForm(initial=initial)

    ## render the form template
    template_name = template_name or 'base_shorturl_create.html'
    return render_to_response(template_name,
                              locals(),
                              context_instance=RequestContext(request))
예제 #3
0
def create(request, api=False, template_name=None):

    ## process or display the form based on the request method
    if request.method == 'POST' or api is True:

        ## include the remote address int the submitted data
        data = getattr(request, request.method).copy()
        data['remote_user'] = request.META['REMOTE_ADDR']

        ## validate and process the form
        form = ShortURLForm(data)
        if form.is_valid():
            form.save(request, api)
            instance = form.instance

            ## if page is being accessed as API, return clean data
            if api is True:
                response = ''
                if request.GET.get('canonical') == '1':
                    response = instance.canonical_url
                if not response:
                    response = instance.get_short_full_url()
                return _api_response(response, data.get('type'))

            ## redirect to the preview page
            return HttpResponseRedirect(instance.get_absolute_url())

        ## form is not valid and the page is being accessed as API
        elif api is True:
            return _api_response('ERROR: Invalid data.')

    ## the page was not requested as POST neithe as API
    else:
        initial = { 'url': request.GET.get('url') }
        if initial['url'] == 'referer':
            initial['url'] = request.META.get('HTTP_REFERER')
        form = ShortURLForm(initial=initial)

    ## render the form template
    template_name = template_name or 'base_shorturl_create.html'
    return render_to_response(template_name, locals(),
        context_instance=RequestContext(request))
예제 #4
0
파일: views.py 프로젝트: pH-7/URL-Shortener
def new(request):
    """Adding a redirection"""
    if request.method == "POST":
        form = ShortURLForm(request.POST)
        if form.is_valid():
            short = form.save(commit=False)
            short.code = generate(4) # Number of characters
            short.save()

            return redirect(list)
    else:
        form = ShortURLForm()

    return render(request, 'shorturl/new.html', {'form':form})
def add(request):
    if request.method == 'POST':
        form = ShortURLForm(request.POST)
        if form.is_valid():
            short_url = form.save()
            return HttpResponseRedirect(reverse('urlshortener-preview', args=[short_url.key]))
    else:
        if 'u' in request.GET:
            form = ShortURLForm(initial={'url': request.GET['u']})
        else:
            form = ShortURLForm()
    context = {'form': form, 'current_site': RequestSite(request)}
    if request.GET.get('p', False):
        context['base_template'] = 'urlshortener/popup_base.html'
    return render(request, 'urlshortener/add.html', context)
예제 #6
0
def create(request, template_name=None, next=None):
    """create new redirection"""
    if request.method == "POST":
        form = ShortURLForm(request.POST)
        if form.is_valid():
            mini = form.save()
            mini.code = encoder.encode_url(mini.id)
            mini.save()
            return HttpResponseRedirect(reverse(next))
    else:
        form = ShortURLForm()

    c = RequestContext(request, {
        'form': form,
    })
    return render_to_response(template_name, c)
예제 #7
0
def create(request, template_name=None, next=None):
    """create new redirection"""
    if request.method == "POST":
        form = ShortURLForm(request.POST)
        if form.is_valid():
            mini = form.save()
            mini.code = encoder.encode_url(mini.id)    
            mini.save()
            return HttpResponseRedirect(reverse(next))
    else:
        form = ShortURLForm()
        
    c = RequestContext(request, {
                                 'form': form,
                                 })
    return render_to_response(template_name, c)