[RESOLVED] How to programmatically create users without stopping a (standalone) instance?

I'd like to programmatically create users on a (standalone) Plone 5.2 site. I succeeded to do it with the interactive interpreter but, as specified:

The instance must be stopped in order to run this.

And the same happens for running scripts.

I noticed:

If [I] have a ZEO cluster [I] can have one ZEO client reserved for command line processing.

But how to programmatically create users without stopping my (standalone) instance?

https://docs.plone.org/develop/plone.api/docs/api/user.html ?

Yes, that's what I used, but I don't how to use it without stopping my (standalone) instance.

This will not work without stopping a standalone instance. Use ZEO instead or just stop the instance..these are your options.

For a standalone instance, you would need to let the Zope/Plone server perform the action to avoid stopping the server. This means that the server must be prepared for this.

One option would be to have a special view (e.g. "@@create_users") which you can activate as usual (for a view) via your browser. It would look somewhere for instructions which users it should create -- and do it. The view code itself could be quite similar to that you executed in the interactive interpreter (in fact simpler because you are already in a request and portal context).

OK, thanks.

I hope this solution for migrate from standalone to ZEO is still OK for Plone 5.2.

Dealing with views is currently beyond my scope but I'll read some documentation about this subject.

If I'm right, collective.mass_subscriptions typically makes use of a view, isn't it?

Views are really elementary to modern Zope/Plone versions. Almost everything (related to user interaction) uses views, including Plone itself. Therefore, reading view related documentation is a good investment.

@dbitouze plone.restapi has an endpoint for querying and creating users. Check

https://plonerestapi.readthedocs.io/en/latest/users.html

Thanks for the pointer. Unfortunately, I currently don't understand aaaanything to restapi :woozy_face: Do you know a nice introductory tutorial about the subject?

Seriously, the plone.restapi is heavily documented and full of examples.

I do not deny it. I just don't have the background in REST API.

Sorry but we are in 2019 and dealing with REST APIs over HTTP is a basic skill.
And said: read the plone.restapi docs...it contains enough examples.

There are probably several ways to do what you want.
Maybe you can explain your use case?

Personally, I would make a browser view if you plan to do this a lot.


If you (for some weird reason) have a list of uses that you want to add to a lot of sites, this can even be done in a profile ( I think ).

Although probably not a very 'correct way', at least it would be very easy to make.
Then you could just edit the xml file and reinstall it when you need new users.

You certainly right, but I'm not a professional on the subject and only accepted to be the administrator of the site of my technological institute.

Except that I even don't understand the first paragraphs of https://plonerestapi.readthedocs.io/en/latest/introduction.html:

The entry point to the Plone RESTful API is the portal root. The client can ask for a REST API response by setting the 'Accept' HTTP header to 'application/json' :
GET /plone HTTP/1.1 Accept: application/json Authorization: Basic YWRtaW46c2VjcmV0

I have to add about one hundred users and I would like to not do it by hand. Because my site is a Plone 5.2/python3 one, I guess I could not use collective.mass_subscriptions that in addition seems to have some issues.

I came across the nice way of programmatically create users given in my OP so I wanted to do it but my site is a standalone one and is already in production. Hence I'm reluctant to stopping it.

I plan to this not a lot, but regularly (each year when we have to add new students to our intra/extranet. I guess immersing myself in browser views would be less time consuming than in REST API. So I will probably go that way.

So, only for one site.

Thanks!

It worked great for me with 5.1. Have you tried it with 5.2 ?
There is not much code, so the best (for everyone) would be if it could be upgraded to 5.2.

I guess it won't since, with the following in the buildout.cfg file:

eggs =
    Plone
    Pillow
    collective.mass_subscriptions

I got:

$ ./bin/buildout 
Uninstalling zodbupdate.
Uninstalling unifiedinstaller.
Uninstalling zopepy.
Uninstalling instance.
Unused options for buildout: 'deprecation-warnings'.
Installing instance.
Getting distribution for 'collective.mass_subscriptions'.
warning: no files found matching '*.txt'
warning: no previously-included files matching '*pyc' found anywhere in distribution
  File "build/bdist.linux-x86_64/egg/collective/mass_subscriptions/browser/subscriptions.py", line 143
    except ValueError, inst:
                     ^
SyntaxError: invalid syntax

  File "/home/bitouze/test-plone-5-2-python3/buildout-cache/eggs/tmpfb33g1hg/collective.mass_subscriptions-0.1.1-py3.7.egg/collective/mass_subscriptions/browser/subscriptions.py", line 143
    except ValueError, inst:
                     ^
SyntaxError: invalid syntax

Got collective.mass-subscriptions 0.1.1.
[...]

About Python 2 to Python 3 migration, I am probably not the right to ask.

But could you try changing that line (143) to:

except ValueError as inst:

Me neither! :grinning:

Indeed, the add-on installs smoothly but it cannot be used:

Traceback (innermost last):
  Module ZPublisher.WSGIPublisher, line 155, in transaction_pubevents
  Module ZPublisher.WSGIPublisher, line 337, in publish_module
  Module ZPublisher.WSGIPublisher, line 255, in publish
  Module ZPublisher.mapply, line 85, in mapply
  Module ZPublisher.WSGIPublisher, line 61, in call_object
  Module collective.mass_subscriptions.browser.subscriptions, line 33, in __call__
  Module collective.mass_subscriptions.browser.subscriptions, line 106, in _importUsers
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)