def moderate_content()

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


def moderate_content(request):
    """Display flagged content that needs moderation."""
    product_slug = request.GET.get("product")
    assignee = request.GET.get("assignee")

    if (
        assignee
        and not User.objects.filter(
            is_active=True, username=assignee, groups__name="Content Moderators"
        ).exists()
    ):
        return HttpResponseNotFound()

    content_type = ContentType.objects.get_for_model(Question)
    objects = (
        get_flagged_objects(
            reason=FlaggedObject.REASON_CONTENT_MODERATION,
            content_model=content_type,
            product_slug=product_slug,
        )
        .select_related("content_type", "creator", "assignee")
        .prefetch_related("content_object__product", "content_object__tags")
    )

    if request.method == "POST":
        if not (assignee and (request.user.username == assignee)):
            return HttpResponseForbidden()

        action = request.POST.get("action")
        if not (action and (action in ("assign", "unassign"))):
            return HttpResponseBadRequest()

        if action == "assign":
            # Assign another batch of objects to the user.
            assigment_qs = objects.filter(assignee__isnull=True)[:20].values_list("id", flat=True)
            objects.filter(id__in=assigment_qs).update(
                assignee=request.user, assigned_timestamp=Now()
            )
        else:
            # Unassign all of the user's objects.
            objects.filter(assignee=request.user).update(assignee=None, assigned_timestamp=None)

    if assignee:
        objects = objects.filter(assignee__username=assignee)

    # It's essential that the objects are ordered for pagination. The
    # default ordering for flagged objects is by ascending created date.
    objects = paginate(request, objects)

    objects = set_form_action_for_objects(
        objects, reason=FlaggedObject.REASON_CONTENT_MODERATION, product_slug=product_slug
    )
    available_tags = SumoTag.objects.segmentation_tags().values("id", "name")

    product_topics_cache = {}
    unique_products = set()

    for obj in objects:
        question = obj.content_object
        if question.product:
            unique_products.add(question.product)

    for product in unique_products:
        product_topics_cache[product.id] = get_hierarchical_topics(product)

    for obj in objects:
        question = obj.content_object
        obj.available_topics = product_topics_cache.get(question.product.id, [])
        obj.available_tags = available_tags
        obj.saved_tags = question.tags.values_list("id", flat=True)

    return render(
        request,
        "flagit/content_moderation.html",
        {
            "objects": objects,
            "locale": request.LANGUAGE_CODE,
            "products": [
                (p.slug, p.title)
                for p in Product.active.filter(codename="", aaq_configs__is_active=True)
            ],
            "selected_product": product_slug,
            "assignees": sorted(
                (user.username, user.get_full_name() or user.username)
                for user in User.objects.filter(
                    is_active=True,
                    groups__name="Content Moderators",
                ).distinct()
            ),
            "selected_assignee": assignee,
            "current_username": request.user.username,
        },
    )