<commit_before>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

from datetime import timedelta
from uuid import uuid4
import os

from django.conf import settings
from django.contrib import messages
from django.db.models import Avg, Count, Prefetch
from django.urls import reverse
from django.utils import timezone
from docx import Document

from utils import render_template, setting_handler, notify_helpers
from core import models as core_models, files
from review import models
from events import logic as event_logic
from submission import models as submission_models


def get_reviewer_candidates(article, user=None):
    """ Builds a queryset of candidates for peer review for the given article
    :param article: an instance of submission.models.Article
    :param user: The user requesting candidates who would be filtered out
    """
    review_assignments = article.reviewassignment_set.filter(review_round=article.current_review_round_object())
    reviewers = [review.reviewer.pk for review in review_assignments]
    if user:
        reviewers.append(user.pk)
    prefetch_review_assignment = Prefetch(
        'reviewer',
        queryset=models.ReviewAssignment.objects.filter(
            article__journal=article.journal
        ).order_by("-date_complete")
    )

    reviewers = article.journal.users_with_role('reviewer').exclude(
        pk__in=reviewers,
    ).prefetch_related(
        prefetch_review_assignment,
        'interest',
    ).filter(
        reviewer__reviewerrating__assignment__article__journal=article.journal,
    ).annotate(
        rating_average=Avg('reviewer__reviewerrating__rating'),
    ).annotate(
        active_reviews_count=Count('reviewer'),
    )

    return reviewers


def get_suggested_reviewers(article, reviewers):
    suggested_reviewers = []
    keywords = [keyword.word for keyword in article.keywords.all()]
    for reviewer in reviewers:
        interests = [interest.name for interest in reviewer.interest.all()]
        for interest in interests:
            if interest in keywords:
                suggested_reviewers.append(reviewer)
                break

    return suggested_reviewers


def get_assignment_content(request, article, editor, assignment):
    email_context = {
        'article': article,
        'editor': editor,
        'assignment': assignment,
    }

    return render_template.get_message_content(request, email_context, 'editor_assignment')<commit_msg>#1512: Swap review count to subquery for filtering<commit_after>__copyright__ = "Copyright 2017 Birkbeck, University of London"
__author__ = "<NAME> & <NAME>"
__license__ = "AGPL v3"
__maintainer__ = "Birkbeck Centre for Technology and Publishing"

from datetime import timedelta
from uuid import uuid4
import os

from django.conf import settings
from django.contrib import messages
from django.db.models import (
    Avg,
    Count,
    IntegerField,
    OuterRef,
    Prefetch,
    Subquery,
)

from django.urls import reverse
from django.utils import timezone
from docx import Document

from utils import render_template, setting_handler, notify_helpers
from core import models as core_models, files
from review import models
from events import logic as event_logic
from submission import models as submission_models


def get_reviewer_candidates(article, user=None):
    """ Builds a queryset of candidates for peer review for the given article
    :param article: an instance of submission.models.Article
    :param user: The user requesting candidates who would be filtered out
    """
    review_assignments = article.reviewassignment_set.filter(review_round=article.current_review_round_object())
    reviewers = [review.reviewer.pk for review in review_assignments]
    if user:
        reviewers.append(user.pk)
    prefetch_review_assignment = Prefetch(
        'reviewer',
        queryset=models.ReviewAssignment.objects.filter(
            article__journal=article.journal
        ).order_by("-date_complete")
    )
    active_reviews_count = models.ReviewAssignment.objects.filter(
        is_complete=False,
        reviewer=OuterRef("id"),
    ).annotate(
        rev_count=Count("pk"),
    ).values("rev_count")

    reviewers = article.journal.users_with_role('reviewer').exclude(
        pk__in=reviewers,
    ).prefetch_related(
        prefetch_review_assignment,
        'interest',
    ).annotate(
        active_reviews_count=Subquery(
            active_reviews_count,
            output_field=IntegerField(),
        )
    ).filter(
        reviewer__reviewerrating__assignment__article__journal=article.journal,
    ).annotate(
        rating_average=Avg('reviewer__reviewerrating__rating'),
    )

    return reviewers


def get_suggested_reviewers(article, reviewers):
    suggested_reviewers = []
    keywords = [keyword.word for keyword in article.keywords.all()]
    for reviewer in reviewers:
        interests = [interest.name for interest in reviewer.interest.all()]
        for interest in interests:
            if interest in keywords:
                suggested_reviewers.append(reviewer)
                break

    return suggested_reviewers


def get_assignment_content(request, article, editor, assignment):
    email_context = {
        'article': article,
        'editor': editor,
        'assignment': assignment,
    }

    return render_template.get_message_content(request, email_context, 'editor_assignment')
{'fraction_non_alphanumeric': 0.0747427333453692, 'fraction_numerical': 0.0028886080519949448, 'mean_word_length': 3.6632996632996635, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '725660', 'n_tokens_mistral': 1528, 'n_tokens_neox': 1487, 'n_words': 410}