Open agusmakmun opened 7 years ago
It is possible to write a own compare for special fields. What does your model field look like?
Currently, I handle the markdown syntax only on front-end form using django-markdown-editor, not in the models.py
Here is my models.py
@python_2_unicode_compatible
class QuestionSuggestedEdits(TimeStampedModel):
question = models.ForeignKey(
Question, related_name='suggested_edits_question')
editor = models.ForeignKey(
User, related_name='suggested_edits_editor')
title = models.CharField(
_('Title'), max_length=200)
tags = models.ManyToManyField(
Tag, related_name='suggested_edits_tags')
STATUS_CHOICES = (
('approved', _('Approved')),
('rejected', _('Rejected')),
('pending', _('Pending'))
)
status = models.CharField(
_('Status'), max_length=20,
choices=STATUS_CHOICES, default='pending')
description = models.TextField(_('Description'))
comment = models.TextField(_('Revision Comment'))
@property
def slug(self):
return slugify(self.title)
class Meta:
verbose_name_plural = _('question suggested edits')
ordering = ['-created']
and the views.py
from reversion_compare.views import HistoryCompareDetailView
from app_faq.models.question import QuestionSuggestedEdits
class QuestionSuggestedEditsReversions(HistoryCompareDetailView):
template_name = 'app_faq/question_revisions.html'
context_object_name = 'question_suggested_edits'
model = QuestionSuggestedEdits
compare_fields = ('description', )
# def fallback_compare(self, obj_compare):
# value1, value2 = obj_compare.to_string()
# html = html_diff_custom(value1, value2)
# return html
def get_object(self):
return get_object_or_404(self.model, pk=self.kwargs['pk'])
def compare_ManyToOneRel(self, obj_compare):
change_info = obj_compare.get_m2o_change_info()
context = {'change_info': change_info}
return render_to_string('reversion-compare/question/compare_generic_many_to_many.html', context)
def compare_ManyToManyField(self, obj_compare):
change_info = obj_compare.get_m2m_change_info()
context = {'change_info': change_info}
return render_to_string('reversion-compare/question/compare_generic_many_to_many.html', context)
I still focusing on this reversion_compare/helpers.py
, I just think maybe it can be handle with some logic here, but I still confusing what the best way to force as markdown syntax looks like https://github.com/etianen/django-reversion/wiki/_history
def html_diff(value1, value2, cleanup=SEMANTIC):
"""
Generates a diff used google-diff-match-patch is exist or ndiff as fallback
The cleanup parameter can be SEMANTIC, EFFICIENCY or None to clean up the diff
for greater human readibility.
"""
value1 = force_text(value1)
value2 = force_text(value2)
if dmp is not None:
# Generate the diff with google-diff-match-patch
....
else:
# fallback: use built-in difflib
value1 = value1.splitlines()
value2 = value2.splitlines()
....
html = mark_safe(html)
return html
You use https://github.com/agusmakmun/django-markdown-editor ? But i didn't see a MartorField
in your QuestionSuggestedEdits
model...
e.g. if you have something like this:
from django.db import models
from martor.models import MartorField
class Post(models.Model):
description = MartorField()
Then you can do something like this:
class YourAdmin(CompareVersionAdmin):
def compare_description(self, obj_compare):
return "%r <-> %r" % (obj_compare.value1, obj_compare.value2)
In compare_description()
you have to get the markdown source and compare it. See:
See: https://github.com/jedie/django-reversion-compare#customize
If I following
The html output is;
The
{{ field_diff.diff }}
is converted as html. How I can handle the markdown syntax?https://github.com/etianen/django-reversion/wiki/_history
or maybe like this;