Created
October 9, 2014 12:14
-
-
Save edgabaldi/7cadb7dd31f06837473d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from middleware import get_current_schema | |
class OtherRouter(object): | |
def db_for_read(self, model, **hints): | |
return get_current_schema() | |
def db_for_write(self, model, **hints): | |
return get_current_schema() | |
def allow_relation(self, obj1, obj2, **hints): | |
return None | |
def allow_migrate(self, db, model): | |
return False | |
class BaseRouter(object): | |
app_list = ['sessions'] | |
def db_for_read(self, model, **hints): | |
if model._meta.app_label in self.app_list: | |
return 'default' | |
return None | |
def db_for_write(self, model, **hints): | |
if model._meta.app_label in self.app_list: | |
return 'default' | |
return None | |
def allow_relation(self, obj1, obj2, **hints): | |
for app in self.app_list: | |
if obj1._meta.app_label == app or\ | |
obj2._meta.app_label == app: | |
return True | |
return None | |
def allow_migrate(self, db, model): | |
return False |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponseRedirect | |
from threading import local | |
_thread_locals = local() | |
def get_current_schema(): | |
schema = getattr(_thread_locals, 'schema', 'default') | |
return schema | |
class SchemaUpdateMiddleware(object): | |
def process_request(self, request): | |
try: | |
schema_name = request.session['schema'] | |
except KeyError: | |
schema_name = None | |
setattr(_thread_locals, 'schema', schema_name) | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DATABASE_ROUTERS = ['project.db_router.BaseRouter', 'project.db_router.OtherRouter'] | |
DATABASES = { | |
'default': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': 'website', | |
}, | |
'foo': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': 'foo', | |
}, | |
'bar': { | |
'ENGINE': 'django.db.backends.sqlite3', | |
'NAME': 'bar', | |
}, | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ProcessSchemaMixin(object): | |
def get(self, request, *args, **kwargs): | |
try: | |
self.schema = self.kwargs['schema'] | |
except KeyError: | |
raise Http404() | |
if self.schema not in settings.DATABASES: | |
messages.error(request, 'Schema não existe') | |
else: | |
request.session['schema'] = self.schema | |
return super(ProcessSchemaMixin, self).get(request, *args, **kwargs) | |
def post(self, request, *args, **kwargs): | |
try: | |
self.schema = self.kwargs['schema'] | |
except KeyError: | |
raise Http404() | |
if self.schema not in settings.DATABASES: | |
raise Http404() | |
return super(ProcessSchemaMixin, self).post(request, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment