squash_revisions.py 986 Bytes
Newer Older
1
import django.dispatch
2
from reversion.models import Version
3
4


5
def squash_revision_by_user(sender, obj, **kwargs):
6
    """
7
    It should also work with moderation as obj will be a versioned object
8
    """
9
10
11
12
13
    versions = (
        Version.objects.get_for_object(obj)
        .select_related("revision")
        .order_by("-revision__date_created")
    )
14
15
16
17
18
19
20
21
22
23
24

    last_edit = versions[0]
    user_made_last_edit = last_edit.revision.user

    if user_made_last_edit is not None:
        for v in versions[1:]:
            if v.revision.user == user_made_last_edit:
                v.delete()
            else:
                break

25
26
27
28
29
    # We update the number of versions directly here
    # So that it doesn't have to be recomputed every time
    obj.nb_versions = len(Version.objects.get_for_object(obj))
    obj.save()

30

31
32
new_revision_saved = django.dispatch.Signal(providing_args=["obj"])

33
new_revision_saved.connect(
34
    squash_revision_by_user, dispatch_uid="receiver_concat_revisions"
35
)