-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP: Fix #148 add a compare fallback
work-a-round for: etianen/django-reversion#859
- Loading branch information
Showing
11 changed files
with
515 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
Helper to compare raw version data. | ||
see: https://github.com/etianen/django-reversion/issues/859 | ||
:copyleft: 2021 by the django-reversion-compare team, see AUTHORS for more details. | ||
:license: GNU GPL v3 or above, see LICENSE for more details. | ||
""" | ||
|
||
|
||
import json | ||
import pprint | ||
|
||
from django.core.serializers.json import DjangoJSONEncoder | ||
from reversion.models import Version | ||
|
||
|
||
def get_version_data(version): | ||
""" | ||
Get field data from a Version instance. | ||
""" | ||
assert isinstance(version, Version) | ||
assert version.format == 'json', f'{version.format!r} not supported (only JSON)' | ||
|
||
json_string = version.serialized_data | ||
version_data = json.loads(json_string) | ||
# version_data looks like: | ||
# [{'fields': {'info': 'Migration state 2 - version 4', | ||
# 'number_then_text': 111, | ||
# 'text': 'Now this is a short text!!!'}, | ||
# 'model': 'reversion_compare_tests.migrationmodel', | ||
# 'pk': 1}] | ||
assert len(version_data) == 1 | ||
fields_data = version_data[0]['fields'] | ||
return fields_data | ||
|
||
|
||
def pformat(value): | ||
try: | ||
return DjangoJSONEncoder(indent=4, sort_keys=True, ensure_ascii=False).encode(value) | ||
except TypeError: | ||
# Fallback if values are not serializable with JSON: | ||
return pprint.pformat(value, width=120) |
69 changes: 69 additions & 0 deletions
69
reversion_compare/templates/reversion-compare/compare_raw.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
{% extends "admin/base_site.html" %} | ||
{% load i18n %} | ||
|
||
{% block extrastyle %}{{ block.super }} | ||
<style type="text/css"> | ||
/* minimal style for the diffs */ | ||
pre.highlight { | ||
max-width: 900px; | ||
} | ||
del, ins { | ||
color: #000; | ||
text-decoration: none; | ||
} | ||
del { background-color: #fdb8c0 } | ||
ins { background-color: #acf2bd; } | ||
sup.follow { color: #5555ff; } | ||
.diff-line { | ||
display: inline-block; | ||
width: 100%; | ||
margin-left: -1.0em; | ||
padding-left: 0.5em; | ||
background-color: #f6f6f6; | ||
color: #222; | ||
} | ||
.diff-line.diff-ins { border-left: 0.5em solid #bef5cb; background-color: #e6ffed; } | ||
.diff-line.diff-del { border-left: 0.5em solid #fdaeb7; background-color: #ffeef0; } | ||
.diff-line.diff-ins.diff-del { border-left: 0.5em solid #bef5cb; background-color: #e6ffed; } /* mixed del/ins == green */ | ||
</style> | ||
{% endblock %} | ||
|
||
{% block breadcrumbs %} | ||
<div class="breadcrumbs"> | ||
<a href="{% url 'admin:index' %}">{% trans 'Home' %}</a> › | ||
<a href="{% url 'admin:app_list' app_label %}">{{app_label|capfirst|escape}}</a> › | ||
<a href="{{changelist_url}}">{{opts.verbose_name_plural|capfirst}}</a> › | ||
<a href="{{history_url}}">{% trans "History" %}</a> › | ||
{% blocktrans with opts.verbose_name_plural|escape as name %}Compare {{name}}{% endblocktrans %} | ||
{{title}} | ||
</div> | ||
{% endblock %} | ||
|
||
|
||
{% block content %} | ||
<div id="content-main"> | ||
|
||
{% block object-tools %} | ||
<ul class="object-tools"> | ||
{% block object-tools-items %} | ||
{% include "reversion-compare/compare_links_partial.html" %} | ||
{% endblock %} | ||
</ul> | ||
{% endblock %} | ||
|
||
<p class="help"> | ||
{% blocktrans with date1=version1.revision.date_created|date:_("DATETIME_FORMAT") date2=version2.revision.date_created|date:_("DATETIME_FORMAT") %} | ||
Compare <strong>{{ date1 }}</strong> with <strong>{{ date2 }}</strong>: | ||
{% endblocktrans %} | ||
</p> | ||
‹ <a href="{{history_url}}">{% trans "Go back to history list" %}</a> | ||
| | ||
<a href="{{save_url}}">{% trans "Revert to this version" %}</a> › | ||
|
||
{% include "reversion-compare/compare_raw_partial.html" %} | ||
|
||
‹ <a href="{{history_url}}">{% trans "Go back to history list" %}</a> | ||
| | ||
<a href="{{save_url}}">{% trans "Revert to this version" %}</a> › | ||
</div> | ||
{% endblock %} |
17 changes: 17 additions & 0 deletions
17
reversion_compare/templates/reversion-compare/compare_raw_partial.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{% load i18n %} | ||
|
||
{{ diff_html }} | ||
|
||
<h4>{% trans "Edit comment:" %}</h4> | ||
<blockquote>{{ version2.revision.comment|default:_("(no comment exists)") }}</blockquote> | ||
|
||
<h4>{% trans "Note:" %}</h4> | ||
<p> | ||
This is the fallback compare, because the normal compare can't be applied | ||
between the two selected version: | ||
</p> | ||
<pre>{{ compare_error }}</pre> | ||
<p> | ||
(More info: <a href="https://github.com/etianen/django-reversion/issues/859"> | ||
django-reversion issues #859</a>) | ||
</p> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
reversion_compare_tests/management/commands/fill_migration_model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
""" | ||
:copyleft: 2015-2016 by the django-reversion-compare team, see AUTHORS for more details. | ||
:created: 2015 by JensDiemer.de | ||
:license: GNU GPL v3 or above, see LICENSE for more details. | ||
""" | ||
|
||
|
||
from django.core.management import BaseCommand, call_command | ||
from reversion import create_revision, set_comment | ||
|
||
from reversion_compare_tests.models import MigrationModel | ||
|
||
|
||
lorem_ipsum = ( | ||
'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor' | ||
' invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et' | ||
' accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata' | ||
' sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing' | ||
' elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat,' | ||
' sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita' | ||
' kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.' | ||
) | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Run Unittest-Server" | ||
|
||
def verbose_call(self, command, **kwargs): | ||
self.stdout.write("\n") | ||
self.stdout.write("_" * 79) | ||
self.stdout.write(self.style.NOTICE(f" *** call '{command}' command:")) | ||
self.stdout.write("\n") | ||
call_command(command, **kwargs) | ||
|
||
def handle(self, *args, **options): | ||
# Migration state 1: | ||
# number_then_text = models.CharField() | ||
# text = models.TextField() | ||
with create_revision(): | ||
info = 'Migration state 1 - version 1' | ||
item = MigrationModel.objects.create( | ||
info=info, | ||
number_then_text='123', | ||
text=lorem_ipsum | ||
) | ||
set_comment(f'Reversion comment: {info}') | ||
|
||
if self.verbose: | ||
print("version 1:", item1) | ||
|
||
with create_revision(): | ||
item1.text = "version two" | ||
item1.save() | ||
set_comment("simply change the CharField text.") |
Oops, something went wrong.