(self)
return atomic_attachments()
Context manager for atomically writing attachments

        Usage:
            with form.attachment_writer() as write_attachments, \\
                    transaction.atomic(using=form.db, savepoint=False):
                form.save()
                write_attachments()
                ...
Context manager for atomically writing attachments
Context manager for atomically writing attachments
def attachment_writer(self):
        """Context manager for atomically writing attachments

        Usage:
            with form.attachment_writer() as write_attachments, \\
                    transaction.atomic(using=form.db, savepoint=False):
                form.save()
                write_attachments()
                ...
        """
        if all(isinstance(a, BlobMeta) for a in self.attachments_list):
            # do nothing if all attachments have already been written
            class NoopWriter():
                def write(self):
                    pass

                def commit(self):
                    pass

            @contextmanager
            def noop_context():
                yield NoopWriter()

            return noop_context()

        class Writer():
            def __init__(self, form, blob_db):
                self.form = form
                self.blob_db = blob_db

            def write(self):
                self.saved_attachments = [
                    attachment.write(self.blob_db, self.form)
                    for attachment in self.form.attachments_list
                ]

            def commit(self):
                self.form._attachments_list = self.saved_attachments

        @contextmanager
        def atomic_attachments():
            unsaved = self.attachments_list
            assert all(isinstance(a, Attachment) for a in unsaved), unsaved
            with AtomicBlobs(get_blob_db()) as blob_db:
                yield Writer(self, blob_db)

        return atomic_attachments()
def attachment_writer ( self ) : if all ( isinstance ( a , BlobMeta ) for a in self . attachments_list ) : # do nothing if all attachments have already been written class NoopWriter ( ) : def write ( self ) : pass def commit ( self ) : pass @ contextmanager def noop_context ( ) : yield NoopWriter ( ) return noop_context ( ) class Writer ( ) : def __init__ ( self , form , blob_db ) : self . form = form self . blob_db = blob_db def write ( self ) : self . saved_attachments = [ attachment . write ( self . blob_db , self . form ) for attachment in self . form . attachments_list ] def commit ( self ) : self . form . _attachments_list = self . saved_attachments @ contextmanager def atomic_attachments ( ) : unsaved = self . attachments_list assert all ( isinstance ( a , Attachment ) for a in unsaved ) , unsaved with AtomicBlobs ( get_blob_db ( ) ) as blob_db : yield Writer ( self , blob_db ) return atomic_attachments ( )