def get_images_paginated(query, origins, page_num, last_id=None): args = None queryset = Image.objects.all().exclude(hidden=True).exclude(tags__isnull=True) if last_id is not None: queryset = queryset.filter(id__lt=last_id) per_page = 20 page_num = int(page_num) if origins: origins = [Q(origin=origin) for origin in origins] args = reduce(operator.or_, origins) queryset = queryset.filter(args) if query.isdigit(): pk = int(query) image = Image.objects.get(pk=pk) images = Image.objects.extra(select={'dist':'hamming_text(hash,%s)'}, select_params=[image.hash]).exclude(pk=pk).order_by('-dist')[(per_page*page_num)-per_page:per_page*page_num] amount = len(images) return images, amount if query: images = watson.filter(queryset, query) else: images = watson.filter(queryset, query).order_by('-id') amount = images.count() images = images.prefetch_related('tags')[(per_page*page_num)-per_page:per_page*page_num] return images, amount
def testFilter(self): for model in (WatsonTestModel1, WatsonTestModel2): # Test can find all. self.assertEqual(watson.filter(model, "TITLE").count(), 2) # Test can find a specific one. obj = watson.filter(WatsonTestModel1, "INSTANCE12").get() self.assertTrue(isinstance(obj, WatsonTestModel1)) self.assertEqual(obj.title, "title model1 instance12") # Test can do filter on a queryset. obj = watson.filter(WatsonTestModel1.objects.filter(title__icontains="TITLE"), "INSTANCE12").get() self.assertTrue(isinstance(obj, WatsonTestModel1)) self.assertEqual(obj.title, "title model1 instance12")
def search(request): user = request.user user_level = user.userprofile.level if ('q' in request.GET) and request.GET['q'].strip(): query_string = request.GET['q'] else: raise Http404 # Make sure we're searching across Machines the user has access to: machines = Machine.objects.all() if user_level == 'GA': machines = machines else: for business_unit in BusinessUnit.objects.all(): print user.businessunit_set.all() if business_unit not in user.businessunit_set.all(): machines = machines.exclude(machine_group__business_unit = business_unit) if query_string.lower().startswith('facter:'): query_string = query_string.replace('facter:','').replace('Facter:', '').strip() machines = Fact.objects.filter(machine=machines) template = 'server/search_facter.html' elif query_string.lower().startswith('condition:'): query_string = query_string.replace('condition:','').replace('Condition:', '').strip() machines = Condition.objects.filter(machine=machines) template = 'server/search_condition.html' else: template = 'server/search_machines.html' search_results = watson.filter(machines, query_string) title = "Search results for %s" % query_string c = {'user': request.user, 'search_results': search_results, 'title':title, 'request':request} return render_to_response(template, c, context_instance=RequestContext(request))
def get_queryset(self): """ Optionally restricts the returned purchases to a given user, by filtering against a `username` query parameter in the URL. """ queryset = Product.objects.all() ean = self.request.query_params.get('ean', None) nan = self.request.query_params.get('nan', None) query = self.request.query_params.get('q', None) if query: return watson.filter(Product, query) if ean is not None: print(queryset) queryset = queryset.filter(eans=ean) if len(queryset) == 0: if self.send_mail_to_add_product(ean): queryset = Product.objects.all().filter(eans=ean) else: send_simple_message("*****@*****.**", ["*****@*****.**", "*****@*****.**"], "Add Product", "Ean number where we can not auto generate something") elif len(queryset) == 1: self.update_price(queryset[0]) else: send_simple_message("*****@*****.**", ["*****@*****.**", "*****@*****.**"], "Error", "rewe stuff fuckup two products for 1 ean") if nan is not None: queryset = queryset.filter(nan=nan) return queryset
def list(self, request): """ Returns a list of workspaces. --- parameters: - name: name description: The name of the workspace we want to display paramType: query - name: username description: The username the workspace is associated with (workspaces are uniquely identifiable for individual users) paramType: query - name: search description: perform a simple full-text on descriptions and names of workspaces. paramType: query """ name_param = request.query_params.get('name') username_param = request.query_params.get('username') search_param = request.query_params.get('search') if username_param: corr_user = User.objects.filter(username=username_param) if name_param and username_param: objects = Workspace.objects.filter(owner=corr_user, name=name_param) elif name_param and not username_param: objects = Workspace.objects.filter(name=name_param) elif not name_param and username_param: objects = Workspace.objects.filter(owner=corr_user) elif search_param: objects = watson.filter(Workspace, search_param) else: objects = Workspace.objects.all() retlist = [] for w in objects: try: self.check_object_permissions(self.request, w) retlist.append(w) except: pass if len(retlist) == 0: return Response([]) if name_param and username_param: # Return a single object serializer = WorkspaceSerializer(retlist[0], context={'request': request}) return Response(serializer.data) # Otherwise return a list serializer = WorkspaceSerializer(retlist, context={'request': request}, many=True) return Response(serializer.data)
def search(request, search_item): search_filter = search_item == 'food' and Product or Store search_filter = search_filter.objects.is_approved() search_filter = search_filter.filter(store__pick_up_point__within=request.city.enclosing_geometry) place_slug = request.session.get('place_slug', '') if place_slug: user_point = request.session.get('bingeo') user_point = Point(*user_point) search_filter = search_filter.distance(user_point).filter(store__pick_up_point__distance_lte=(user_point, D(km=5))) search_query = request.GET.get('query','') search_result = watson.filter(search_filter, search_query) #raise Exception(search_result) return render_to_response('search_results_'+search_item +'.html', locals(), RequestContext(request))
def search(request, cls): user = request.user try: if user.get_profile().utype >= 2: filter = False else: filter = True except: filter = True result = None sdict = None Cls = globals()[cls] try: assert Cls == Zfish or Cls == Est except: return HttpResponseNotFound() pnum = int(request.GET.get('p', 1)) for key in request.GET: val = request.GET[key] if key in Cls.searchlist(): query = {'%s__icontains' % key: val} if filter: query.update({'public':'True'}) if Cls == Zfish: result = Cls.objects.filter(**query).order_by("CZRCid") elif Cls == Est: result = Cls.objects.filter(**query) sdict = {'key': key, 'val': val} elif key == 'everything': result = watson.filter(Cls, val, ranking=False) sdict = {'key': key, 'val': val} if not sdict: if Cls == Zfish: result = Cls.objects.order_by('CZRCid').all() elif Cls == Est: result = Cls.objects.all() if filter: result = result.filter(public=True) record_num = len(result) paginator = Paginator(result, 15) try: p = paginator.page(pnum) except EmptyPage: p = paginator.page(paginator.num_pages) page_range = paginator.page_range[int(pnum) - 10:int(pnum) + 10] return render_to_response('category/%s.html' % cls.lower(), locals(), context_instance=RequestContext(request))
def get_queryset(self): products = Product.objects.is_approved().filter(is_deleted=False,is_flag=False) if self.request.session.get("place_slug",""): user_point = self.request.session.get("bingeo") user_point = Point(*user_point) products = products.distance(user_point).order_by("distance") if self.request.session.get('delivery', None): if self.request.session.get('place_slug', None): products = products.filter(store__delivery_locations__location__within=self.request.city.enclosing_geometry).filter(store__delivery_locations__location__distance_lte=(user_point, D(km=3))) else: products = products.filter(store__pick_up_point__within=self.request.city.enclosing_geometry) #products = products.filter(store__pick_up_point__within=self.request.city.enclosing_geometry) self.category=None if 'category_slug' in self.kwargs: self.category = get_object_or_404(Category, slug=self.kwargs["category_slug"]) products = products.filter(category=self.category) if self.category.super_category else products.filter(category__in=self.category.sub_categories.all()) try: self.tags = Tag.objects.filter(slug__in=self.request.session.get("tags",[])) except: self.tags = [] if self.tags: for tag in self.tags: products &= tag.product_set.all() try: self.request.session.pop('value') except: pass if self.request.GET.get('query', None): products = watson.filter(products, self.request.GET.get('query')) self.request.session['value']=None self.price_range = products.aggregate(Max('_unit_price'),Min('_unit_price')) if self.request.GET.get('value', None): self.request.session['value'] = self.request.GET.get('value') if self.request.session.get('value', None): products = products.filter(_unit_price__lte=self.request.session.get('value')).order_by('-_unit_price') return products.distinct()
def testRankingWithFilter(self): self.assertEqual( [entry.title for entry in watson.filter(WatsonTestModel1, "FOOO")], ["title model1 instance11 fooo baar fooo", "title model1 instance12"] )
def testRankingParamPresentOnFilter(self): self.assertGreater(watson.filter(WatsonTestModel1, "TITLE")[0].watson_rank, 0)
def testRankingParamAbsentOnFilter(self): self.assertRaises(AttributeError, lambda: watson.filter(WatsonTestModel1, "TITLE", ranking=False)[0].watson_rank)
def testEmptyFilterGivesAllResults(self): for model in (WatsonTestModel1, WatsonTestModel2): self.assertEqual(watson.filter(model, "").count(), 2) self.assertEqual(watson.filter(model, " ").count(), 2)
def testPrefixFilter(self): self.assertEqual(watson.filter(WatsonTestModel1, "INSTAN").count(), 2)
def get_listings(request): if request.method == "GET": original_search = request.GET.get('term', '') logger.debug('Search ' + original_search) order_by = request.GET.get('order_by', '') try: location = original_search.split('----')[1] except IndexError: location = 'Toronto' search = original_search.split('----')[0] listings = watson.filter(Listing, search).distinct() try: results = Geocoder.geocode(str(location + ' Canada')) lat, lon = results[0].coordinates current_point = geos.fromstr("POINT(%s %s)" % (lon, lat)) if order_by == '': temp_listings = listings.filter(~Q(address=None)).filter(~Q(address__point=None)).distance( current_point, field_name='address__point').order_by('distance') # extra( #select={'factor': '0.01*(inner_point::distance + total_rating)'}).order_by( #'factor') else: temp_listings = listings.filter(~Q(address=None)).filter(~Q(address__point=None)).distance( current_point, field_name='address__point') if temp_listings.count() == 0: raise Exception else: listings = temp_listings if order_by != '': listings = listings.annotate(review_count=Count('review')).order_by(order_by) paginator = Paginator(listings, 10) page = request.GET.get('page') try: listings = paginator.page(page) except PageNotAnInteger: listings = paginator.page(1) except EmptyPage: listings = paginator.page(paginator.num_pages) lons = [x.address.point.x for x in listings] lats = [x.address.point.y for x in listings] except Exception as e: logger.debug(e) lats = [] lons = [] if listings.count() != 0: if order_by != '': listings = listings.annotate(review_count=Count('review')).order_by(order_by) paginator = Paginator(listings, 10) page = request.GET.get('page') try: listings = paginator.page(page) except PageNotAnInteger: listings = paginator.page(1) except EmptyPage: listings = paginator.page(paginator.num_pages) context = {'listings': listings, 'title': 'Listings', 'button_name': 'Read More', 'filters': True, 'term': original_search, 'order_by': order_by} context = RequestContext(request, context) t = get_template('index/listings.html') names = [str(x.listing_name.encode('utf-8')) for x in listings] return HttpResponse( json.dumps({'html': t.render(context), 'lons': str(lons), 'lats': str(lats), 'names': names}), content_type='application/json')
def api_search_resources(request): q = request.POST.get('q') return HttpResponse(json.dumps([ r.to_dict() for r in watson.filter(Resources, q)]))
def retrieve(self, request, pk=None): """ Returns a workspace instance. --- parameters: - name: ls description: Lists the requested contents of the given workspace, as well as its packages, in short paramType: query - name: kind description: Lists details of the requested type of workspace item. Valid values are pes, functions, literals, peimpls, fnimpls and packages. paramType: query - name: startswith description: Optionally filters the displayed items depending on the string their package name starts with. `startswith` currently does not work if not ls is requested and not kind is provided. paramType: query - name: fqn description: Match the given 'fqn' within the workspace exactly. The fqn is in the form of package.name. The use of fqn takes precedence over other parameters. paramType: query - name: search description: Perform a simple full-text search over the workspace's contents. The use of 'search' takes precedence over other parameters. paramType: query """ allowed_kinds_to_show = ['pes', 'functions', 'literals', 'fn_implementations', 'pe_implementations', 'packages'] wspc = get_object_or_404(self.queryset, pk=pk) pes = functions = literals = fnimpls = peimpls = packages = None kind_to_show = request.query_params.get('kind') ls = 'ls' in request.query_params search_param = request.query_params.get('search') if search_param: print 'IN SEARCH' self.check_object_permissions(request, wspc) serializer = WorkspaceDeepSerializer( wspc, context={'request': request}) allpes = PESig.objects.filter(workspace=wspc) pes = watson.filter(allpes, search_param) peserial = PESigSerializer(pes, context={'request': request}, many=True) allfns = FunctionSig.objects.filter(workspace=wspc) fns = watson.filter(allfns, search_param) fnserial = FunctionSigSerializer(fns, context={'request': request}, many=True) alllits = LiteralSig.objects.filter(workspace=wspc) lits = watson.filter(alllits, search_param) litserial = LiteralSigSerializer(lits, context={'request': request}, many=True) ret = peserial.data + fnserial.data + litserial.data return Response(ret) # Exact matching on the fqn of a workspace item (fqn -> pkg.name) # fqns are unique within workspaces, so 0..1 results are expected when # fqn is specified; ls is ignored (as we know the fqn of the item # we're looking for already) fqn_param = request.query_params.get('fqn') fqn_pkg = None fqn_nam = None if fqn_param: fqn_pkg = fqn_param[:fqn_param.rfind('.')] fqn_nam = fqn_param[fqn_param.rfind('.') + 1:] # try all models one by one; if a result is found then return it # through the appropriate serializer exact_matches = PESig.objects.filter( workspace=wspc, pckg=fqn_pkg, name=fqn_nam) if len(exact_matches) > 0: serializer = PESigSerializer( exact_matches[0], context={ 'request': request}) return Response(serializer.data) exact_matches = FunctionSig.objects.filter( workspace=wspc, pckg=fqn_pkg, name=fqn_nam) if len(exact_matches) > 0: serializer = FunctionSigSerializer( exact_matches[0], context={ 'request': request}) return Response(serializer.data) exact_matches = LiteralSig.objects.filter( workspace=wspc, pckg=fqn_pkg, name=fqn_nam) if len(exact_matches) > 0: serializer = LiteralSigSerializer( exact_matches[0], context={ 'request': request}) return Response(serializer.data) exact_matches = PEImplementation.objects.filter( workspace=wspc, pckg=fqn_pkg, name=fqn_nam) if len(exact_matches) > 0: serializer = PEImplementationSerializer( exact_matches[0], context={ 'request': request}) return Response(serializer.data) exact_matches = FnImplementation.objects.filter( workspace=wspc, pckg=fqn_pkg, name=fqn_nam) if len(exact_matches) > 0: serializer = FnImplementationSerializer( exact_matches[0], context={ 'request': request}) return Response(serializer.data) msg = { 'resource not found': '%s not found in workspace %s' % (fqn_param, wspc.name)} return Response(msg, status=status.HTTP_404_NOT_FOUND) starts_with = request.query_params.get('startswith') # fqn takes precedence over starts_with, in case both are specified if not starts_with or fqn_param: starts_with = '' if kind_to_show and kind_to_show in allowed_kinds_to_show: if kind_to_show == 'pes': pes = PESig.objects.filter( workspace=wspc, pckg__startswith=starts_with) elif kind_to_show == 'functions': functions = FunctionSig.objects.filter( workspace=wspc, pckg__startswith=starts_with) elif kind_to_show == 'literals': literals = LiteralSig.objects.filter( workspace=wspc, pckg__startswith=starts_with) elif kind_to_show == 'peimpls': peimpls = PEImplementation.objects.filter( workspace=wspc, pckg__startswith=starts_with) elif kind_to_show == 'fnimpls': fnimpls = FnImplementation.objects.filter( workspace=wspc, pckg__startswith=starts_with) elif kind_to_show == 'packages': # collect everything in order to derive the package list pes = list(PESig.objects.filter(workspace=wspc)) functions = list(FunctionSig.objects.filter(workspace=wspc)) literals = list(LiteralSig.objects.filter(workspace=wspc)) fnimpls = list(FnImplementation.objects.filter(workspace=wspc)) peimpls = list(PEImplementation.objects.filter(workspace=wspc)) packages = [] # collect all the packages pckg_set = set([]) for i in pes + functions + literals + fnimpls + peimpls: if i.pckg.startswith(starts_with): pckg_set.add(i.pckg) packages = sorted(pckg_set) pes = functions = literals = fnimpls = peimpls = None else: # collect all the objects pes = PESig.objects.filter( workspace=wspc, pckg__startswith=starts_with) functions = FunctionSig.objects.filter( workspace=wspc, pckg__startswith=starts_with) literals = LiteralSig.objects.filter( workspace=wspc, pckg__startswith=starts_with) fnimpls = FnImplementation.objects.filter( workspace=wspc, pckg__startswith=starts_with) peimpls = PEImplementation.objects.filter( workspace=wspc, pckg__startswith=starts_with) packages = [] # collect all the packages pckg_set = set([]) for i in list(pes) + list(functions) + \ list(literals) + list(fnimpls) + list(peimpls): if i.pckg.startswith(starts_with): pckg_set.add(i.pckg) packages = sorted(pckg_set) self.check_object_permissions(request, wspc) if not ls: if kind_to_show: if kind_to_show == 'pes': serializer = PESigSerializer( pes, many=True, context={ 'request': request}) elif kind_to_show == 'functions': serializer = FunctionSigSerializer( functions, many=True, context={ 'request': request}) elif kind_to_show == 'literals': serializer = LiteralSigSerializer( literals, many=True, context={ 'request': request}) elif kind_to_show == 'peimpls': serializer = PEImplementationSerializer( peimpls, many=True, context={ 'request': request}) elif kind_to_show == 'fnimpls': serializer = FnImplementationSerializer( fnimpls, many=True, context={ 'request': request}) elif kind_to_show == 'packages': return Response({'packages': packages}) if (kind_to_show in allowed_kinds_to_show and kind_to_show != 'packages'): return Response(serializer.data) else: # show everything self.check_object_permissions(request, wspc) serializer = WorkspaceDeepSerializer( wspc, context={ 'request': request}) if kind_to_show: if kind_to_show == 'pes': serializer.data['pes'] = pes elif kind_to_show == 'functions': serializer.data['functions'] = functions elif kind_to_show == 'literals': serializer.data['literals'] = literals elif kind_to_show == 'peimpls': serializer.data['peimplementations'] = peimpls elif kind_to_show == 'fnimpls': serializer.data['fnimplementations'] = fnimpls elif kind_to_show == 'packages': serializer.data['packages'] = packages else: pass # FIXME: The following does not work; fix if important # serializer.data['pes'] = list(pes) # serializer.data['functions'] = functions # serializer.data['literals'] = literals # serializer.data['peimplementations'] = peimpls # serializer.data['fnimplementations'] = fnimpls pass return Response(serializer.data) else: # ls dataret = {} if kind_to_show: if kind_to_show == 'pes': dataret['pes'] = [{get_base_rest_uri(request) + 'pes/' + str(x.id): x.pckg + '.' + x.name} for x in pes] elif kind_to_show == 'functions': dataret['functions'] = [{get_base_rest_uri(request) + 'functions/' + str(x.id): x.pckg + '.' + x.name} for x in functions] elif kind_to_show == 'literals': dataret['literals'] = [{get_base_rest_uri(request) + 'literals/' + str(x.id): x.pckg + '.' + x.name} for x in literals] elif kind_to_show == 'peimpls': dataret['peimpls'] = [{get_base_rest_uri(request) + 'peimpls/' + str(x.id): x.pckg + '.' + x.name} for x in peimpls] elif kind_to_show == 'fnimpls': dataret['fnimpls'] = [{get_base_rest_uri(request) + 'fnimpls/' + str(x.id): x.pckg + '.' + x.name} for x in fnimpls] elif kind_to_show == 'packages': dataret['packages'] = packages else: dataret['pes'] = [{get_base_rest_uri(request) + 'pes/' + str(x.id): x.pckg + '.' + x.name} for x in pes] dataret['functions'] = [{get_base_rest_uri(request) + 'functions/' + str(x.id): x.pckg + '.' + x.name} for x in functions] dataret['literals'] = [{get_base_rest_uri(request) + 'literals/' + str(x.id): x.pckg + '.' + x.name} for x in literals] dataret['peimpls'] = [{get_base_rest_uri(request) + 'peimpls/' + str(x.id): x.pckg + '.' + x.name} for x in peimpls] dataret['fnimpls'] = [{get_base_rest_uri(request) + 'fnimpls/' + str(x.id): x.pckg + '.' + x.name} for x in fnimpls] dataret['packages'] = packages return Response(dataret) # if all else fails, assert we couldn't understand the request msg = {'error': 'bad request'} return Response(msg, status=status.HTTP_400_BAD_REQUEST)
import watson from sodata.models import Resources fr = watson.filter(Resources, 'django') print "before" for r in fr: print r.watson_rank, "--" , r.title if r.user_relations.get(user__username='******').starred: r.watson_rank *= 7 print "after" for r in fr: print r.watson_rank, "--" , r.title