def review_revision()

in kitsune/wiki/views.py [0:0]


def review_revision(request, document_slug, revision_id):
    """Review a revision of a wiki document."""
    rev = get_visible_revision_or_404(
        request.user,
        pk=revision_id,
        document__slug=document_slug,
        document__locale=request.LANGUAGE_CODE,
    )
    doc = rev.document

    if not doc.allows(request.user, "review_revision"):
        raise PermissionDenied

    form = ReviewForm(
        initial={
            "needs_change": doc.needs_change,
            "needs_change_comment": doc.needs_change_comment,
        }
    )

    # Don't ask significance if this doc is a translation or if it has no
    # former approved versions:
    should_ask_significance = not doc.parent and doc.current_revision

    based_on_revs = doc.revisions.all()
    last_approved_date = getattr(doc.current_revision, "created", datetime.fromordinal(1))
    based_on_revs = based_on_revs.filter(created__gt=last_approved_date)
    revision_contributors = list(set(based_on_revs.values_list("creator__username", flat=True)))

    # Get Unreviewed Revisions
    unreviewed_revisions = Revision.objects.filter(
        document=doc, is_approved=False, reviewed=None
    ).order_by("-id")

    # Get latest revision which is still not approved. Use **latest_revision_id** to get the id.
    try:
        latest_unapproved_revision = unreviewed_revisions[0]
        latest_unapproved_revision_id = latest_unapproved_revision.id
    except IndexError:
        latest_unapproved_revision_id = None

    # Get Current Revision id only if there is any approved revision.
    # Return None if there is no current revision
    if doc.current_revision is not None:
        current_revision_id = doc.current_revision.id
        unreviewed_revisions = unreviewed_revisions.exclude(id__lt=current_revision_id)
    else:
        current_revision_id = None

    # Get latest 5 unapproved revisions and exclude the revision which is being reviewed
    unreviewed_revisions = unreviewed_revisions.exclude(id=rev.id)[:5]

    # Don't include the reviewer in the recent contributors list.
    if request.user.username in revision_contributors:
        revision_contributors.remove(request.user.username)

    if request.method == "POST":
        form = ReviewForm(request.POST)
        if form.is_valid() and not rev.reviewed:
            # Don't allow revisions to be reviewed twice
            rev.is_approved = "approve" in request.POST
            rev.reviewer = request.user
            rev.reviewed = datetime.now()

            if should_ask_significance and form.cleaned_data["significance"]:
                rev.significance = form.cleaned_data["significance"]
            elif not should_ask_significance and not doc.parent:
                # This is a new document without approved revisions.
                # Significance is MAJOR.
                rev.significance = MAJOR_SIGNIFICANCE

            # If document is localizable and revision was approved and
            # user has permission, set the is_ready_for_localization value.
            if (
                doc.allows(request.user, "mark_ready_for_l10n")
                and rev.is_approved
                and rev.can_be_readied_for_localization()
            ):
                rev.is_ready_for_localization = form.cleaned_data["is_ready_for_localization"]

                # If the revision is ready for l10n, store the date
                # and the user.
                if rev.is_ready_for_localization:
                    rev.readied_for_localization = rev.reviewed
                    rev.readied_for_localization_by = rev.reviewer

            rev.save()

            # Update the needs change bit (if approved, default language and
            # user has permission).
            if (
                doc.locale == settings.WIKI_DEFAULT_LANGUAGE
                and doc.allows(request.user, "edit_needs_change")
                and rev.is_approved
            ):
                doc.needs_change = form.cleaned_data["needs_change"]

                # Remove comment if no changes are needed.
                if doc.needs_change:
                    doc.needs_change_comment = form.cleaned_data["needs_change_comment"]
                else:
                    doc.needs_change_comment = ""

                doc.save()

            # Send notifications of approvedness and readiness:
            if rev.is_ready_for_localization or rev.is_approved:
                ApprovedOrReadyUnion(rev).fire(exclude=[rev.creator, request.user])

            # Send an email (not really a "notification" in the sense that
            # there's a Watch table entry) to revision creator.
            msg = form.cleaned_data["comment"]
            send_reviewed_notification.delay(rev.id, doc.id, msg)
            based_on_revs_ids = based_on_revs.values_list("id", flat=True)
            send_contributor_notification(based_on_revs_ids, rev.id, doc.id, msg)

            render_document_cascade.delay(doc.id)

            return HttpResponseRedirect(reverse("wiki.document_revisions", args=[document_slug]))

    if doc.parent:  # A translation
        # For diffing the based_on revision against, to help the user see if he
        # translated all the recent changes:
        parent_revision = rev.based_on or doc.parent.localizable_or_latest_revision()
        template = "wiki/review_translation.html"
    else:
        parent_revision = None
        template = "wiki/review_revision.html"

    data = {
        "revision": rev,
        "document": doc,
        "form": form,
        "parent_revision": parent_revision,
        "revision_contributors": list(revision_contributors),
        "should_ask_significance": should_ask_significance,
        "latest_unapproved_revision_id": latest_unapproved_revision_id,
        "current_revision_id": current_revision_id,
        "unreviewed_revisions": unreviewed_revisions,
    }
    return render(request, template, data)