Exemplo n.º 1
0
    def get_extra_context(self, request, instance):
        related_changes = ObjectChange.objects.restrict(
            request.user, 'view').filter(
                request_id=instance.request_id).exclude(pk=instance.pk)
        related_changes_table = tables.ObjectChangeTable(
            data=related_changes[:50], orderable=False)

        objectchanges = ObjectChange.objects.restrict(
            request.user, 'view').filter(
                changed_object_type=instance.changed_object_type,
                changed_object_id=instance.changed_object_id,
            )

        next_change = objectchanges.filter(
            time__gt=instance.time).order_by('time').first()
        prev_change = objectchanges.filter(
            time__lt=instance.time).order_by('-time').first()

        if not instance.prechange_data and instance.action in [
                'update', 'delete'
        ] and prev_change:
            non_atomic_change = True
            prechange_data = prev_change.postchange_data
        else:
            non_atomic_change = False
            prechange_data = instance.prechange_data

        if prechange_data and instance.postchange_data:
            diff_added = shallow_compare_dict(
                prechange_data or dict(),
                instance.postchange_data or dict(),
                exclude=['last_updated'],
            )
            diff_removed = {x: prechange_data.get(x)
                            for x in diff_added} if prechange_data else {}
        else:
            diff_added = None
            diff_removed = None

        return {
            'diff_added': diff_added,
            'diff_removed': diff_removed,
            'next_change': next_change,
            'prev_change': prev_change,
            'related_changes_table': related_changes_table,
            'related_changes_count': related_changes.count(),
            'non_atomic_change': non_atomic_change
        }
Exemplo n.º 2
0
    def get(self, request, pk):

        objectchange = get_object_or_404(self.queryset, pk=pk)

        related_changes = ObjectChange.objects.restrict(
            request.user, 'view').filter(
                request_id=objectchange.request_id).exclude(pk=objectchange.pk)
        related_changes_table = tables.ObjectChangeTable(
            data=related_changes[:50], orderable=False)

        objectchanges = ObjectChange.objects.restrict(
            request.user, 'view').filter(
                changed_object_type=objectchange.changed_object_type,
                changed_object_id=objectchange.changed_object_id,
            )

        next_change = objectchanges.filter(
            time__gt=objectchange.time).order_by('time').first()
        prev_change = objectchanges.filter(
            time__lt=objectchange.time).order_by('-time').first()

        if prev_change:
            diff_added = shallow_compare_dict(
                prev_change.object_data,
                objectchange.object_data,
                exclude=['last_updated'],
            )
            diff_removed = {
                x: prev_change.object_data.get(x)
                for x in diff_added
            }
        else:
            # No previous change; this is the initial change that added the object
            diff_added = diff_removed = objectchange.object_data

        return render(
            request, 'extras/objectchange.html', {
                'objectchange': objectchange,
                'diff_added': diff_added,
                'diff_removed': diff_removed,
                'next_change': next_change,
                'prev_change': prev_change,
                'related_changes_table': related_changes_table,
                'related_changes_count': related_changes.count()
            })