Example #1
0
    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
Example #2
0
def test_user_saved_professional_delete(
    user: User,
    client_with_token: Client,
    user_saved_professionals: QuerySet,
):
    """Should be able to delete a user saved professionals."""
    obj = user_saved_professionals.filter(user=user).first()
    response = client_with_token.delete(
        reverse("user-saved-professionals-detail", args=[obj.pk]))
    assert response.status_code == 204
    assert user_saved_professionals.filter(pk=obj.pk).count() == 0
Example #3
0
def test_user_professional_closed_periods_delete(
    user: User,
    client_with_token: Client,
    professional_closed_periods: QuerySet,
):
    """Should be able to delete a user professional closed period."""
    obj = professional_closed_periods.filter(professional__user=user).first()
    response = client_with_token.delete(
        reverse("user-professional-closed-periods-detail", args=[obj.pk]))
    assert response.status_code == 204
    assert professional_closed_periods.filter(pk=obj.pk).count() == 0
Example #4
0
def test_sent_unread_message_delete(
    user: User,
    client_with_token: Client,
    messages: QuerySet,
):
    """Should be able to delete a unread sent message."""
    obj = messages.filter(sender=user).first()
    response = client_with_token.delete(
        reverse("messages-sent-detail", args=[obj.pk]))
    assert response.status_code == 204
    assert messages.filter(pk=obj.pk).count() == 0
Example #5
0
def test_user_service_schedule_delete(
    user: User,
    client_with_token: Client,
    service_schedules: QuerySet,
):
    """Should be able to delete a user service schedule."""
    obj = service_schedules.filter(service__professional__user=user).first()
    response = client_with_token.delete(
        reverse("user-service-schedule-detail", args=[obj.pk]))
    assert response.status_code == 204
    assert service_schedules.filter(pk=obj.pk).count() == 0
Example #6
0
def test_user_languages_delete(
    user: User,
    client_with_token: Client,
    user_languages: QuerySet,
):
    """Should be able to update a user language."""
    lang = user_languages.filter(user=user).first()
    response = client_with_token.delete(
        reverse("user-languages-detail", args=[lang.pk]))
    assert response.status_code == 204
    assert user_languages.filter(user=user).count() == 0
Example #7
0
    def queryset(self, request: Any, queryset: QuerySet) -> Optional[QuerySet]:
        today = datetime.date.today()
        year, week, _ = today.isocalendar()

        if self.value() == "thisWeek":
            return queryset.filter(store_date__week=week,
                                   store_date__year=year)

        if self.value() == "nextWeek":
            return queryset.filter(store_date__week=week + 1,
                                   store_date__year=year)
Example #8
0
    def apply_filters(self, bands: QuerySet, filters: QueryDict) -> QuerySet:
        city_id = filters.get('city')
        style_id = filters.get('style')

        if city_id:
            bands = bands.filter(city_id=city_id).all()

        if style_id:
            bands = bands.filter(styles__in=[style_id]).all()

        return bands
Example #9
0
def test_user_settings_delete(
    user: User,
    client_with_token: Client,
    user_settings: QuerySet,
):
    """Should be able to delete a user settings."""
    obj = user_settings.filter(user=user).first()
    response = client_with_token.delete(
        reverse("user-settings-detail", args=[obj.pk]))
    assert response.status_code == 204
    assert user_settings.filter(user=user).count() == 0
Example #10
0
def test_user_location_delete(
    user: User,
    client_with_token: Client,
    user_locations: QuerySet,
):
    """Should be able to delete a user language."""
    obj = user_locations.filter(user=user).first()
    response = client_with_token.delete(
        reverse("user-locations-detail", args=[obj.pk]))
    assert response.status_code == 204
    assert user_locations.filter(user=user, pk=obj.pk).count() == 0
Example #11
0
def test_user_review_comment_delete(
    user: User,
    client_with_token: Client,
    review_comments: QuerySet,
):
    """Should be able to delete a user review."""
    obj = review_comments.filter(user=user).first()
    response = client_with_token.delete(
        reverse("user-review-comments-detail", args=[obj.pk]))
    assert response.status_code == 204
    assert review_comments.filter(pk=obj.pk).count() == 0
Example #12
0
def test_user_service_photos_delete(
    user: User,
    client_with_token: Client,
    services: QuerySet,
):
    """Should be able to delete a user service photo."""
    obj = services.filter(professional__user=user).first().photos.first()
    response = client_with_token.delete(
        reverse("user-service-photos-detail", args=[obj.pk]))
    assert response.status_code == 204

    assert not services.filter(professional__user=user).first().photos.count()
def test_validate_parent(admin: User, messages: QuerySet):
    """Should validate the message parent message."""
    messages = messages.filter(sender=admin)
    message = messages[0]
    message.parent = messages[1]
    with pytest.raises(ValidationError):
        validate_message_parent(message)
    message.is_read = True
    with pytest.raises(ValidationError):
        validate_message_parent(message)
    message.parent = messages.filter(recipient=admin).first()
    validate_message_parent(message)
Example #14
0
def test_user_order_reminders_delete(
    user: User,
    client_with_token: Client,
    order_reminders: QuerySet,
):
    """Should be able to delete a user professional tag."""
    obj: OrderReminder = order_reminders.filter(recipient=user).first()
    response = client_with_token.delete(
        reverse("user-order-reminders-detail", args=[obj.pk]))
    assert response.status_code == 204

    assert not order_reminders.filter(pk=obj.pk).count()
Example #15
0
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)
Example #16
0
    def apply_filters(self, musicians: QuerySet,
                      filters: QueryDict) -> Optional[QuerySet]:
        '''Extract all filters'''
        city_id = filters.get('city')
        instrument_id = filters.get('instrument')
        '''Apply filters'''
        if city_id:
            musicians = musicians.filter(city_id=city_id).all()
        if instrument_id:
            instrument = Instrument.objects.filter(id=instrument_id).first()
            musicians = musicians.filter(instruments__in=[instrument]).all()

        return musicians
Example #17
0
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
Example #18
0
 def filter_queryset(self, request, queryset: QuerySet, view):
     try:
         if 'user__id' in request.query_params.keys():
             queryset = queryset.filter(
                 author__user__id=request.query_params['user__id'])
         if 'user__email' in request.query_params.keys():
             queryset = queryset.filter(
                 author__user__email=request.query_params['user__email'])
         if 'user__username' in request.query_params.keys():
             queryset = queryset.filter(author__user__username=request.
                                        query_params['user__username'])
         if 'author__id' in request.query_params.keys():
             queryset = queryset.filter(
                 author__id=request.query_params['author__id'])
         if 'author__ORCID' in request.query_params.keys():
             queryset = queryset.filter(
                 author__ORCID=request.query_params['author__ORCID'])
         if 'article__id' in request.query_params.keys():
             queryset = queryset.filter(
                 article__id=request.query_params['article__id'])
         if 'article__title' in request.query_params.keys():
             queryset = queryset.filter(
                 article__title=request.query_params['article__title'])
         if 'is_email_author' in request.query_params.keys():
             queryset = queryset.filter(
                 is_email_author=request.query_params['is_email_author'])
     except Exception as e:
         queryset = queryset.none()
     return queryset
Example #19
0
def test_user_service_prices_delete(
    user: User,
    client_with_token: Client,
    services: QuerySet,
):
    """Should be able to delete a user professional tag."""
    # pylint: disable=unused-argument
    obj = services.filter(professional__user=user).first().price
    response = client_with_token.delete(
        reverse("user-service-prices-detail", args=[obj.pk]))
    assert response.status_code == 204

    with pytest.raises(ObjectDoesNotExist):
        _ = services.filter(professional__user=user).first().price
    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
Example #21
0
def test_professionals_list(
    client_with_token: Client,
    professionals: QuerySet,
    user_languages: QuerySet,
):
    """Should return a professionals list."""
    obj = professionals.filter().first()
    response = client_with_token.get(reverse("professionals-list"))
    data = response.json()
    assert response.status_code == 200
    assert data["count"] == 4
    entry = data["results"][0]
    assert entry["name"] == obj.name
    lang = user_languages.filter(user__pk=entry["user"]["id"]).first()
    assert entry["user"]["languages"][0]["language"] == lang.language
Example #22
0
def collect_and_remove(objs, using):
        collector = Collector(using=using)
        with transaction.atomic():
            now = timezone.now()
            collector.collect(objs)
            for qs in collector.fast_deletes:
                if issubclass(qs.model, SoftDeletableModel):
                    # only touch related objects which aren't already softdeleted
                    # to prevent clobbering the removed date on already removed objects
                    qs.filter(removed__isnull=True).update(removed=now)
            for model, instances in collector.data.items():
                if issubclass(model, SoftDeletableModel):
                    pks = [obj.pk for obj in instances]
                    qs = QuerySet(model=model, using=collector.using)
                    qs.filter(pk__in=pks).filter(removed__isnull=True).update(removed=now)
Example #23
0
    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
Example #24
0
    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
Example #25
0
    def filter(self, *args, **kwargs):
        '''
        allows to query over all MapObj's, incliding child specfic fields.
        The queryset returned has only MapObj's in it, yet when each one is
        pulled, __getitem__ converts back to original object, creating a
        pseudo heterogenous queryset
        ex:
            can now do
                MapObj.objects.filter(abbreviation="MAP")
                MapObj.objects.filter(permit_type="Greek Row")
            yet these attributes are not apart of MapObj
        '''

        # process the query
        if len(args):
            query = args[0]
        else:
            query = Q(**kwargs)
        new_query   = Q(pk="~~~ no results ~~~")
        if query.connector != "OR":
            new_query = query
        else:
            for c in query.children:
                # shoudl probably have some recurrsion here
                # will fail with complex / nested queries
                new_query = new_query | self.leaf_filter(Q(c))

        # return a QuerySet of MapObj's
        return QuerySet.filter(self, new_query)
Example #26
0
 def search_filter(self, queryset:QuerySet, name, value):
     if value == "":
         return queryset.none()
     # qval = Q(wylie__istartswith=value) | Q(sa2ru1__istartswith=value) | Q(sa2en1__istartswith=value) | Q(sa2ru2__istartswith=value) | Q(sa2en2__istartswith=value) | Q(sa2ru3__istartswith=value) | Q(sa2en3__istartswith=value) | Q(tibetan__istartswith=value) | Q(sanscrit__istartswith=value)
     qval = Q(wylie__iexact=value) | Q(sa2ru1__iexact=value) | Q(sa2en1__iexact=value) | Q(sa2ru2__iexact=value) | Q(sa2en2__iexact=value) | Q(sa2ru3__iexact=value) | Q(sa2en3__iexact=value) | Q(tibetan__iexact=value) | Q(sanscrit__iexact=value)
         
     return queryset.filter(qval)
Example #27
0
    def apply_filters(cls, args: dict, qs: QuerySet) -> QuerySet:
        pk = args.get('pk', None)

        if pk:
            qs = qs.filter(pk=pk)

        return qs
Example #28
0
    def search_filter(self, queryset:QuerySet, name, value):
        if value == "":
            return queryset.none()
#        qval = Q(meaning__icontains=value)
        # qval = Q(meaning__istartswith=value)
        qval = Q(meaning__iexact=value)
        return queryset.filter(qval)
Example #29
0
    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
Example #30
0
    def filter(self, *args, **kwargs):
        '''
        allows to query over all MapObj's, incliding child specfic fields.
        The queryset returned has only MapObj's in it, yet when each one is
        pulled, __getitem__ converts back to original object, creating a
        pseudo heterogenous queryset
        ex:
            can now do
                MapObj.objects.filter(abbreviation="MAP")
                MapObj.objects.filter(permit_type="Greek Row")
            yet these attributes are not apart of MapObj
        '''

        # process the query
        if len(args):
            query = args[0]
        else:
            query = Q(**kwargs)
        new_query = Q(pk="~~~ no results ~~~")
        if query.connector != "OR":
            new_query = query
        else:
            for c in query.children:
                # shoudl probably have some recurrsion here
                # will fail with complex / nested queries
                new_query = new_query | self.leaf_filter(Q(c))

        # return a QuerySet of MapObj's
        return QuerySet.filter(self, new_query)
Example #31
0
def paginate_request(filters: Optional[dict], list_to_paginate: QuerySet, page_number: str = '1'):
    if filters is not None:
        list_to_paginate = list_to_paginate.filter(**filters).distinct()

    paginator = Paginator(list_to_paginate, 6)
    page = paginator.get_page(page_number)
    return page, paginator
Example #32
0
    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
Example #33
0
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
    })
Example #34
0
 def find_by_city(
     *,
     city_id: int,
     queryset: QuerySet,
 ) -> QuerySet:
     """Find postal codes by the city."""
     return queryset.filter(Q(city__pk=city_id) | Q(city__isnull=True))
Example #35
0
 def get_filtered_queryset(self, queryset: QuerySet) -> QuerySet:
     try:
         return queryset.filter(**self.used_parameters)
     except (ValueError, ValidationError) as e:
         # Fields may raise a ValueError or ValidationError when converting
         # the parameters to the correct type.
         raise IncorrectLookupParameters(e)
Example #36
0
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 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
Example #38
0
    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.model_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
Example #39
0
    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()
Example #40
0
File: admin.py Project: soitun/dg
    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()
Example #41
0
    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 test_validate_message_recipient(admin: User, messages: QuerySet):
    """Should mark the message read."""
    message = messages.filter(sender=admin).first()
    validate_message_recipient(message)
    message.recipient = admin
    with pytest.raises(ValidationError):
        validate_message_recipient(message)
Example #43
0
def test_user_professional_certificate_create(
    user: User,
    client_with_token: Client,
    professionals: QuerySet,
):
    """Should be able to create a user professional certificates object."""
    obj = professionals.filter(user=user).first()
    response = client_with_token.post(
        reverse("user-professional-certificates-list"),
        {
            "name":
                "test name",
            "organization":
                "test organization",
            "professional":
                obj.pk,
            "photo":
                ("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKBAMAA"
                 "AB/HNKOAAAAGFBMVEXMzMyWlpajo6O3t7fFxcWcnJyxsbG+vr50Rsl6AAAAC"
                 "XBIWXMAAA7EAAAOxAGVKw4bAAAAJklEQVQImWNgwADKDAwsAQyuDAzMAgyMb"
                 "OYMAgyuLApAUhnMRgIANvcCBwsFJwYAAAAASUVORK5CYII=")
        },
    )
    certificate = obj.certificates.first()
    photo_path = f"certificates/{slugify(obj)}"
    assert response.status_code == 201
    assert certificate.name == "test name"
    assert photo_path in certificate.photo.name
    assert certificate.photo_thumbnail is not None
    certificate.photo.delete()
Example #44
0
    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
Example #45
0
 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()
Example #46
0
    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()
Example #47
0
    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.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()
Example #49
0
    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
Example #50
0
 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()
Example #51
0
    def handle(self, **options):

        limit = options.get('limit',None)
        loop_sleep = options.get('loop_sleep', 0.001)
        info = options.get('info', False)

        if info:
            qs = QuerySet(model=Page)
            nonmigrated = qs.filter(layout_migrated=False).count()
            total = qs.count()
            l = len(str(total))
            print str(total-nonmigrated).rjust(l), 'pages allready migrated'
            print str(nonmigrated).rjust(l), 'pages NOT YET migrated'
            print str(total).rjust(l), "pages TOTAL"
            return

        migrate(limit, loop_sleep)
Example #52
0
File: common.py Project: digicyc/q
def admin_keyword_search(model, fields, keywords):
    """
    """
    if not keywords:
        return []

    keywords = keywords.split(" ")
    qs = QuerySet(model)
    for keyword in keywords:
        or_queries = [ Q(**{'%s__icontains' % field: keyword}) for field in fields ]
        other_qs = QuerySet(model)
        if qs.select_related:
            other_qs = other_qs.select_related()
        other_qs = other_qs.filter(reduce(operator.or_, or_queries))
        qs = qs & other_qs

    return qs
def get_json(request, token):
    """Return matching results as JSON"""
    result = []
    searchtext = request.GET['q']
    if len(searchtext) >= 1:
        pickled = _simple_autocomplete_queryset_cache.get(token, None)
        if pickled is not None:
            app_label, model_name, query = pickle.loads(pickled)
            model = get_model(app_label, model_name)
            queryset = QuerySet(model=model, query=query)
            fieldname = get_search_fieldname(model)
            di = {'%s__istartswith' % fieldname: searchtext}
            items = queryset.filter(**di).order_by(fieldname)[:10]
            for item in items:
                result.append((item.id, getattr(item, fieldname)))
        else:
            result = 'CACHE_MISS'
    return HttpResponse(simplejson.dumps(result))
Example #54
0
    def get_searched_queryset(self, qs):
        model = self.model
        term = self.GET["term"]

        try:
            search_fields = model.autocomplete_search_fields()
        except AttributeError:
            search_fields = AUTOCOMPLETE_SEARCH_FIELDS[model._meta.app_label][model._meta.module_name]
        except KeyError:
            search_fields = ()

        for word in term.split():
            search = [models.Q(**{smart_str(item): smart_str(word)}) for item in 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
Example #55
0
    def search(self, request):
        query = request.GET.get("q", None)
        id = request.GET.get("id", None)
        app_label = request.GET.get("app_label", None)
        model_name = request.GET.get("model_name", None)
        db_field_name = request.GET.get("db_field_name", None)
        search_fields = self.related_search_fields[db_field_name]

        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)
            try:
                if id:

                    qs = model.objects.filter(id=query)
                else:
                    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.dup_select_related(qs)
                        other_qs = other_qs.filter(reduce(operator.or_, or_queries))
                        qs = qs & other_qs
            except:
                qs = ()
            data = "".join([u"%s|%s\n" % (self.to_string_function(db_field_name)(f), f.pk) for f in qs])
            return HttpResponse(data)
        return HttpResponseNotFound()
Example #56
0
File: views.py Project: jpic/jlibs
def search(request):
    query = request.GET.get('q', None)
    app_label = request.GET.get('app_label', None)
    model_name = request.GET.get('model_name', None)

    if 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)

        if not hasattr(model, 'can_search') \
            or not model.can_search(request.user):
            return HttpResponseForbidden()

        if hasattr(model, 'get_search_fields'):
            search_fields = model.get_search_fields()

        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 get_searched_queryset(self, qs):
        model = self.model
        term = self.GET["term"]

        try:
            term = model.autocomplete_term_adjust(term)
        except AttributeError:
            pass

        search_fields = get_autocomplete_search_fields(self.model)
        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 get_json(request, token):
    """Return matching results as JSON"""
    result = []
    searchtext = request.GET['q']
    if len(searchtext) >= 3:
        pickled = _simple_autocomplete_queryset_cache.get(token, None)
        if pickled is not None:
            app_label, model_name, query = pickle.loads(pickled)
            model = apps.get_model(app_label, model_name)
            queryset = QuerySet(model=model, query=query)
            fieldname = get_search_fieldname(model)
            di = {'%s__istartswith' % fieldname: searchtext}
            app_label_model = '%s.%s' % (app_label, model_name)
            max_items = get_setting(app_label_model, 'max_items', 10)
            items = queryset.filter(**di).order_by(fieldname)[:max_items]

            # Check for duplicate strings
            counts = {}
            for item in items:
                key = unicode(item)
                counts.setdefault(key, 0)
                counts[key] += 1

            # Assemble result set
            for item in items:
                key = value = unicode(item)
                value = getattr(item, fieldname)
                if counts[key] > 1:
                    func = get_setting(
                        app_label_model,
                        'duplicate_format_function',
                        lambda obj, model, content_type: content_type.name
                    )
                    content_type = ContentType.objects.get_for_model(model)
                    value = '%s (%s)' % (value, func(item, model, content_type))
                result.append((item.id, value))

        else:
            result = 'CACHE_MISS'

    return HttpResponse(json.dumps(result))
Example #59
0
    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)
        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:

            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" % (to_string_function(f), f.pk) for f in qs])
            return HttpResponse(data)
        return HttpResponseNotFound()