<commit_before>from django.utils.translation import ugettext_lazy as _

import django_filters

from projects import constants
from projects.models import Project

ANY_REPO = (
    ('', _('Any')),
)

REPO_CHOICES = ANY_REPO + constants.REPO_CHOICES


class ProjectFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(label=_("Name"), name='name',
                                     lookup_type='icontains')
    slug = django_filters.CharFilter(label=_("Slug"), name='slug',
                                     lookup_type='icontains')
    pub_date = django_filters.DateRangeFilter(label=_("Created Date"),
                                              name="pub_date")
    repo = django_filters.CharFilter(label=_("Repository URL"), name='repo',
                                     lookup_type='icontains')
    repo_type = django_filters.ChoiceFilter(
        label=_("Repository Type"),
        name='repo',
        lookup_type='icontains',
        choices=REPO_CHOICES,
    )

    class Meta:
        model = Project
        fields = ['name', 'slug', 'pub_date', 'repo', 'repo_type']
<commit_msg>Handle sorting of slug based on sanity. This should really be easier.<commit_after>from django.utils.translation import ugettext_lazy as _

import django_filters

from projects import constants
from projects.models import Project

ANY_REPO = (
    ('', _('Any')),
)

REPO_CHOICES = ANY_REPO + constants.REPO_CHOICES

def sort_slug(queryset, query):
  queryset = queryset.filter(slug__icontains=query)
  ret = []
  ret.extend([q.pk for q in queryset if q.slug == query])
  ret.extend([q.pk for q in queryset if q.slug.startswith(query) and q.pk not in ret])
  ret.extend([q.pk for q in queryset if q.slug.endswith(query) and q.pk not in ret])
  ret.extend([q.pk for q in queryset if q.pk not in ret])

  # Create a QS preserving ordering
  # http://blog.mathieu-leplatre.info/django-create-a-queryset-from-a-list-preserving-order.html
  clauses = ' '.join(['WHEN id=%s THEN %s' % (pk, i) for i, pk in enumerate(ret)])
  ordering = 'CASE %s END' % clauses
  ret_queryset = Project.objects.filter(pk__in=ret).extra(
             select={'ordering': ordering}, order_by=('ordering',))
  return ret_queryset

class ProjectFilter(django_filters.FilterSet):
    name = django_filters.CharFilter(label=_("Name"), name='name',
                                     lookup_type='icontains')
    slug = django_filters.CharFilter(label=_("Slug"), name='slug', action=sort_slug)
    pub_date = django_filters.DateRangeFilter(label=_("Created Date"),
                                              name="pub_date")
    repo = django_filters.CharFilter(label=_("Repository URL"), name='repo',
                                     lookup_type='icontains')
    repo_type = django_filters.ChoiceFilter(
        label=_("Repository Type"),
        name='repo',
        lookup_type='icontains',
        choices=REPO_CHOICES,
    )

    class Meta:
        model = Project
        fields = ['name', 'slug', 'pub_date', 'repo', 'repo_type']

{'fraction_non_alphanumeric': 0.12285902503293808, 'fraction_numerical': 0.0, 'mean_word_length': 2.8394437420986094, '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': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5693874', 'n_tokens_mistral': 916, 'n_tokens_neox': 910, 'n_words': 201}