Ejemplo n.º 1
0
def convert_url(request):
    """
    Module to receive a URL POST from the rendered template.
    Module automatically adds http:// if it wasnt included on the submission.
    URL is saved into the DB and the Primary Key (PK) is used as this shortened URL
    as in,  www.*****.com/2, or www.*****.com/1224
    """

    if request.POST:
        form = ShortLinksForm(request.POST)
        if form.is_valid:
            long_url = request.POST["long_url"]
            # force "http://" to be saved into the DB for faster retrieval later
            if "http://" not in long_url:
                long_url = "http://" + long_url
            visitor_ip = request.META["REMOTE_ADDR"]
            # one of various methods to ENCRYPT data, "base64", an included library
            short_url = base64.urlsafe_b64encode(long_url)
            # extend the Save operation to include the extra vars
            temp = form.save()
            temp.visitor_ip = visitor_ip
            temp.short_url = short_url
            temp.save()
            template_dict = {"long_url": long_url, "short_url": SITE_URL + short_url}
            return render_to_response('newurl.html', template_dict)
    else:
        form = ShortLinksForm()
        return render_to_response('newurl.html', {'short_links_form': form})
Ejemplo n.º 2
0
    def good_form_test(self):
        """
        Test a good form.
        """

        test_data = {'long_url': 'www.google.com'}
        form = ShortLinksForm(data = test_data)
        self.assertEqual(form.is_valid(), True)
Ejemplo n.º 3
0
    def bad_form_test(self):
        """
        Test a bad form.
        """

        test_data = {'long_url': ''}
        form = ShortLinksForm(data = test_data)
        self.assertEqual(form.is_valid(), False)