Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Does django reversion support with markdown syntax? #97

Open
agusmakmun opened this issue Sep 22, 2017 · 3 comments
Open

Does django reversion support with markdown syntax? #97

agusmakmun opened this issue Sep 22, 2017 · 3 comments

Comments

@agusmakmun
Copy link

agusmakmun commented Sep 22, 2017

markdown

If I following

{% for field_diff in compare_data %}
    {{ field_diff }}

The html output is;

{'diff': '<span>Dynamically evolve tactical e-business whereas client-focused technology. Monotonectally evolve top-line strategic theme areas after optimal technology. Authoritatively empower fully researched results for backend synergy. Credibly maintain ethical imperatives without ethical scenarios. Intrinsicly incubate bricks-and-clicks infrastructures without intuitive partnerships\r</br>\r</br></span><del style="background:#ffe6e6;">Interactively implement exceptional human capital after team driven e-services. Energistically reinvent orthogonal meta-services before empowered strategic</del><ins style="background:#e6ffe6;">```python\r</br>class QuestionEdit(LoginRequiredMixin, RevisionMixin, FormView):\r</br> </ins><span> t</span><del style="background:#ffe6e6;">h</del><span>em</span><del style="background:#ffe6e6;">e areas. Appropriately orchestrate transparent experiences without scalable meta-services. Efficiently empower seamless best practices rather than extensive niche markets. Rapidiously transition future-proof manufactured products and empowered communities.</del><ins style="background:#e6ffe6;">plate_name = \'app_faq/question_edit.html\'\r</br> form_class = QuestionForm\r</br> model = Question\r</br>\r</br> def post(self, request, **kwargs):\r</br> reversion_set_comment("foo bar")\r</br> return super(QuestionEdit, self).post(request, **kwargs)\r</br>```</ins>', 'is_related': False, 'follow': None, 'field': <django.db.models.fields.TextField: description>}

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;

two

@jedie
Copy link
Owner

jedie commented Oct 2, 2017

It is possible to write a own compare for special fields. What does your model field look like?

@agusmakmun
Copy link
Author

agusmakmun commented Oct 2, 2017

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

@jedie
Copy link
Owner

jedie commented Oct 4, 2017

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants