def handle(self): """ Handle TCP datagrams """ data = loads(self.request.recv(1024).strip()) print "Recebido de '%s': %s \n" % (self.client_address[0], data) try: obj = Url.objects.filter(title=data['hostname']).update( ip=data['ip'], urls=data['urls'], slug=data['customer']) if obj: self.request.sendall(dumps({'return': 'Successfully updated'})) else: obj = Url(title=data['hostname'], ip=data['ip'], urls=data['urls'], slug=data['customer']) obj.save() self.request.sendall(dumps({'return': 'Successfully created'})) except Url.DoesNotExist: obj = Url(title=data['hostname'], ip=data['ip'], urls=data['urls'], slug=data['customer']) obj.save() self.request.sendall(dumps({'return': 'Successfully created'}))
def test_set_info(self): url = 'www.google.com' request = self.factory.get('/app/') url_obj = Url() url_obj.set_info(request, url) url_obj.save() self.assertEqual(url_obj.long_url, url) self.assertEqual(url_obj.ip, '127.0.0.1') self.assertIsNotNone(url_obj.time) self.assertIsNotNone(url_obj.shortened_url)
def handle(self): """ Handle TCP datagrams """ data = loads(self.request.recv(1024).strip()) print "Recebido de '%s': %s \n" % (self.client_address[0], data) try: obj = Url.objects.filter(title=data['hostname']).update(ip=data['ip'],urls=data['urls'],slug=data['customer']) if obj: self.request.sendall(dumps({'return':'Successfully updated'})) else: obj = Url(title=data['hostname'],ip=data['ip'],urls=data['urls'],slug=data['customer']) obj.save() self.request.sendall(dumps({'return':'Successfully created'})) except Url.DoesNotExist: obj = Url(title=data['hostname'],ip=data['ip'],urls=data['urls'],slug=data['customer']) obj.save() self.request.sendall(dumps({'return':'Successfully created'}))
def test_redirecting_speed_without_redis(self): for i in range(1000): url_obj = Url() url_obj.long_url = f'www.google.com/{i}' url_obj.time = datetime.now(pytz.utc) url_obj.shortened_url = url_obj.generate_id(6) url_obj.save() url_obj_500 = Url.objects.filter(long_url='www.google.com/500').first() start = process_time() for i in range(1000): response = self.client.get(f'/app/detail_without_redis/{url_obj_500.shortened_url}/') self.assertEqual(response.status_code, 302) self.assertEqual(response.url, 'https://www.google.com/500') end = process_time() print(f'Process time without Redis: {end - start}')
def add_node(request): ctx = {} provider = request.POST.get('new_provider') parent_id = request.POST.get('parent_id') parent_provider = request.POST.get('parent_provider') relation = request.POST.get('relation') relation_back = request.POST.get('relation_back') if request.method == 'POST' and relation != '': if provider == 'text': node = Node(user=request.user, text=request.POST.get('text')) node.save() make_relation(request.user, parent_id, parent_provider, node.id, provider, relation, relation_back) elif provider == 'url': url_text = request.POST.get('url') import hashlib m = hashlib.md5() m.update(url_text.encode('utf-8')) url_hash = m.hexdigest() try: title, image = get_url_info(url_text, url_hash) url = Url(user=request.user, url=url_text, name=title, image=image) url.url_hash = url_hash url.save() except: url = Url.objects.get(url_hash=url_hash) make_relation(request.user, parent_id, parent_provider, url_hash, provider, relation, relation_back) elif provider == 'file': # todo - add files new_path = os.path.join(parent_id, request.POST.get('name')) if os.path.isdir(parent_id) and os.path.isdir(new_path): os.mkdir(new_path) make_relation(request.user, parent_id, parent_provider, new_path, provider, relation, relation_back) return HttpResponseRedirect(request.META['HTTP_REFERER'])