__squash_revision_by_user.py 987 Bytes
Newer Older
1
2
3
4
5
6
from reversion.models import Version
import django.dispatch

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


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

    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

27
28
29
30
31
    # 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()

32
33

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