Skip to content

Commit

Permalink
Changes in system providing constants to frontend and backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechjelinek committed Feb 17, 2017
1 parent c3b4587 commit 63260f0
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 22 deletions.
2 changes: 1 addition & 1 deletion constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ class Constants(dict):
__getattr__ = dict.__getitem__

with open(os.path.join('assets', 'constants.json'), 'r') as f:
CONSTANTS = Constants(json.load(f))
constants = Constants(json.load(f)) #pylint: disable=invalid-name
32 changes: 32 additions & 0 deletions constants_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Copyright 2017 The Oppia Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# 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.

"""Tests for Constants object and cosntants.json file."""

import os
import json

from core.tests import test_utils #pylint: disable=relative-import

class ConstantsTests(test_utils.GenericTestBase):

def test_constants_file_is_existing(self):
"""Test if the constants file is existing."""
self.assertTrue(os.path.isfile(os.path.join(
'assets', 'constants.json')))

def test_constants_file_is_valid_json(self):
"""Test if the constants file is valid json file."""
with open(os.path.join('assets', 'constants.json'), 'r') as f:
self.assertTrue(isinstance(json.load(f), dict))
4 changes: 2 additions & 2 deletions core/controllers/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import jinja2

from constants import CONSTANTS
from constants import constants
from core.controllers import base
from core.domain import config_domain
from core.domain import dependency_registry
Expand Down Expand Up @@ -160,7 +160,7 @@ class ExplorationPage(EditorHandler):

def get(self, exploration_id):
"""Handles GET requests."""
if exploration_id in CONSTANTS.DISABLED_EXPLORATION_IDS:
if exploration_id in constants.DISABLED_EXPLORATION_IDS:
self.render_template(
'pages/error/disabled_exploration.html',
iframe_restriction=None)
Expand Down
5 changes: 2 additions & 3 deletions core/templates/dev/head/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
/**
* @fileoverview Initialization and basic configuration for the Oppia module.
*/

// TODO(sll): Remove the check for window.GLOBALS. This check is currently
// only there so that the Karma tests run, since it looks like Karma doesn't
// 'see' the GLOBALS variable that is defined in base.html. We should fix this
Expand All @@ -30,8 +29,8 @@ var oppia = angular.module(
window.GLOBALS ? (window.GLOBALS.ADDITIONAL_ANGULAR_MODULES || [])
: []));

for (var constantName in CONSTANTS) {
oppia.constant(constantName, CONSTANTS[constantName]);
for (var constantName in constants) {
oppia.constant(constantName, constants[constantName]);
}

oppia.constant(
Expand Down
16 changes: 16 additions & 0 deletions core/templates/dev/head/appSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -233,3 +233,19 @@ describe('Code Normalization', function() {
);
});
});

describe('Constants Generating', function() {
beforeEach(module('oppia'));

var $injector = null;
beforeEach(inject(function(_$injector_) {
$injector = _$injector_.get('$injector');
}));

it('should transform all key value pairs to angular constants', function() {
for (var constantName in constants) {
expect($injector.has(constantName)).toBeTruthy();
expect($injector.get(constantName)).toBe(constants[constantName]);
}
});
});
14 changes: 0 additions & 14 deletions core/templates/dev/head/pages/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,20 +119,6 @@
{% endif %}
</script>

<!-- Note to developers: This script needs to be loaded before external
libraries. Also, if you modify this script, please change
core/tests/karma-constants.js accordingly -->
<script>
var CONSTANTS = (function() {
var request = new XMLHttpRequest();
request.overrideMimeType('application/json');
request.open(
'GET', GLOBALS.ASSET_DIR_PREFIX + '/assets/constants.json', false);
request.send(null);
return JSON.parse(request.responseText);
})();
</script>

{% block header_js %}
{% include 'pages/header_js_libs.html' %}
{% endblock header_js %}
Expand Down
13 changes: 13 additions & 0 deletions core/templates/dev/head/pages/header_js_libs.html
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
<!-- Note to developers: This script needs to be loaded before external
libraries. Also, if you modify this script, please change
core/tests/karma-constants.js accordingly -->
<script>
var constants = (function() {
var request = new XMLHttpRequest();
request.overrideMimeType('application/json');
request.open(
'GET', GLOBALS.ASSET_DIR_PREFIX + '/assets/constants.json', false);
request.send(null);
return JSON.parse(request.responseText);
})();
</script>
<!-- jquery.js, angular.js and jquery-ui.js are removed from bundled js because
they need to be at the header. Including bundled js at the header will block
rendering.-->
Expand Down
4 changes: 2 additions & 2 deletions core/tests/karma-constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2016 The Oppia Authors. All Rights Reserved.
// Copyright 2017 The Oppia Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -17,7 +17,7 @@
*/

/* This should be kept in parity with how CONSTANTS is set in base.html */
var CONSTANTS = (function() {
var constants = (function() {
var request = new XMLHttpRequest();
request.overrideMimeType('application/json');
request.open(
Expand Down
3 changes: 3 additions & 0 deletions core/tests/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ module.exports = function(config) {
'extensions/**/protractor.js'
],
proxies: {
// Karma serves files are under the /base directory.
// We need to access files in assets folder, without modifiing the code,
// so ween need to proxy the requests from /assets/ to /base/assets/.
'/assets/': '/base/assets/'
},
preprocessors: {
Expand Down

0 comments on commit 63260f0

Please sign in to comment.