class EditPaymentRequestForm(PaymentRequestBaseForm): receipt_list = forms.ModelMultipleChoiceField( widget=forms.CheckboxSelectMultiple(attrs={'class': 'delete'}), queryset=PaymentReceipt.objects.all(), required=False, label='Receipts') receipts = MultiFileField(label='', required=False) def __init__(self, user=None, instance=None, *args, **kwargs): super().__init__(*args, instance=instance, **kwargs) self.fields['receipt_list'].queryset = instance.receipts.all() self.fields['requested_value'].label = 'Value' @transaction.atomic def save(self, commit=True): request = super().save(commit=commit) removed_receipts = self.cleaned_data['receipt_list'] removed_receipts.delete() to_add = self.cleaned_data['receipts'] if to_add: PaymentReceipt.objects.bulk_create( PaymentReceipt(payment_request=request, file=receipt) for receipt in to_add) return request
class EditInvoiceForm(FileFormMixin, InvoiceBaseForm): document = SingleFileField(label=_('Invoice File'), required=True) supporting_documents = MultiFileField(required=False) field_order = ['document', 'supporting_documents', 'message_for_pm'] @transaction.atomic def save(self, commit=True): invoice = super().save(commit=commit) not_deleted_original_filenames = [ file['name'] for file in json.loads( self.cleaned_data['supporting_documents-uploads']) ] for f in invoice.supporting_documents.all(): if f.document.name not in not_deleted_original_filenames: f.document.delete() f.delete() for f in self.cleaned_data["supporting_documents"]: if not isinstance(f, FieldFile): try: SupportingDocument.objects.create(invoice=invoice, document=f) finally: f.close() return invoice
class CreatePaymentRequestForm(PaymentRequestBaseForm): receipts = MultiFileField() def save(self, commit=True): request = super().save(commit=commit) PaymentReceipt.objects.bulk_create( PaymentReceipt(payment_request=request, file=receipt) for receipt in self.cleaned_data['receipts']) return request
class CreatePaymentRequestForm(FileFormMixin, PaymentRequestBaseForm): receipts = MultiFileField(required=False) def save(self, commit=True): request = super().save(commit=commit) receipts = self.cleaned_data['receipts'] or [] PaymentReceipt.objects.bulk_create( PaymentReceipt(payment_request=request, file=receipt) for receipt in receipts ) return request
class CreateInvoiceForm(FileFormMixin, InvoiceBaseForm): document = SingleFileField(label='Invoice File', required=True) supporting_documents = MultiFileField( required=False, help_text=_('Files that are related to the invoice. They could be xls, microsoft office documents, open office documents, pdfs, txt files.') ) field_order = ['date_from', 'date_to', 'amount', 'document', 'supporting_documents', 'message_for_pm'] def save(self, commit=True): invoice = super().save(commit=commit) supporting_documents = self.cleaned_data['supporting_documents'] or [] SupportingDocument.objects.bulk_create( SupportingDocument(invoice=invoice, document=document) for document in supporting_documents ) return invoice
class ReportEditForm(FileFormMixin, forms.ModelForm): public_content = RichTextField(help_text=_( 'This section of the report will be shared with the broader community.' )) private_content = RichTextField(help_text=_( 'This section of the report will be shared with staff only.')) file_list = forms.ModelMultipleChoiceField( widget=forms.CheckboxSelectMultiple(attrs={'class': 'delete'}), queryset=ReportPrivateFiles.objects.all(), required=False, label='Files') files = MultiFileField(required=False, label='') class Meta: model = Report fields = ( 'public_content', 'private_content', 'file_list', 'files', ) def __init__(self, *args, user=None, initial={}, **kwargs): self.report_files = initial.pop( 'file_list', ReportPrivateFiles.objects.none(), ) super().__init__(*args, initial=initial, **kwargs) self.fields['file_list'].queryset = self.report_files self.user = user def clean(self): cleaned_data = super().clean() public = cleaned_data['public_content'] private = cleaned_data['private_content'] if not private and not public: missing_content = _( 'Must include either public or private content when submitting a report.' ) self.add_error('public_content', missing_content) self.add_error('private_content', missing_content) return cleaned_data @transaction.atomic def save(self, commit=True): is_draft = 'save' in self.data version = ReportVersion.objects.create( report=self.instance, public_content=self.cleaned_data['public_content'], private_content=self.cleaned_data['private_content'], submitted=timezone.now(), draft=is_draft, author=self.user, ) if is_draft: self.instance.draft = version else: # If this is the first submission of the report we track that as the # submitted date of the report if not self.instance.submitted: self.instance.submitted = version.submitted self.instance.current = version self.instance.draft = None instance = super().save(commit) removed_files = self.cleaned_data['file_list'] ReportPrivateFiles.objects.bulk_create( ReportPrivateFiles(report=version, document=file.document) for file in self.report_files if file not in removed_files) added_files = self.cleaned_data['files'] if added_files: ReportPrivateFiles.objects.bulk_create( ReportPrivateFiles(report=version, document=file) for file in added_files) return instance
class CreateVendorFormStep3(FileFormMixin, BaseVendorForm, forms.Form): due_diligence_documents = MultiFileField(required=True) def __init__(self, *args, **kwargs): super(CreateVendorFormStep3, self).__init__(*args, **kwargs) self.fields = self.apply_form_settings(self.fields)