'When an attachment revision form is saved, this is used to attach
the new attachment to the given document via an intermediate M2M
model that stores some extra data like the user and the revision\'s
filename.'

def attach(self, document, user, revision):

    DocumentAttachment = document.files.through
    try:
        document_attachment = DocumentAttachment.objects.get(file_id=self.pk)
    except document.files.through.DoesNotExist:
        document_attachment = DocumentAttachment.objects.create(file=self, document=document, attached_by=user, name=revision.filename, is_original=True)
    else:
        document_attachment.is_original = True
        document_attachment.attached_by = user
        document_attachment.name = revision.filename
        document_attachment.save()
    return document_attachment
