An environment variable to transition towards removing global variables
This commit is contained in:
parent
15c86f3a57
commit
b88ca698dd
@ -42,6 +42,8 @@ from mediagoblin.tools.pluginapi import PluginManager, hook_transform
|
|||||||
from mediagoblin.tools.crypto import setup_crypto
|
from mediagoblin.tools.crypto import setup_crypto
|
||||||
from mediagoblin.auth.tools import check_auth_enabled, no_auth_logout
|
from mediagoblin.auth.tools import check_auth_enabled, no_auth_logout
|
||||||
|
|
||||||
|
from mediagoblin.tools.transition import DISABLE_GLOBALS
|
||||||
|
|
||||||
|
|
||||||
_log = logging.getLogger(__name__)
|
_log = logging.getLogger(__name__)
|
||||||
|
|
||||||
@ -150,9 +152,14 @@ class MediaGoblinApp(object):
|
|||||||
# certain properties need to be accessed globally eg from
|
# certain properties need to be accessed globally eg from
|
||||||
# validators, etc, which might not access to the request
|
# validators, etc, which might not access to the request
|
||||||
# object.
|
# object.
|
||||||
|
#
|
||||||
|
# Note, we are trying to transition this out;
|
||||||
|
# run with environment variable DISABLE_GLOBALS=true
|
||||||
|
# to work on it
|
||||||
#######################################################
|
#######################################################
|
||||||
|
|
||||||
setup_globals(app=self)
|
if not DISABLE_GLOBALS:
|
||||||
|
setup_globals(app=self)
|
||||||
|
|
||||||
# Workbench *currently* only used by celery, so this only
|
# Workbench *currently* only used by celery, so this only
|
||||||
# matters in always eager mode :)
|
# matters in always eager mode :)
|
||||||
@ -174,12 +181,7 @@ class MediaGoblinApp(object):
|
|||||||
# --------------
|
# --------------
|
||||||
|
|
||||||
# Is a context provided?
|
# Is a context provided?
|
||||||
if ctx is not None:
|
if ctx is None:
|
||||||
# Do special things if this is a request
|
|
||||||
if isinstance(ctx, Request):
|
|
||||||
ctx = self._request_only_gen_context(ctx)
|
|
||||||
|
|
||||||
else:
|
|
||||||
ctx = Context()
|
ctx = Context()
|
||||||
|
|
||||||
# Attach utilities
|
# Attach utilities
|
||||||
@ -192,6 +194,11 @@ class MediaGoblinApp(object):
|
|||||||
ctx.db = self.db
|
ctx.db = self.db
|
||||||
ctx.staticdirect = self.staticdirector
|
ctx.staticdirect = self.staticdirector
|
||||||
|
|
||||||
|
# Do special things if this is a request
|
||||||
|
# --------------------------------------
|
||||||
|
if isinstance(ctx, Request):
|
||||||
|
ctx = self._request_only_gen_context(ctx)
|
||||||
|
|
||||||
return ctx
|
return ctx
|
||||||
|
|
||||||
def _request_only_gen_context(self, request):
|
def _request_only_gen_context(self, request):
|
||||||
|
@ -19,8 +19,10 @@ from sqlalchemy.ext.declarative import declarative_base
|
|||||||
from sqlalchemy.orm import scoped_session, sessionmaker, object_session
|
from sqlalchemy.orm import scoped_session, sessionmaker, object_session
|
||||||
from sqlalchemy import inspect
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
Session = scoped_session(sessionmaker())
|
from mediagoblin.tools.transition import DISABLE_GLOBALS
|
||||||
|
|
||||||
|
if not DISABLE_GLOBALS:
|
||||||
|
Session = scoped_session(sessionmaker())
|
||||||
|
|
||||||
|
|
||||||
class GMGTableBase(object):
|
class GMGTableBase(object):
|
||||||
@ -28,7 +30,8 @@ class GMGTableBase(object):
|
|||||||
def _session(self):
|
def _session(self):
|
||||||
return inspect(self).session
|
return inspect(self).session
|
||||||
|
|
||||||
query = Session.query_property()
|
if not DISABLE_GLOBALS:
|
||||||
|
query = Session.query_property()
|
||||||
|
|
||||||
def get(self, key):
|
def get(self, key):
|
||||||
return getattr(self, key)
|
return getattr(self, key)
|
||||||
@ -38,9 +41,10 @@ class GMGTableBase(object):
|
|||||||
return getattr(self, key)
|
return getattr(self, key)
|
||||||
|
|
||||||
def save(self, commit=True):
|
def save(self, commit=True):
|
||||||
sess = object_session(self)
|
sess = self._session
|
||||||
if sess is None:
|
if sess is None and not DISABLE_GLOBALS:
|
||||||
sess = Session()
|
sess = Session()
|
||||||
|
assert sess is not None, "Can't save, %r has a detached session" % self
|
||||||
sess.add(self)
|
sess.add(self)
|
||||||
if commit:
|
if commit:
|
||||||
sess.commit()
|
sess.commit()
|
||||||
@ -49,7 +53,7 @@ class GMGTableBase(object):
|
|||||||
|
|
||||||
def delete(self, commit=True):
|
def delete(self, commit=True):
|
||||||
"""Delete the object and commit the change immediately by default"""
|
"""Delete the object and commit the change immediately by default"""
|
||||||
sess = object_session(self)
|
sess = self._session
|
||||||
assert sess is not None, "Not going to delete detached %r" % self
|
assert sess is not None, "Not going to delete detached %r" % self
|
||||||
sess.delete(self)
|
sess.delete(self)
|
||||||
if commit:
|
if commit:
|
||||||
|
21
mediagoblin/tools/transition.py
Normal file
21
mediagoblin/tools/transition.py
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
# GNU MediaGoblin -- federated, autonomous media hosting
|
||||||
|
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU Affero General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU Affero General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU Affero General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
# one global to disable them all
|
||||||
|
DISABLE_GLOBALS = os.environ.get("DISABLE_GLOBALS", "false").lower() == "true"
|
Loading…
x
Reference in New Issue
Block a user