from Products.CMFCore.utils import getToolByName

TEAMSPACES_FOLDER_DATA = {'id': 'teamspaces', 'title': 'TeamSpaces'}

teams_map = {'t1':{'title':'Team 1',},
             't2':{'title':'Team 2',},
             't3':{'title':'Team 3',},
             }

members_map = {'m1':{'fullname':'Member One',
                     'password':'testy',
                     'email':'notreal@xyxyxyxy.com',
                     'teams': ['t1', 't2']},
               'm2':{'fullname':'Member Two',
                     'password':'testy',
                     'email':'notreal@xyxyxyxy.com',
                     'teams': ['t2', 't3']},
               'm3':{'fullname':'Member Three',
                     'password':'testy',
                     'email':'notreal@xyxyxyxy.com',
                     'teams': ['t3', 't1']},
               }

def create_test_content(self):
    """ populates a site w/ some dummy test content """
    portal = getToolByName(self, 'portal_url').getPortalObject()
    mdc = getToolByName(self, 'portal_memberdata')
    tm_tool = getToolByName(self, 'portal_teams')
    wf_tool = getToolByName(self, 'portal_workflow')
    
    ts_folder_id = TEAMSPACES_FOLDER_DATA['id']
    tcontainer = getattr(portal, ts_folder_id, None)

    if tcontainer is None:
        portal.invokeFactory('Folder', ts_folder_id,
                             Title=TEAMSPACES_FOLDER_DATA['title'])
        tcontainer = getattr(portal, ts_folder_id, None)

    if tcontainer is None:
        return "ERROR: no '%s' folder" % ts_folder_id

    out = []
    for tm_id, tm_data in teams_map.items():
        tm_tool.invokeFactory('Team', tm_id)
        tm = tm_tool.getTeamById(tm_id)
        tm.update(**tm_data) # doing this in invokeFactory failed... why?
        out.append('Team %s added' % tm_id)
        tcontainer.invokeFactory('TeamSpace', tm_id, space_teams=(tm,),
                                 **tm_data)
        out.append('TeamSpace %s added' % tm_id)

    for mem_id, mem_data in members_map.items():
        mdc.invokeFactory('Member', mem_id, **mem_data)
        mem = mdc._getOb(mem_id)
        mem._setPassword(mem_data['password'])
        out.append('Member %s added' % mem_id)
        for tm_id in mem_data['teams']:
            team = tm_tool.getTeamById(tm_id)
            team.addMember(mem_id)
            out.append('-> added to team %s' % tm_id)
            mship = team.getMembershipByMemberId(mem_id)
            wf_tool.doActionFor(mship, 'activate')
            out.append('-> activated for team %s' % tm_id)

    return "\n".join(out)
