def add_special_index(model_class, field_name, indexer, operator, value=None): from djangae.utils import in_testing from django.conf import settings index_type = indexer.prepare_index_type(operator, value) field_name = field_name.encode("utf-8") # Make sure we are working with strings load_special_indexes() if special_index_exists(model_class, field_name, index_type): return if environment.is_production_environment() or ( in_testing() and not getattr(settings, "GENERATE_SPECIAL_INDEXES_DURING_TESTING", False) ): raise RuntimeError( "There is a missing index in your djangaeidx.yaml - \n\n{0}:\n\t{1}: [{2}]".format( _get_table_from_model(model_class), field_name, index_type ) ) _project_special_indexes.setdefault( _get_table_from_model(model_class), {} ).setdefault(field_name, []).append(str(index_type)) write_special_indexes()
def fix_sandbox(): """ This is the nastiest thing in the world... This WSGI middleware is the first chance we get to hook into anything. On the dev_appserver at this point the Python sandbox will have already been initialized. The sandbox replaces stuff like the subprocess module, and the select module. As well as disallows _sqlite3. These things are really REALLY useful for development. So here we dismantle parts of the sandbox. Firstly we add _sqlite3 to the allowed C modules. This only happens on the dev_appserver, it would only die on live. Everything is checked so that changes are only made if they haven't been made already. """ if environment.is_production_environment(): return from google.appengine.tools.devappserver2.python import sandbox if '_sqlite3' not in sandbox._WHITE_LIST_C_MODULES: fix_c_whitelist() # Reload the system socket.py, because of bug #9246 import imp import os import ast psocket = os.path.join(os.path.dirname(ast.__file__), 'socket.py') imp.load_source('socket', psocket)
def deferred(request): from google.appengine.ext.deferred.deferred import ( run, SingularTaskFailure, PermanentTaskFailure ) response = HttpResponse() if 'HTTP_X_APPENGINE_TASKEXECUTIONCOUNT' in request.META: logger.debug("[DEFERRED] Retry %s of deferred task", request.META['HTTP_X_APPENGINE_TASKEXECUTIONCOUNT']) if 'HTTP_X_APPENGINE_TASKNAME' not in request.META: logger.critical('Detected an attempted XSRF attack. The header "X-AppEngine-Taskname" was not set.') response.status_code = 403 return response in_prod = environment.is_production_environment() if in_prod and os.environ.get("REMOTE_ADDR") != "0.1.0.2": logger.critical('Detected an attempted XSRF attack. This request did not originate from Task Queue.') response.status_code = 403 return response try: run(request.body) except SingularTaskFailure: logger.debug("Failure executing task, task retry forced") response.status_code = 408 except PermanentTaskFailure: logger.exception("Permanent failure attempting to execute task") return response
def middleware(request): if is_production_environment(): logging.warning( "local_iap_login_middleware is for local development only, " "and will not work on production. " "You should remove it from your MIDDLEWARE setting") response = get_response(request) elif request.path == "/_dj/login/": response = _login_view(request) elif request.path == "/_dj/logout/": if os.path.exists(_CREDENTIALS_FILE): os.remove(_CREDENTIALS_FILE) if REDIRECT_FIELD_NAME in request.GET: return HttpResponseRedirect(request.GET[REDIRECT_FIELD_NAME]) else: return HttpResponseRedirect("/_dj/login/") else: if os.path.exists(_CREDENTIALS_FILE): # Update the request headers with the stored credentials with open(_CREDENTIALS_FILE, "r") as f: data = f.read() email = data.strip() request.META[ "HTTP_X_GOOG_AUTHENTICATED_USER_ID"] = "auth.example.com:%s" % id_from_email( email) request.META[ "HTTP_X_GOOG_AUTHENTICATED_USER_EMAIL"] = "auth.example.com:%s" % email response = get_response(request) return response
def add_special_index(model_class, field_name, indexer, operator, value=None): from djangae.utils import in_testing from django.conf import settings index_type = indexer.prepare_index_type(operator, value) field_name = field_name.encode( "utf-8") # Make sure we are working with strings load_special_indexes() if special_index_exists(model_class, field_name, index_type): return if environment.is_production_environment() or ( in_testing() and not getattr( settings, "GENERATE_SPECIAL_INDEXES_DURING_TESTING", False)): raise RuntimeError( "There is a missing index in your djangaeidx.yaml - \n\n{0}:\n\t{1}: [{2}]" .format(_get_table_from_model(model_class), field_name, index_type)) _project_special_indexes.setdefault(_get_table_from_model(model_class), {}).setdefault(field_name, []).append( str(index_type)) write_special_indexes()
def load_special_indexes(): global _project_special_indexes global _app_special_indexes global _last_loaded_times global _indexes_loaded if _indexes_loaded and environment.is_production_environment(): # Index files can't change if we're on production, so once they're loaded we don't need # to check their modified times and reload them return def _read_file(filepath): # Load any existing indexes with open(filepath, "r") as stream: data = yaml.load(stream) return data project_index_file = _get_project_index_file() app_files = _get_app_index_files() files_to_reload = {} # Go through and reload any files that we find for file_path in [project_index_file] + app_files: if not os.path.exists(file_path): continue mtime = os.path.getmtime(file_path) if _last_loaded_times.get( file_path) and _last_loaded_times[file_path] == mtime: # The file hasn't changed since last time, so do nothing continue else: # Mark this file for reloading, store the current modified time files_to_reload[file_path] = mtime # First, reload the project index file, if project_index_file in files_to_reload: mtime = files_to_reload[project_index_file] _project_special_indexes = _read_file(project_index_file) _last_loaded_times[project_index_file] = mtime # Remove it from the files to reload del files_to_reload[project_index_file] # Now, load the rest of the files and update any entries for file_path in files_to_reload: mtime = files_to_reload[project_index_file] new_data = _read_file(file_path) _last_loaded_times[file_path] = mtime # Update the app special indexes list for model, indexes in new_data.items(): for field_name, values in indexes.items(): _app_special_indexes.setdefault(model, {}).setdefault( field_name, []).extend(values) _indexes_loaded = True logger.debug("Loaded special indexes for %d models", len(_merged_indexes()))
def load_special_indexes(): global _project_special_indexes global _app_special_indexes global _last_loaded_times global _indexes_loaded if _indexes_loaded and environment.is_production_environment(): # Index files can't change if we're on production, so once they're loaded we don't need # to check their modified times and reload them return def _read_file(filepath): # Load any existing indexes with open(filepath, "r") as stream: data = yaml.load(stream) return data project_index_file = _get_project_index_file() app_files = _get_app_index_files() files_to_reload = {} # Go through and reload any files that we find for file_path in [project_index_file] + app_files: if not os.path.exists(file_path): continue mtime = os.path.getmtime(file_path) if _last_loaded_times.get(file_path) and _last_loaded_times[file_path] == mtime: # The file hasn't changed since last time, so do nothing continue else: # Mark this file for reloading, store the current modified time files_to_reload[file_path] = mtime # First, reload the project index file, if project_index_file in files_to_reload: mtime = files_to_reload[project_index_file] _project_special_indexes = _read_file(project_index_file) _last_loaded_times[project_index_file] = mtime # Remove it from the files to reload del files_to_reload[project_index_file] # Now, load the rest of the files and update any entries for file_path in files_to_reload: mtime = files_to_reload[project_index_file] new_data = _read_file(file_path) _last_loaded_times[file_path] = mtime # Update the app special indexes list for model, indexes in new_data.items(): for field_name, values in indexes.items(): _app_special_indexes.setdefault( model, {} ).setdefault(field_name, []).extend(values) _indexes_loaded = True logger.debug("Loaded special indexes for %d models", len(_merged_indexes()))
def _get_authentication_credentials(): """ Returns authentication credentials depending on environment. See https://developers.google.com/api-client-library/python/auth/service-accounts """ if is_production_environment(): credentials = app_engine.Credentials(scopes=AUTH_SCOPES) else: service_account_path = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] credentials = service_account.Credentials.from_service_account_file( service_account_path, scopes=AUTH_SCOPES) return credentials
def middleware(request): request._through_local_iap_middleware = True if is_production_environment(): logging.warning( "local_iap_login_middleware is for local development only, " "and will not work on production. " "You should remove it from your MIDDLEWARE setting") response = get_response(request) elif request.path == "/_dj/login/": response = _login_view(request) elif request.path == "/_dj/logout/": if os.path.exists(_CREDENTIALS_FILE): os.remove(_CREDENTIALS_FILE) if REDIRECT_FIELD_NAME in request.GET: return HttpResponseRedirect(request.GET[REDIRECT_FIELD_NAME]) else: return HttpResponseRedirect("/_dj/login/") else: if os.path.exists(_CREDENTIALS_FILE): # Update the request headers with the stored credentials with open(_CREDENTIALS_FILE, "r") as f: data = f.readline() email = data.strip() # Set is_superuser according to the # data in the credentials file. data = f.readline() is_superuser = data.strip() == "True" defaults = dict( is_superuser=is_superuser, email=email, ) if is_superuser: defaults["is_staff"] = True user, _ = User.objects.update_or_create( google_iap_id=id_from_email(email), google_iap_namespace="auth.example.com", defaults=defaults) request.META[_GOOG_JWT_ASSERTION_HEADER] = "JWT TOKEN" request.META[ _GOOG_AUTHENTICATED_USER_ID_HEADER] = "auth.example.com:%s" % user.google_iap_id request.META[ _GOOG_AUTHENTICATED_USER_EMAIL_HEADER] = "auth.example.com:%s" % email response = get_response(request) return response
def _get_authentication_credentials(): """ Returns authentication credentials depending on environment. See https://developers.google.com/api-client-library/python/auth/service-accounts """ if is_production_environment(): credentials = app_engine.Credentials(scopes=AUTH_SCOPES) else: service_account_path = os.environ['GOOGLE_APPLICATION_CREDENTIALS'] credentials = service_account.Credentials.from_service_account_file( service_account_path, scopes=AUTH_SCOPES ) return credentials
def _get_storage_client(): """Gets an instance of a google CloudStorage Client Note: google storage python library depends on env variables read at module import time, so that should be set before import if overwrite needed """ http = None if not is_production_environment(): http = requests.Session() from google.cloud import storage return storage.Client( project=project_id(), _http=http, )
def get_public_url(self, name): """Gets the public URL of a blob Note: the public url is not guaranteed to be accessible. This depends on your bucket/object ACL and IAM. When using the gcs emulator, all objects are treated as publicly accessible Arguments: name {str} -- name of the blob Returns: str -- Public url """ if is_production_environment(): blob = self.bucket.blob(name) return blob.public_url else: return "{}/{}/{}".format(os.environ["STORAGE_EMULATOR_HOST"], self._bucket_name, name)
from .boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = 'project.settings.production' if is_production_environment() else 'project.settings' os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings) application = DjangaeApplication(get_wsgi_application())
import os from djangae import environment from .boot import AppConfig ON_PROD = environment.is_production_environment() BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) SECRET_KEY = AppConfig.get().secret_key DEBUG = not ON_PROD INTERNAL_IPS = ('127.0.0.1', '::1') INSTALLED_APPS = [ 'djangae', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'pasty', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'session_csrf.CsrfMiddleware', 'pasty.middleware.GoogleUserMiddleware', 'pasty.middleware.PastyVersionMiddleware', 'pasty.middleware.CSPHostnameMiddleware', 'csp.middleware.CSPMiddleware',
""" WSGI config for gae_resume project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from gae_resume.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "gae_resume.settings_live" if is_production_environment( ) else "gae_resume.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
import sys import os from os.path import dirname, abspath, join PROJECT_DIR = dirname(abspath(__file__)) SITEPACKAGES_DIR = join(PROJECT_DIR, "sitepackages") MIXINS_DIR = join(PROJECT_DIR, "mixins") if MIXINS_DIR not in sys.path: sys.path.insert(1, MIXINS_DIR) if SITEPACKAGES_DIR not in sys.path: sys.path.insert(1, SITEPACKAGES_DIR) from django.core.wsgi import get_wsgi_application from djangae.wsgi import DjangaeApplication from djangae.environment import is_production_environment settings = "settings_live" if is_production_environment() else "settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for djangae_react_blog project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from djangae_react_blog.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "djangae_react_blog.settings_live" if is_production_environment( ) else "djangae_react_blog.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for attract project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from attract.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "attract.settings_live" if is_production_environment( ) else "attract.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for wedding project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from wedding.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "wedding.settings_live" if is_production_environment() else "wedding.settings_local" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for {{ project_name }} project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from {{ project_name }}.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "{{ project_name }}.settings_live" if is_production_environment() else "{{ project_name }}.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for djangaevscode project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from djangaevscode.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "djangaevscode.settings_live" if is_production_environment( ) else "djangaevscode.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for py2lunch project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from project.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "project.settings_live" if is_production_environment() else "project.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for commenthead project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from commenthead.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "commenthead.settings_live" if is_production_environment( ) else "commenthead.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for StreamingUI project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from StreamingUI.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "StreamingUI.settings_live" if is_production_environment( ) else "StreamingUI.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for gae_resume project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from gae_resume.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "gae_resume.settings_live" if is_production_environment() else "gae_resume.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
def default(scopes=None): if is_production_environment(): return ServiceAccountCredentials(scopes=scopes) else: return google.auth.default(scopes=scopes)[0]
""" WSGI config for scaffold project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from scaffold.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "scaffold.settings_live" if is_production_environment() else "scaffold.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
""" WSGI config for slidesprodv2 project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from slidesprodv2.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "slidesprodv2.settings_live" if is_production_environment( ) else "slidesprodv2.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
def get_settings_name(env_name='local'): if is_production_environment(): env_name = SETTINGS_MAPPING[APPLICATION_NAME] return 'core.settings.' + env_name
""" WSGI config for translated_book project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.6/howto/deployment/wsgi/ """ from translated_book.boot import fix_path fix_path() import os from django.core.wsgi import get_wsgi_application from djangae.environment import is_production_environment from djangae.wsgi import DjangaeApplication settings = "translated_book.settings_live" if is_production_environment( ) else "translated_book.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())
def test_is_production_environment(self): self.assertFalse(is_production_environment()) os.environ["GAE_ENV"] = 'standard' self.assertTrue(is_production_environment()) del os.environ["GAE_ENV"]
from djangae.environment import is_production_environment FILE_CACHE_LOCATION = '/tmp/cache' if is_production_environment() else '.cache' CACHES = { # We default to the filesystem cache, since it's quick and easy for simple app # For larger application you should consider Cloud Memory Store (which does not have a free tier) 'default': { 'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache', 'LOCATION': FILE_CACHE_LOCATION, } } LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'filters': { 'require_debug_false': { '()': 'django.utils.log.RequireDebugFalse' } }, 'handlers': { 'mail_admins': { 'level': 'ERROR', 'filters': ['require_debug_false'], 'class': 'django.utils.log.AdminEmailHandler' } }, 'loggers': { 'django.request': { 'handlers': ['mail_admins'],
# http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software distributed # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR # CONDITIONS OF ANY KIND, either express or implied. See the License for the # specific language governing permissions and limitations under the License. ## """ WSGI config for subscribae project. It exposes the WSGI callable as a module-level variable named ``application``. For more information on this file, see https://docs.djangoproject.com/en/1.9/howto/deployment/wsgi/ """ from subscribae.boot import fix_path fix_path() import os # noqa from django.core.wsgi import get_wsgi_application # noqa from djangae.environment import is_production_environment # noqa from djangae.wsgi import DjangaeApplication # noqa settings = "subscribae.settings_live" if is_production_environment( ) else "subscribae.settings" os.environ.setdefault("DJANGO_SETTINGS_MODULE", settings) application = DjangaeApplication(get_wsgi_application())