Bases: FileFormMixin
, ModelForm
Source code in hypha/apply/activity/forms.py
| def __init__(self, *args, user=None, **kwargs):
# Get `submission_partner_list` kwarg and remove it before initializing parent.
submission_partner_list = None
if "submission_partner_list" in kwargs:
submission_partner_list = kwargs.pop("submission_partner_list")
super().__init__(*args, **kwargs)
self.visibility_choices = self._meta.model.visibility_choices_for(
user, submission_partner_list
)
visibility = self.fields["visibility"]
# Set default visibility to "Applicant" for staff and staff can view everything.
visibility.initial = self.visibility_choices[0]
if len(self.visibility_choices) > 1:
visibility.choices = self.visibility_choices
else:
visibility.required = False
visibility.choices = self.visibility_choices
visibility.initial = visibility.initial[0]
visibility.widget = forms.HiddenInput()
if not user.is_apply_staff:
self.fields["assign_to"].widget = forms.HiddenInput()
|
attachments = MultiFileField(label=gettext_lazy('Attachments'), required=False)
assign_to = ModelChoiceField(queryset=filter(groups__name=STAFF_GROUP_NAME), required=False, empty_label=gettext_lazy('Select...'), label=gettext_lazy('Assign to'))
visibility_choices = visibility_choices_for(user, submission_partner_list)
fields = ('message', 'visibility', 'assign_to')
labels = {'visibility': 'Visible to', 'message': 'Message'}
help_texts = {'visibility': 'Select a relevant user role. Staff can view every comment.'}
widgets = {'visibility': RadioSelect(), 'message': PagedownWidget()}
Source code in hypha/apply/activity/forms.py
| @transaction.atomic
def save(self, commit=True):
instance = super().save(commit=True)
added_files = self.cleaned_data["attachments"]
assigned_user = self.cleaned_data["assign_to"]
if assigned_user:
# add task to assigned user
add_task_to_user(
code=COMMENT_TASK,
user=assigned_user,
related_obj=instance,
)
if added_files:
ActivityAttachment.objects.bulk_create(
ActivityAttachment(activity=instance, file=file) for file in added_files
)
return instance
|