1
\$\begingroup\$

I need to inject same variables into settings.py for multiple django apps.hence i wrote a module which takes in output of local() and modifies. This works but is it the right way to do things?

def enable_theme(theme_name, container):
"""
Enable the settings for a custom theme, whose files should be stored
in ENV_ROOT/themes/THEME_NAME (e.g., edx_all/themes/stanford).

The THEME_NAME setting should be configured separately since it can't
be set here (this function closes too early). An idiom for doing this
is:

   THEME_NAME = "stanford"
   enable_theme(THEME_NAME)
"""
    container["THEME_NAME"] = theme_name
    container['MITX_FEATURES']['USE_CUSTOM_THEME'] = True
    # Calculate the location of the theme's files
    theme_root = container['THEME_ROOT'] / "themes" / theme_name
    # Include the theme's templates in the template search paths
    container['TEMPLATE_DIRS'].append(theme_root / 'templates')
    container['THEME_SASS_DIRECTORY'] = theme_root / 'static/saas'
    container['MAKO_TEMPLATES']['main'].append(theme_root / 'templates')
    # Namespace the theme's static files to 'themes/<theme_name>' to
    # avoid collisions with default edX static files
    container['STATICFILES_DIRS'].append((u'themes/%s' % theme_name,
                                   theme_root / 'static'))
    container['FAVICON_PATH'] = 'themes/%s/images/favicon.ico' % theme_name

#Test case
from theming_utils.template_customizer import enable_theme
THEME_NAME = "sokratik"
PIPELINE = False
MITX_FEATURES['USE_DJANGO_PIPELINE'] = False
DEBUG = True
TEMPLATE_DEBUG = True
enable_theme(THEME_NAME, locals())
\$\endgroup\$
1
  • \$\begingroup\$ The code you posted doesn't use local(). Also, you need to format your code so that it shows up correctly. \$\endgroup\$ – unholysampler Sep 6 '13 at 13:15
1
\$\begingroup\$

You're not trying to modify a local environment, you're trying to modify the module's dictionary, which you can access by calling globals().

Note: the dictionary returned by locals() isn't meant to be modified.

\$\endgroup\$
2
  • \$\begingroup\$ that explains a bit of stuff, in case I am actually using globals, is it fine to modify globals in this way?? \$\endgroup\$ – himangshuj Sep 6 '13 at 19:29
  • \$\begingroup\$ It's okay to modify a module's dictionary, I don't know whether or not it's the right way to handle themes in Django. \$\endgroup\$ – Changaco Sep 6 '13 at 20:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.