Plone/Python Create User Account - python

I have a Plone instance (4.2.2) running and need to automate the creation of user accounts. I'd really like to do this using an external Python script or some other Linux-based command line utility (such as "curl").
If I use curl, I can authenticate to gain access to the "##new_user" page, but I can't seem to get the right POST setup in the headers.
If I don't use curl and use a Python script instead, are there any utilities or libraries that can do this? I've tried using libraries such as Products.CMFCore.utils.getToolByName and getting the "portal_registration" - but I can't seem to get that to work in a regular script (one that has no request/context).
This script needs to run once every N minutes on the server (where the user information is grabbed from an external database). I also need there to be no password - and select the option to email the user to set their own password, and I need to add this user to a pre-defined group.
Are there any suggestions - perhaps another utility or built-in library that would better suite these requirements?

There are many scripts floating around for batch imports of users. A good one is Tom Lazar's CSV import http://plone.org/documentation/kb/batch-adding-users . Tom's script could be very easily adapted to run as a Zope "run" script if you need to run it from the command line, say as a cron job. "Run" scripts are scripts processed via command like "bin/client5 run my_script.py" using a Zope or ZEO client instance.
However, a strategy like this is one-way. It sound's from the conversation you had with Martijn like you might be better off by creating a Pluggable Authentication System (PAS) plugin to provide a connection to your external database source. The great thing about this strategy is that you can very precisely determine which user properties have which source (external DB or Plone membership properties manager). Docs (as MJ indicated) are at http://plone.org/documentation/manual/developer-manual/users-and-security/pluggable-authentication-service/referencemanual-all-pages .
Make sure to look and see if there is already a plugin written for your external data source. There are already plugins for many auth schemes and dbs (like LDAP and SQLAlchemy).

Related

Generate clients from Swagger definition programmatically

I have a Swagger JSON definition file. I would like to generate Python client binding for it. I would like to generate it as part of my development cycle, i.e., every time the definition changes I can run a script locally to regenerate my client
I am aware of Swagger Editor and Swagger Online Generators but they are no satisfying my needs:
both methods rely on an external service I need to call
as result I am getting .zip file I need to unzip - that makes whole process more complex.
I remember times when I could generate Java client binding for SOAP service running Apache CXF locally. Is there something like that for Swagger? I've seen there's an option to clone the whole swagger-codegen project, for all possible programming languages, but that seems like an overkill. Is there another option?
How companies are handling issue like mine?
I've used https://github.com/OpenAPITools/openapi-generator
You don't need to clone the whole repository, just download the JAR file and use it to generate the client/server.

GAE: Best practice for dynamically generated projects

Let's say I am creating a python-based CMS on GAE (similar to Squarespace/Shopify) which allows users to create a website.
The platform will (automatically?) create a subdomain for each new user and duplicates the application.
Now there are two options:
1) Create a new Database for the new user, WITHIN the master GAE project. (I'm worried that if one user gets a lot of traffic it might slow down ALL websites.)
2) Duplicate the entire project. (This method seems difficult to accomplish because either I have to manually create an instance of the application for each user, or I have to figure out how to hijack gcloud.py (or appcfg.py) somehow and store my login credentials in the code.)
Which choice will most likely provide the most performance for the price? Is choice 2 allowed by Google (or even possible)?
Edit:
I've done some more research about this, and it's not documented very much. I found this in the docs https://cloud.google.com/sdk/docs/scripting-gcloud which talks about running gcloud from scripts, although I don't think that means from python. I am looking into appengine-jenkins to see if it will work for my purpose. Let me know if you have any additional information about this.
Also, it seems like gcloud is adding a create command within the projects command which might be useful for me if I can figure out how to run gcloud from my script. https://cloud.google.com/sdk/gcloud/reference/alpha/projects/create

python server-side program to handle email?

What is the easiest way to write a simple Python daemon/server-side program that, in a reasonably secure way, processes incoming messages from an email account? For example, if you have an account 'foo#bar.org' and you have the username/password to the program, you want to be able to have the program read the contents of the email and save them to a database (e.g. with sqlite) in Python. What's the best framework/library for doing this? It sounds like it might be overkill to use Django for something so simple -- can it be done purely with the Python standard libraries?
There are python poplib (http://docs.python.org/2/library/poplib.html) and python imaplib (http://docs.python.org/2/library/imaplib.html). For accessing mailboxes.
Then you have lamson (http://lamsonproject.org/), which is not only excellent for sending and recieving mails. But it can also help you with parsing messages, detecting if they are spam or not - look into lamsons code to see exactly what you can do with it.
Then there are many examples of python daemons, which you can periodically run to pick up mails using poplib/imaplib and then save them somewhere using sqlalchemy or django or whatever.
OR you could skip python daemons and rather create small django project for doing all that. Combined with Celery (https://pypi.python.org/pypi/django-celery), you can create excellent daemonized backend for accessing mailbox via POP or IMAP and saving stuff to your own database.

GUI interface for sqlite data entry in Python

I am making a simple sqlite database for storing some non-sensitive client information. I am very familiar with python+sqlite and would prefer to stick with this combo on this project. I would like to create an simple GUI interface for data entry and searching of the database... something very similar to what MS Access provides. I want my wife to be able to enter/search data easily, so PHPmyadmin style things are out of the question.
I understand I could just give in and get MS Access, but if reasonbly possible would rather just write the code myself so it will run on my computers (*nix) and is flexible (so I can later integrate it with a web application and our smart phones.)
Can you developers recommend any interfaces/packages/etc (preferably pythonic) that can accomplish this with reasonable ease?
Thanks!
Since you're interested in future integration with a web application, you might consider using a Python web framework and running the app locally on your machine, using your web browser as the interface. In that case, one easy option would be web2py. Just download, unzip, and run, and you can use the web-based IDE (demo) to create a simple CRUD app very quickly (if you really want to keep it simple, you can even use the "New application wizard" (demo) to build the app). It includes its own server, so you can run your app locally, just like a desktop app.
You can use the web2py DAL (database abstraction layer) to define and create your SQLite database and tables (without writing any SQL). For example:
db = DAL('sqlite://storage.db')
db.define_table('customer',
Field('name', requires=IS_NOT_IN_DB(db, 'customer.name')),
Field('address'),
Field('email', requires=IS_EMAIL()))
The above code will create a SQLite database called storage.db and create a table called 'customer'. It also specifies form validators for the 'name' and 'email' fields, so whenever those fields are filled via a form, the entries will be validated ('name' cannot already be in the DB, and 'email' must be a valid email address format) -- if validation fails, the form will display appropriate error messages (which can be customized).
The DAL will also handle schema migrations automatically, so if you change your table definitions, the database schema will be updated (if necessary, you can turn off migrations completely or on a per table basis).
Once you have your data models defined, you can use web2py's CRUD system to handle all the data entry and searching. Just include these two lines (actually, they're already included in the 'welcome' scaffolding application):
from gluon.tools import Crud
crud = Crud(db)
And in a controller, define the following action:
def data():
return dict(form=crud())
That will expose a set of pre-defined URLs that will enable you to create, list, search, view, update, and delete records in any table.
Of course, if you don't like some of the default behavior, there are lots of ways to customize the CRUD forms/displays, or you can use some of web2py's other forms functionality to build a completely custom interface. And web2py is a full-stack framework, so it will be easy to add functionality to your app as your needs expand (e.g., access control, notifications, etc.).
Note, web2py requires no installation or configuration and has no dependencies, so it's very easy to distribute your app to other machines -- just zip up the entire web2py folder (which will include your app folder) and unzip it on another machine. It will run on *nix, Mac, and Windows (on Windows, you will either need to install Python or download the web2py Windows binary instead of the source version -- the Windows binary includes its own Python interpreter).
If you have any questions, there's a very helpful and responsive mailing list. You might also get some ideas from some existing web2py applications.
I normally use GTK+ that has well documented Python bindings.
The biggest advantage is that you can use a fairly intuitive GUI editor (Glade) and automagically link callbacks to events (to be honest most other major graphical toolkits have this possibility too, like for example QT, but my perception is that GTK+ enjoys wider adoption in the Python community). EDIT: additionally GTK is used by Gnome and many other desktop environments (KDE uses QT though).
That said, if all you need is truly just data insertion from a trusted person, you could use something already done like SQLite manager (it's a FireFox plugin).
Radically alternative solution: use django and you can literally pass from reading the tutorial to have your app up and running in a matter of hours, inclusive of user authentications, back-end administrative interface etc. (your project is exactly what I did with it to allow my wife to insert expenses in our family budget).
Django is written in Python and you can use SQLite as a back-end.

I18N Using Django/Python

Im looking to optimize our translation workflow for a django/python based project.
Currently we run a command to export our gettext files, send it to the translators, receive it back. Well you get the drill.
What in your opinion is the best way to optimize this workflow. Tools which integrate nicely and allow translations to be pushed and pulled from and to the system?
Options i've seen so far:
http://trac.transifex.org/ (supported in django 1.3)
Transifex was designed for pretty much this. It doesn't pull the strings from the project/app automatically yet, but it can be extended to do so if desired.
Transifex has two ways to automate this: If your POT file is on a public server, you can setup a resource to auto-fetch the POT file frequently and update your resource.
The second option is to use the client app and run it every time you build/deploy/commit.

Categories

Resources