Skip to content

Commit

Permalink
Fix part of oppia#5002: Remove DEV_MODE from GLOBALS. (oppia#5618)
Browse files Browse the repository at this point in the history
* Move DEV_MODE to constants.js

* Fix frontend tests

* Fix frontend tests

* Fix backend tests

* Remove FORCE_PROD_MODE from feconf

* Remove PLATFORM constant from codebase

* Edit exception text and comment in feconf

* Fix linting

* Add line back

* Move GAE_PLATFORM to module level

* Remove check for SERVER_SOFTWARE env variable
  • Loading branch information
vojtechjelinek authored and seanlip committed Sep 13, 2018
1 parent 954eaaf commit 1b66c8d
Show file tree
Hide file tree
Showing 31 changed files with 110 additions and 133 deletions.
4 changes: 3 additions & 1 deletion assets/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -500,5 +500,7 @@ var constants = {
}
},

"CURRENT_STATES_SCHEMA_VERSION": 25
"CURRENT_STATES_SCHEMA_VERSION": 25,

"DEV_MODE": true
};
7 changes: 4 additions & 3 deletions core/controllers/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import logging
import random

from constants import constants
from core import jobs
from core import jobs_registry
from core.controllers import base
Expand Down Expand Up @@ -195,7 +196,7 @@ def post(self):
raise

def _reload_exploration(self, exploration_id):
if feconf.DEV_MODE:
if constants.DEV_MODE:
logging.info(
'[ADMIN] %s reloaded exploration %s' %
(self.user_id, exploration_id))
Expand All @@ -206,7 +207,7 @@ def _reload_exploration(self, exploration_id):
raise Exception('Cannot reload an exploration in production.')

def _reload_collection(self, collection_id):
if feconf.DEV_MODE:
if constants.DEV_MODE:
logging.info(
'[ADMIN] %s reloaded collection %s' %
(self.user_id, collection_id))
Expand All @@ -230,7 +231,7 @@ def _generate_dummy_explorations(
Exception: Environment is not DEVMODE.
"""

if feconf.DEV_MODE:
if constants.DEV_MODE:
logging.info(
'[ADMIN] %s generated %s number of dummy explorations' %
(self.user_id, num_dummy_exps_to_generate))
Expand Down
7 changes: 4 additions & 3 deletions core/controllers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import traceback
import urlparse

from constants import constants
from core.domain import config_domain
from core.domain import config_services
from core.domain import rights_manager
Expand Down Expand Up @@ -90,7 +91,7 @@ def get(self):
url_to_redirect_to = str(self.request.get('return_url') or '/')
_clear_login_cookies(self.response.headers)

if feconf.DEV_MODE:
if constants.DEV_MODE:
self.redirect(users.create_logout_url(url_to_redirect_to))
else:
self.redirect(url_to_redirect_to)
Expand Down Expand Up @@ -222,7 +223,7 @@ def dispatch(self):

# In DEV_MODE, clearing cookies does not log out the user, so we
# force-clear them by redirecting to the logout URL.
if feconf.DEV_MODE and self.partially_logged_in:
if constants.DEV_MODE and self.partially_logged_in:
self.redirect(users.create_logout_url(self.request.uri))
return

Expand Down Expand Up @@ -318,7 +319,7 @@ def render_template(
values.update({
'BEFORE_END_HEAD_TAG_HOOK': jinja2.utils.Markup(
BEFORE_END_HEAD_TAG_HOOK.value),
'DEV_MODE': feconf.DEV_MODE,
'DEV_MODE': constants.DEV_MODE,
'DOMAIN_URL': '%s://%s' % (scheme, netloc),
'ACTIVITY_STATUS_PRIVATE': (
rights_manager.ACTIVITY_STATUS_PRIVATE),
Expand Down
8 changes: 2 additions & 6 deletions core/controllers/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,13 @@ def setUp(self):
def test_dev_indicator_appears_in_dev_and_not_in_production(self):
"""Test dev indicator appears in dev and not in production."""

with self.swap(feconf, 'DEV_MODE', True):
with self.swap(constants, 'DEV_MODE', True):
response = self.testapp.get(feconf.LIBRARY_INDEX_URL)
self.assertIn('DEV_MODE: JSON.parse(\'true\')',
response.body)
self.assertIn('<div ng-if="DEV_MODE" class="oppia-dev-mode">',
response.body)

with self.swap(feconf, 'DEV_MODE', False):
with self.swap(constants, 'DEV_MODE', False):
response = self.testapp.get(feconf.LIBRARY_INDEX_URL)
self.assertIn('DEV_MODE: JSON.parse(\'false\')',
response.body)
self.assertIn('<div ng-if="DEV_MODE" class="oppia-dev-mode">',
response.body)

Expand Down
5 changes: 3 additions & 2 deletions core/controllers/classifier.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import hmac
import json

from constants import constants
from core.controllers import base
from core.domain import acl_decorators
from core.domain import classifier_services
Expand Down Expand Up @@ -100,7 +101,7 @@ def post(self):
signature = self.payload.get('signature')
message = self.payload.get('message')
vm_id = self.payload.get('vm_id')
if vm_id == feconf.DEFAULT_VM_ID and not feconf.DEV_MODE:
if vm_id == feconf.DEFAULT_VM_ID and not constants.DEV_MODE:
raise self.UnauthorizedUserException

if not validate_job_result_message_dict(message):
Expand Down Expand Up @@ -148,7 +149,7 @@ def post(self):
vm_id = self.payload.get('vm_id')
message = self.payload.get('message')

if vm_id == feconf.DEFAULT_VM_ID and not feconf.DEV_MODE:
if vm_id == feconf.DEFAULT_VM_ID and not constants.DEV_MODE:
raise self.UnauthorizedUserException
if not verify_signature(message, vm_id, signature):
raise self.UnauthorizedUserException
Expand Down
5 changes: 3 additions & 2 deletions core/controllers/classifier_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
import os

from constants import constants
from core.controllers import classifier
from core.domain import classifier_services
from core.domain import exp_services
Expand Down Expand Up @@ -119,7 +120,7 @@ def test_trained_classifier_handler(self):

def test_error_on_prod_mode_and_default_vm_id(self):
# Turn off DEV_MODE.
with self.swap(feconf, 'DEV_MODE', False):
with self.swap(constants, 'DEV_MODE', False):
self.post_json(
'/ml/trainedclassifierhandler', self.payload,
expect_errors=True, expected_status_int=401)
Expand Down Expand Up @@ -195,7 +196,7 @@ def test_next_job_handler(self):

def test_error_on_prod_mode_and_default_vm_id(self):
# Turn off DEV_MODE.
with self.swap(feconf, 'DEV_MODE', False):
with self.swap(constants, 'DEV_MODE', False):
self.post_json(
'/ml/nextjobhandler', self.payload,
expect_errors=True, expected_status_int=401)
Expand Down
4 changes: 2 additions & 2 deletions core/controllers/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,12 +760,12 @@ def post(self, exploration_id):
# Image files are stored to the datastore in the dev env, and to GCS
# in production.
file_system_class = (
fs_domain.ExplorationFileSystem if feconf.DEV_MODE
fs_domain.ExplorationFileSystem if constants.DEV_MODE
else fs_domain.GcsFileSystem)
fs = fs_domain.AbstractFileSystem(file_system_class(
'exploration/%s' % exploration_id))
filepath = (
filename if feconf.DEV_MODE
filename if constants.DEV_MODE
else ('%s/%s' % (self._FILENAME_PREFIX, filename)))

if fs.isfile(filepath):
Expand Down
3 changes: 2 additions & 1 deletion core/controllers/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import StringIO
import datetime

from constants import constants
from core.controllers import base
from core.domain import acl_decorators
from core.domain import exp_domain
Expand Down Expand Up @@ -128,7 +129,7 @@ def post(self, exploration_id):
# Audio files are stored to the datastore in the dev env, and to GCS
# in production.
file_system_class = (
fs_domain.ExplorationFileSystem if feconf.DEV_MODE
fs_domain.ExplorationFileSystem if constants.DEV_MODE
else fs_domain.GcsFileSystem)
fs = fs_domain.AbstractFileSystem(file_system_class(
'exploration/%s' % exploration_id))
Expand Down
4 changes: 2 additions & 2 deletions core/domain/exp_jobs_one_off.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def entity_classes_to_map_over(cls):

@staticmethod
def map(exp_model):
if not feconf.DEV_MODE:
if not constants.DEV_MODE:
exp_id = exp_model.id
fs_old = fs_domain.AbstractFileSystem(
fs_domain.GcsFileSystem(exp_id))
Expand Down Expand Up @@ -961,7 +961,7 @@ def entity_classes_to_map_over(cls):

@staticmethod
def map(exp_model):
if not feconf.DEV_MODE:
if not constants.DEV_MODE:
exp_id = exp_model.id
fs_old = fs_domain.AbstractFileSystem(
fs_domain.GcsFileSystem(exp_id))
Expand Down
8 changes: 4 additions & 4 deletions core/domain/exp_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -1655,25 +1655,25 @@ def save_original_and_compressed_versions_of_image(
user_id: str. The id of the user who wants to upload the image.
"""
filepath = (
filename if feconf.DEV_MODE else 'image/%s' % filename)
filename if constants.DEV_MODE else 'image/%s' % filename)

filename_wo_filetype = filename[:filename.rfind('.')]
filetype = filename[filename.rfind('.') + 1:]

compressed_image_filename = '%s_compressed.%s' % (
filename_wo_filetype, filetype)
compressed_image_filepath = (
compressed_image_filename if feconf.DEV_MODE
compressed_image_filename if constants.DEV_MODE
else 'image/%s' % compressed_image_filename)

micro_image_filename = '%s_micro.%s' % (
filename_wo_filetype, filetype)
micro_image_filepath = (
micro_image_filename if feconf.DEV_MODE
micro_image_filename if constants.DEV_MODE
else 'image/%s' % micro_image_filename)

file_system_class = (
fs_domain.ExplorationFileSystem if feconf.DEV_MODE
fs_domain.ExplorationFileSystem if constants.DEV_MODE
else fs_domain.GcsFileSystem)
fs = fs_domain.AbstractFileSystem(file_system_class(
'exploration/%s' % exp_id))
Expand Down
5 changes: 3 additions & 2 deletions core/domain/html_validation_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import logging

import bs4
from constants import constants
from core.domain import fs_domain
from core.domain import rte_component_registry
from core.platform import models
Expand Down Expand Up @@ -818,12 +819,12 @@ def get_filename_with_dimensions(old_filename, exp_id):
str. The new filename of the image file.
"""
file_system_class = (
fs_domain.ExplorationFileSystem if feconf.DEV_MODE
fs_domain.ExplorationFileSystem if constants.DEV_MODE
else fs_domain.GcsFileSystem)
fs = fs_domain.AbstractFileSystem(file_system_class(
'exploration/%s' % exp_id))
filepath = (
old_filename if feconf.DEV_MODE
old_filename if constants.DEV_MODE
else ('image/%s' % old_filename))
try:
content = fs.get(filepath.encode('utf-8'))
Expand Down
4 changes: 2 additions & 2 deletions core/platform/app_identity/gae_app_identity_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

"""Provides app identity services."""

import feconf
from constants import constants

from google.appengine.api import app_identity

Expand All @@ -43,7 +43,7 @@ def get_gcs_resource_bucket_name():
str or None. The bucket name for the application's GCS resources, in
production mode.
"""
if feconf.DEV_MODE:
if constants.DEV_MODE:
return None
else:
return get_application_id() + _GCS_RESOURCE_BUCKET_NAME_SUFFIX
4 changes: 2 additions & 2 deletions core/platform/app_identity/gae_app_identity_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from constants import constants
from core.platform.app_identity import gae_app_identity_services
from core.tests import test_utils
import feconf


class GaeAppIdentityServicesTest(test_utils.GenericTestBase):
Expand All @@ -34,7 +34,7 @@ def test_get_application_id(self):

def test_get_gcs_resource_bucket_name_prod(self):
# Turn off DEV_MODE.
with self.swap(feconf, 'DEV_MODE', False):
with self.swap(constants, 'DEV_MODE', False):
self.assertEqual(
gae_app_identity_services.get_gcs_resource_bucket_name(),
self.expected_bucket_name)
Expand Down
4 changes: 2 additions & 2 deletions core/platform/image/gae_image_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import io

from PIL import Image
from constants import constants
from core.platform import models
import feconf

from google.appengine.api import images

Expand Down Expand Up @@ -61,7 +61,7 @@ def compress_image(image_content, scaling_factor):
Returns:
str. Returns the content of the compressed image.
"""
if not feconf.DEV_MODE:
if not constants.DEV_MODE:
height, width = get_image_dimensions(image_content)
new_width = int(width * scaling_factor)
new_height = int(height * scaling_factor)
Expand Down
4 changes: 3 additions & 1 deletion core/platform/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
'recommendations', 'skill', 'statistics', 'story', 'suggestion', 'topic',
'user')

GAE_PLATFORM = 'gae'


class _Platform(object):
"""A base class for platform-specific imports related to GAE."""
Expand Down Expand Up @@ -249,7 +251,7 @@ def _get(cls):
Returns:
class: The corresponding platform-specific interface class.
"""
return cls._PLATFORM_MAPPING.get(feconf.PLATFORM)
return cls._PLATFORM_MAPPING.get(GAE_PLATFORM)

@classmethod
def import_models(cls, model_names):
Expand Down
38 changes: 20 additions & 18 deletions core/templates/dev/head/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,25 +339,27 @@ oppia.config([
]);

oppia.config(['$provide', function($provide) {
$provide.decorator('$log', ['$delegate', function($delegate) {
var _originalError = $delegate.error;

if (window.GLOBALS && !window.GLOBALS.DEV_MODE) {
$delegate.log = function() {};
$delegate.info = function() {};
// TODO(sll): Send errors (and maybe warnings) to the backend.
$delegate.warn = function() { };
$delegate.error = function(message) {
if (String(message).indexOf('$digest already in progress') === -1) {
_originalError(message);
}
};
// This keeps angular-mocks happy (in tests).
$delegate.error.logs = [];
}
$provide.decorator('$log', ['$delegate', 'DEV_MODE',
function($delegate, DEV_MODE) {
var _originalError = $delegate.error;

if (!DEV_MODE) {
$delegate.log = function() {};
$delegate.info = function() {};
// TODO(sll): Send errors (and maybe warnings) to the backend.
$delegate.warn = function() { };
$delegate.error = function(message) {
if (String(message).indexOf('$digest already in progress') === -1) {
_originalError(message);
}
};
// This keeps angular-mocks happy (in tests).
$delegate.error.logs = [];
}

return $delegate;
}]);
return $delegate;
}
]);
}]);

oppia.config(['toastrConfig', function(toastrConfig) {
Expand Down
Loading

0 comments on commit 1b66c8d

Please sign in to comment.