Exemplo n.º 1
0
 def test_apply_search(self):
     qs = User.objects.all()
     try:
         qs = apply_search(qs, 'groups = None')
         qs.count()
     except Exception as e:
         self.fail(e)
Exemplo n.º 2
0
    def get_queryset(self):
        """Prefetch related tables to speed up queries. Also order result by get-parameters."""
        qs = super().get_queryset()
        if self.search_urlparameter and self.search_urlparameter in self.request.GET:
            searchquery = self.request.GET[self.search_urlparameter].strip()
            if searchquery.startswith("="):
                try:
                    qs = apply_search(qs, searchquery[1:])
                except DjangoQLError as e:
                    messages.error(
                        self.request,
                        _("Bad filter string '%s': '%s'") % (searchquery, e),
                    )

            else:
                qs = self.model.objects.filter(
                    queryset_from_fields.get_field_queryset(
                        [
                            *self.model._meta.fields,
                            *self.model._meta.many_to_many
                        ],
                        searchquery,
                    ))

        selectedobjects = self.request.GET.getlist(self.objectids_urlparameter)
        if selectedobjects and "all" not in selectedobjects:
            qs &= super().get_queryset().filter(pk__in=selectedobjects)

        qs = order_queryset_by_urlparameter(
            qs, self.request.GET.get(self.orderingurlparameter))
        return qs
Exemplo n.º 3
0
    def get_queryset(self):
        queryset = self.model.objects.select_related("repository")
        queryterm = self.request.GET.get("q", None)

        if queryterm:
            SIMPLE_QUERY_PATTERN = r"^[\w-]+$"
            URL_QUERY_PATTERN = r"^https?[:][/][/]\S+$"

            if re.match(SIMPLE_QUERY_PATTERN, queryterm):
                queryset = queryset.filter(
                    Q(name__icontains=queryterm)
                    | Q(owner__icontains=queryterm)
                    | Q(status__icontains=queryterm)
                    | Q(impact__icontains=queryterm))

            elif re.match(URL_QUERY_PATTERN, queryterm):
                queryset = queryset.filter(
                    Q(docs_url__icontains=queryterm)
                    | Q(environment__health_check_url__icontains=queryterm)
                    | Q(environment__service_urls__icontains=queryterm))

            else:
                try:
                    queryset = apply_search(queryset, queryterm,
                                            models.ServiceQLSchema)
                except DjangoQLError:
                    log.exception("services.query_error", queryterm=queryterm)
                    return self.model.objects.none()

        return queryset.order_by("name")
Exemplo n.º 4
0
def parsequeryexpression(basequeryset, expression):
    if not expression:
        return QueryValue(basequeryset.all(), expression)
    try:
        return QueryValue(apply_search(basequeryset, expression), expression)
    except DjangoQLError as e:
        raise ValidationError(str(e))
Exemplo n.º 5
0
def completion_demo(request):
    q = request.GET.get('q', '')
    error = ''
    query = User.objects.all().order_by('username')
    if q:
        try:
            query = apply_search(query, q, schema=UserQLSchema)
        except DjangoQLError as e:
            query = query.none()
            error = str(e)
    return render(request, 'completion_demo.html', context={
        'q': q,
        'error': error,
        'search_results': query,
        'introspections': json.dumps(UserQLSchema(query.model).as_dict()),
    })
Exemplo n.º 6
0
def entryexitlog_search(request):
    q = request.GET.get('q', '')
    error = ''
    query = EntryExitLogs.objects.filter(timeofentry__contains=today).order_by('-timeofentry')
    if q:
        try:
            query = apply_search(query, q, schema=EntryExitLogsQLSchema)
        except DjangoQLError as e:
            query = query.none()
            error = str(e)
    return render_to_response('opac/search/entryexitlogs_searchql.html', {
        'q': q,
        'error': error,
        'search_results': query,
        'introspections': json.dumps(EntryExitLogsQLSchema(query.model).as_dict()),
    })
Exemplo n.º 7
0
def catalog_search(request):
    q = request.GET.get('q', '')
    error = ''
    query = Biblio.objects.all().order_by('title')
    if q:
        try:
            query = apply_search(query, q, schema=BiblioQLSchema)
        except DjangoQLError as e:
            query = query.none()
            error = str(e)
    return render_to_response('opac/search/catalog_search.html', {
        'q': q,
        'error': error,
        'search_results': query,
        'introspections': json.dumps(BiblioQLSchema(query.model).as_dict()),
    })
Exemplo n.º 8
0
 def get_search_results(self, request, queryset, search_term):
     use_distinct = False
     if not search_term:
         return queryset, use_distinct
     try:
         return (
             apply_search(queryset, search_term, self.djangoql_schema),
             use_distinct,
         )
     except (DjangoQLError, ValueError, FieldError) as e:
         msg = text_type(e)
     except ValidationError as e:
         msg = e.messages[0]
     queryset = queryset.none()
     messages.add_message(request, messages.WARNING, msg)
     return queryset, use_distinct
Exemplo n.º 9
0
def user_search_query(request):
    q = request.GET.get('q', '')
    error = ''
    query = User.objects.all().order_by('email')
    if q:
        try:
            query = apply_search(query, q, schema=UserQLSchema)
        except DjangoQLError as e:
            query = query.none()
            error = str(e)
    return render_to_response(
        'user_search_query.html', {
            'q': q,
            'error': error,
            'search_results': query,
            'introspections': json.dumps(UserQLSchema(query.model).as_dict()),
        })
Exemplo n.º 10
0
def completion_demo(request):
    q = request.GET.get('q', '')
    error = ''
    query = User.objects.all().order_by('username')
    if q:
        try:
            query = apply_search(query, q, schema=UserQLSchema)
        except DjangoQLError as e:
            query = query.none()
            error = str(e)
    # Authenticate user to be able to work with the saved queries
    user = authenticate(username='******', password='******')
    login(request, user)
    return render_to_response(
        'completion_demo.html', {
            'q': q,
            'error': error,
            'search_results': query,
            'introspections': json.dumps(UserQLSchema(query.model).as_dict()),
        })
Exemplo n.º 11
0
def completion_demo(request):
    q = request.GET.get('q', '')
    error = ''
    query = User.objects.all().order_by('username')
    if q:
        try:
            query = apply_search(query, q, schema=UserQLSchema)
        except DjangoQLError as e:
            query = query.none()
            error = str(e)
    # You may want to use SuggestionsAPISerializer and an additional API
    # endpoint (see in djangoql.views) for asynchronous suggestions loading
    introspections = DjangoQLSchemaSerializer().serialize(
        UserQLSchema(query.model),
    )
    return render(request, 'completion_demo.html', context={
        'q': q,
        'error': error,
        'search_results': query,
        'introspections': json.dumps(introspections),
    })
Exemplo n.º 12
0
 def test_empty_datetime(self):
     qs = apply_search(User.objects.all(), 'last_login = None')
     where_clause = str(qs.query).split('WHERE')[1].strip()
     self.assertEqual('"auth_user"."last_login" IS NULL', where_clause)