def render(self, context): request = context['request'] # get the full URL for the current page url = request.build_absolute_uri() # see if it's worth shortening this URL path = request.path.strip('/') next_shortcut = gen_shortcut(IttyBittyURL.objects.count() + 1) # if we have a shortcut that matches the path, we shouldn't make an Itty Bitty URL if IttyBittyURL.objects.filter(shortcut__exact=path).count() or \ len(path) <= len(next_shortcut): # it's shorter than the generated shortcut would be... don't store # the object and just use the URL itself ittybitty = IttyBittyURL(url=url, shortcut=path) else: # get the Itty Bitty URL object for this URL, and create one if one # doesn't already exist ittybitty, created = IttyBittyURL.objects.get_or_create(url=url) if self.varname: # if the user specified a varname, inject the object into the context context[self.varname] = ittybitty return '' else: # if no varname given, just spit the shortcut URL into the template return ittybitty.get_shortcut()
def render(self, context): request = context["request"] # get the full URL for the current page url = request.build_absolute_uri() # see if it's worth shortening this URL path = request.path.strip("/") next_shortcut = gen_shortcut(IttyBittyURL.objects.count() + 1) # if we have a shortcut that matches the path, we shouldn't make an Itty Bitty URL if IttyBittyURL.objects.filter(shortcut__exact=path).count() or len(path) <= len(next_shortcut): # it's shorter than the generated shortcut would be... don't store # the object and just use the URL itself ittybitty = IttyBittyURL(url=url, shortcut=path) else: # get the Itty Bitty URL object for this URL, and create one if one # doesn't already exist ittybitty, created = IttyBittyURL.objects.get_or_create(url=url) if self.varname: # if the user specified a varname, inject the object into the context context[self.varname] = ittybitty return "" else: # if no varname given, just spit the shortcut URL into the template return ittybitty.get_shortcut()
def set_shortcut(sender, instance, created, *args, **kwargs): """ Generates the shortcut for an Itty Bitty URL object if it hasn't already been generated. """ if not instance.shortcut: instance.shortcut = gen_shortcut(instance.id) instance.save() return instance