You are not logged in Log in Join
You are here: Home » Members » gregweb » Automatically Add Objects at CMF Member Creation Time

Log in
Name

Password

 

Automatically Add Objects at CMF Member Creation Time

Abstract

This How-To covers how to add default objects to a newly created members My Stuff area programmatically.

This How-To describes how doing this by hotfixing the CMFDefault/MembershipTool class.

This is only one way how to do it. Decide on your own if you like the hotfix method. Have also a look at Casey Duncan's How-To about hotfixing Zope.

Introduction

Every time a new member area is created the createMemberarea method of the CMFDefault.MembershipTool class is called.

The actual CMF (version 1.1, newer versions will possibly allow this by the click and pray :-) adds a index_html Document programatically which you possibly don't like beeing copied automatically.

Hotfixing a CMF Class Method (is easy!)

We replace the whole createMemberarea method by a customized one.

This way your customization is independant*) from the rest of the CMF code and you won't loose your changes by installing new versions of the CMF.

*) Ok, it doesn't give you 100% independancy. If the code in MembershipTool changes totaly it possibly breaks your hotfix and then you possibly have to adapt the changes in MembershipTool.

Seven Steps to Heaven

  1. Add a new folder in your Products folder. I named it CMFDefaultHotfix to signalize that it is a Hotfix for the CMFDefault Product.
  2. Put a __init__.py into the CMFDefaultHotfix folder with the code listed at the end of this How-To.
  3. Create a folder named img in the CMFDefaultHotfix folder and put a gif-picture into it. The code below assumes ist has the name earthsmile.gif.
  4. Create the optional version.txt, README.txt, CHANGES.txt, and INSTALL.txt files products normaly ship with.
  5. Restart Zope!
  6. Test it by adding a new member (join link in the action_box).
  7. Have fun!

Customizing the hotfix product

Now customize the part between ----- begin object instanations ----- and ----- end object instanations -----.

The type names of objects other than the images one can be viewed under /portal_types/manage_main of your CMF instance.

__init__.py

The code:

      """ Hotfix with altered createMemberarea
      """

      import os

      import Globals

      from Products.CMFDefault.MembershipTool import MembershipTool
      from Products.CMFDefault import Image

      # get the path of the Products folder
      product_path = os.path.join(Globals.package_home(globals()))

      def createMemberarea(self, member_id):
          """
          create a member area
          """
          parent = self.aq_inner.aq_parent
          members =  getattr(parent, 'Members', None)

          if members is not None and not hasattr(members, member_id):
              members.manage_addPortalFolder(member_id)
              f=getattr(members, member_id)

              # Grant ownership to Member
              acl_users = self.acl_users
              if not hasattr(acl_users, 'getUsers'):
                  # This hack works around the absence of getUsers() in LoginManager.
                  # Gets the PersistentUserSource object that stores our users
                  for us in acl_users.UserSourcesGroup.objectValues():
                      if us.meta_type == 'Persistent User Source':
                          acl_users = us.__of__(acl_users)
                          break

              user = acl_users.getUser(member_id).__of__(acl_users)
              f.changeOwnership(user)
              f.manage_setLocalRoles(member_id, ['Owner'])

              # ----- begin object instanations -----
              # Create default logo: open and read 'earthsmile.gif' in 'Products/img'
              fileobj = open(os.path.join(product_path, 'img', 'earthsmile.gif'), 'rb')
              filedata = fileobj.read()
              fileobj.close()

              # instanate a Image with the name 'org_logo'
              Image.addImage(f,
                             'org_logo',
                             title = '%s Home' % member_id,
                             file = filedata,
                             format = 'image/gif')

              # give the image the correct portal type
              ob = f._getOb( 'org_logo' )
              if hasattr(ob, '_setPortalTypeName'):
                  ob._setPortalTypeName('Image')
              # ----- end object instanations -----

              # Overcome an apparent catalog bug.
              f.org_logo.reindexObject()

      MembershipTool.createMemberarea = createMemberarea

Update 01.03.2003 (Thanks to Colin Leath):

for a TTW method to achieve the same thing, visit: http://www.zopelabs.com/cookbook/1046292285

About this document

This document will not be kept in-sync with the latest Zope CMF version by the current maintainer.

Version
  1. 1.0 - initial release
Author

Grégoire Weber zope@i-con.ch

Current Maintainer

Send questions and patches to Grégoire Weber zope@i-con.ch