signals.py 791 Bytes
Newer Older
1
2
3
4
5
6
7
8
from reversion.models import Version
import django.dispatch

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


def squashRevisionByUser(sender, obj, **kwargs):
    """
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
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
    """
    versions = Version.objects\
        .get_for_object(obj)\
        .select_related('revision')\
        .order_by('-revision__date_created')

    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


new_revision_saved.connect(
    squashRevisionByUser,
    dispatch_uid="receiver_concat_revisions"
)