An environment variable to transition towards removing global variables

This commit is contained in:
Christopher Allan Webber 2014-11-30 12:49:26 -06:00
parent 15c86f3a57
commit b88ca698dd
3 changed files with 44 additions and 12 deletions

View File

@ -42,6 +42,8 @@ from mediagoblin.tools.pluginapi import PluginManager, hook_transform
from mediagoblin.tools.crypto import setup_crypto
from mediagoblin.auth.tools import check_auth_enabled, no_auth_logout
from mediagoblin.tools.transition import DISABLE_GLOBALS
_log = logging.getLogger(__name__)
@ -150,9 +152,14 @@ class MediaGoblinApp(object):
# certain properties need to be accessed globally eg from
# validators, etc, which might not access to the request
# 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
# matters in always eager mode :)
@ -174,12 +181,7 @@ class MediaGoblinApp(object):
# --------------
# Is a context provided?
if ctx is not None:
# Do special things if this is a request
if isinstance(ctx, Request):
ctx = self._request_only_gen_context(ctx)
else:
if ctx is None:
ctx = Context()
# Attach utilities
@ -192,6 +194,11 @@ class MediaGoblinApp(object):
ctx.db = self.db
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
def _request_only_gen_context(self, request):

View File

@ -19,8 +19,10 @@ from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker, object_session
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):
@ -28,7 +30,8 @@ class GMGTableBase(object):
def _session(self):
return inspect(self).session
query = Session.query_property()
if not DISABLE_GLOBALS:
query = Session.query_property()
def get(self, key):
return getattr(self, key)
@ -38,9 +41,10 @@ class GMGTableBase(object):
return getattr(self, key)
def save(self, commit=True):
sess = object_session(self)
if sess is None:
sess = self._session
if sess is None and not DISABLE_GLOBALS:
sess = Session()
assert sess is not None, "Can't save, %r has a detached session" % self
sess.add(self)
if commit:
sess.commit()
@ -49,7 +53,7 @@ class GMGTableBase(object):
def delete(self, commit=True):
"""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
sess.delete(self)
if commit:

View 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"