Creating app username with QR code

Hi all,

I want to create QR codes that automatically set the app username for the device using this QR code. The reason is that I want to use this username for setting a filter. As such, automatic setting of the app username will create less problems. Now, I have been using this example for creating a QR code: https://getodk.github.io/pyodk/examples/app_user_provisioner/app_user_provisioner/

It works fine. However, I do not see how I can modify this script to create an app user name. Any direction how to add the right python code is appreciated. Thanks!

The script you've linked should create App Users with provided names and then set the username setting in the QR code to that same value. It sounds like that's what you're trying to do. Is the username not set when you scan in the generated codes? Did I misunderstand what you're trying to do?

Hi @LN ,

That is what I want to do indeed: I want a specific username to appear in the phone (settings) so that the enumerators do not have to write a specific username themselves. I use the username as a filter, for example: "nigaragua - gerardo limes" to filter on the country nicaragua. I want to set this username in QR code so that this username is automatically created in the device of Gerardo Limes. This explanation just to make sure we understand each other well:-)

Well, the current script I am referring to does not do this. I do see the name appearing in the web environment (app users >> Display Name), but the username does not appear in the username of the device that scan's the QR code. Should this work? If so, How can I make this work? Thanks for helping!

Got it, that makes sense!

The script as provided is supposed to set the user name to the App User Display Name. It does this in the get_settings function. You can see that the function has a username parameter whose value is used as part of the QR code settings. That function gets called in the for loop that iterates over the provisioned users. The value passed in is user.displayName.

Did you go look in the Collect settings to verify that the username is blank? Keep in mind that if you use the script as-is, you'll need to provide the admin password (s00p3rs3cr3t by default) to see the username. You could also try a simple form that just tries to access and display the username to verify whether it's set.

I think I understand the script as it is. The username is added to web environment. The username also appears in the text below the QR code. However, when I scan the QR code with my phone and look into the settings of the phone, the username is empty. When I still try to fill in a form the filter is not working, so I suspect that (indeed) no username is set in the device (because it is empty).

Not sure what you mean with the admin password. I do not use admin password settings on my phone. If I use the s00p3rs3cr3t indeed (many of) the settings are fade out (no access) and reentering this password, the settings appear again.

I just remembered that there are, confusingly enough, two usernames that can be set in Collect. The script sets the username in the server settings, not the ones in the "User and device identity" settings. When filling out a form, the server username is used as a fallback when no user identity username is set so you should see it captured by a form that contains a username metadata question.

The script also sets an admin password of s00p3rs3cr3t which you could customize for your needs. One of the big reasons it sets an admin password is to hide the server settings so that they can't be modified by users. This gives you a very high level of confidence that the username is unchanged. Without an admin password and hiding server settings, the user could accidentally or deliberately change that username.

We will try to improve the confusing state around the double username.

1 Like

Thanks for clarifying. So it is not possible (at all) to set the username in the device settings or is it just a matter of modifying the script?

The username should be set by the script. It's the server username, though, so to see it you need to go to Settings > Server and look at the username field there. Do you see it there? It should also be set in a form that requests the username.

If you prefer setting the metadata username for some reason, you can do so by changing username in the settings json to metadata_username. All of the keys are documented at https://docs.getodk.org/collect-import-export/#list-of-keys-for-all-settings If you do this, you may want to hide the settings menu to change the metadata username so that it can't be changed by an end user.

1 Like

The username that I use in the script appears in the web environment as the "Display Name" and in the data submissions in the "Submitted by" column. So that seems to be generated correctly from the CSV. However, when I look at the username in Collect (so under Settings >> Server) the Username is still empty. Nothing there. Neither in the column "username" of a data submission. I really don't understand. Maybe we are communicating Babylonian:-) or there is a real error...? Or I should replace/add some code to the script? Maybe an example with an additional key setting (see https://docs.getodk.org/collect-import-export/#list-of-keys-for-all-settings ) in the example pyODK QR code script would help to clarify things a bit(?)

Are you using the QR codes in Central to configure your devices by any chance? The script should output a PDF with configuration QR codes as well as individual images for each line in users.csv. It does not change any settings on the server.

I just copied the script from the documentation and ran it with a file containing nicaragua - gerardo limes. When I scan in the generated code, I get the username in the server settings as I expect (I enter the admin password to see it).

If you're not seeing the PDF or images as expected, something must have failed when running the script.

Something to note is that you can't run the script multiple times with the same names -- the qr codes will only be generated once when the app user is first created.

I don't know if that would help! You can add any settings you want in the get_settings definition and then check to see whether they're set as expected when you scan the produced code.

That was it! Indeed I scanned the QR code on Central Server, not in the PDF.... Now I did and it works. So happy that it works now! Why didn't I tried this before... I should have because that was the part that I didn't understand in the code. Anyway, very very nice that this works now. Thank you so much for the quick feedbacks and help!

1 Like