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']

@@ -231,13 +231,16 @@
 ers 
-based
+queryset
  on 
@@ -273,19 +273,18 @@
 7 days) 
-and
+or
  month.%0A
@@ -1043,22 +1043,21 @@
     
-and vote score
+or most-liked
 (wit
@@ -1235,38 +1235,37 @@
 iked': (_('Most 
-Recent
+Liked
 '), lambda qs, n
@@ -1617,24 +1617,24 @@
  return qs%0A%0A
-
 class Interv
@@ -1677,16 +1677,145 @@
 erSet):%0A
+    %22%22%22%0A    Filters queryset through an IntervalFilter('interval').%0A    Orders queryset through an OrderFilter('order').%0A    %22%22%22%0A
     inte

copy correction