Events are not created correctly for document upload
When a new document is uploaded, the Event log is not written correctly. The actor for the event_document_create
event will not be set to the user, but instead to the target:
Sept. 4, 2017, 11:27 a.m. jesaja New version uploaded test-upload-4.img
Sept. 4, 2017, 11:27 a.m. test-upload-4.img Document properties edited test-upload-4.img
Sept. 4, 2017, 11:27 a.m. jesaja Document properties edited test-upload-4.img
Sept. 4, 2017, 11:27 a.m. test-upload-4.img Document created test-upload-4.img
There will also be additional events saved that the document was edited and a new version was uploaded, even though the document was only uploaded initially.
The reason is, that the code that passes the user to the document's save
method, document.save(_user=user)
, is not the first invocation of the save
method. It gets invoked right above by calling Document.objects.create
.
In this context the user is None
and the event therefore falls back to the target as actor. When document.save(_user=user)
is called, self.pk
exists and therefore event_document_properties_edit
will be invoked instead of event_document_create
.
def save(self, *args, **kwargs):
user = kwargs.pop('_user', None)
new_document = not self.pk
super(Document, self).save(*args, **kwargs)
if new_document:
if user:
self.add_as_recent_document_for_user(user)
event_document_create.commit(actor=user, target=self)
else:
event_document_create.commit(target=self)
else:
event_document_properties_edit.commit(actor=user, target=self)
I guess we shouldn't use Document.objects.create
which invokes save()
but rather construct the instance without saving to DB at that step.