예제 #1
0
    def render(self, context):
        try:
            obj = self.obj.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        try:
            prefix = self.get_prefix(obj)
        except (AttributeError, KeyError):
            return ''

        tinyid = converter.from_decimal(obj.pk)

        if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:
            short_url = urlparse.urljoin(settings.SHORT_BASE_URL,
                                         prefix + tinyid)
            if self.varname:
                context[self.varname] = short_url
                return ''
            else:
                return short_url

        try:
            short_url = urlresolvers.reverse('shorturls:redirect',
                                             kwargs={
                                                 'prefix': prefix,
                                                 'tiny': tinyid
                                             })
            if self.varname:
                context[self.varname] = short_url
                return ''
            else:
                return short_url
        except urlresolvers.NoReverseMatch:
            return ''
예제 #2
0
    def render(self, context):
        try:
            obj = self.obj.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        if isinstance(obj, six.string_types):
            obj, _created = Link.objects.get_or_create(url=obj)

        try:
            prefix = self.get_prefix(obj)
        except (AttributeError, KeyError):
            return ''

        tinyid = converter.from_decimal(obj.pk)

        if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:
            return urljoin(settings.SHORT_BASE_URL, prefix + tinyid)

        try:
            return reverse(views.redirect, kwargs={
                'prefix': prefix,
                'tiny': tinyid
            })
        except NoReverseMatch:
            return ''
예제 #3
0
def shorturl(obj):
    try:
        prefix = _get_prefix(obj)
    except (AttributeError, KeyError):
        return ''

    tinyid = converter.from_decimal(obj.pk)
    if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:
        return urlparse.urljoin(settings.SHORT_BASE_URL, prefix + tinyid)

    try:
        return urlresolvers.reverse('shorturls.views.redirect', kwargs = {
            'prefix': prefix,
            'tiny': tinyid
        })
    except urlresolvers.NoReverseMatch:
        return ''
예제 #4
0
    def get_short_url(self):
        try:
            prefix = self.get_prefix()
        except (AttributeError, KeyError):
            return ''

        tinyid = converter.from_decimal(self.pk)

        if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:
            return urlparse.urljoin(settings.SHORT_BASE_URL, prefix + tinyid)

        try:
            return urlresolvers.reverse('shorturls.views.redirect', kwargs={
                'prefix': prefix,
                'tiny': tinyid
            })
        except urlresolvers.NoReverseMatch:
            if hasattr(self, 'get_absolute_url'):
                return self.get_absolute_url()
            else:
                return None
예제 #5
0
 def render(self, context):
     try:
         obj = self.obj.resolve(context)
     except template.VariableDoesNotExist:
         return ''
         
     try:
         prefix = self.get_prefix(obj)
     except (AttributeError, KeyError):
         return ''
     
     tinyid = converter.from_decimal(obj.pk)
             
     if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:
         return urlparse.urljoin(settings.SHORT_BASE_URL, prefix+tinyid)
     
     try:
         return urlresolvers.reverse('shorturls.views.redirect', kwargs = {
             'prefix': prefix,
             'tiny': tinyid
         })
     except urlresolvers.NoReverseMatch:
         return ''
예제 #6
0
    def render(self, context):
        try:
            obj = self.obj.resolve(context)
        except template.VariableDoesNotExist:
            return ''

        try:
            prefix = self.get_prefix(obj)
        except (AttributeError, KeyError):
            return ''

        tinyid = converter.from_decimal(obj.pk)

        if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL:
            return urljoin(settings.SHORT_BASE_URL, prefix + tinyid)

        try:
            return urlresolvers.reverse(views.redirect, kwargs={
                'prefix': prefix,
                'tiny': tinyid
            })
        except urlresolvers.NoReverseMatch:
            return ''