def test_import_any_module(self): expected = import_module('math') actual = import_any_module(['invalid_one', 'math']) self.assertEqual(actual, expected) self.assertRaises(ImportError, import_any_module, ['invalid_one', 'invalid_2']) invalid = import_any_module(['invalid_one', 'invalid_2'], raise_error=False) self.assertIsNone(invalid)
def setup_databases(self, **kwargs): # import all models to populate orm.metadata for app in settings.INSTALLED_APPS: import_any_module(['%s.models' % app], raise_error=False) # determine which schemas we need default_schema = orm.engine.url.database schemas = set(t.schema or default_schema \ for t in Base.metadata.tables.values()) url = deepcopy(orm.engine.url) url.database = None self.engine = create_engine(url) insp = inspect(self.engine) # get a list of already-existing schemas existing_schemas = set(insp.get_schema_names()) # if any of the needed schemas exist, do not proceed conflicts = schemas.intersection(existing_schemas) if conflicts: for c in conflicts: print 'drop schema %s;' % c sys.exit('The following schemas are already present: %s. ' \ 'TestRunner cannot proceeed' % ','.join(conflicts)) # create schemas session = Session(bind=self.engine) for schema in schemas: session.execute(CreateSchema(schema)) session.commit() session.bind.dispose() # create tables if len(orm.Base.metadata.tables) > 0: orm.Base.metadata.create_all(checkfirst=False) # generate permissions call_command('createpermissions') return schemas
def setup_databases(self, **kwargs): # import all models to populate orm.metadata for app in settings.INSTALLED_APPS: import_any_module(['%s.models' % app], raise_error=False) # determine which schemas we need default_schema = orm.engine.url.database schemas = set(t.schema or default_schema \ for t in Base.metadata.tables.values()) # get a list of already-existing schemas new_url = str(orm.engine.url).rsplit('/',1)[0] self.engine = create_engine(new_url) conn = self.engine.connect() existing_schemas = set(self.engine.dialect.get_schema_names(conn)) # if any of the needed schemas exist, do not proceed conflicts = schemas.intersection(existing_schemas) if conflicts: import sys for c in conflicts: print 'drop schema %s;' % c sys.exit('The following schemas are already present: %s. ' \ 'TestRunner cannot proceeed' % ','.join(conflicts)) # create schemas for schema in schemas: self.engine.execute(CreateSchema(schema)) # create tables if len(orm.metadata.tables) > 0: orm.metadata.create_all() # generate permissions call_command('createpermissions') return schemas
def setup_databases(self, **kwargs): # import all models to populate orm.metadata for app in settings.INSTALLED_APPS: import_any_module(['%s.models' % app], raise_error=False) # determine which schemas we need default_schema = orm.engine.url.database schemas = set(t.schema or default_schema \ for t in Base.metadata.tables.values()) # get a list of already-existing schemas new_url = str(orm.engine.url).rsplit('/', 1)[0] self.engine = create_engine(new_url) conn = self.engine.connect() existing_schemas = set(self.engine.dialect.get_schema_names(conn)) # if any of the needed schemas exist, do not proceed conflicts = schemas.intersection(existing_schemas) if conflicts: import sys for c in conflicts: print 'drop schema %s;' % c sys.exit('The following schemas are already present: %s. ' \ 'TestRunner cannot proceeed' % ','.join(conflicts)) # create schemas for schema in schemas: self.engine.execute(CreateSchema(schema)) # create tables if len(orm.metadata.tables) > 0: orm.metadata.create_all() # generate permissions call_command('createpermissions') return schemas
"""Sphinx plugins for Django documentation.""" from baph.utils.importing import import_any_module from django.utils.importlib import import_module json = import_any_module(['json', 'simplejson', 'django.utils.simplejson']) from docutils import nodes, transforms import os from sphinx import addnodes, roles from sphinx.builders.html import StandaloneHTMLBuilder from sphinx.util.console import bold from sphinx.util.compat import Directive from sphinx.writers.html import SmartyPantsHTMLTranslator def setup(app): app.add_crossref_type(directivename="setting", rolename="setting", indextemplate="pair: %s; setting") app.add_crossref_type(directivename="templatetag", rolename="ttag", indextemplate="pair: %s; template tag") app.add_crossref_type(directivename="templatefilter", rolename="tfilter", indextemplate="pair: %s; template filter") app.add_crossref_type(directivename="fieldlookup", rolename="lookup", indextemplate="pair: %s; field lookup type") app.add_description_unit(directivename="django-admin", rolename="djadmin", indextemplate="pair: %s; django-admin command",
def handle_noargs(self, **kwargs): for app in settings.INSTALLED_APPS: import_any_module(["%s.models" % app], raise_error=False) if len(orm.metadata.tables) > 0: orm.metadata.create_all()
:mod:`baph.localflavor.generic.forms` -- Custom Generic "Local Flavor" Fields ============================================================================= The generic subpackage defines a :class:`CountryField` and a :class:`LanguageField` that you can use in your form models. They render as a Drop Down and uses the ISO-3166 code. Adapted from: http://code.djangoproject.com/ticket/5446/ Attachment: country_and_language_fields_trunk.4.patch ''' from baph.utils.importing import import_any_module, import_attr from django.conf import settings forms = import_any_module(['django.forms']) from django.utils.encoding import force_unicode from django.utils.html import escape, conditional_escape from django.utils.translation import ugettext_lazy as _ from itertools import chain COUNTRY_PROVINCES = [u'ar', u'ca', u'es', u'nl', u'za'] COUNTRY_STATES = [u'at', u'au', u'br', u'ch', u'de', u'in_', u'mx', u'us'] def _get_country_divisions(country, div_type): country_code = country.strip(u'_') module = 'django.contrib.localflavor.%s.%s_%ss' % \ (country, country_code, div_type) attr = '%s_CHOICES' % div_type.upper() return (country_code.upper(), import_attr([module], attr))
.. moduleauthor:: Mark Lee <*****@*****.**> .. moduleauthor:: JT Thibault <*****@*****.**> ''' from __future__ import absolute_import from baph.utils.importing import import_any_module, import_attr render_to_response = import_attr(['coffin.shortcuts'], 'render_to_response') from django.http import (HttpResponse, HttpResponseRedirect, HttpResponseForbidden) RequestContext = import_attr(['django.template'], 'RequestContext') from functools import wraps json = import_any_module(['json', 'simplejson', 'django.utils.simplejson']) def data_to_json_response(data, **kwargs): '''Takes any input and converts it to JSON, wraps it in an :class:`~django.http.HttpResponse`, and sets the proper MIME type. :param data: The data to be serialized. :param \*\*kwargs: extra keyword parameters given to :func:`json.dumps`. :rtype: :class:`~django.http.HttpResponse` ''' return HttpResponse(json.dumps(data, **kwargs), mimetype='application/json') def render_to_json(func, **json_kwargs):
def validate(self, app=None, display_num_errors=False): from django.conf import settings for app in settings.INSTALLED_APPS: print 'installing models from %s' % app import_any_module(['%s.models' % app], raise_error=False)
def handle_noargs(self, **kwargs): for app in settings.INSTALLED_APPS: import_any_module(['%s.models' % app], raise_error=False) if len(orm.metadata.tables) > 0: orm.metadata.create_all()
# -*- coding: utf-8 -*- ''' :mod:`baph.auth.registration.forms` -- Registration-related Forms ================================================================= Forms and validation code for user registration. ''' from baph.auth.models import User from baph.db.orm import ORM from baph.utils.importing import import_any_module dforms = import_any_module(['django.forms']) from django.utils.translation import ugettext_lazy as _ forms = import_any_module(['registration.forms']) Checkbox = dforms.CheckboxInput orm = ORM.get() TERMS_ERROR_MSG = _(u'You must agree to the terms to register') TERMS_LABEL = _(u'I have read and agree to the Terms of Service') class RegistrationForm(forms.RegistrationForm): '''An SQLAlchemy-based version of django-registration's ``RegistrationForm``. ''' def clean_username(self): '''Validate that the username is alphanumeric and is not already in use. ''' session = orm.sessionmaker()
# -*- coding: utf-8 -*- ''' :mod:`baph.auth.registration.forms` -- Registration-related Forms ================================================================= Forms and validation code for user registration. ''' from baph.auth.models import User from baph.db.orm import ORM from baph.utils.importing import import_any_module dforms = import_any_module(['django.forms']) from django.utils.translation import ugettext_lazy as _ forms = import_any_module(['registration.forms']) Checkbox = dforms.CheckboxInput orm = ORM.get() TERMS_ERROR_MSG = _(u'You must agree to the terms to register') TERMS_LABEL = _(u'I have read and agree to the Terms of Service') class RegistrationForm(forms.RegistrationForm): '''An SQLAlchemy-based version of django-registration's ``RegistrationForm``. ''' def clean_username(self): '''Validate that the username is alphanumeric and is not already in use. ''' session = orm.sessionmaker() user_ct = session.query(User) \