示例#1
0
 def test_protocol(self):
     abs_url = absurl(protocol='http', domain='www.codescale.net', path='/')
     self.assertEqual(abs_url, 'http://www.codescale.net/')
     abs_url = absurl(protocol='https',
                      domain='www.codescale.net',
                      path='/')
     self.assertEqual(abs_url, 'https://www.codescale.net/')
示例#2
0
    def get_redirect_url(self, **kwargs):
        # No token present
        try:
            token = kwargs.pop('token')
        except KeyError:
            log.exception('Token not found in keyword arguments')
            raise Http404

        connection = get_connection(
            'brevisurl.backends.local.BrevisUrlBackend')
        short_url_obj = None

        # Seeking domain in settings.
        if brevisurl.settings.LOCAL_BACKEND_DOMAIN is not None:
            domain = brevisurl.settings.LOCAL_BACKEND_DOMAIN.rstrip('/')
            short_url = absurl(domain=domain,
                               path=reverse('brevisurl_redirect',
                                            kwargs={'token': token}))
            try:
                short_url_obj = ShortUrl.objects.get(
                    backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain in django site framework and current site.
        if short_url_obj is None:
            try:
                site = Site.objects.get_current()
                short_url = absurl(site=site,
                                   path=reverse('brevisurl_redirect',
                                                kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(
                    backend=connection.class_path, shortened_url=short_url)
            except ImproperlyConfigured:
                log.exception(
                    'No site object configured for this django project')
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain from current request.
        if short_url_obj is None:
            try:
                short_url = absurl(domain=self.request.get_host(),
                                   path=reverse('brevisurl_redirect',
                                                kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(
                    backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                log.exception(
                    'No shortened url found for backend: "%s" and token: "%s"',
                    connection.class_path, token)
                raise Http404

        return short_url_obj.original_url
示例#3
0
 def test_site_and_domain(self):
     site = Site(domain='www.codescale.net', name='CodeScale.net')
     abs_url = absurl(protocol='http',
                      domain='sub.codescale.net',
                      site=site,
                      path='/')
     self.assertEqual(abs_url, 'http://sub.codescale.net/')
示例#4
0
 def __generate_shortened_url(self, domain):
     chars = brevisurl.settings.LOCAL_BACKEND_TOKEN_CHARS
     size = brevisurl.settings.LOCAL_BACKEND_TOKEN_LENGTH
     random.shuffle(chars)
     token = ''.join([random.choice(chars) for x in range(size)])
     shortened_url = absurl(domain=domain,
                            path=reverse('brevisurl_redirect',
                                         kwargs={'token': token}))
     return shortened_url
示例#5
0
 def __generate_shortened_url(self, domain):
     chars = brevisurl.settings.LOCAL_BACKEND_TOKEN_CHARS
     size = brevisurl.settings.LOCAL_BACKEND_TOKEN_LENGTH
     random.shuffle(chars)
     token = ''.join([random.choice(chars) for x in range(size)])
     shortened_url = absurl(domain=domain,
                            path=reverse('brevisurl_redirect',
                                         kwargs={'token': token}))
     return shortened_url
示例#6
0
    def get_redirect_url(self, **kwargs):
        # No token present
        try:
            token = kwargs.pop('token')
        except KeyError:
            log.exception('Token not found in keyword arguments')
            raise Http404

        connection = get_connection('brevisurl.backends.local.BrevisUrlBackend')
        short_url_obj = None

        # Seeking domain in settings.
        if brevisurl.settings.LOCAL_BACKEND_DOMAIN is not None:
            domain = brevisurl.settings.LOCAL_BACKEND_DOMAIN.rstrip('/')
            short_url = absurl(domain=domain, path=reverse('brevisurl_redirect', kwargs={'token': token}))
            try:
                short_url_obj = ShortUrl.objects.get(backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain in django site framework and current site.
        if short_url_obj is None:
            try:
                site = Site.objects.get_current()
                short_url = absurl(site=site, path=reverse('brevisurl_redirect', kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(backend=connection.class_path, shortened_url=short_url)
            except ImproperlyConfigured:
                log.exception('No site object configured for this django project')
            except ShortUrl.DoesNotExist:
                pass

        # Seeking domain from current request.
        if short_url_obj is None:
            try:
                short_url = absurl(domain=self.request.get_host(), path=reverse('brevisurl_redirect', kwargs={'token': token}))
                short_url_obj = ShortUrl.objects.get(backend=connection.class_path, shortened_url=short_url)
            except ShortUrl.DoesNotExist:
                log.exception('No shortened url found for backend: "%s" and token: "%s"', connection.class_path, token)
                raise Http404

        return short_url_obj.original_url
示例#7
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
示例#8
0
 def test_no_site_no_domain(self):
     abs_url = absurl(protocol='http', path='/')
     current_site = Site.objects.get_current()
     self.assertEqual(abs_url, 'http://' + current_site.domain + '/')
示例#9
0
 def test_site_and_domain(self):
     site = Site(domain='www.codescale.net', name='CodeScale.net')
     abs_url = absurl(protocol='http', domain='sub.codescale.net', site=site, path='/')
     self.assertEqual(abs_url, 'http://sub.codescale.net/')
示例#10
0
 def test_no_site_no_domain(self):
     abs_url = absurl(protocol='http', path='/')
     current_site = Site.objects.get_current()
     self.assertEqual(abs_url, 'http://' + current_site.domain + '/')
示例#11
0
 def test_domain(self):
     abs_url = absurl(protocol='http', domain='www.codescale.net', path='/')
     self.assertEqual(abs_url, 'http://www.codescale.net/')
     abs_url = absurl(protocol='http', domain='sub.codescale.net', path='/')
     self.assertEqual(abs_url, 'http://sub.codescale.net/')