from django import forms
from django.urls import reverse_lazy
from django.views.generic import ListView, UpdateView

from dronken.models import City, Association


class CityList(ListView):
    template_name = 'cities.html'
    model = City
    ordering = ['name']


class AssociationList(ListView):
    template_name = 'associations.html'
    model = Association
    city = None
    ordering = ['short_name']

    def get_queryset(self):
        self.city = City.objects.get(slug=self.kwargs['city'])
        return Association.objects.filter(city=self.city)

    def get_context_data(self, *args, **kwargs):
        context = super(AssociationList, self).get_context_data(*args, **kwargs)
        context['city'] = self.city
        return context


class DrunkUpdateForm(forms.ModelForm):
    state = forms.CharField(
        widget=forms.Select(choices=Association.STATES),
    )

    class Meta:
        model = Association
        fields = ['state']


class AssociationDetail(UpdateView):
    template_name = 'association.html'
    model = Association
    form_class = DrunkUpdateForm

    def get_success_url(self):
        return reverse_lazy('dronken:association_list', kwargs={'city': self.get_object().city.slug})