from django import forms
from django.forms.models import inlineformset_factory

from .models import Protocol, Institution, Topic


class InstitutionForm(forms.ModelForm):
    class Meta:
        model = Institution
        fields = ("name", )


class TopicForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        instance = super(TopicForm, self).save(commit=False)
        self.protocol = Protocol.objects.get(kwargs['protocol'])
        self.save_m2m()
        return instance

    class Meta:
        model = Topic
        exclude = ('protocol', 'attachment')
        fields = (
            "name",
            "voted_for",
            "voted_against",
            "voted_abstain",
            "statement", )


TopicFormSet = inlineformset_factory(Protocol, Topic, extra=2)


class ProtocolForm(forms.ModelForm):
    def save(self, *args, **kwargs):
        return super(ProtocolForm, self).save()

    class Meta:
        model = Protocol
        fields = (
            "institution",
            "number",
            "scheduled_time",
            "excused",
            "absent",
            "attendents",
            "start_time",
            "additional",
            "quorum",
            "majority",
            "current_majority",
            "voted_for",
            "voted_against",
            "voted_abstain",
            "information", )

@@ -71,16 +71,63 @@
 _factory
+%0Afrom django.shortcuts import get_object_or_404
 %0A%0Afrom .
@@ -1401,12 +1401,308 @@
 rmation%22, )%0A
+%0A%0Aclass AttendanceForm(forms.ModelForm):%0A%0A    institution_id = forms.CharField(max_length=64, required=False)%0A%0A    def search(self):%0A        institution_id = self.cleaned_data.get(%22institution_id%22)%0A        institution = get_object_or_404(Institution, id=institution_id)%0A        return institution

Add attendance form