from datetime import datetime, timedelta

from django.conf import settings
from django.core.urlresolvers import RegexURLResolver, Resolver404
from django.contrib.contenttypes.models import ContentType
from django.utils.encoding import smart_str

from secretballot.models import Vote

class Item(object):
    def __init__(self, request, title, default):
        self.request=request
        self.title=title
        self.default=default
            
class GetItem(Item):
    def __init__(self, request, title, get, default):
        get['value'] = str(get['value'])
        self.get=get
        super(GetItem, self).__init__(request, title, default)

    def is_active(self, request):
        if hasattr(self, 'get'):
            if request.GET.has_key(self.get['name']):
                return request.GET[self.get['name']] == self.get['value']

        return False
    
    def get_absolute_url(self):
        addition_pairs = [(self.get['name'], self.get['value']),]
        remove_keys = ['page', ]
        q = dict([(k, v) for k, v in self.request.GET.items()])
        for key in remove_keys:
            if q.has_key(key):
                del q[key]
        for key, value in addition_pairs:
            if key:
                if value:
                   q[key] = value
                else:
                    q.pop(key, None)
            qs = '&'.join(['%s=%s' % (k, v) for k, v in q.items()])
        return '?' + qs if len(q) else ''

class IntegerFieldRangeItem(GetItem):
    def __init__(self, request, title, get, field_name, filter_range, default=False):
        self.field_name = field_name
        self.filter_range = filter_range
        super(IntegerFieldRangeItem, self).__init__(request, title, get, default)

    def action(self, queryset):
        return queryset.filter(**{"%s__range" % self.field_name: self.filter_range})

class URLPatternItem(Item):
    def __init__(self, request, title, path, matching_pattern_names, default):
        self.path = path
        self.matching_pattern_names = matching_pattern_names
        super(URLPatternItem, self).__init__(request, title, default)
    
    def resolve_pattern_name(self, resolver, path):
        tried = []
        match = resolver.regex.search(path)
        if match:
            new_path = path[match.end():]
            for pattern in resolver.url_patterns:
                try:
                    sub_match = pattern.resolve(new_path)
                except Resolver404, e:
                    sub_tried = e.args[0].get('tried')
                    if sub_tried is not None:
                        tried.extend([(pattern.regex.pattern + '   ' + t) for t in sub_tried])
                    else:
                        tried.append(pattern.regex.pattern)
                else:
                    if sub_match:
                        sub_match_dict = dict([(smart_str(k), v) for k, v in match.groupdict().items()])
                        sub_match_dict.update(resolver.default_kwargs)
                        for k, v in sub_match[2].iteritems():
                            sub_match_dict[smart_str(k)] = v
                        try:
                            return pattern.name
                        except AttributeError:
                            return self.resolve_pattern_name(pattern, new_path)
                    tried.append(pattern.regex.pattern)
            raise Resolver404, {'tried': tried, 'path': new_path}
        raise Resolver404, {'path' : path}
    
    def is_active(self, request):
        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
        resolver = RegexURLResolver(r'^/', urlconf)

        url_name = self.resolve_pattern_name(resolver, request.path)
        return url_name in self.matching_pattern_names

    def action(self, queryset):
        return queryset

    def get_absolute_url(self):
        return self.path

class MostRecentItem(GetItem):
    def action(self, queryset):
        return queryset.order_by('-created')

class MostLikedItem(GetItem):
    def action(self, queryset):
        return queryset.extra(select={
                'vote_score': '(SELECT COUNT(*) from %s WHERE vote=1 AND object_id=%s.%s AND content_type_id=%s) - (SELECT COUNT(*) from %s WHERE vote=-1 AND object_id=%s.%s AND content_type_id=%s)' % (Vote._meta.db_table, queryset.model._meta.db_table, queryset.model._meta.pk.attname, ContentType.objects.get_for_model(queryset.model).id, Vote._meta.db_table, queryset.model._meta.db_table, queryset.model._meta.pk.attname, ContentType.objects.get_for_model(queryset.model).id)}).order_by('-vote_score')

class ThisWeekItem(GetItem):
    def action(self, queryset):
        return queryset.filter(**{
            'created__gte': (datetime.today() - timedelta(days=7)).strftime('%Y-%m-%d'),
            'created__lt': (datetime.today()+timedelta(days=1)).strftime('%Y-%m-%d'),
        })

class ThisMonthItem(GetItem):
    def action(self, queryset):
        return queryset.filter(**{
            'created__year': datetime.today().year,
            'created__month': datetime.today().month
        })

@@ -506,24 +506,39 @@
 le, get,
+ field_name='',
  default
 ):%0A     
@@ -521,32 +521,38 @@
 name='', default
+=False
 ):%0A        get%5B'
@@ -600,16 +600,53 @@
 get=get%0A
+        self.field_name = field_name%0A
         
@@ -1774,16 +1774,28 @@
 le, get,
+ field_name,
  default
@@ -4035,16 +4035,29 @@
 y('-
-created'
+%25s' %25 self.field_name
 )%0A%0Ac
@@ -4776,21 +4776,34 @@
    '
-created__gte'
+%25s__gte' %25 self.field_name
 : (d
@@ -4878,20 +4878,353 @@
    '
-created__lt'
+%25s__lt' %25 self.field_name: (datetime.today()+timedelta(days=1)).strftime('%25Y-%25m-%25d'),%0A        %7D)%0A        %0Aclass ThisWeekendItem(GetItem):%0A    def action(self, queryset):%0A        return queryset.filter(**%7B%0A            '%25s__gte' %25 self.field_name: (datetime.today() - timedelta(days=7)).strftime('%25Y-%25m-%25d'),%0A            '%25s__lt' %25 self.field_name
 : (d
@@ -5406,22 +5406,35 @@
    '
-created__year'
+%25s__year' %25 self.field_name
 : da
@@ -5471,23 +5471,18 @@
    '
-created
+%25s
 __month'
 : da
@@ -5477,16 +5477,34 @@
 __month'
+ %25 self.field_name
 : dateti

refactor and added weekend filter