2

Is there a good way to store configuration settings for a web2py app?

I have written a little app that includes a script which looks up user attributes (names, email addresses, etc) from LDAP. Our corporate LDAP server requires a bind, before it allows a search for user details.

I want to share my app from a GitHub repository but not before I remove the credentials used for the bind.

Coming from a .Net background, I'm used to putting configuration like this into an app|web.config file. But this seems to be frowned on in web2py.

Whats a Pythonic or web2pyonic way of doing this?

def user_info(username):
    #todo: move these parameters to config
    ldap_host = 'example.com'
    ldap_port = 389
    ldap_base_dn = 'OU=DK,DC=example,DC=com'
    ldap_bind_dn = 'CN=<removed>,OU=DK,DC=example,DC=com'
    ldap_bind_pw = '<removed>'
    ldap_attr_uid = 'sAMAccountName'
    ldap_attr_forename = 'givenName'
    ldap_attr_surname = 'sn'
    ldap_attr_display_name = 'displayName'
    ldap_attr_department = 'department'
    ldap_attr_employee_type = 'employeeType'
    ldap_attr_email = 'mail'

    l = ldap.initialize('ldap://%s:%s' % (ldap_host, ldap_port))
    l.simple_bind_s(ldap_bind_dn, ldap_bind_pw)
    r = l.search_s(base=ldap_base_dn,
                   scope=ldap.SCOPE_SUBTREE,
                   filterstr='(%s=%s)' % (ldap_attr_uid, username),
                   attrlist=[
                       ldap_attr_forename,
                       ldap_attr_surname,
                       ldap_attr_email,
                       ldap_attr_display_name,
                       ldap_attr_employee_type,
                       ldap_attr_department])
    if r:
        dn, e = r[0]
        return {
            'dn': dn,
            'forename': e[ldap_attr_forename][0],
            'surname': e[ldap_attr_surname][0],
            'email': e[ldap_attr_email][0],
            'display_name': e[ldap_attr_display_name][0],
            'department': e[ldap_attr_department][0],
            'employee_type': e[ldap_attr_employee_type][0]}
    return None
Community
  • 1
  • 1
grenade
  • 31,451
  • 23
  • 97
  • 126
  • I'm not hugely familiar with web2py, but in Django this can be achieved by having a separate, unversioned `local_settings.py` file - e.g. http://stackoverflow.com/questions/4909958/django-local-settings. – will-hart Aug 16 '13 at 12:00

3 Answers3

2

There are different options, but one approach is just to put the settings in a module and import. In /yourapp/modules/ldap_settings.py:

ldap_host = 'example.com'
ldap_port = 389
...

And then in your function:

def user_info(username):
    from ldap_settings import *
    ...
Anthony
  • 25,466
  • 3
  • 28
  • 57
  • Thanks, seems like the most practical solution. I ended up with something similar to @will-hart's [linked](http://stackoverflow.com/a/14545196/68115) suggestion, which allows me to override the settings with an unversioned file. – grenade Aug 19 '13 at 07:28
2

I believe the proper web2py method is to use AppConfig module. Take a look at it in the web2py reference manual here: http://web2py.com/books/default/chapter/29/13/deployment-recipes#AppConfig-module

KEHT
  • 43
  • 3
0

As Keht mentioned AppConfig is the proper way to do it. The ini file is present in private folder. You can see that default email smtp settings are already there that are used by web2py for sending user verification emails/forgot password emails.

You can a entry here for your required key-value and then create the AppConfig object and read the required key.

mtk
  • 13,221
  • 16
  • 72
  • 112