# HG changeset patch # User Lennard de Rijk # Date 1239999997 0 # Node ID efa28a1ccf767ec999d4599ff8a20e3983e6c78b # Parent 6b424ec5b0ae2023bb7bcd35126e36e16247dcc9 Moved the code to retrieve all Proposal which should be accepted for one Organization to the Proposal Logic module. Patch by: Lennard de Rijk Reviewed by: to-be-reviewed diff -r 6b424ec5b0ae -r efa28a1ccf76 app/soc/logic/models/student_proposal.py --- a/app/soc/logic/models/student_proposal.py Fri Apr 17 19:54:27 2009 +0000 +++ b/app/soc/logic/models/student_proposal.py Fri Apr 17 20:26:37 2009 +0000 @@ -64,6 +64,60 @@ return ranker + def getProposalsToBeAcceptedForOrg(self, org_entity, stepsize=25): + """Returns all StudentProposals which will be accepted into the program + for the given organization. + + params: + org_entity: the Organization for which the proposals should be checked + stepsize: optional parameter to specify the ammount of Student Proposals + that should be retrieved per roundtrip to the datastore + + returns: + List with all StudentProposal which will be accepted into the program + """ + + # check if there are already slots taken by this org + fields = {'org': org_entity, + 'status': 'accepted'} + + query = self.getQueryForFields(fields) + + slots_left_to_assign = max(0, org_entity.slots - query.count()) + + if slots_left_to_assign == 0: + # no slots left so return nothing + return [] + + fields = {'org': org_entity, + 'status': 'pending'} + order = ['-score'] + + # get the the number of proposals that would be assigned a slot + query = self.getQueryForFields( + fields, order=order) + + proposals = query.fetch(slots_left_to_assign) + proposals = [i for i in proposals if i.mentor] + + offset = slots_left_to_assign + + # retrieve as many additional proposals as needed in case the top + # N do not have a mentor assigned + while len(proposals) < slots_left_to_assign: + new_proposals = query.fetch(stepsize, offset=offset) + + if not new_proposals: + # we ran out of proposals` + break + + new_proposals = [i for i in new_proposals if i.mentor] + proposals += new_proposals + offset += stepsize + + # cut off any superfluous proposals + return proposals[:slots_left_to_assign] + def _onCreate(self, entity): """Adds this proposal to the organization ranker entity. """ diff -r 6b424ec5b0ae -r efa28a1ccf76 app/soc/views/models/program.py --- a/app/soc/views/models/program.py Fri Apr 17 19:54:27 2009 +0000 +++ b/app/soc/views/models/program.py Fri Apr 17 20:26:37 2009 +0000 @@ -508,50 +508,16 @@ org_data['admin_name'] = org_admin.name() org_data['admin_email'] = org_admin.email - # check if there are already slots taken by this org - fields = {'org': org, - 'status': 'accepted'} + proposals = student_proposal_logic.logic.getProposalsToBeAcceptedForOrg( + org, stepsize=program_entity.max_slots) - query = student_proposal_logic.logic.getQueryForFields(fields) - - slots_left_to_assign = max(0, org.slots - query.count()) - - if slots_left_to_assign == 0: - # no slots left so next org + if not proposals: + # nothing to accept, next organization continue # store information about the org orgs_data[org.key().id_or_name()] = org_data - fields = {'org': org, - 'status': 'pending'} - order = ['-score'] - - # get the the number of proposals that would be assigned a slot - query = student_proposal_logic.logic.getQueryForFields( - fields, order=order) - - proposals = query.fetch(slots_left_to_assign) - proposals = [i for i in proposals if i.mentor] - - offset = slots_left_to_assign - stepsize = program_entity.max_slots - - # retrieve as many additional proposals as needed in case the top - # N do not have a mentor assigned - while len(proposals) < slots_left_to_assign: - new_proposals = query.fetch(stepsize, offset=offset) - # we ran out of proposals - if not new_proposals: - break - - new_proposals = [i for i in new_proposals if i.mentor] - proposals += new_proposals - offset += stepsize - - # cut off any superfluas proposals - del proposals[slots_left_to_assign:] - # store each proposal in the dictionary for proposal in proposals: student_entity = proposal.scope