blob: 34239776a62ca224eb6e0169f285ceb65bcbd7f5 [file] [log] [blame]
# Copyright 2014 the Melange authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Logic for profiles."""
from google.appengine.ext import ndb
from django.utils import translation
from melange.models import profile as profile_model
from soc.logic import mail_dispatcher
from summerofcode.logic import proposal as proposal_logic
from summerofcode.models import proposal as proposal_model
_DEF_ACCEPTED_STUDENT_WELCOME_EMAIL_SUBJECT = translation.ugettext(
'Welcome to %s')
def queryAcceptedStudentsForProgram(program_key):
"""Returns a query instance that fetches all profiles of students who are
accepted for the specified program.
Args:
program_key: Program key.
Returns:
A query instance that fetches all profiles of students who are accepted for
the specified program.
"""
program_key = ndb.Key.from_old_key(program_key)
return profile_model.Profile.query(
profile_model.Profile.program == program_key,
profile_model.Profile.is_student == True,
profile_model.Profile.student_data.number_of_projects > 0)
def hasProject(profile):
"""Tells whether the specified profile has at least one project assigned.
Args:
profile: Profile entity.
Returns:
True if the profile is a student and has at least one project;
False otherwise.
"""
return profile.is_student and bool(profile.student_data.number_of_projects)
def getProfilesForNewProposalNotification(org_key):
"""Returns profiles that should be notified when a new proposal is submitted
to the specified organization.
Args:
org_key: ndb.Key of organization entity.
Returns:
A list of profile_model.Profile entities.
"""
# NOTE: This approach may not be sufficient for organization with
# hundreds of members.
query = profile_model.Profile.query(
profile_model.Profile.mentor_for == org_key,
profile_model.Profile.notification_settings.org_new_proposals == True)
return query.fetch(1000)
def getProfilesForNewProposalCommentNotification(org_key, author_key):
"""Returns profiles of organization members that should be notified when
a new comment is added for a proposal.
Args:
org_key: ndb.Key of organization entity.
author_key: profile_model.Profile entity of the author of the comment.
Returns:
A list of profile_model.Profile entities.
"""
# NOTE: This approach may not be sufficient for organization with
# hundreds of members.
query = profile_model.Profile.query(
profile_model.Profile.mentor_for == org_key,
profile_model.Profile.notification_settings.org_proposal_comments == True)
return [profile for profile in query if profile.key != author_key]
def setToBeAcceptedProposals(profile_key):
"""Sets value for to_be_accepted_proposals property for the specified profile.
Args:
profile_key: ndb.Key of the profile.
"""
all_proposals = proposal_model.Proposal.query(
proposal_model.Proposal.status == proposal_model.Status.PENDING,
ancestor=profile_key).fetch(1000)
organizations = ndb.get_multi(
set(proposal.organization for proposal in all_proposals))
to_be_accepted_proposals = []
for organization in organizations:
to_be_accepted_proposals.extend(
proposal for proposal in
proposal_logic.getProposalsToBeAcceptedForOrg(organization)
if proposal.key.parent() == profile_key)
@ndb.transactional
def _setToBeAcceptedProposalsTxn(to_be_accepted_proposals):
profile = profile_key.get()
# return gracefully for non student profiles
if profile.is_student:
profile.student_data.to_be_accepted_proposals = [
proposal.key for proposal in to_be_accepted_proposals]
profile.put()
_setToBeAcceptedProposalsTxn(to_be_accepted_proposals)
def getProfilesWithDuplicates(program_key):
"""Returns profiles who have duplicates for the specified program according
to the value defined by has_duplicates property.
Args:
program_key: db.Key of the program.
Returns:
A list of profile_model.Profile entities who have duplicates.
"""
return profile_model.Profile.query(
profile_model.Profile.program == ndb.Key.from_old_key(program_key),
profile_model.Profile.status == profile_model.Status.ACTIVE,
profile_model.Profile.student_data.has_duplicates == True).fetch(1000)
def dispatchAcceptedStudentWelcomeEmail(
student, program, program_messages, site):
"""Dispatches a task to sent accepted student welcome email to the specified
student for the specified program.
Args:
student: profile_model.Profile entity of the accepted student.
program: program_model.Program entity.
program_messages: program_model.ProgramMessages entity.
site: site_model.Site entity.
"""
sender_name, sender = mail_dispatcher.getDefaultMailSender(site=site)
context = {
'to': student.contact.email,
'to_name': student.public_name,
'sender': sender,
'sender_name': sender_name,
'program_name': program.name,
'subject': _DEF_ACCEPTED_STUDENT_WELCOME_EMAIL_SUBJECT % program.name,
}
template_string = program_messages.accepted_students_welcome_msg
mail_dispatcher.getSendMailFromTemplateStringTxn(
template_string, context, parent=student)()