isonzeinterndronken/dronken/views.py

58 lines
1.6 KiB
Python

from django import forms
from django.urls import reverse_lazy
from django.views.generic import ListView, UpdateView
from dronken.models import City, Association, ExtraState
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']
def __init__(self, *args, **kwargs):
super(DrunkUpdateForm, self).__init__(*args, **kwargs)
self.fields['state'] = forms.ChoiceField(
choices=list(Association.STATES) + [(o.value, o.name) for o in self.instance.extra_states.all()]
)
def clean(self):
print(self.instance.state)
return super(DrunkUpdateForm, self).clean()
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})