<commit_before>from datetime import datetime, timedelta

from django.utils.translation import ugettext_lazy as _

from content.models import ModelBase

import django_filters

class IntervalFilter(django_filters.DateRangeFilter):
    """
    Filters based on week (in reality the last 7 days) and month.
    """
    options = {
        'week': (_('This Week'), lambda qs, name: qs.filter(**{
            '%s__gte' % name: (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'),
            '%s__lt' % name: (datetime.today()+timedelta(days=1)).strftime('%Y-%m-%d'),
        })),
        'month': (_('This Month'), lambda qs, name: qs.filter(**{
            '%s__year' % name: datetime.today().year,
            '%s__month' % name: datetime.today().month
        })),
    }
    def filter(self, qs, value):
        try:
            return self.options[value][1](qs, self.name)
        except KeyError:
            return qs

class OrderFilter(ChoiceFilter):
    pass

def order_action(qs, value):
    if value == '' or 'most-recent':
        return qs.order_by('-created')

class ContentFilter(django_filters.FilterSet):
    order_choices = ('most-recent', 'most-liked')
    order = django_filters.ChoiceFilter(
        name='classname', 
        label='Order By',
        action=order_action,
        choices=(
            ('most-recent', 'Most Recent'),
            ('most-liked', 'Most Liked'),
        )
    )
    interval = IntervalFilter(
        name="created",
        label="Filter By",
    )
    class Meta:
        model = ModelBase
        fields = ['order', 'interval']<commit_msg>added order filte<commit_after>from datetime import datetime, timedelta

from django.utils.translation import ugettext_lazy as _

from content.models import ModelBase

import django_filters

class IntervalFilter(django_filters.DateRangeFilter):
    """
    Filters based on week (in reality the last 7 days) and month.
    """
    options = {
        'week': (_('This Week'), lambda qs, name: qs.filter(**{
            '%s__gte' % name: (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'),
            '%s__lt' % name: (datetime.today()+timedelta(days=1)).strftime('%Y-%m-%d'),
        })),
        'month': (_('This Month'), lambda qs, name: qs.filter(**{
            '%s__year' % name: datetime.today().year,
            '%s__month' % name: datetime.today().month
        })),
    }
    def filter(self, qs, value):
        try:
            return self.options[value][1](qs, self.name)
        except KeyError:
            return qs

class OrderFilter(django_filters.ChoiceFilter):
    """
    Ordering filter ordering queryset items by most-recent(by created)
    and vote score(with score being calculated by positive votes).
    """
    options = {
        'most-recent': (_('Most Recent'), lambda qs, name: qs.order_by('-%s' % name)),
        'most-liked': (_('Most Recent'), lambda qs, name: qs.extra(
            select={
                'score': 'SELECT COUNT(*) FROM votes WHERE votes.object_id = content_modelbase.id AND votes.vote = 1'
            },
        ).order_by('-score')),
    }
    def filter(self, qs, value):
        try:
            return self.options[value][1](qs, self.name)
        except KeyError:
            return qs

class IntervalOrderFilterSet(django_filters.FilterSet):
    interval = IntervalFilter(
        name="created",
        label="Filter By",
    )
    order = OrderFilter(
        name="created",
        label="Order By",
    )
    class Meta:
        model = ModelBase
        fields = ['interval', 'order']
{'fraction_non_alphanumeric': 0.14602639707947207, 'fraction_numerical': 0.002808199943836001, 'mean_word_length': 2.492156862745098, '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': '2050997', 'n_tokens_mistral': 1096, 'n_tokens_neox': 1071, 'n_words': 262}