def count(self): return QuerySet.count(self) count = cache.get(self._get_cache_key('count')) if count is None: count = int(QuerySet.count(self)) cache.set(self._get_cache_key('count'), count, self.cache_timeout) return count
def appquery(self, djp): '''This function implements the search query. The query is build using the search fields specifies in :attr:`djpcms.views.appsite.ModelApplication.search_fields`. It returns a queryset. ''' qs = super(SearchView,self).appquery(djp) request = djp.request slist = self.appmodel.opts.search_fields if request.method == 'GET': data = dict(request.GET.items()) else: data = dict(request.POST.items()) search_string = data.get(self.search_text,None) if slist and search_string: bits = smart_split(search_string) #bits = search_string.split(' ') for bit in bits: bit = isexact(bit) if not bit: continue or_queries = [Q(**{construct_search(field_name): bit}) for field_name in slist] other_qs = QuerySet(self.appmodel.modelsearch()) other_qs.dup_select_related(qs) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) qs = qs & other_qs return qs
def autocomplete_lookup(request): if not (request.user.is_active and request.user.is_staff): return HttpResponseForbidden('<h1>Permission denied</h1>') data = [] if request.method == 'GET': if request.GET.has_key('term') and request.GET.has_key('app_label') and request.GET.has_key('model_name'): term = request.GET.get("term") app_label = request.GET.get('app_label') model_name = request.GET.get('model_name') model = models.get_model(app_label, model_name) filters = {} # FILTER if request.GET.get('query_string', None): for item in request.GET.get('query_string').split("&"): if item.split("=")[0] != "t": filters[smart_str(item.split("=")[0])]=smart_str(item.split("=")[1]) # SEARCH qs = model._default_manager.all() for bit in term.split(): search = [models.Q(**{smart_str(item):smart_str(bit)}) for item in model.autocomplete_search_fields()] search_qs = QuerySet(model) search_qs.dup_select_related(qs) search_qs = search_qs.filter(reduce(operator.or_, search)) qs = qs & search_qs data = [{"value":f.pk,"label":u'%s' % get_label(f)} for f in qs[:10]] label = ungettext( '%(counter)s result', '%(counter)s results', len(data)) % { 'counter': len(data), } #data.insert(0, {"value":None,"label":label}) return HttpResponse(simplejson.dumps(data), mimetype='application/javascript') data = [{"value":None,"label":_("Server error")}] return HttpResponse(simplejson.dumps(data), mimetype='application/javascript')
def foreignkey_autocomplete(self, request): """ Searches in the fields of the given related model and returns the result as a simple string to be used by the jQuery Autocomplete plugin """ query = request.GET.get('q', None) app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) search_fields = request.GET.get('search_fields', None) object_pk = request.GET.get('object_pk', None) try: to_string_function = self.related_string_functions[model_name] except KeyError: if six.PY3: to_string_function = lambda x: x.__str__() else: to_string_function = lambda x: x.__unicode__() if search_fields and app_label and model_name and (query or object_pk): def construct_search(field_name): # use different lookup methods depending on the notation if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name model = apps.get_model(app_label, model_name) queryset = model._default_manager.all() data = '' if query: for bit in query.split(): or_queries = [models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')] other_qs = QuerySet(model) other_qs.query.select_related = queryset.query.select_related other_qs = other_qs.filter(reduce(operator.or_, or_queries)) queryset = queryset & other_qs additional_filter = self.get_related_filter(model, request) if additional_filter: queryset = queryset.filter(additional_filter) if self.autocomplete_limit: queryset = queryset[:self.autocomplete_limit] data = ''.join([six.u('%s|%s\n') % (to_string_function(f), f.pk) for f in queryset]) elif object_pk: try: obj = queryset.get(pk=object_pk) except Exception: # FIXME: use stricter exception checking pass else: data = to_string_function(obj) return HttpResponse(data, content_type='text/plain') return HttpResponseNotFound()
def get_searched_queryset(self, qs): model = self.model term = self.GET["term"] try: term = model.autocomplete_term_adjust(term) except AttributeError: pass try: search_fields = model.autocomplete_search_fields() except AttributeError: try: search_fields = AUTOCOMPLETE_SEARCH_FIELDS[model._meta.app_label][model._meta.module_name] except KeyError: search_fields = () if search_fields: for word in term.split(): search = [models.Q(**{smart_text(item): smart_text(word)}) for item in search_fields] search_qs = QuerySet(model) search_qs.query.select_related = qs.query.select_related search_qs = search_qs.filter(reduce(operator.or_, search)) qs &= search_qs else: qs = model.objects.none() return qs
def leaf_filter(self, query): ''' Should not be called directly, use filter() Executes a query over leaf instances of MapObj allows for some complex / cross model queries ex: q1 = Q(name__icontains='commons') q2 = Q(abbreviation__icontains='map') MapObj.objects.filter(q1|q2) returns: [<Group: Ferrell Commons>, <Building: Libra Commons>, <Building: Math & Physics>, ...] ''' # grab all the models that extend MapObj if not MapQuerySet.campus_models: MapQuerySet.campus_models = [] for ct in ContentType.objects.filter(app_label="campus"): model = models.get_model("campus", ct.model) if issubclass(model, campus.models.MapObj): MapQuerySet.campus_models.append(model) # return queryset containing MapObj's mob_query = Q(pk="~~~ no results ~~~") for m in self.campus_models: if m == campus.models.MapObj: continue try: qs = QuerySet(m) results = qs.filter(query) for o in results: mob_query = mob_query | Q(id=o.mapobj_ptr_id) except FieldError: continue return mob_query
def order_by(self, *field_names): if 'title' in field_names: field_list = [field.replace('title','lower_title') for field in field_names] self = self.extra(select={'lower_title': 'lower(title)'}) return QuerySet.order_by(self, *field_list) return QuerySet.order_by(self, *field_names)
def search (query): contactos = Contacto.objects.filter ( reduce (operator.or_, [ models.Q(dato__icontains = bit) for bit in query.split() ]) ) search_fields = [ 'nombre__icontains', 'apellidos__icontains', 'documento_identificacion__icontains', 'lugar_nacimiento__icontains', 'nacionalidad__icontains', ] # your search fields here personasEncontradas = QuerySet(Persona) for bit in query.split (): or_queries = [models.Q(**{field_name: bit}) for field_name in search_fields] other_qs = QuerySet(Persona) other_qs.dup_select_related(personasEncontradas) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) personasEncontradas = personasEncontradas & other_qs pe = [] for p in personasEncontradas: pe.append (p) for c in contactos: if c.persona not in pe: pe.append (c.persona) return pe
def people_detail(request, people_permalink, meter_permalink=None): people = get_object_or_404(People, permalink=people_permalink) meter_list = Meter.objects.all().order_by('order') meter_statement_count = [(meter, meter.statement_set.filter(status=STATUS_PUBLISHED, quoted_by=people).count()) for meter in meter_list] statement_list = statement_query_base(request.user.is_anonymous(), request.user.is_staff, request.user) statement_list = statement_list.filter(Q(quoted_by=people)|Q(relate_peoples=people)).order_by('-uptodate') query = statement_list.query query.group_by = ['id'] statement_list = QuerySet(query=query, model=Statement) request_meter = None if meter_permalink: request_meter = get_object_or_404(Meter, permalink=meter_permalink) statement_list = statement_list.filter(meter=request_meter) statement_list = pagination_build_query(request, statement_list, 5) people_list = people_query_base(is_anonymous=True) people_list = people_list.exclude(id=people.id) people_list = people_list[0:2] return render(request, 'domain/people_detail.html', { 'people': people, 'meter_statement_count': meter_statement_count, 'statement_list': statement_list, 'people_list': people_list, 'request_meter': request_meter })
def search(self, request): """ Searches in the fields of the given related model and returns the result as a simple string to be used by the jQuery Autocomplete plugin """ query = request.GET.get('q', None) app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) search_fields = request.GET.get('search_fields', None) if search_fields and app_label and model_name and query: def construct_search(field_name): # use different lookup methods depending on the notation if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name model = models.get_model(app_label, model_name) qs = model._default_manager.all() for bit in query.split(): or_queries = [models.Q(**{construct_search( smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')] other_qs = QuerySet(model) other_qs.dup_select_related(qs) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) qs = qs & other_qs data = ''.join([u'%s|%s\n' % (f.__unicode__(), f.pk) for f in qs]) return HttpResponse(data) return HttpResponseNotFound()
def filter_callback(self, items): items = sorted(items, key=lambda t: six.text_type( t)) if (self.getparam('CRITERIA') is None) and (self.getparam('show_filter', 0) == 2): items = [item for item in items if abs(item.get_total()) > 0.0001] res = QuerySet(model=Third) res._result_cache = items return res
def get_items_from_filter(self): items = XferListEditor.get_items_from_filter(self) items = sorted(items, key=lambda t: six.text_type(t).lower()) if self.getparam('show_filter', 0) == 2: items = [item for item in items if abs(item.get_total()) > 0.0001] res = QuerySet(model=Third) res._result_cache = items return res
def migrate(limit=None, loop_sleep=0): #qs = Page.objects.filter(layout_migrated=False).only('id', 'layout_json') qs = QuerySet(Page).filter(layout_migrated=False).only('id', 'layout_json') if limit: qs = qs[:limit] for page in qs.iterator(): migrate_page(page) time.sleep(loop_sleep)
def get_query_set(self): """Returns a new QuerySet object. Subclasses can override this method to easily customize the behavior of the Manager. """ qs = QuerySet(self.model) if self._db is not None: qs = qs.using(self._db) return qs
def get_query_set(self): qs = self.manager.get_query_set() lookup_params = self.params.copy() # a dictionary of the query string for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR): if i in lookup_params: del lookup_params[i] for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[smart_str(key)] = value # Apply lookup parameters from the query string. qs = qs.filter(**lookup_params) # Use select_related() if one of the list_display options is a field # with a relationship. if self.lookup_opts.admin.list_select_related: qs = qs.select_related() else: for field_name in self.lookup_opts.admin.list_display: try: f = self.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: pass else: if isinstance(f.rel, models.ManyToOneRel): qs = qs.select_related() break # Set ordering. if self.order_field: qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field)) # Apply keyword searches. def construct_search(field_name): if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name if self.lookup_opts.admin.search_fields and self.query: for bit in self.query.split(): or_queries = [models.Q(**{construct_search(field_name): bit}) for field_name in self.lookup_opts.admin.search_fields] other_qs = QuerySet(self.model) other_qs.dup_select_related(qs) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) qs = qs & other_qs if self.opts.one_to_one_field: qs = qs.complex_filter(self.opts.one_to_one_field.rel.limit_choices_to) return qs
def assertCopied(self, original, site): """Assert that the model was copied to the new site.""" # Use the QuerySet to get around the forced site filter when using the CurrentSiteManager copied = QuerySet(original.__class__).filter(slug=original.slug, site=site) self.assertTrue(copied.exists()) self.assertEqual(copied.count(), 1) self.assertTrue(copied.exists()) copy = copied[0] self.assertNotEqual(copy.pk, original.pk)
def render(self, name, value, attrs=None): if value is None: value = "" display = "" if self.url: url = self.url display = self.initial_display else: dc, dc, query = pickle.loads(_simple_autocomplete_queryset_cache[self.token]) queryset = QuerySet(model=self.model, query=query) threshold = get_threshold_for_model(self.model) if threshold and (queryset.count() < threshold): # Render the normal select widget if size below threshold return super(AutoCompleteWidget, self).render(name, value, attrs) else: url = reverse("simple-autocomplete", args=[self.token]) fieldname = get_search_fieldname(self.model) if value: display = getattr(queryset.get(pk=value), fieldname) html = """ <script type="text/javascript"> $(document).ready(function(){ $("#id_%(name)s_helper").autocomplete({ source: function(request, response){ $.ajax({ url: "%(url)s", data: {q: request.term}, success: function(data) { if (data != 'CACHE_MISS') response($.map(data, function(item) { return { label: item[1], value: item[1], real_value: item[0] }; })); }, dataType: "json" }); }, select: function(event, ui) { $('#id_%(name)s').val(ui.item.real_value); }, minLength: 3 }); }); </script> <input id="id_%(name)s_helper" type="text" value="%(display)s" /> <a href="#" title="Clear" onclick="$('#id_%(name)s_helper').val(''); $('#id_%(name)s').val(''); return false;">x<small></small></a> <input name="%(name)s" id="id_%(name)s" type="hidden" value="%(value)s" />""" % dict( name=name, url=url, display=display, value=value ) return mark_safe(html)
def foreignkey_autocomplete(self, request): """ Searches in the fields of the given related model and returns the result as a simple string to be used by the jQuery Autocomplete plugin """ query = request.GET.get('q', None) app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) search_fields = request.GET.get('search_fields', None) object_pk = request.GET.get('object_pk', None) try: to_string_function = \ self.related_string_functions[model_name] except KeyError: to_string_function = lambda x: x.__unicode__() if search_fields and app_label and model_name and (query or object_pk): def construct_search(field_name): # use different lookup methods depending on the notation if field_name.startswith('^'): return '%s__istartswith' % field_name[1:] elif field_name.startswith('='): return '%s__iexact' % field_name[1:] elif field_name.startswith('@'): return '%s__search' % field_name[1:] else: return '%s__icontains' % field_name model = models.get_model(app_label, model_name) queryset = model._default_manager.all() data = '' if query: for bit in query.split(): or_queries = \ [models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')] other_qs = QuerySet(model) other_qs.dup_select_related(queryset) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) queryset = queryset & other_qs data = ''.join([u'%s|%s\n' % (to_string_function(f), f.pk) for f in queryset]) elif object_pk: try: obj = queryset.get(pk=object_pk) except: pass else: data = to_string_function(obj) return HttpResponse(data) return HttpResponseNotFound()
def foreignkey_autocomplete(self, request): """ Searches in the fields of the given related model and returns the result as a simple string to be used by the jQuery Autocomplete plugin """ query = request.GET.get("q", None) app_label = request.GET.get("app_label", None) model_name = request.GET.get("model_name", None) search_fields = request.GET.get("search_fields", None) object_pk = request.GET.get("object_pk", None) try: to_string_function = self.related_string_functions[model_name] except KeyError: to_string_function = lambda x: x.__unicode__() if search_fields and app_label and model_name and (query or object_pk): def construct_search(field_name): # use different lookup methods depending on the notation if field_name.startswith("^"): return "%s__istartswith" % field_name[1:] elif field_name.startswith("="): return "%s__iexact" % field_name[1:] elif field_name.startswith("@"): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name model = models.get_model(app_label, model_name) queryset = model._default_manager.all() data = "" if query: for bit in query.split(): or_queries = [ models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(",") ] other_qs = QuerySet(model) other_qs.query.select_related = queryset.query.select_related other_qs = other_qs.filter(reduce(operator.or_, or_queries)) queryset = queryset & other_qs if self.autocomplete_limit: queryset = queryset[: self.autocomplete_limit] data = "".join([six.u("%s|%s\n") % (to_string_function(f), f.pk) for f in queryset]) elif object_pk: try: obj = queryset.get(pk=object_pk) except: pass else: data = to_string_function(obj) return HttpResponse(data) return HttpResponseNotFound()
def foreignkey_autocomplete(self, request): def _restrict_queryset(queryset, search_fields): for bit in search_fields.split(','): if bit[0] == '#': key, val = bit[1:].split('=') queryset = queryset.filter(**{key: val}) return queryset query = request.GET.get('q', None) app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) search_fields = request.GET.get('search_fields', None) object_pk = request.GET.get('object_pk', None) try: to_string_function = self.related_string_functions[model_name] except KeyError: to_string_function = lambda x: x.__unicode__() if search_fields and app_label and model_name and (query or object_pk): def construct_search(field_name): if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name model = models.get_model(app_label, model_name) queryset = model._default_manager.all() data = '' if query: for bit in query.split(): or_queries = [] for field_name in search_fields.split(','): if field_name[0] == "#": continue or_queries.append( models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)})) other_qs = QuerySet(model) other_qs.dup_select_related(queryset) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) queryset = queryset & other_qs queryset = _restrict_queryset(queryset, search_fields) data = ''.join([u'%s|%s\n' % ( to_string_function(f), f.pk) for f in queryset]) elif object_pk: try: obj = queryset.get(pk=object_pk) except: pass else: data = to_string_function(obj) return HttpResponse(data) return HttpResponseNotFound()
def get_searched_queryset(self, qs): model = self.model term = self.GET["term"] for word in term.split(): search = [models.Q(**{smart_str(item): smart_str(word)}) for item in model.autocomplete_search_fields()] search_qs = QuerySet(model) search_qs.dup_select_related(qs) search_qs = search_qs.filter(reduce(operator.or_, search)) qs &= search_qs return qs
def foreignkey_autocomplete(self, request): """ Searches in the fields of the given related model and returns the result as a simple string to be used by the jQuery Autocomplete plugin """ query = request.GET.get('q', None) app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) search_fields = request.GET.get('search_fields', None) object_pk = request.GET.get('object_pk', None) try: to_string_function = self.related_string_functions[model_name] except KeyError: to_string_function = lambda x: x.__unicode__() if search_fields and app_label and model_name and (query or object_pk): def construct_search(field_name): # use different lookup methods depending on the notation if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name model = models.get_model(app_label, model_name) queryset = model._default_manager.all() data = [] if query: for bit in query.split(): or_queries = [models.Q(**{construct_search( smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')] other_qs = QuerySet(model) other_qs.dup_select_related(queryset) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) queryset = queryset & other_qs if len(queryset) > 0: for f in queryset: label = to_string_function(f) thumb = f.get_thumbnail() id = f.pk data.append({'id': id, 'label': label, 'thumb': thumb}) else: data.append({'id': '', 'label': 'No results found', 'thumb': ''}) elif object_pk: try: obj = queryset.get(pk=object_pk) except: pass else: data = to_string_function(obj) return HttpResponse(simplejson.dumps(data), content_type="application/json") return HttpResponseNotFound()
def search(request, **kwargs): rg = request.REQUEST.get group = kwargs.get("group", False) q = rg("q", None) if not q: return HttpResponseRedirect("%s/publications/" % settings.URLBASE) search_fields = ['title', 'abstract', 'authorship__author__last_name', 'publication_name', 'book_title'] q = rg("q", "") if not q.split() == []: for bit in q.split(): or_queries = [Q(**{'%s__search' % field_name: bit}) for field_name in search_fields] qs = QuerySet(Publication) qs = qs.filter(reduce(operator.or_, or_queries)).distinct() else: qs = Publication.objects.all()[:0] if group: g = Group.objects.get(acronym=group) qs = qs.filter(groupship__group=g) css = g.defaultcss logo = g.get_logo_url() del kwargs['group'] else: css = settings.DEFAULT_CSS logo = settings.DEFAULT_LOGO g = False kwargs["queryset"] = qs kwargs["allow_empty"] = True #query = Q(title__search=q) | \ # Q(abstract__search=q) | \ # Q(authorship__author__last_name__search=q) | \ # Q(publication_name__search=q) # #Q(editorship__editor__last_name__search=q) #if rg("year"): # query &= Q(year__exact=rg("year")) #if rg("journal"): # query &= Q(journal__icontains=rg("journal", "")) #if rg("author"): # query &= Q(authorship__author__last_name__exact=rg("author")) d = request.GET.copy() if "page" in d: del d["page"] if "submit" in d: del d["submit"] kwargs["extra_context"] = {"query": urlencode(d), "query_readable": d["q"], "group": g, "css": css, "logo": logo} return object_list(request, **kwargs)
def foreignkey_autocomplete(self, request): """ Searches in the fields of the given related model and returns the result as a simple string to be used by the jQuery Autocomplete plugin """ query = request.GET.get('q', None) app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) search_fields = request.GET.get('search_fields', None) object_pk = request.GET.get('object_pk', None) try: to_string_function = self.related_string_functions[model_name] except KeyError: to_string_function = lambda x: x.__unicode__() if search_fields and app_label and model_name and (query or object_pk): def construct_search(field_name): # use different lookup methods depending on the notation if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name model = models.get_model(app_label, model_name) # as of Django 1.1 there is an Admin Specific record filter: # https://docs.djangoproject.com/en/1.1/ref/contrib/admin/#django.contrib.admin.ModelAdmin.queryset try: # See if there is a custom admin queryset: queryset = model.queryset(request) except: # If not, then make our own: queryset = model._default_manager.all() data = '' if query: for bit in query.split(): or_queries = [models.Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields.split(',')] other_qs = QuerySet(model) other_qs.dup_select_related(queryset) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) queryset = queryset & other_qs data = ''.join([six.u('%s|%s\n' % (to_string_function(f), f.pk)) for f in queryset]) elif object_pk: try: obj = queryset.get(pk=object_pk) except: pass else: data = to_string_function(obj) return HttpResponse(data) return HttpResponseNotFound()
def search(terms): query = terms.replace("+"," ") search_results = [] if query: for clazz, search_fields, filter_terms in class_search_fields: or_query=Q() other_qs = QuerySet(clazz) # your class here for bit in query.split(): or_queries = [Q(**{'%s__icontains' % field_name: bit}) for field_name in search_fields] other_qs = other_qs.filter(reduce(operator.or_, or_queries)) search_results.append((clazz, other_qs.filter(**filter_terms))) return search_results
def search (query, user): datos_contactos = DatoContacto.objects.filter ( reduce (operator.or_, [ models.Q(dato__icontains = bit, contacto__usuario = user) for bit in query.split() ]) ) search_fields = ['domicilio', 'codPostal', 'poblacion', 'provincia', 'pais'] direccion_contactos = QuerySet(Direccion) for bit in query.split (): or_queries = [models.Q(**{'%s__icontains' % field_name: bit, 'contacto__usuario': user}) for field_name in search_fields] other_qs = QuerySet(Direccion) other_qs.dup_select_related(direccion_contactos) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) direccion_contactos = direccion_contactos & other_qs search_fields = ['nombre', 'apellidos', ] # your search fields here personasEncontradas = QuerySet(Contacto) for bit in query.split (): or_queries = [models.Q(**{'%s__icontains' % field_name: bit, 'usuario': user}) for field_name in search_fields] other_qs = QuerySet(Contacto) other_qs.dup_select_related(personasEncontradas) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) personasEncontradas = personasEncontradas & other_qs pe = [] for p in personasEncontradas: pe.append(p) for c in datos_contactos: if c.contacto not in pe: pe.append(c.contacto) for d in direccion_contactos: if d.contacto not in pe: pe.append(d.contacto) return pe
def queryset(self, request: HttpRequest, queryset: QuerySet) -> QuerySet: """ Adjust the queryset based on the selection :param request: The incoming request :param queryset: The original queryset :return: The modified queryset """ val = self.value() if val == 'slow': return queryset.filter(response_ts__gt=F('request_ts') + timedelta(seconds=1)) elif val == 'no': return queryset.filter(response='') else: return queryset
def tag_cloud_context(tags: QuerySet) -> Dict[Any, Any]: if not tags.exists(): return {} min_size = 9 max_size = 20 tags = tags.annotate(Count('posts')).order_by('posts__count') min_count = tags[0].posts__count max_count = tags[tags.count()-1].posts__count def font_size(count): if max_count == min_count: return (max_size + min_size) / 2 else: slope = (max_size - min_size) / (max_count - min_count) return min_size + slope * count return {tag: font_size(tag.posts__count) for tag in tags}
def get(self, *args, **kwargs): """ Get an object by querying the translations model and returning a combined instance. """ # Enforce a language_code to be used newargs, newkwargs = self._translate_args_kwargs(*args, **kwargs) # Enforce 'select related' onto 'master' # Get the translated instance found = False qs = self if 'language_code' in newkwargs: language_code = newkwargs.pop('language_code') qs = self.language(language_code) found = True elif args: language_code = None for arg in args: if not isinstance(arg, Q): continue language_code = self._find_language_code(arg) if language_code: break if language_code: qs = self.language(language_code) found = True else: found = self._scan_for_language_where_node(qs.query.where.children) if not found: qs = self.language() # self.iterator already combines! Isn't that nice? return QuerySet.get(qs, *newargs, **newkwargs)
def get_rarity_ratio_rows(self, dates: List[date], decks: QuerySet, exclude_lands: bool = False) -> List[List[float]]: """ Gets the rows of rarity ratios for each of the given dates :param dates: The dates to create the rarity ratios for :param decks: The queryset of decks :param exclude_lands: Whether to exclude lands from the results :return: The rarity ratio rows """ rows = [] for created_date in dates: date_decks = decks.filter(date_created=created_date) row = [0] * len(self.rarities) deck_count = 0 for deck in date_decks: if sum(x.count for x in deck.cards.all()) < 60: continue rarity_ratios = self.get_deck_rarity_ratios(deck, exclude_lands) for idx, rarity in enumerate(self.rarities): row[idx] += rarity_ratios[rarity] deck_count += 1 if deck_count > 0: row = [x / deck_count for x in row] rows.append(row) return rows
def _execute_sql( self, filter: Filter, date_from: datetime.datetime, date_to: datetime.datetime, target_entity: Entity, returning_entity: Entity, is_first_time_retention: bool, team: Team, ) -> Dict[Tuple[int, int], Dict[str, Any]]: period = filter.period events: QuerySet = QuerySet() def get_entity_condition(entity: Entity) -> Q: if entity.type == TREND_FILTER_TYPE_EVENTS: return Q(event=entity.id) elif entity.type == TREND_FILTER_TYPE_ACTIONS: return Q(action__pk=entity.id) else: raise ValueError(f"Entity type not supported") entity_condition = get_entity_condition(target_entity) returning_condition = get_entity_condition(returning_entity) events = ( Event.objects.filter(team_id=team.pk) .filter(returning_condition | entity_condition) .add_person_id(team.pk) .annotate(event_date=F("timestamp")) ) trunc, fields = self._get_trunc_func("timestamp", period) if is_first_time_retention: filtered_events = events.filter(filter.properties_to_Q(team_id=team.pk)) first_date = ( filtered_events.filter(entity_condition).values("person_id").annotate(first_date=Min(trunc)).distinct() ) final_query = ( filtered_events.filter(returning_condition) .values_list("person_id", "event_date") .union(first_date.values_list("first_date", "person_id")) ) else: filtered_events = events.filter(filter.date_filter_Q).filter(filter.properties_to_Q(team_id=team.pk)) first_date = filtered_events.annotate(first_date=trunc).values("first_date", "person_id").distinct() final_query = filtered_events event_query, events_query_params = final_query.query.sql_with_params() reference_event_query, first_date_params = first_date.query.sql_with_params() final_query = """ SELECT {fields} COUNT(DISTINCT "events"."person_id"), array_agg(DISTINCT "events"."person_id") as people FROM ({event_query}) events LEFT JOIN ({reference_event_query}) first_event_date ON (events.person_id = first_event_date.person_id) WHERE event_date >= first_date GROUP BY date, first_date """.format( event_query=event_query, reference_event_query=reference_event_query, fields=fields ) start_params = (date_from, date_from) if period == "Month" or period == "Hour" else (filter.date_from,) with connection.cursor() as cursor: cursor.execute( final_query, start_params + events_query_params + first_date_params, ) data = namedtuplefetchall(cursor) scores: dict = {} for datum in data: key = round(datum.first_date, 1) if not scores.get(key, None): scores.update({key: {}}) for person in datum.people: if not scores[key].get(person, None): scores[key].update({person: 1}) else: scores[key][person] += 1 by_dates = {} for row in data: people = sorted(row.people, key=lambda p: scores[round(row.first_date, 1)][int(p)], reverse=True,) random_key = "".join( random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(10) ) cache_key = generate_cache_key("{}{}{}".format(random_key, str(round(row.first_date, 0)), str(team.pk))) cache.set( cache_key, people, 600, ) by_dates.update( { (int(row.first_date), int(row.date)): { "count": row.count, "people": people[0:100], "offset": 100, "next": cache_key if len(people) > 100 else None, } } ) return by_dates
def add_orderings(qs: QuerySet, orderings: str) -> QuerySet: orderings = orderings.split(',') qs = qs.order_by(*orderings) return qs
def foreignkey_autocomplete(self, request): """ Searches in the fields of the given related model and returns the result as a simple string to be used by the jQuery Autocomplete plugin """ query = request.GET.get('q', None) app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) search_fields = request.GET.get('search_fields', None) object_pk = request.GET.get('object_pk', None) try: to_string_function = self.related_string_functions[model_name] except KeyError: if six.PY3: to_string_function = lambda x: x.__str__() else: to_string_function = lambda x: x.__unicode__() if search_fields and app_label and model_name and (query or object_pk): def construct_search(field_name): # use different lookup methods depending on the notation if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name model = apps.get_model(app_label, model_name) queryset = model._default_manager.all() data = '' if query: for bit in query.split(): or_queries = [ models.Q( **{ construct_search(smart_str(field_name)): smart_str(bit) }) for field_name in search_fields.split(',') ] other_qs = QuerySet(model) other_qs.query.select_related = queryset.query.select_related other_qs = other_qs.filter(reduce(operator.or_, or_queries)) queryset = queryset & other_qs additional_filter = self.get_related_filter(model, request) if additional_filter: queryset = queryset.filter(additional_filter) if self.autocomplete_limit: queryset = queryset[:self.autocomplete_limit] data = ''.join([ six.u('%s|%s\n') % (to_string_function(f), f.pk) for f in queryset ]) elif object_pk: try: obj = queryset.get(pk=object_pk) except Exception: # FIXME: use stricter exception checking pass else: data = to_string_function(obj) return HttpResponse(data, content_type='text/plain') return HttpResponseNotFound()
def get_queryset(self): return QuerySet() # nocv
def log_entry_queryset(self): log_entries = getattr(self._log_target, "log_entries", None) if log_entries is None: return QuerySet(BaseLogEntry).none() # `BaseLogEntry` doesn't have `objects` as it's abstract return log_entries.all()
def filter_by_topic_name_via_message(query: QuerySet, topic_name: str) -> QuerySet: return query.filter(message__subject__iexact=topic_name)
def get_query_set(self): qs = self.root_query_set lookup_params = self.params.copy() # a dictionary of the query string for i in (ALL_VAR, ORDER_VAR, ORDER_TYPE_VAR, SEARCH_VAR, IS_POPUP_VAR): if i in lookup_params: del lookup_params[i] for key, value in lookup_params.items(): if not isinstance(key, str): # 'key' will be used as a keyword argument later, so Python # requires it to be a string. del lookup_params[key] lookup_params[smart_str(key)] = value # if key ends with __in, split parameter into separate values if key.endswith('__in'): lookup_params[key] = value.split(',') # Apply lookup parameters from the query string. try: qs = qs.filter(**lookup_params) # Naked except! Because we don't have any other way of validating "params". # They might be invalid if the keyword arguments are incorrect, or if the # values are not in the correct type, so we might get FieldError, ValueError, # ValicationError, or ? from a custom field that raises yet something else # when handed impossible data. except: raise IncorrectLookupParameters # Use select_related() if one of the list_display options is a field # with a relationship. if self.list_select_related: qs = qs.select_related() else: for field_name in self.list_display: try: f = self.lookup_opts.get_field(field_name) except models.FieldDoesNotExist: pass else: if isinstance(f.rel, models.ManyToOneRel): qs = qs.select_related() break # Set ordering. if self.order_field: qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field)) # Apply keyword searches. def construct_search(field_name): if field_name.startswith('^'): return "%s__istartswith" % field_name[1:] elif field_name.startswith('='): return "%s__iexact" % field_name[1:] elif field_name.startswith('@'): return "%s__search" % field_name[1:] else: return "%s__icontains" % field_name if self.search_fields and self.query: for bit in self.query.split(): or_queries = [models.Q(**{construct_search(field_name): bit}) for field_name in self.search_fields] other_qs = QuerySet(self.model) other_qs.dup_select_related(qs) other_qs = other_qs.filter(reduce(operator.or_, or_queries)) qs = qs & other_qs for field_name in self.search_fields: if '__' in field_name: qs = qs.distinct() break return qs
def _determine_query_params(self, filter: RetentionFilter, team: Team): period = filter.period is_first_time_retention = filter.retention_type == RETENTION_FIRST_TIME events: QuerySet = QuerySet() entity_condition, entity_condition_strigified = self.get_entity_condition( filter.target_entity, "first_event_date" ) returning_condition, returning_condition_stringified = self.get_entity_condition( filter.returning_entity, "events" ) events = Event.objects.filter(team_id=team.pk).add_person_id(team.pk).annotate(event_date=F("timestamp")) trunc, fields = self._get_trunc_func("timestamp", period) if is_first_time_retention: filtered_events = events.filter( properties_to_Q(filter.properties, team_id=team.pk, filter_test_accounts=filter.filter_test_accounts) ) first_date = ( filtered_events.filter(entity_condition) .values("person_id", "event", "action") .annotate(first_date=Min(trunc)) .filter(filter.custom_date_filter_Q("first_date")) .distinct() ) final_query = ( filtered_events.filter(filter.date_filter_Q) .filter(returning_condition) .values_list("person_id", "event_date", "event", "action") .union(first_date.values_list("first_date", "person_id", "event", "action")) ) else: filtered_events = events.filter(filter.date_filter_Q).filter( properties_to_Q(filter.properties, team_id=team.pk, filter_test_accounts=filter.filter_test_accounts) ) first_date = ( filtered_events.filter(entity_condition) .annotate(first_date=trunc) .values("first_date", "person_id", "event", "action") .distinct() ) final_query = ( filtered_events.filter(returning_condition) .values_list("person_id", "event_date", "event", "action") .union(first_date.values_list("first_date", "person_id", "event", "action")) ) start_params = ( (filter.date_from, filter.date_from) if period == "Month" or period == "Hour" else (filter.date_from,) ) event_query, events_query_params = final_query.query.sql_with_params() reference_event_query, first_date_params = first_date.query.sql_with_params() event_params = (filter.target_entity.id, filter.returning_entity.id, filter.target_entity.id) return ( { "event_query": event_query, "reference_event_query": reference_event_query, "fields": fields, "return_condition": returning_condition_stringified, "target_condition": entity_condition_strigified, }, start_params + events_query_params + first_date_params + event_params, )
class ProducerProductAddForm(forms.Form): error_css_class = 'error' required_css_class = 'required' product = forms.ModelChoiceField( required=False, queryset=QuerySet(model=Product), widget=forms.Select(attrs={ 'class': 'added_product', })) producer_price = forms.DecimalField( required=False, widget=forms.TextInput(attrs={ 'class': 'added-producer-price', 'size': '6' })) #qty_per_year = forms.DecimalField(required=False, widget=forms.TextInput(attrs={'class': # 'quantity-field', # 'size': '6'})) min = forms.CharField(required=False, widget=forms.TextInput( attrs={ 'readonly': 'true', 'class': 'read-only-input', 'size': '6', 'style': 'text-align: right', })) max = forms.CharField(required=False, widget=forms.TextInput( attrs={ 'readonly': 'true', 'class': 'read-only-input', 'size': '6', 'style': 'text-align: right', })) def __init__(self, products, *args, **kwargs): super(ProducerProductAddForm, self).__init__(*args, **kwargs) self.fields['product'].choices = [('', '----------')] + [ (p.id, p.name_with_method()) for p in products ] def clean(self): cleaned_data = self.cleaned_data if self.errors: return cleaned_data product = cleaned_data.get("product") if product: producer_price = cleaned_data.get("producer_price") min = product.producer_price_minimum max = product.producer_price_maximum msg = None if min: if producer_price < min: msg = u" ".join( ["Set price is less than minimum of", str(min)]) if max: if producer_price > max: msg = u" ".join( ["Set price is more than maximum of", str(max)]) if msg: #self._errors["producer_price"] = self.error_class([msg]) del cleaned_data["producer_price"] raise forms.ValidationError(msg) return cleaned_data
def get_query_set(self): """Returns a new QuerySet object. Subclasses can override this method to easily customize the behavior of the Manager. """ return QuerySet(self.model, using=self._db)
def only_deleted(self): if self.model: return QuerySet(self.model, using=self._db).filter(status=self.model.DELETED)
def deactivate_templates( self, request: HttpRequest, queryset: QuerySet ) -> HttpResponseRedirect: count = queryset.update(is_active=False) messages.success(request, _lazy("Deactivated %s templates" % count)) return HttpResponseRedirect(request.path)
def sorted(self, request, queryset, order_by): if not isinstance(queryset, type(QuerySet())): return queryset return queryset.order_by(order_by)
def test_get_backend(self): assert isinstance(get_backend(QuerySet()), DjangoBackend) assert isinstance(get_backend(Manager()), DjangoBackend)
def test_supports(self): assert DjangoBackend.supports(QuerySet()) assert DjangoBackend.supports(Manager()) assert not DjangoBackend.supports(Q())
def search(request): """ Searches in the fields of the given model and returns the result as a simple json list of objects to be used by the jQuery autocomplete plugin. Usage: In settings set: AUTOCOMPLETE_MODELS = { 'profiles.Profile': ['email', 'first_name', 'last_name',], } /autocomplete/?ct=47&q=a >>> [ {"value": 2, "label": "Jim Morris"}, {"value": 1, "label": "Ad Min"} ] """ content_type = request.GET.get('ct', None) if content_type is not None: model = ContentType.objects.get(pk=content_type).model_class() else: app_label = request.GET.get('app_label', None) model_name = request.GET.get('model_name', None) model = models.get_model(app_label, model_name) if not model: return HttpResponseNotFound() allowed_fields = get_searchable_fields(model) if not allowed_fields: raise ImproperlyConfigured('The model %s.%s being autocompleted it does not have allowed fields. ' 'If you are using the autocomplete search whithout an autocomplete widget then add the ' 'app_label.model and searchable fields to the AUTOCOMPLETE_MODELS dictionary in your setting.' % \ (model._meta.app_label, model._meta.module_name)) search_fields = request.GET.get('sf', None) if search_fields is None: search_fields = allowed_fields else: # Limit to search fields search_fields = search_fields.split(',') search_fields = filter( lambda f: get_field_lookup_pair(f)[0] in map( lambda f: get_field_lookup_pair(f)[0], allowed_fields), search_fields) query = request.GET.get('term', None) if search_fields and query: qs = model._default_manager.all() for bit in query.split(): or_queries = [ models. Q(**{construct_search(smart_str(field_name)): smart_str(bit)}) for field_name in search_fields ] other_qs = QuerySet(model) other_qs.query.select_related = qs.query.select_related other_qs = other_qs.filter(reduce(operator.or_, or_queries)) qs = qs & other_qs data = [{'label': o.__unicode__(), 'value': o.pk} for o in qs] return HttpResponse(json.dumps(data), content_type='application/javascript') return HttpResponseNotFound()
def send_test_emails( self, request: HttpRequest, queryset: QuerySet ) -> HttpResponseRedirect: selected = ",".join([str(s) for s in queryset.values_list("id", flat=True)]) url = "{}?templates={}".format(reverse("appmail:send_test_email"), selected) return HttpResponseRedirect(url)
def get_empty_query_set(self): return QuerySet(self.model, using=self._db).none()
def get_default_queryset(self, queryset: QuerySet) -> QuerySet: return queryset.filter( **{self.parameter_name: self.get_default_value()})
def queryset(self, request: Any, queryset: QuerySet) -> Optional[QuerySet]: if self.value() == "active_with_verified_team_membership": return queryset.filter(is_active=True, teams__verified=True)
def queryset_mock_factory(*args): """Creates a mock queryset with the given model instances""" queryset = QuerySet() queryset._result_cache = args return queryset
def _fetch_all(self): """ Fetch all uses the standard iterator but sorts the values on the way out, this maintains the lazy evaluation of querysets """ def locate_pk_column(query): pk_name = self.model._meta.pk.name if django.VERSION >= (1, 9): if "pk" in query.values_select: return query.values_select.index("pk") elif pk_name in query.values_select: return query.values_select.index(pk_name) else: for i, col in enumerate(query.select): if hasattr(col, "field") and col.field.name == pk_name: return i if self._result_cache is None: # Making this work efficiently is tricky. We never want to fetch more than self.ordered_pks # and the __getitem__ implementation sets this to a single item, so in that case we only want # to fetch one. So we do a few things here: # 1. We clone the Queryset as a normal Queryset, then we do a pk__in on the ordered_pks. # 2. We add the primary key if this was a values_list query without it being specified, otherwise # we can't match up the ordering # 3. We execute the clone, and then remove the additional PK column from the result set # There are various combinations to handle depending on whether it's a "flat" values_list or # whether or not we added the PK manually to the result set if django.VERSION >= (1, 9): clone = QuerySet(model=self.model, query=self.query, using=self._db) clone._iterable_class = self._iterable_class else: if isinstance(self, ValuesListQuerySet): clone = ValuesListQuerySet(model=self.model, query=self.query, using=self._db) clone._fields = self._fields clone.field_names = self.field_names clone.extra_names = self.extra_names clone.annotation_names = self.annotation_names clone.flat = self.flat else: clone = QuerySet(model=self.model, query=self.query, using=self._db) clone = clone.filter(pk__in=self.ordered_pks) pk_col = 0 pk_added = False if django.VERSION >= (1, 9): values_select = clone.query.values_select else: values_select = [x.field.name for x in clone.query.select] if values_select: pk_col = locate_pk_column(clone.query) if pk_col is None: # Manually add the PK to the result set clone = clone.values_list(*(["pk"] + values_select)) values_select = [x.field.name for x in clone.query.select] pk_col = 0 pk_added = True # Hit the database results = list(clone) ordered_results = [] pk_hash = {} if django.VERSION >= (1, 9): flat = self._iterable_class == FlatValuesListIterable else: # On Django 1.8 things are different flat = getattr(self, "flat", False) for x in results: if isinstance(x, models.Model): # standard query case pk_hash[x.pk] = x elif len(values_select) == 1: # Only PK case pk_hash[x if flat else x[pk_col]] = x else: # Multiple columns (either passed in, or as a result of the PK being added) if flat: pk_hash[x[pk_col]] = x[1] if pk_added else x else: pk_hash[x[pk_col]] = x[1:] if pk_added else x for pk in self.ordered_pks: obj = pk_hash.get(pk) if obj: ordered_results.append(obj) self._result_cache = ordered_results if self._prefetch_related_lookups and not self._prefetch_done: self._prefetch_related_objects()
def all_with_deleted(self): if self.model: return QuerySet(self.model, using=self._db).all()
def filter_by_exact_message_topic(query: QuerySet, message: Message) -> QuerySet: topic_name = message.topic_name() return query.filter(subject=topic_name)
def order_by(self, *fields): # Django only allows to order querysets by direct fields and # foreign-key chains. In order to bypass this behaviour and order # by EAV attributes, it is required to construct custom order-by # clause manually using Django's conditional expressions. # This will be slow, of course. order_clauses = [] query_clause = self config_cls = self.model._eav_config_cls for term in [t.split('__') for t in fields]: # Continue only for EAV attributes. if len(term) == 2 and term[0] == config_cls.eav_attr: # Retrieve Attribute over which the ordering is performed. try: attr = Attribute.objects.get(slug=term[1]) except ObjectDoesNotExist: raise ObjectDoesNotExist( 'Cannot find EAV attribute "{}"'.format(term[1])) field_name = 'value_%s' % attr.datatype pks_values = Value.objects.filter( # Retrieve pk-values pairs of the related values # (i.e. values for the specified attribute and # belonging to entities in the queryset). attribute__slug=attr.slug, entity_id__in=self ).order_by( # Order values by their value-field of # appriopriate attribute data-type. field_name ).values_list( # Retrieve only primary-keys of the entities # in the current queryset. 'entity_id', field_name) # Retrive ordered values from pk-value list. _, ordered_values = zip(*pks_values) # Add explicit ordering and turn # list of pairs into look-up table. val2ind = dict(zip(ordered_values, count())) # Finally, zip ordered pks with their grouped orderings. entities_pk = [(pk, val2ind[val]) for pk, val in pks_values] # Using ordered primary-keys, construct # CASE clause of the form: # # CASE # WHEN id = 2 THEN 1 # WHEN id = 5 THEN 2 # WHEN id = 9 THEN 2 # WHEN id = 4 THEN 3 # END # when_clauses = [When(id=pk, then=i) for pk, i in entities_pk] order_clause = Case(*when_clauses, output_field=IntegerField()) clause_name = '__'.join(term) # Use when-clause to construct # custom order-by clause. query_clause = query_clause.annotate( **{clause_name: order_clause}) order_clauses.append(clause_name) elif len(term) >= 2 and term[0] == config_cls.eav_attr: raise NotSupportedError( 'EAV does not support ordering through ' 'foreign-key chains') else: order_clauses.append(term[0]) return QuerySet.order_by(query_clause, *order_clauses)
def mark_finished(self, _, queryset: QuerySet): queryset.update(status=self.model.FINISHED) queryset.filter(finished=None).update(finished=timezone.now())
def get_query_set(self): if hasattr(self.model, 'CustomQuerySet'): return self.model.CustomQuerySet(self.model) else: # If they haven't defined a CustomQuerySet internal class, then return a normal QuerySet to keep things rolling. return QuerySet(self.model)
def get_article1_and_groups_if_many_articles(self, articles: QuerySet): article1 = articles[0] articles = articles.exclude(author=article1.author, name=article1.name) groups = self.get_groups(articles) return article1, groups
def filter_queryset(self, request: Request, queryset: QuerySet, view) -> QuerySet: return queryset.filter(**{self.owner_key: request.user})
def get_query_set(self): q = CustomQuery(self.model, CustomWhere) return QuerySet(self.model, q)