import django_filters
from harvest.models import Harvest, HARVESTS_STATUS_CHOICES
from member.models import AuthUser, Neighborhood
from django import forms
from django_filters import FilterSet, ChoiceFilter, ModelChoiceFilter, NumberFilter

FILTER_HARVEST_CHOICES = list(HARVESTS_STATUS_CHOICES)
FILTER_HARVEST_CHOICES.insert(0, ('', '---------'))

def get_seasons():
    """Return a list of years based on past and current harvests dates"""
    seasons = []
    t_seasons = ()
    for y in Harvest.objects.all():
        if y.start_date != None:
            t_seasons = (y.start_date.strftime("%Y"), y.start_date.strftime("%Y"))
            seasons.append(t_seasons)
    seasons.insert(0, ('', '---------'))
    return set(seasons)

FILTER_HARVEST_SEASONS = get_seasons()

class HarvestFilter(FilterSet):

    class Meta:
        model = Harvest
        fields = {
            'status': ['exact'],
            'pick_leader': ['exact'],
            'trees': ['exact'],
            'property__neighborhood':['exact'],
            'start_date': ['exact']
        }

    status = ChoiceFilter(
        choices=FILTER_HARVEST_CHOICES
    )

    pick_leader = ModelChoiceFilter(
        queryset=AuthUser.objects.filter(
            is_staff=True
        ),
        required=False
    )

    start_date = ChoiceFilter(
        choices=FILTER_HARVEST_SEASONS,
        label="Season",
        lookup_expr=('year')
    )


@@ -346,432 +346,8 @@
 ))%0A%0A
-def get_seasons():%0A    %22%22%22Return a list of years based on past and current harvests dates%22%22%22%0A    seasons = %5B%5D%0A    t_seasons = ()%0A    for y in Harvest.objects.all():%0A        if y.start_date != None:%0A            t_seasons = (y.start_date.strftime(%22%25Y%22), y.start_date.strftime(%22%25Y%22))%0A            seasons.append(t_seasons)%0A    seasons.insert(0, ('', '---------'))%0A    return set(seasons)%0A%0AFILTER_HARVEST_SEASONS = get_seasons()%0A
 %0Acla
@@ -374,16 +374,33 @@
 terSet):
+%0A    seasons = %5B%5D
 %0A%0A    cl
@@ -650,16 +650,500 @@
      %7D%0A%0A
+    def __init__(self, *args, **kwargs):%0A        %22%22%22Return a list of years based on past and current harvests dates%22%22%22%0A        super(HarvestFilter, self).__init__(*args, **kwargs)%0A        seasons = %5B%5D%0A        for y in Harvest.objects.all():%0A            if y.start_date != None:%0A                t_seasons = (y.start_date.strftime(%22%25Y%22), y.start_date.strftime(%22%25Y%22))%0A                seasons.append(t_seasons)%0A        seasons.insert(0, ('', '---------'))%0A        self.seasons = seasons%0A%0A
     stat
@@ -1404,30 +1404,15 @@
 ces=
-FILTER_HARVEST_SEASONS
+seasons
 ,%0A  

refactor HarvestFilter to allow upload of module to migrate