from Products.CMFCore.utils import getToolByName

from Products.TeamSpace.relations import TeamMembershipRelation
from Products.TeamSpace.relations import MemberTeamRelation

def addMemberTeamReferences(self):
    rcat = getToolByName(self, 'reference_catalog')
    mshiprefbrains = rcat(relationship=TeamMembershipRelation.relationship)
    for mshiprefbrain in mshiprefbrains:
        mshipref = mshiprefbrain.getObject()
        mem = mshipref.getSourceObject()
        team = mshipref.getTargetObject().getTeam()
        mem.addReference(team, relationship=MemberTeamRelation.relationship,
                         referenceClass=MemberTeamRelation)

    ttool = getToolByName(self, 'portal_teams')
    ttool._addedMemberTeamReferences = True
