[Zope-CMF] Re: How-To: Automatically Add Objects at CMF Member Creation Time

josh@pdxbands.com josh@pdxbands.com
Mon, 20 Aug 2001 20:37:17 -0700


Greg/Norman,

	I have been following this and am interested in achieving the same
solution.  Earlier I ask this same question a while back and received a g=
ood
solution from another user(Marc and Dan).  Below is the last email I
received with some sample code.  I have yet to figure out the last part o=
f
this as I can not seem to understand how to get the Afteradd to call the =
new
script.  If that was achieved then all the objects in the directory would
then automatically populate into the users home directory.  You could the=
n
make a template area and when you want to add a new default file for a us=
er
you just have to add it to the directory and presto it is added when a us=
er
is created.  It seems to me as an easier solution.

	Would either you or anyone else know how I call the script from
registertool.afteradd?

Thanks.  I would really like to see this added to the CMF core as it seem=
s
like a logical solution for many people.  I relate the creation of a user
similar to the creation of a user on a system.  In unix you have a skel d=
ir
that includes all the default files to be created when a new user is adde=
d.
Makes sense and makes the default user customizable very easily.

Josh

Code>>>>



Message: 5
Date: Thu, 02 Aug 2001 00:21:42 -0400
Subject: Re: [Zope-CMF] how to modify the add user method?
From: marc lindahl <marc@bowery.com>
To: <zope-cmf@zope.org>

I did this, and was going to put together a how-to, haven't had time yet.
But basically, in register.dtml, called this script
(createUserDefaults.pys), which I'll put below.  Modified registered.dtml
also, so that I could print a result string from the function (optional
geekiness).  Also, the .pys had to have a proxy role of owner in order to
work.  Here's the pythonscript (the lines will probably be wrapped in the
wrong place...):

#copy stuff from user_defaults to user folder, build default mypic image
#depends on folder user_defaults with everything there copied
#and an image user_default_mypic both in the same folder as this script
#
uf =3D container.portal_membership.getHomeFolder(username)

uf.manage_permission('Add Documents, Images, and Files', ['Anonymous',
'Member', 'Owner','Manager','Reviewer'], 1)
uf.manage_permission('Add portal content', ['Anonymous', 'Member',
'Owner','Manager','Reviewer'], 1)

obs =3D
container.user_defaults.manage_copyObjects(container.user_defaults.object=
Ids
())
uf.manage_pasteObjects(obs)

#uf.mypic.manage_setLocalRoles(username, ['Owner'])

#change this one back, since we're operating in a known, limited situatio=
n
uf.manage_permission('Add Documents, Images, and Files', [], 1)
uf.manage_permission('Add portal content', [], 1)

return 'Set up ' + `uf.getId()` +' with default content '+
`container.user_defaults.objectIds()`

> From: <josh@pdxbands.com>
> Date: Wed, 1 Aug 2001 19:59:04 -0700
> To: <dan@control.com>
> Cc: <zope-cmf@zope.org>
> Subject: Re: [Zope-CMF] how to modify the add user method?
>
> Dan,
>
> This would be a great idea.  That is, add in a module or class I can
modify
> to customize the ending effects.  I think the afteradd is a great way t=
o
> achieve this as I would like to add a default image and folder to the
users
> home directory that will have them as the owner.  I believe that will
happen
> if I make the call to create an object in the afteradd.  Is that correc=
t?
>
> Does your code that you have in the afteradd achieve this?  If so could
you
> give me a sample as I'm new to python and Zope.
>
> Thanks.
>
> Josh
>
>
> Message: 4
> Date: Wed, 01 Aug 2001 11:19:10 -0400
> From: "Dan L. Pierson" <dan@control.com>
> To: "zope-cmf@zope.org" <zope-cmf@zope.org>
> Subject: Re: [Zope-CMF] how to modify the add user method?
>
>
>
> --On Wednesday, August 01, 2001 07:56:59 AM -0400 Jens Vagelpohl
> <jens@zope.com> wrote:
>
>> - create your own Membership Tool by subclassing from
>> CMFDefault/MembershipTool.py - implement addMember in your new members=
hip
>> tool class with all the modifications you want - replace the default
>> membership tool in the CMF with your own
>>
>> that's the "clean and correct" way of doing it. for a code example loo=
k
>> at these files:
>>
>> http://cvs.dataflake.org:8080/CMFLDAP/
>
> There are two other hooks that may make this easier and, IMHO, just as
> clean and correct:
>
> 1. If all you need to add is additional member properties, the standard
> addMember processing calls member.setMemberProperties with *all* the
> properties from the registration form.  The default MemberDataTool turn=
s
> this into a call on MemberData.setMemberProperties (via a path I don't
> remember) which filters them to the properties declared in the
> MemberDataTool.
>
> This means that if you are using the default membership support you can
> define additional member properties by adding them to portal_memberdata=
's
> Properties tab and the registration form.
>
> If you are not using the default membership support you can make your
> member object (e.g. ZClass) implement setMemberProperties (this is what
> we do for LoginManager based members).
>
> 2. The last thing RegistrationTool.addMember does is call
> RegistrationTool.afterAdd.  This means that you can subclass
> CMFCore.RegistrationTool and define your own afterAdd method.
> We have one which simply calls a PythonScript skin method named
> new_member_actions to do any additional member processing.
> IMHO, this would be a good standard feature for a future CMF.
>

>

<<<< code


-----Original Message-----
From: zope-cmf-admin@zope.org [mailto:zope-cmf-admin@zope.org]On Behalf
Of Norman Khine
Sent: Monday, August 20, 2001 8:03 PM
To: Gr=E9goire Weber
Cc: Zope-CMF
Subject: RE: [Zope-CMF] Re: How-To: Automatically Add Objects at CMF
Member Creation Time


Hi Greg,
I looked into both sources and tried to make this work both ways, first:
I modified the CMFDefault/MembershipTool.py, by replacing, the original
entry for the creation of the index_html with:-

# Create index_html: open and read 'index_html.dtml' from 'Products/dtml'
  fileobj =3D open(os.path.join(product_path, 'dtml', 'index_html.dtml'),=
 'r')
  filedata =3D fileobj.read()
  fileobj.close()

  # Create Member's home page.
  # default_member_content ought to be configurable per
  # instance of MembershipTool.
  Document.addDocument( f
                      , 'index_html'
                      , member_id+"'s Home"
                      , member_id+"'s front page"
                      , "structured-text"
                      , (filedata % id)
                      )
  # give the image the correct portal type
  ob =3D f._getOb( 'org_logo' )
  if hasattr(ob, '_setPortalTypeName'):
      ob._setPortalTypeName('Document')
  # Overcome an apparent catalog bug.
  f.index_html.reindexObject()


but got an error on restarting the server

2001-08-21T01:13:12 ERROR(200) ZODB Couldn't load state for
'\x00\x00\x00\x00\x00\x00|B'
Traceback (innermost last):
  File /usr/home/dsuk5/zope/2.4.0/lib/python/ZODB/Connection.py, line 544=
,
in setstate
SystemError: Failed to import class _ZClass_for_DefaultDublinCoreImpl fro=
m
module Products.CMFDefault

9 times

So, reinstalling the original MemebrshipTool.py and adding the file creat=
ion
to the CMFDefaultHotfix __init__.py, which is listed below.

Thinking this will overide the existing index_html creation and pull in t=
he
file I have created in the CMFDefaultHotfix/dtml/index_html.dtml

but it does not, this brings the original index_html file created by the
MembershipTool.py, what am I doing wrong?

my index_html.dtml file is in CMFDefaultHotfix/dtml/ directory and
newtons.gif is in CMFDefaultHotfix/img/ directory

index_html.dtml

        <h1>Congratulations %s!</h1>
        <p>You've successfully joined blabla ...</p>
        <img src=3D"pic.gif" />

Thanks
Norman


CMFDefaultHotfix/__init__.py
""" 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 =3D os.path.join(Globals.package_home(globals()))



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

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

        # Grant ownership to Member
        acl_users =3D 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 =3D=3D 'Persistent User Source':
                    acl_users =3D us.__of__(acl_users)
                    break

        user =3D 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 =3D open(os.path.join(product_path, 'img', 'newtons.gif')=
,
'rb')
        filedata =3D fileobj.read()
        fileobj.close()
# instanate a Image with the name 'org_logo'
        Image.addImage(f,
                       'org_logo',
                        title =3D '%s Home' % member_id,
                        file =3D filedata,
                        format =3D 'image/gif')

        # give the image the correct portal type
        ob =3D f._getOb( 'org_logo' )
        if hasattr(ob, '_setPortalTypeName'):
            ob._setPortalTypeName('Image')
        # ----- end object instanations -----
# Overcome an apparent catalog bug.
        f.org_logo.reindexObject()

# Create index_html: open and read 'index_html.dtml' from 'Products/dtml'
        fileobj =3D open(os.path.join(product_path, 'dtml',
'index_html.dtml'), 'r')
        filedata =3D fileobj.read()
        fileobj.close()
# Create Member's home page.
# default_member_content ought to be configurable per
# instance of MembershipTool.
        Document.addDocument( f
                     , 'index_html'                # 1)
                     , member_id+"'s Home"
                     , member_id+"'s front page"
                     , "structured-text"           # 2)
                     , (filedata % id)             # 3)
                     )
  # give the index_html the correct portal type
        obj =3D f._getOb( 'index_html' )
        if hasattr(obj, '_setPortalTypeName'):
	    obj._setPortalTypeName('Document')
  # Overcome an apparent catalog bug.
        f.index_html.reindexObject()



_______________________________________________
Zope-CMF maillist  -  Zope-CMF@zope.org
http://lists.zope.org/mailman/listinfo/zope-cmf

See http://www.zope.org/Products/PTK/Tracker for bug reports and feature
requests