• Remember Mailing List

  • Why fullname cannot be a unicode string?

    from Claudio Battaglino on Jul 29, 2008 11:33 AM
     This is the "register" method into the file remember/content/member.py (remember 1.0rc1):
    
    
        def register(self):
            """
            perform any registration information necessary after a member is registered
            """
            rtool = getToolByName(self, 'portal_registration')
            site_props = getToolByName(self, 'portal_properties').site_properties
    
            # XXX unicode names break sending the email
            unicode_name = self.getFullname()
            self.setFullname(str(unicode_name))
            if site_props.validate_email or self.getMail_me():
                rtool.registeredNotify(self.getUserName())
    
    
    Often, registering a new member with a script, I have this error: 
    
    ....
        * Module None, line 2, in registerAndSendEmails
          <PythonScript at /Plone/portal_workflow/eumember_approval_workflow/scripts/registerAndSendEmails>
          Line 2
        * Module Products.remember.content.member, line 635, in register
    
    UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 2: ordinal not in range(128)
    
    
    The error happens at this line: self.setFullname(str(unicode_name))
    
    Why is there a cast of the string "unicode_name" ?
    
    If I modify that line in this way: self.setFullname(unicode_name) I have no errors.
    According to you, is this modification right?
    
    
    claudio
    
    
    
    
    
    
    Thread Outline:
  • Re: Why fullname cannot be a unicode string?

    from ra on Jul 29, 2008 02:46 PM
    Claudio Battaglino wrote:
    > This is the "register" method into the file remember/content/member.py 
    > (remember 1.0rc1):
    > 
    > 
    >    def register(self):
    >        """
    >        perform any registration information necessary after a member is 
    > registered
    >        """
    >        rtool = getToolByName(self, 'portal_registration')
    >        site_props = getToolByName(self, 
    > 'portal_properties').site_properties
    > 
    >        # XXX unicode names break sending the email
    >        unicode_name = self.getFullname()
    >        self.setFullname(str(unicode_name))
    >        if site_props.validate_email or self.getMail_me():
    >            rtool.registeredNotify(self.getUserName())
    
    you left out the next line:
    
              self.setFullname(unicode_name)
    
    which sets the fullname back to its original unicode value.  considering this, 
    and the comment which says "unicode names break sending the email", it's clear 
    that this code is a bit of a hackish workaround.  if the fullname is a unicode 
    string, then something in the registeredNotify call can have problems when 
    using that fullname to send emails.
    
    you're right that this isn't very robust, though.  the code should probably 
    look something like this:
    
             reset_fullname = False
             unicode_name = self.getFullname()
             if type(unicode_name) == unicode:
                 self.setFullname(unicode_name.encode('utf-8', 'replace')
                 reset_fullname = True
             if site_props.validate_email or self.getMail_me():
                 rtool.registeredNotify(self.getUserName())
    
             if reset_fullname:
                 self.setFullname(unicode_name)
    
    try this out, see if it works for you.  i'm pretty sure it will... if so, i'll 
    work on writing a test that demonstrates the problem and committing this to 
    the remember trunk.  faster turnaround guaranteed if you (or anyone out there 
    in remember-list-land) writes and commits a failing test that demonstrates the 
    problem.
    
    -r