mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-22 11:23:10 +00:00
Attempt to add diff view in event logs
This commit is contained in:
parent
3c61a4cd04
commit
022e9aca93
2 changed files with 87 additions and 37 deletions
109
logs/models.py
109
logs/models.py
|
@ -40,43 +40,6 @@ from topologie.models import Port
|
|||
from .forms import classes_for_action_type
|
||||
|
||||
|
||||
class VersionAction:
|
||||
def __init__(self, version):
|
||||
self.version = version
|
||||
|
||||
def name(self):
|
||||
return self.version.object_repr
|
||||
|
||||
def application(self):
|
||||
return self.version.content_type.app_label
|
||||
|
||||
def model_name(self):
|
||||
return self.version.content_type.model
|
||||
|
||||
def object_id(self):
|
||||
return self.version.object_id
|
||||
|
||||
def object_type(self):
|
||||
return apps.get_model(self.application(), self.model_name())
|
||||
|
||||
|
||||
class RevisionAction:
|
||||
"""A Revision may group multiple Version objects together"""
|
||||
def __init__(self, revision):
|
||||
self.performed_by = revision.user
|
||||
self.revision = revision
|
||||
self.versions = [VersionAction(v) for v in revision.version_set.all()]
|
||||
|
||||
def id(self):
|
||||
return self.revision.id
|
||||
|
||||
def date_created(self):
|
||||
return self.revision.date_created
|
||||
|
||||
def comment(self):
|
||||
return self.revision.get_comment()
|
||||
|
||||
|
||||
class ActionsSearch:
|
||||
def get(self, params):
|
||||
"""
|
||||
|
@ -441,6 +404,78 @@ class History:
|
|||
self._last_version = version
|
||||
|
||||
|
||||
class VersionAction(HistoryEvent):
|
||||
def __init__(self, version):
|
||||
self.version = version
|
||||
|
||||
def is_useful(self):
|
||||
# Some versions are duplicates, and don't have a reference
|
||||
# to any object, so ignore them
|
||||
return self.version.object_id is not None
|
||||
|
||||
def name(self):
|
||||
return "{} {}".format(self.model_name().title(), self.version.object_repr)
|
||||
|
||||
def application(self):
|
||||
return self.version.content_type.app_label
|
||||
|
||||
def model_name(self):
|
||||
return self.version.content_type.model
|
||||
|
||||
def object_id(self):
|
||||
return self.version.object_id
|
||||
|
||||
def object_type(self):
|
||||
return apps.get_model(self.application(), self.model_name())
|
||||
|
||||
def edits(self, hide=["password", "pwd_ntlm", "gpg_fingerprint"]):
|
||||
self.previous_version = self._previous_version()
|
||||
self.edited_fields = self._compute_diff(self.version, self.previous_version)
|
||||
return super(VersionAction, self).edits(hide)
|
||||
|
||||
def _previous_version(self):
|
||||
model = self.object_type()
|
||||
return next(
|
||||
filter(
|
||||
lambda x: x.field_dict["id"] == self.object_id() and x.revision.date_created < self.version.revision.date_created,
|
||||
Version.objects.get_for_model(model).order_by("-revision__date_created")
|
||||
)
|
||||
)
|
||||
|
||||
def _compute_diff(self, v1, v2):
|
||||
"""
|
||||
Find the edited field between two versions
|
||||
:param v1: Version
|
||||
:param v2: Version
|
||||
:param ignoring: List, a list of fields to ignore
|
||||
:return: List of field names
|
||||
"""
|
||||
fields = []
|
||||
|
||||
for key in v1.field_dict.keys():
|
||||
if v1.field_dict[key] != v2.field_dict[key]:
|
||||
fields.append(key)
|
||||
|
||||
return fields
|
||||
|
||||
|
||||
class RevisionAction:
|
||||
"""A Revision may group multiple Version objects together"""
|
||||
def __init__(self, revision):
|
||||
self.performed_by = revision.user
|
||||
self.revision = revision
|
||||
self.versions = [VersionAction(v) for v in revision.version_set.all() if v.is_useful()]
|
||||
|
||||
def id(self):
|
||||
return self.revision.id
|
||||
|
||||
def date_created(self):
|
||||
return self.revision.date_created
|
||||
|
||||
def comment(self):
|
||||
return self.revision.get_comment()
|
||||
|
||||
|
||||
class UserHistoryEvent(HistoryEvent):
|
||||
def _repr(self, name, value):
|
||||
"""
|
||||
|
|
|
@ -38,6 +38,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
<th>{% include 'buttons/sort.html' with prefix='logs' col='author' text=tr_edited_by %}</th>
|
||||
{% trans "Date of editing" as tr_date_of_editing %}
|
||||
<th>{% include 'buttons/sort.html' with prefix='logs' col='date' text=tr_date_of_editing %}</th>
|
||||
<th>{% trans "Edited" %}</th>
|
||||
<th>{% trans "Comment" %}</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
@ -64,6 +65,20 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
{% endif %}
|
||||
</td>
|
||||
<td>{{ revision.date_created }}</td>
|
||||
<td>
|
||||
{% for edit in version.edits %}
|
||||
{% if edit.1 is None and edit.2 is None %}
|
||||
<strong>{{ edit.0 }}</strong><br/>
|
||||
{% elif edit.1 is None %}
|
||||
<strong>{{ edit.0 }}:</strong>
|
||||
<i class="text-success"> {{ edit.2 }}</i><br/>
|
||||
{% else %}
|
||||
<strong>{{ edit.0 }}:</strong>
|
||||
<i class="text-danger"> {{ edit.1 }} </i>
|
||||
➔ <i class="text-success">{{ edit.2 }}</i><br/>
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</td>
|
||||
<td>{{ revision.comment }}</td>
|
||||
{% can_edit_history %}
|
||||
<td>
|
||||
|
|
Loading…
Reference in a new issue