def _update(self, portal):

        context = portal.restrictedTraverse('who-who/destinet-users')
        cat = portal['portal_catalog']
        acl_users = portal['acl_users']
        users = acl_users.getUsers()
        auth_tool = portal.getAuthenticationTool()
        #manager = self.request.AUTHENTICATED_USER.getUserName()

        # first, cleanup wrongly created users
        wrong = [o for o in context.objectValues()
                 if o._owner[1] == 'tibiadmin']
        self.log.info("Deleting %s wrong contacts" % len(wrong))
        for obj in wrong:
            cat.uncatalog_object(ofs_path(obj))

        context.manage_delObjects([o.id for o in wrong])

        self.log.info("Migration: start migration of contacts for old users")
        EmailTool.divert_mail()

        counter = 0
        for user in users:
            fullname = auth_tool.getUserFullNameByID(user.name)
            contacts = cat.searchResults(path=ofs_path(context),
                                         contributor=user.name)

            if not contacts:
                counter += 1

                id = uniqueId(
                    slugify(user.name or 'contact', removelist=[]),
                    lambda x: context._getOb(x, None) is not None)

                ob = _create_NyContact_object(context, id, user.name)

                ob.approveThis(1, user.name)    # 1, manager
                ob.submitThis()

                ob.set_localpropvalue('title', 'en', fullname)
                ob.set_localpropvalue('description', 'en', "")
                #ob.release_date = DateTime()
                new_user = user.__of__(acl_users)
                ob.changeOwnership(user=new_user)
                ob.giveEditRights()

                context.recatalogNyObject(ob)
                #crashes with unicodedecodeerror:
                #notify(NyContentObjectAddEvent(ob, user.name, {}))

                #log post date
                auth_tool.changeLastPost(user.name)

                self.log.info("Migration: %s - added contact for user: %s",
                              counter, id)

        EmailTool.divert_mail(False)
        self.log.info("Migration: end migration")

        return True
Example #2
0
def process_create_account(context, request):
    """ """
    referer = request.HTTP_REFERER
    site = context.getSite()
    schema = site.getSchemaTool()['NyContact']
    register_schema = context.getSite().getSchemaTool()['registration']
    form_data, form_errors = validate_widgets(schema, register_schema, request.form)

    # if filling up the lower part, then the upper part is required as well
    if form_data['landscape_type'] and not form_data['topics']:
        form_errors['topics'] = ['Value is required for Topics']

    if form_errors:
        prepare_error_response(context, schema, register_schema, form_errors, request.form)
        # we need to put ourselves the user specific values in form
        args_for_session = {}
        for key in EW_REGISTER_FIELD_NAMES:
            args_for_session[key] = request.form.get(key)
        args_for_session['name'] = request.form.get('username')
        site.setRequestRoleSession(**args_for_session)
        request.RESPONSE.redirect(referer)
    else:
        # OBS: email is already sent here:
        real_comment = request.form.get('comments')
        if not real_comment:
            request.form['comments'] = " "
        site.processRequestRoleForm(request)
        redirect = request.RESPONSE.headers.get('location')
        if redirect != referer:
            # redirects to referer only when something is wrong in register form
            where = site['who-who']['destinet-users']
            username = request.form['username']
            contact_name = "%(firstname)s %(lastname)s" % request.form
            ob = _create_NyContact_object(where, username, username)
            ob.releasedate = site.utGetTodayDate()
            ob.approveThis(1, username)
            ob.submitThis()
            ob.giveEditRights()
            # voodoo for setting ownership using AccessControl.Owned API
            new_user = site.acl_users.getUser(username).__of__(site.acl_users)
            ob.changeOwnership(new_user)

            site.admin_addroles([username], ['Contributor'], '', send_mail=True)

            # hack to edit object without permissions (no auth)
            setattr(ob, 'checkPermissionEditObject', lambda: True)
            ob.manageProperties(title=contact_name,
                                firstname=request.form['firstname'],
                                lastname=request.form['lastname'],
                                organisation=request.form['organisation'],
                                approved=True,
                                description=real_comment,
                                **form_data)
            handle_groups(ob, request.form)
            delattr(ob, 'checkPermissionEditObject')
        else:
            # also call this to prefill values in form for contact
            prepare_error_response(context, schema, register_schema, form_errors, request.form)