示例#1
0
文件: base.py 项目: pooldin/pooldlib
 def create_campaign_association(self, campaign, user, role, pledge=None):
     ca = CampaignAssociation()
     ca.user_id = user.id
     ca.campaign_id = campaign.id
     ca.role = role
     if pledge is not None:
         ca.pledge = pledge
     self.commit_model(ca)
     return ca
示例#2
0
def associate_user(campaign, user, role, goal_participation, pledge=None):
    """Associate a user with a campaign filling a specified role.

    :param campaign: The campaign with which to associate the user.
    :type campaign: :class:`pooldlib.postgresql.models.Campaign`
    :param user: The user for which to create the association.
    :type user: :class:`pooldlib.postgresql.models.User`
    :param role: The role to assign the user (either `organizer` or `participant`)
    :type role: string
    :param goal_participation: The participation description for the campaign goals.
                               One of `participating`, `nonparticipating`, `opted-in` or `opted-out`
                               (See :func:`pooldlib.api.campaign.associate_user_with_goal`)
    :type goal_participation: string

    :raises: :class:`pooldlib.exceptions.InvalidUserRoleError`
             :class:`pooldlib.exceptions.DuplicateCampaignUserAssociationError`

    :return: :class:`pooldlib.postgresql.models.CampaignAssociation`
    """
    # NOTE :: We intentionally associate the user with all existing goals, not just
    # NOTE :: active ones. Date stamps can distinguish users who joined prior to goal
    # NOTE :: becoming inactive.
    campaign_goals = goals(campaign, filter_inactive=False)
    ca = CampaignAssociationModel()
    ca.enabled = True
    ca.campaign = campaign
    ca.user = user
    ca.role = role
    goal_pledge = None
    if pledge is not None:
        ca.pledge = pledge
        if campaign_goals:
            goal_pledge = pledge / len(campaign_goals)
    for goal in campaign_goals:
        associate_user_with_goal(goal,
                                 user,
                                 goal_participation,
                                 pledge=goal_pledge)

    # Check to see if this user was invited and mark them as accepted
    update_invitee = None
    for invitee in campaign.invitees:
        if invitee.user == user or invitee.email == user.email:
            invitee.accepted = pytz.UTC.localize(datetime.utcnow())
            invitee.user = user
            update_invitee = invitee

    with transaction_session() as session:
        session.add(ca)
        if update_invitee is not None:
            session.add(update_invitee)
        try:
            session.commit()
        except SQLAlchemyDataError:
            raise InvalidUserRoleError()
        except SQLAlchemyIntegrityError:
            raise DuplicateCampaignUserAssociationError()
    return ca
示例#3
0
文件: base.py 项目: pooldin/pooldlib
 def create_campaign_association(self, campaign, user, role, pledge=None):
     ca = CampaignAssociation()
     ca.user_id = user.id
     ca.campaign_id = campaign.id
     ca.role = role
     if pledge is not None:
         ca.pledge = pledge
     self.commit_model(ca)
     return ca
示例#4
0
def associate_user(campaign, user, role, goal_participation, pledge=None):
    """Associate a user with a campaign filling a specified role.

    :param campaign: The campaign with which to associate the user.
    :type campaign: :class:`pooldlib.postgresql.models.Campaign`
    :param user: The user for which to create the association.
    :type user: :class:`pooldlib.postgresql.models.User`
    :param role: The role to assign the user (either `organizer` or `participant`)
    :type role: string
    :param goal_participation: The participation description for the campaign goals.
                               One of `participating`, `nonparticipating`, `opted-in` or `opted-out`
                               (See :func:`pooldlib.api.campaign.associate_user_with_goal`)
    :type goal_participation: string

    :raises: :class:`pooldlib.exceptions.InvalidUserRoleError`
             :class:`pooldlib.exceptions.DuplicateCampaignUserAssociationError`

    :return: :class:`pooldlib.postgresql.models.CampaignAssociation`
    """
    # NOTE :: We intentionally associate the user with all existing goals, not just
    # NOTE :: active ones. Date stamps can distinguish users who joined prior to goal
    # NOTE :: becoming inactive.
    campaign_goals = goals(campaign, filter_inactive=False)
    ca = CampaignAssociationModel()
    ca.enabled = True
    ca.campaign = campaign
    ca.user = user
    ca.role = role
    goal_pledge = None
    if pledge is not None:
        ca.pledge = pledge
        if campaign_goals:
            goal_pledge = pledge / len(campaign_goals)
    for goal in campaign_goals:
        associate_user_with_goal(goal, user, goal_participation, pledge=goal_pledge)

    # Check to see if this user was invited and mark them as accepted
    update_invitee = None
    for invitee in campaign.invitees:
        if invitee.user == user or invitee.email == user.email:
            invitee.accepted = pytz.UTC.localize(datetime.utcnow())
            invitee.user = user
            update_invitee = invitee

    with transaction_session() as session:
        session.add(ca)
        if update_invitee is not None:
            session.add(update_invitee)
        try:
            session.commit()
        except SQLAlchemyDataError:
            raise InvalidUserRoleError()
        except SQLAlchemyIntegrityError:
            raise DuplicateCampaignUserAssociationError()
    return ca