Esempio n. 1
0
 def test_model_save_invalid_shortened_url(self):
     with self.assertRaises(ValidationError):
         connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
         short_url = ShortUrl()
         short_url.original_url = 'http://www.codescale.net/'
         short_url.shortened_url = 'www.codescale.'
         short_url.backend = connection.class_path
         short_url.save()
Esempio n. 2
0
 def test_model_save_invalid_shortened_url(self):
     with self.assertRaises(ValidationError):
         connection = get_connection(
             'brevisurl.backends.local.BrevisUrlBackend')
         short_url = ShortUrl()
         short_url.original_url = 'http://www.codescale.net/'
         short_url.shortened_url = 'www.codescale.'
         short_url.backend = connection.class_path
         short_url.save()
Esempio n. 3
0
 def test_model_save(self):
     site = Site.objects.get_current()
     connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
     short_url = ShortUrl()
     short_url.original_url = 'http://www.codescale.net/'
     short_url.shortened_url = '{0}://{1}/12345'.format(brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL,
                                                        site.domain)
     short_url.backend = connection.class_path
     short_url.save()
     self.assertIsNotNone(short_url.pk)
Esempio n. 4
0
 def test_model_save(self):
     site = Site.objects.get_current()
     connection = get_connection(
         'brevisurl.backends.local.BrevisUrlBackend')
     short_url = ShortUrl()
     short_url.original_url = 'http://www.codescale.net/'
     short_url.shortened_url = '{0}://{1}/12345'.format(
         brevisurl.settings.LOCAL_BACKEND_DOMAIN_PROTOCOL, site.domain)
     short_url.backend = connection.class_path
     short_url.save()
     self.assertIsNotNone(short_url.pk)
Esempio n. 5
0
    def shorten_url(self, original_url):
        """
        :raises: ImproperlyConfigured, django.core.exceptions.ValidationError
        :raises: brevisurl.backends.local.TokensExhaustedError
        """
        try:
            short_url = ShortUrl.objects.get(backend=self.class_path, original_url=original_url)
            log.info('Url "%s" already shortened to "%s"', original_url, short_url.shortened_url)
            return short_url
        except ShortUrl.DoesNotExist:
            pass

        try:
            short_url = ShortUrl()
            if self.kwargs.get('domain') is not None:
                # Domain is present in keyword arguments supplied by constructor.
                domain = self.kwargs.get('domain')
            elif brevisurl.settings.LOCAL_BACKEND_DOMAIN is not None:
                # Domain is defined in settings.
                domain = brevisurl.settings.LOCAL_BACKEND_DOMAIN
            else:
                # Domain is taken from django site framework.
                domain = Site.objects.get_current().domain
            # Saving newly generated shortened url.
            short_url.original_url = original_url
            short_url.shortened_url = absurl(domain=domain, path=reverse('brevisurl_redirect',
                                                                         kwargs={'token': self.__generate_token()}))
            short_url.backend = self.class_path
            short_url.save()
            log.info('Url "%s" shortened to "%s"', original_url, short_url.shortened_url)
            return short_url
        except Exception:
            if self.fail_silently:
                return None
            else:
                log.exception('Unknown exception raised while shortening url "%s"', original_url)
                raise