def get_context_data(self, **kwargs): context = super(ResolveView, self).get_context_data(**kwargs) #Check for permalink view. plink = kwargs.get('tiny', None) if plink: #Convert incoming permalink to database pk. rid = base62.to_decimal(plink) #Get resource from cache if possible. cache_key = "resource-%s" % rid resource = cache.get(cache_key, None) if resource is None: resource = Resource.objects.get(id=rid) cache.set(cache_key, resource, CACHE_TIMEOUT) self.resource = resource sersol = self.get_data(query=resource.query) context['is_permalink'] = True #Show index screen elif not self.query: self.template_name = 'bul_link/index.html' return context #Resolve the query else: sersol = self.get_data() resolved = Resolved(sersol) #Always using the first citation and linkGroups returned. It's not #clear when multiple citations would be useful. context['citation'] = resolved.citation context['link_groups'] = resolved.link_groups context['resource'] = self.resource return context
def decode_embed(request, code): pk = base62.to_decimal(code) try: obj = Embed.objects.get(pk=pk) except Embed.DoesNotExist: raise Http404 return render_to_response('cwod/embed.html', {'embed': obj}, context_instance=RequestContext(request))
def follow(request, base62_id): """ View which gets the link for the given base62_id value and redirects to it. """ key = base62.to_decimal(base62_id) link = get_object_or_404(Link, pk = key) link.usage_count += 1 link.save() return HttpResponsePermanentRedirect(link.url)
def getItemByUrl(url): db = get_database()[Bookmark.collection_name] if slugRe.match(url): item=db.find_one({'seq':base62.to_decimal(url)}) else: url=fixApacheMadness(url) url=urlSanitize(url) item=db.find_one({'url':url}) if not item: raise Http404 return item
def getItemByUrl(url): db = get_database()[Bookmark.collection_name] if slugRe.match(url): item = db.find_one({'seq': base62.to_decimal(url)}) else: url = fixApacheMadness(url) url = urlSanitize(url) item = db.find_one({'url': url}) if not item: raise Http404 return item
def info(request, base62_id): """ View which shows information on a particular link """ key = base62.to_decimal(base62_id) link = get_object_or_404(Link, pk = key) values = default_values(request) values['link'] = link return render_to_response( 'url_shortener_link_info.html', values, context_instance=RequestContext(request))
def info(request, base62_id): """ View which shows information on a particular link """ if not request.user.is_authenticated(): # TODO redirect to an error page raise Http404 key = base62.to_decimal(base62_id) link = get_object_or_404(Link, pk = key) values = default_values(request) values['link'] = link return render_to_response( 'shortener/link_info.html', values, context_instance=RequestContext(request))
def json_data(request, obj_hash=None): status = 200 work = None if obj_hash: id = base62.to_decimal(obj_hash) work = JSONData.get_by_id(id - BASE_ID) if not work: raise Http404 # create work if request.method == "POST": w = JSONData() d = json.loads(request.raw_post_data) w.put(); d['id'] = base62.from_decimal(BASE_ID + w.unique_id()) data = w.json = json.dumps(d) w.put(); # update elif request.method == "PUT": if work: data = work.json = request.raw_post_data work.put() # remove elif request.method == "DELETE": work.delete(); status = 204 data = '' pass # get else: if not work: data = '{"error": "does not exist"}' status = 404 else: data = work.json pass if not data: raise Http404 return HttpResponse(data, status=status, mimetype='application/json')
def work(request, work_hash=None): status = 200 work = None if work_hash: id = base62.to_decimal(work_hash) work = Work.get_by_id(id - BASE_ID) if not work: raise Http404 # create work if request.method == "POST": w = Work() w.put(); data = json.dumps({'id': base62.from_decimal(BASE_ID + w.unique_id())}) # update elif request.method == "PUT": if work: work.json = request.raw_post_data work.put() data = request.raw_post_data pass # remove elif request.method == "DELETE": work.delete(); status = 204 data = '' pass # get else: if not work: data = '{"error": "does not exist"}' status = 404 else: data = work.json pass return HttpResponse(data, status=status, mimetype='application/json')
def work(request, work_hash=None): status = 200 work = None if work_hash: id = base62.to_decimal(work_hash) work = Work.get_by_id(id - BASE_ID) if not work: raise Http404 # create work if request.method == "POST": w = Work() w.put() data = json.dumps({'id': base62.from_decimal(BASE_ID + w.unique_id())}) # update elif request.method == "PUT": if work: work.json = request.raw_post_data work.put() data = request.raw_post_data pass # remove elif request.method == "DELETE": work.delete() status = 204 data = '' pass # get else: if not work: data = '{"error": "does not exist"}' status = 404 else: data = work.json pass return HttpResponse(data, status=status, mimetype='application/json')