mirror of
https://github.com/ae-utbm/sith.git
synced 2026-01-11 13:30:08 +00:00
add diff ratio to the heuristic
This commit is contained in:
@@ -43,15 +43,17 @@ class TestEditPage:
|
|||||||
user = baker.make(User, is_superuser=True)
|
user = baker.make(User, is_superuser=True)
|
||||||
page = baker.prepare(Page)
|
page = baker.prepare(Page)
|
||||||
page.save(force_lock=True)
|
page.save(force_lock=True)
|
||||||
first_rev = baker.make(PageRev, author=user, page=page, date=now())
|
first_rev = baker.make(
|
||||||
|
PageRev, author=user, page=page, date=now(), content="Hello World"
|
||||||
|
)
|
||||||
client.force_login(user)
|
client.force_login(user)
|
||||||
url = reverse("core:page_edit", kwargs={"page_name": page._full_name})
|
url = reverse("core:page_edit", kwargs={"page_name": page._full_name})
|
||||||
client.post(url, data={"content": "Hello World"})
|
client.post(url, data={"content": "Hello World!"})
|
||||||
assert page.revisions.count() == 1
|
assert page.revisions.count() == 1
|
||||||
assert page.revisions.last() == first_rev
|
assert page.revisions.last() == first_rev
|
||||||
first_rev.refresh_from_db()
|
first_rev.refresh_from_db()
|
||||||
assert first_rev.author == user
|
assert first_rev.author == user
|
||||||
assert first_rev.content == "Hello World"
|
assert first_rev.content == "Hello World!"
|
||||||
|
|
||||||
def test_pagerev_not_reused(self, client):
|
def test_pagerev_not_reused(self, client):
|
||||||
"""Test that a new revision is created if too much time
|
"""Test that a new revision is created if too much time
|
||||||
@@ -71,6 +73,7 @@ class TestEditPage:
|
|||||||
|
|
||||||
@pytest.mark.django_db
|
@pytest.mark.django_db
|
||||||
def test_page_revision(client: Client):
|
def test_page_revision(client: Client):
|
||||||
|
"""Test the GET to request to a specific revision page."""
|
||||||
page = baker.prepare(Page)
|
page = baker.prepare(Page)
|
||||||
page.save(force_lock=True)
|
page.save(force_lock=True)
|
||||||
page.view_groups.add(settings.SITH_GROUP_SUBSCRIBERS_ID)
|
page.view_groups.add(settings.SITH_GROUP_SUBSCRIBERS_ID)
|
||||||
|
|||||||
@@ -20,6 +20,7 @@
|
|||||||
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
# Place - Suite 330, Boston, MA 02111-1307, USA.
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
import difflib
|
||||||
import re
|
import re
|
||||||
from datetime import date, datetime, timedelta
|
from datetime import date, datetime, timedelta
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
@@ -385,29 +386,43 @@ class PageRevisionForm(forms.ModelForm):
|
|||||||
|
|
||||||
Notes:
|
Notes:
|
||||||
Saving this form won't always result in a new revision.
|
Saving this form won't always result in a new revision.
|
||||||
If the previous revision on the same page was made less
|
If the previous revision on the same page was made :
|
||||||
than 20 minutes ago by the same author,
|
|
||||||
|
- less than 20 minutes ago
|
||||||
|
- by the same author
|
||||||
|
- with a diff ratio higher than 20%
|
||||||
|
|
||||||
then the latter will be edited and the new revision won't be created.
|
then the latter will be edited and the new revision won't be created.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
UPDATE_THRESHOLD = timedelta(minutes=20)
|
TIME_THRESHOLD = timedelta(minutes=20)
|
||||||
|
DIFF_THRESHOLD = 0.2
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = PageRev
|
model = PageRev
|
||||||
fields = ["title", "content"]
|
fields = ["title", "content"]
|
||||||
widgets = {"content": MarkdownInput}
|
widgets = {"content": MarkdownInput}
|
||||||
|
|
||||||
def __init__(self, *args, author: User, page: Page, **kwargs):
|
def __init__(
|
||||||
super().__init__(*args, **kwargs)
|
self, *args, author: User, page: Page, instance: PageRev | None = None, **kwargs
|
||||||
|
):
|
||||||
|
super().__init__(*args, instance=instance, **kwargs)
|
||||||
self.author = author
|
self.author = author
|
||||||
self.page = page
|
self.page = page
|
||||||
|
self.initial_content = instance.content if instance else ""
|
||||||
|
|
||||||
|
def diff_ratio(self, new_str: str) -> float:
|
||||||
|
return difflib.SequenceMatcher(
|
||||||
|
None, self.initial_content, new_str
|
||||||
|
).quick_ratio()
|
||||||
|
|
||||||
def save(self, commit=True): # noqa FBT002
|
def save(self, commit=True): # noqa FBT002
|
||||||
revision: PageRev = self.instance
|
revision: PageRev = self.instance
|
||||||
if (
|
if (
|
||||||
revision._state.adding
|
revision._state.adding
|
||||||
or revision.author != self.author
|
or revision.author != self.author
|
||||||
or revision.date + self.UPDATE_THRESHOLD < now()
|
or revision.date + self.TIME_THRESHOLD < now()
|
||||||
|
or self.diff_ratio(revision.content) < (1 - self.DIFF_THRESHOLD)
|
||||||
):
|
):
|
||||||
revision.author = self.author
|
revision.author = self.author
|
||||||
revision.page = self.page
|
revision.page = self.page
|
||||||
|
|||||||
Reference in New Issue
Block a user