def create(self, request): queryset = UrlEntry.objects.create( OriginDomain=self.request.POST.get('OriginDomain')) serializer = GetShortUrlSerializer(queryset, context={'request': request}) g = UrlEntry.objects.filter(pk=queryset.pk).update( ShortUrl=domain + base62.from_decimal(queryset.pk), FriendlyName=base62.from_decimal(queryset.pk), FriendlyKey=queryset.pk) queryset.refresh_from_db() return Response(serializer.data)
def render(self, context): try: obj = self.obj.resolve(context) except template.VariableDoesNotExist: return '' try: prefix = self.get_prefix(obj) except (AttributeError, KeyError): return '' if settings.SHORTEN_MODELS[prefix].find(":") > 0: _, key = settings.SHORTEN_MODELS[prefix].split(':') tinyid = getattr(obj, key) else: tinyid = base62.from_decimal(obj.pk) if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL: url = urlparse.urljoin(settings.SHORT_BASE_URL, prefix+tinyid) else: try: url = urlresolvers.reverse('shorturls.views.redirect', kwargs = { 'prefix': prefix, 'tiny': tinyid }) except urlresolvers.NoReverseMatch: url = '' if self.varname is not None: context[self.varname] = url return '' return url
def get_absolute_url(self, request_view=False): from shorturls.baseconv import base62 tiny = base62.from_decimal(self.id) if not request_view: return ('delivery:short', (), {'tiny': tiny}) else: return ('delivery:request-short', (), {'tiny': tiny})
def get_shorturl(self, obj): try: prefix = self.get_prefix(obj) except (AttributeError, KeyError): return None tinyid = base62.from_decimal(obj.pk) if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL: return urlparse.urljoin(settings.SHORT_BASE_URL, prefix+tinyid)
def get_shorturl(obj): try: prefix = get_prefix(obj) except (AttributeError, KeyError): raise NoReverseMatch tinyid = base62.from_decimal(obj.pk) if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL: return urlparse.urljoin(settings.SHORT_BASE_URL, prefix+tinyid) return reverse('shorturls.views.redirect', kwargs = { 'prefix': prefix, 'tiny': tinyid })
def shortify(obj): prefix = prefixmap.get_prefix(obj) tinyid = base62.from_decimal(obj.pk) if hasattr(settings, 'SHORT_BASE_URL') and settings.SHORT_BASE_URL: return settings.SHORT_BASE_URL+prefix+tinyid try: return urlresolvers.reverse('shorturls.views.redirect', kwargs = { 'prefix': prefix, 'tiny': tinyid }) except urlresolvers.NoReverseMatch: return ''
def get_short_url(self): obj = self try: prefix = self.get_prefix(obj) except (AttributeError, KeyError): return '' tinyid = base62.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 ''
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 = base62.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 ''
def set_shortcut(sender, instance, created, *args, **kwargs): """ Generates the shortcut for an Itty Bitty URL object if it hasn't already been generated. """ m = False if not instance.shortcut: # instance.shortcut = gen_shortcut(instance.id) instance.shortcut = base62.from_decimal ( 100000 + instance.id ) instance.save () m = True if not instance.title: title = get_url_title ( instance.url ) code = detect ( title ) title = title.decode ( code['encoding'] ) instance.title = title# get_url_title ( instance.url ) m = True if m: instance.save () return instance
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 = base62.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 ''
def enc(id): return base62.from_decimal(id)
Examples: Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: path('', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.urls import include, path 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) """ from django.contrib import admin from django.urls import path, include from django.conf.urls import url, include from shorturls.baseconv import base62 from shorturls.routers import router from shorturls.viewset import OriginViewSet print(base62.from_decimal(12345)) urlpatterns = [ path('admin/', admin.site.urls), # path('A/<slug:title>/', admin.site.urls), url(r'^', include(router.urls)), url(r'^api_auth/', include('rest_framework.urls', namespace='rest_framework')) # path('api_auth/', admin.site.urls), # path('shorturl/', include('shorturls.urls')), ]
def get_shortcut(self): return base62.from_decimal(self.id)