<commit_before>from django.db.models.query_utils import Q

from rest_framework import generics
from rest_framework.permissions import IsAuthenticated

from . import get_project_model
from .models import (
    ProjectTheme, ProjectPhase, ProjectBudgetLine, ProjectDetailField)
from .serializers import (
    ManageProjectSerializer, ProjectPreviewSerializer, ProjectThemeSerializer,
    ProjectSerializer, ProjectPhaseSerializer, ProjectBudgetLineSerializer,
    ProjectDetailFieldSerializer)
from .permissions import IsProjectOwner, IsProjectOwnerOrReadOnly

PROJECT_MODEL = get_project_model()


class ProjectPreviewList(generics.ListAPIView):
    model = PROJECT_MODEL
    serializer_class = ProjectPreviewSerializer
    paginate_by = 8
    paginate_by_param = 'page_size'
    max_paginate_by = 100

    def get_queryset(self):
        qs = PROJECT_MODEL.objects.filter(status__viewable=True)

        # For some reason the query fails if the country filter is defined before this.
        ordering = self.request.QUERY_PARAMS.get('ordering', None)

        if ordering == 'newest':
            qs = qs.order_by('-created')
        elif ordering == 'title':
            qs = qs.order_by('title')
        elif ordering == 'deadline':
            qs = qs.order_by('projectcampaign__deadline')

        country = self.request.QUERY_PARAMS.get('country', None)
        if country:
            qs = qs.filter(projectplan__country=country)

        theme = self.request.QUERY_PARAMS.get('theme', None)
        if theme:
            qs = qs.filter(projectplan__theme_id=theme)

        status = self.request.QUERY_PARAMS.get('status', None)
        if status:
            qs = qs.filter(Q(status_id=status))

        text = self.request.QUERY_PARAMS.get('text', None)
        if text:
            qs = qs.filter(Q(title__icontains=text) |
                           Q(pitch__icontains=text) |
                           Q(description__icontains=text))
<commit_msg>Fix project search filter<commit_after>from django.db.models.query_utils import Q

from rest_framework import generics
from rest_framework.permissions import IsAuthenticated

from . import get_project_model
from .models import (
    ProjectTheme, ProjectPhase, ProjectBudgetLine, ProjectDetailField)
from .serializers import (
    ManageProjectSerializer, ProjectPreviewSerializer, ProjectThemeSerializer,
    ProjectSerializer, ProjectPhaseSerializer, ProjectBudgetLineSerializer,
    ProjectDetailFieldSerializer)
from .permissions import IsProjectOwner, IsProjectOwnerOrReadOnly

PROJECT_MODEL = get_project_model()


class ProjectPreviewList(generics.ListAPIView):
    model = PROJECT_MODEL
    serializer_class = ProjectPreviewSerializer
    paginate_by = 8
    paginate_by_param = 'page_size'
    max_paginate_by = 100

    def get_queryset(self):
        qs = PROJECT_MODEL.objects.filter(status__viewable=True)

        # For some reason the query fails if the country filter is defined before this.
        ordering = self.request.QUERY_PARAMS.get('ordering', None)

        if ordering == 'newest':
            qs = qs.order_by('-created')
        elif ordering == 'title':
            qs = qs.order_by('title')
        elif ordering == 'deadline':
            qs = qs.order_by('projectcampaign__deadline')

        country = self.request.QUERY_PARAMS.get('country', None)
        if country:
            qs = qs.filter(country=country)

        theme = self.request.QUERY_PARAMS.get('theme', None)
        if theme:
            qs = qs.filter(theme_id=theme)

        status = self.request.QUERY_PARAMS.get('status', None)
        if status:
            qs = qs.filter(Q(status_id=status))

        text = self.request.QUERY_PARAMS.get('text', None)
        if text:
            qs = qs.filter(Q(title__icontains=text) |
                           Q(pitch__icontains=text) |
                           Q(description__icontains=text))

{'fraction_non_alphanumeric': 0.08452868852459017, 'fraction_numerical': 0.0020491803278688526, 'mean_word_length': 3.034090909090909, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6246580', 'n_tokens_mistral': 1150, 'n_tokens_neox': 1101, 'n_words': 237}