def create_database(self): if not self._errors: from django.db import connections from django.core.exceptions import ImproperlyConfigured from maker.core.domains import setup_domain_database database = { 'ENGINE' : self.cleaned_data['sql_engine'], 'NAME' : self.cleaned_data['sql_database'], 'USER' : self.cleaned_data['sql_user'], 'PASSWORD': self.cleaned_data['sql_password'], } #creates database settings.DATABASES['maker_new_db'] = database try: setup_domain_database('maker_new_db', True) except ImproperlyConfigured as exc: self._errors['sql_engine'] = self.error_class([_("Can't connect to engine. Error is ") + exc.message]) del self.cleaned_data['sql_engine'] except Exception as exc: del connections._connections['maker_new_db'] raise ValidationError(_("Can't create database. SQL error is") + ' %s' % exc) finally: del settings.DATABASES['maker_new_db'] #save database settings settings.DATABASES[router.db_for_read(ConfigSetting)] = database connections._connections.clear() if not getattr(settings, 'MAKER_MULTITENANCY', False): settings_filepath = sys.modules[ os.environ['DJANGO_SETTINGS_MODULE'] ].__file__ if settings_filepath.endswith('.pyc'): settings_filepath = settings_filepath[:-1] with open(settings_filepath, 'r') as fl: lines = fl.readlines() with open(settings_filepath, 'w') as fl: lines = iter(lines) for line in lines: if 'DATABASES' not in line: fl.write(line) else: fl.write('DATABASES = ') break fl.write(repr(settings.DATABASES)) fl.write('\n\n') for line in lines: if '=' in line: fl.write(line) break for line in lines: fl.write(line)
def process_request(self, request): "Identify the current domain and database, set up appropriate variables in the pandora box" domain = request.get_host().split('.')[0] try: setup_domain(domain) except DatabaseNotFound: evergreen_url = getattr(settings, 'EVERGREEN_BASE_URL', 'http://tree.io/') return HttpResponseRedirect(evergreen_url) except DatabaseError: from django.db import router from maker.core.models import ConfigSetting setup_domain_database(router.db_for_read(ConfigSetting)) return HttpResponseRedirect(reverse('database_setup')) box['request'] = request