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

Show wiki diff #381

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ Pillow
redis
requests
sentry-sdk
beautifulsoup4
6 changes: 4 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# This file is autogenerated by pip-compile with python 3.10
# To update, run:
#
# pip-compile --output-file=requirements.txt requirements.in
# pip-compile
#
arrow==1.2.2
# via ics
Expand All @@ -17,7 +17,9 @@ attrs==21.4.0
backcall==0.2.0
# via ipython
beautifulsoup4==4.11.1
# via django-bootstrap4
# via
# -r requirements.in
# django-bootstrap4
bleach[css]==5.0.1
# via wiki
build==0.8.0
Expand Down
18 changes: 13 additions & 5 deletions wiki/forms.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
from django.forms import ModelForm, Textarea

from django import forms
from .models import Article


class ArticleForm(ModelForm):
class ArticleForm(forms.ModelForm):
class Meta:
model = Article
exclude = ['creator', 'last_modifier']

widgets = {
'content': Textarea(attrs={'rows': 20}),
'commit': Textarea(attrs={'rows': 3}),
'content': forms.Textarea(attrs={'rows': 20}),
'commit': forms.Textarea(attrs={'rows': 3}),
}


class DiffForm(forms.Form):
base_commit = forms.ModelChoiceField(queryset=Article.history.model.objects.all(), required=True, label="Version de base")
comp_commit = forms.ModelChoiceField(queryset=Article.history.model.objects.all(), required=True, label="Version à comparer")

# def __init__(self, *args, **kwargs):
# if 'article' in kwargs:
# article = kwargs.pop('article')
2 changes: 1 addition & 1 deletion wiki/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class Meta:
verbose_name = "Article"

def __str__(self):
return self.title
return f"{self.title} ({self.commit})"

def get_absolute_url(self):
return reverse('view_article', args=[self.pk])
Expand Down
9 changes: 8 additions & 1 deletion wiki/templates/article_detail.html
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,14 @@ <h1>{{article.title}}</h1>
</div>
</div>
{% endfor %}
{% endif %}
<form action="{% url "diff_article" %}" method="POST">
{% csrf_token %}
<div class="form-group">
{{ diff_form.as_p }}
</div>
<input class="btn btn-success" type="submit" value="Montrer la différence">
</form>
{% endif %}
</div>
</div>

Expand Down
83 changes: 83 additions & 0 deletions wiki/templates/diff_article.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
{% extends "base.html" %}
{% load bootstrap4 %}
{% load static %}

{% block title %}
Formulaire article
{% endblock %}

{% block head %}
{{ form.media }}
<!-- <link href="{% static "css/bootstrap-multiselect.css" %}" rel="stylesheet"> -->
<!-- <script type="text/javascript" src="{% static "js/bootstrap-multiselect.js" %}"></script> -->
<style>
.diff_add {
background-color: #aaffaa;
}

.diff_sub {
background-color: #ffaaaa;
}

.diff_chg {
background-color: #ffffaa;
}
</style>

{% endblock %}

{% block content %}
<h2>Différences:</h2>

<div class="row">
<div class="col-2 row-elem"></div>
<div class="col-2 row-elem">
<p>{{ old_article.commit }}</p>
</div>
<div class="col-4 row-elem"></div>
<div class="col-2 row-elem">
<p>{{ article.commit }}</p>
</div>
</div>

{{ delta | safe }}

<div class="row">
<div class="col-2 row-elem"></div>
<div class="col-2 row-elem">
<a class="btn btn-primary" href="{% url "view_old_version" initial_article.pk article.pk %}">
Voir cette version
</a>
</div>
<div class="col-4 row-elem"></div>
<div class="col-2 row-elem">
<a class="btn btn-primary" href="{% url "view_old_version" initial_article.pk old_article.pk %}">
Voir cette version
</a>
</div>
</div>

<h4 class="mt-5">Légende:</h4>
<table class="table table-sm">
<thead>
<tr>
<th scope="col" class="col-2">Couleur</th>
<th scope="col">Signification</th>
</tr>
</thead>
<tbody>
<tr>
<td style="background-color: #FFAAAA">(t)op</td>
<td>Supprimé</td>
</tr>
<tr>
<td style="background-color: #AAFFAA">(f)irst change</td>
<td>Ajouté</td>
</tr>
<tr>
<td style="background-color: #FFFF77">(n)ext change</td>
<td>Modifié</td>
</tr>
</tbody>
</table>
{% endblock %}
14 changes: 7 additions & 7 deletions wiki/urls.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from django.urls import path

from .views import (
wiki_home, ArticleDetailView, ArticleAddView, ArticleEditView, ArticleOldDetailView)
from wiki import views

urlpatterns = [
path('', wiki_home, name='wiki_home'),
path('add', ArticleAddView.as_view(), name='add_article'),
path('edit/<int:pk>', ArticleEditView.as_view(), name='edit_article'),
path('<int:pk>', ArticleDetailView.as_view(), name='view_article'),
path('<int:pl>/old/<int:pk>', ArticleOldDetailView.as_view(), name='view_old_version')
path('', views.wiki_home, name='wiki_home'),
path('diff', views.diff_article, name='diff_article'),
path('add', views.ArticleAddView.as_view(), name='add_article'),
path('edit/<int:pk>', views.ArticleEditView.as_view(), name='edit_article'),
path('<int:pk>', views.ArticleDetailView.as_view(), name='view_article'),
path('<int:pl>/old/<int:pk>', views.ArticleOldDetailView.as_view(), name='view_old_version')
]
41 changes: 40 additions & 1 deletion wiki/views.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
from difflib import HtmlDiff
from django.shortcuts import render
from django.views.generic.detail import DetailView
from django.views.generic import CreateView, UpdateView
from django.contrib.auth.mixins import LoginRequiredMixin
from django.utils import timezone
from actstream import action
from bs4 import BeautifulSoup

from .models import Article
from .forms import ArticleForm
from .forms import ArticleForm, DiffForm


def wiki_home(request):
Expand All @@ -21,6 +23,38 @@ def wiki_home(request):
})


def diff_article(request):
"""Compares two versions of an article."""
form = DiffForm()

if request.method == 'POST':
form_post = DiffForm(request.POST)
if form_post.is_valid():
old_article = form_post.cleaned_data['base_commit']
article = form_post.cleaned_data['comp_commit']

# Calculating the delta
delta = HtmlDiff(wrapcolumn=75).make_table(
old_article.content.split("\n"),
article.content.split("\n")
)
# Making the table pretty
table = BeautifulSoup(delta, features="html.parser")
table.table['class'] = table.table.get('class', []) + ["table table-stripped"]

return render(request, "diff_article.html", {
"delta": table.prettify(),
"initial_article": article.history_object,
"article": article,
"old_article": old_article,
"form": form,
})

return render(request, "diff_article.html", {
"form": form,
})


class ArticleAddView(LoginRequiredMixin, CreateView):
form_class = ArticleForm
template_name = 'add_article.html'
Expand Down Expand Up @@ -57,6 +91,11 @@ class ArticleDetailView(DetailView):
template_name = 'article_detail.html'
context_object_name = 'article'

def get_context_data(self, **kwargs):
context = super(ArticleDetailView, self).get_context_data(**kwargs)
context['diff_form'] = DiffForm

return context

class ArticleOldDetailView(DetailView):
model = Article.history.model
Expand Down