def remove_demo_content(context): site = context print '### Removing demo content on "%s" ###' % site mt = getToolByName(site, "portal_membership") membersRemoved = 0 for member in site["people"].listFolderContents(): # leave this member alone if it's not demo-content if not IDemoContent.providedBy(member): continue mt.deleteMembers(member.id) member.aq_parent._delObject(member.id, suppress_events=True) membersRemoved += 1 groups_tool = getToolByName(site, "portal_groups") demo_users = groups_tool.getGroupById("demo-users") if demo_users is not None: groups_tool.removeGroup("demo-users") print "### %i members removed. ###" % membersRemoved catalog = getToolByName(site, "portal_catalog") dcBrains = list(catalog({"object_provides": IDemoContent.__identifier__})) objectsRemoved = 0 while dcBrains: dcBrain = dcBrains.pop() try: dc = dcBrain._unrestrictedGetObject() dc.aq_parent._delObject(dc.id, suppress_events=True) objectsRemoved += 1 except: pass catalog.clearFindAndRebuild() print "### %i objects removed. ###" % objectsRemoved
def add_demo_content( context, minUsers=1, maxUsers=1, minCallenges=1, maxChallenges=1, minWorkspacePerChallenge=1, maxWorkspacePerChallenge=1, minMembersPerWorkspace=1, maxMembersPerWorkspace=1, minPostPerWorkspace=1, maxPostPerWorkspace=1, ): site = context catalog = getToolByName(site, "portal_catalog") print '### Creating demo content on "%s" ###' % site # create a 'demo-users'-group group_id = "demo-users" groups_tool = getToolByName(site, "portal_groups") if not group_id in groups_tool.getGroupIds(): groups_tool.addGroup(group_id) # create users and add them to the 'demo-users'-group nUsers = random.randint(minUsers, maxUsers) for i in range(0, nUsers): first = get_word().title() last = get_word().title() uid = "_".join([first, last]).lower() email = "*****@*****.**" % (uid) passwd = "secret" portrait = None if random.choice([True, False]): portrait = namedfile.NamedBlobImage( random_image(random.randint(150, 300), random.randint(150, 300)), filename=u"%s.jpg" % (get_word()) ) add_cnrd_member(site, uid, last, first, email, passwd, portrait) groups_tool.addPrincipalToGroup(uid, group_id) print "### %i members created. ###" % nUsers # create challenges nChallenges = random.randint(minCallenges, maxChallenges) print " creating %i challenges..." % (nChallenges) challenges = [] for i in range(0, nChallenges): challenges.append( { "portal_type": "cnrd.Challenge", "title": get_words(1, 4), "description": get_words(4, 12), "icon": namedfile.NamedImage(random_image(150, 150), filename=u"nice_image.jpg"), "publish": False, "finalize": [challenge_finalizer, mark_as_democontent], } ) stats = build(challenges, site["challenges"]) print " %i created, %i modified, %i skipped, %i recursions. ###" % stats # create workspaces workspaces = [] for c in site["challenges"].listFolderContents(): # leave this challenge alone if it's not demo-content if not IDemoContent.providedBy(c): continue print "challenge '%s' with %i items" % (c.title, c.getNumWorkspaces()) nWorkspaces = random.randint(minWorkspacePerChallenge, maxWorkspacePerChallenge) print " creating %i workspaces..." % (nWorkspaces) # create some workspaces for i in range(0, nWorkspaces): usersIds = site["people"].keys() nWsMembers = random.randint( min(minMembersPerWorkspace, len(usersIds)), min(maxMembersPerWorkspace, len(usersIds)) ) date = DateTime() - (nWorkspaces - i) * random.randint(6, 66) workspaces.append( { "portal_type": "ixds.Workspace", "title": get_words(1, 4), "description": u"", "challenges": [relatify(c)], "members": set(random.sample(usersIds, nWsMembers)), "images": [ namedfile.NamedImage( random_image(random.randint(100, 400), random.randint(300, 800)), filename=u"%s.jpg" % (get_word()), ) for ii in range(0, random.randint(1, 2)) ], "previouslyEdited": True, "finalize": [update_workspace_image_fields, (set_dates, {"date": date}), mark_as_democontent], } ) stats = build(workspaces, site["workspaces"]) print " %i created, %i modified, %i skipped, %i recursions. ###" % stats # create posts posts = [] for ws in site["workspaces"].listFolderContents(): # leave this workspace alone if it's not demo-content if not IDemoContent.providedBy(ws): continue print "workspace '%s' with %i entries" % (ws.title, ws.getNumEntries()) nPosts = random.randint(minPostPerWorkspace, maxPostPerWorkspace) print " creating %i posts..." % (nPosts) for i in range(0, nPosts): nParagraphs = random.randint(1, 2) text = u"".join((u"<p>%s</p>" % p for p in get_paragraphs(nParagraphs))) delta = DateTime() - ws.created() date = ws.created() + delta * random.random() posts.append( { "portal_type": "ixds.Post", "title": get_words(1, 6), "text": richtextify(text), "parent": ws, "finalize": [ (set_dates, {"date": date}), (set_owner_and_creator, {"userid": random.choice(list(ws.getMemberIds()))}), ], } ) stats = build(posts) print " %i created, %i modified, %i skipped, %i recursions. ###" % stats print print "reindexing... ", catalog.manage_catalogRebuild() print "done."