|
| 1 | +import collections |
| 2 | + |
| 3 | +from junction.base.constants import ProposalReviewVote, ProposalVotesFilter |
| 4 | +from junction.proposals.models import ProposalSection |
| 5 | + |
| 6 | + |
| 7 | +def _sort_proposals_for_dashboard(conference, proposals_qs, user, form): |
| 8 | + """ |
| 9 | + """ |
| 10 | + cps = form.cleaned_data['proposal_section'] |
| 11 | + cpt = form.cleaned_data['proposal_type'] |
| 12 | + votes = form.cleaned_data['votes'] |
| 13 | + review_status = form.cleaned_data['review_status'] |
| 14 | + |
| 15 | + proposal_sections = conference.proposal_sections.all() |
| 16 | + s_items = collections.namedtuple('section_items', 'section proposals') |
| 17 | + proposals = [] |
| 18 | + |
| 19 | + if cps != 'all': |
| 20 | + proposal_sections = ProposalSection.objects.filter(pk=cps) |
| 21 | + if cpt != 'all': |
| 22 | + proposals_qs = proposals_qs.filter(proposal_type__id__in=cpt) |
| 23 | + if votes != 'all': |
| 24 | + votes = int(votes) |
| 25 | + if review_status != 'all': |
| 26 | + proposals_qs = proposals_qs.filter(review_status=review_status) |
| 27 | + |
| 28 | + if votes == ProposalVotesFilter.NO_VOTES: |
| 29 | + proposals_qs = [ |
| 30 | + p for p in proposals_qs if p.get_reviewer_votes_count() == votes] |
| 31 | + elif votes == ProposalVotesFilter.MIN_ONE_VOTE: |
| 32 | + proposals_qs = [ |
| 33 | + p for p in proposals_qs if p.get_reviewer_votes_count() >= votes] |
| 34 | + elif votes == ProposalVotesFilter.SORT_BY_REVIEWER: |
| 35 | + proposals_qs = sorted( |
| 36 | + proposals_qs, |
| 37 | + key=lambda x: x.get_reviewer_vote_value(reviewer=user), |
| 38 | + reverse=True, |
| 39 | + ) |
| 40 | + elif votes == ProposalVotesFilter.SORT_BY_SUM: |
| 41 | + proposals_qs = sorted( |
| 42 | + proposals_qs, key=lambda x: x.get_reviewer_votes_sum(), |
| 43 | + reverse=True) |
| 44 | + proposals = [s_items('', proposals_qs)] |
| 45 | + |
| 46 | + elif votes == ProposalVotesFilter.SORT_BY_SELECTION: |
| 47 | + # Selection of proposal is based on conference guidelines. |
| 48 | + # More info is available at http://tiny.cc/qzo5cy |
| 49 | + |
| 50 | + proposals_qs = [p for p in proposals_qs if not p.has_negative_votes()] |
| 51 | + proposals_qs = sorted(proposals_qs, key=lambda x: x.get_reviewer_votes_sum(), reverse=True) |
| 52 | + |
| 53 | + selected = [p for p in proposals_qs if p.get_reviewer_votes_count_by_value(ProposalReviewVote.MUST_HAVE) > 1] |
| 54 | + proposals.append(s_items('Selected', selected)) |
| 55 | + |
| 56 | + batches = [] |
| 57 | + |
| 58 | + batch1 = [p for p in proposals_qs |
| 59 | + if p.get_reviewer_votes_count_by_value(ProposalReviewVote.MUST_HAVE) > 0 and |
| 60 | + p.get_reviewer_votes_count_by_value(ProposalReviewVote.GOOD) > 1] |
| 61 | + proposals.append(s_items('Batch 1', batch1)) |
| 62 | + batches += batch1 |
| 63 | + |
| 64 | + batch2 = [p for p in proposals_qs |
| 65 | + if p.get_reviewer_votes_count_by_value(ProposalReviewVote.MUST_HAVE) > 0 and |
| 66 | + p.get_reviewer_votes_count_by_value(ProposalReviewVote.GOOD) > 0 and |
| 67 | + p not in batches] |
| 68 | + proposals.append(s_items('Batch 2', batch2)) |
| 69 | + batches += batch2 |
| 70 | + |
| 71 | + batch3 = [p for p in proposals_qs |
| 72 | + if p.get_reviewer_votes_count_by_value(ProposalReviewVote.GOOD) > 1 and |
| 73 | + p not in batches] |
| 74 | + proposals.append(s_items('Batch 3', batch3)) |
| 75 | + batches += batch3 |
| 76 | + |
| 77 | + batch4 = [p for p in proposals_qs |
| 78 | + if p.get_reviewer_votes_count_by_value(ProposalReviewVote.GOOD) > 0 and |
| 79 | + p.get_reviewer_votes_count_by_value(ProposalReviewVote.NOT_BAD) > 1 and |
| 80 | + p not in batches] |
| 81 | + proposals.append(s_items('Batch 4', batch4)) |
| 82 | + |
| 83 | + # proposals = selected + batch1 + batch2 + batch3 + batch4 |
| 84 | + # unique_proposals = set() |
| 85 | + # proposals = [x for x in proposals if not (x in unique_proposals or unique_proposals.add(x))] |
| 86 | + # proposals = [s_items('', set(proposals))] |
| 87 | + |
| 88 | + if votes not in (ProposalVotesFilter.SORT_BY_SUM, ProposalVotesFilter.SORT_BY_SELECTION): |
| 89 | + print('f') |
| 90 | + for section in proposal_sections: |
| 91 | + section_proposals = [p for p in proposals_qs if p.proposal_section == section] |
| 92 | + proposals.append(s_items(section, section_proposals)) |
| 93 | + |
| 94 | + return proposals |
0 commit comments