Skip to content
Snippets Groups Projects
forms.py 3 KiB
Newer Older
  • Learn to ignore specific revisions
  • Todd Dembrey's avatar
    Todd Dembrey committed
    from django import forms
    
    from django_select2.forms import Select2Widget
    
    
    from opentech.apply.users.models import User
    
    from opentech.apply.funds.models import ReviewerRole
    
    Erin Mullaney's avatar
    Erin Mullaney committed
    from .models import ApplicationSubmission
    
    from .widgets import Select2MultiCheckboxesWidget
    
    Todd Dembrey's avatar
    Todd Dembrey committed
    class ProgressSubmissionForm(forms.ModelForm):
    
        action = forms.ChoiceField(label='Take action')
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        class Meta:
            model = ApplicationSubmission
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            fields: list = []
    
    Todd Dembrey's avatar
    Todd Dembrey committed
        def __init__(self, *args, **kwargs):
    
            self.user = kwargs.pop('user')
    
    Todd Dembrey's avatar
    Todd Dembrey committed
            super().__init__(*args, **kwargs)
    
            choices = list(self.instance.get_actions_for_user(self.user))
    
            action_field = self.fields['action']
            action_field.choices = choices
    
            self.should_show = bool(choices)
    
    class ScreeningSubmissionForm(forms.ModelForm):
    
        class Meta:
            model = ApplicationSubmission
    
            fields = ('screening_status',)
    
    
        def __init__(self, *args, **kwargs):
            self.user = kwargs.pop('user')
            super().__init__(*args, **kwargs)
    
            self.should_show = False
            if (self.instance.active and self.user.is_apply_staff) or self.user.is_superuser:
                self.should_show = True
    
    class UpdateSubmissionLeadForm(forms.ModelForm):
        class Meta:
            model = ApplicationSubmission
            fields = ('lead',)
    
        def __init__(self, *args, **kwargs):
    
            kwargs.pop('user')
    
            super().__init__(*args, **kwargs)
    
            lead_field = self.fields['lead']
            lead_field.label = f'Update lead from { self.instance.lead } to'
            lead_field.queryset = lead_field.queryset.exclude(id=self.instance.lead.id)
    
    
    
    class UpdateReviewersForm(forms.ModelForm):
        class Meta:
            model = ApplicationSubmission
            fields: list = []
    
        def __init__(self, *args, **kwargs):
    
            self.user = kwargs.pop('user')
    
            super().__init__(*args, **kwargs)
    
            reviewers = User.objects.staff() | User.objects.reviewers()
            for role in ReviewerRole.objects.all():
                role_name = role.name.replace(" ", "_")
                self.fields[role_name + '_reviewer_' + str(role.pk)] = forms.ModelChoiceField(
                    queryset=reviewers,
                    widget=Select2Widget(attrs={'data-placeholder': 'Select a reviewer'}),
                    required=False,
                )
    
        def save(self, *args, **kwargs):
    
            instance = super().save(*args, **kwargs)
    
            # TODO loop through self.cleaned_data and save reviewers to submission
            # for field_name, value in self.cleaned_data.items():
            #   role_pk = field_name[field_name.rindex("_")+1:]
    
    
    
    class BatchUpdateReviewersForm(forms.Form):
        staff_reviewers = forms.ModelMultipleChoiceField(
            queryset=User.objects.staff(),
            widget=Select2MultiCheckboxesWidget(attrs={'data-placeholder': 'Staff'}),
        )
        submission_ids = forms.CharField(widget=forms.HiddenInput())
    
    
        def clean_submission_ids(self):
            value = self.cleaned_data['submission_ids']
            return [int(submission) for submission in value.split(',')]