Skip to content

Commit

Permalink
Modified references for extensions, added extensions to build process…
Browse files Browse the repository at this point in the history
… and refactored methods in UrlInterpolationService
  • Loading branch information
gvishal committed Jul 17, 2016
1 parent b165745 commit 88c91e7
Show file tree
Hide file tree
Showing 31 changed files with 225 additions and 253 deletions.
3 changes: 2 additions & 1 deletion app.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ handlers:
static_dir: assets/common
secure: always
http_headers:
# TODO(gvishal): set an appropriate time for this.
# WARNING TO DEVELOPERS: Files in this folder may be stale for
# up to an hour after a code release.
Cache-Control: 'public, max-age=3600'
- url: /assets
static_dir: assets
Expand Down
8 changes: 4 additions & 4 deletions core/controllers/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,8 @@ class I18nDictsTest(test_utils.GenericTestBase):

def _extract_keys_from_json_file(self, filename):
return sorted(json.loads(utils.get_file_contents(
os.path.join(os.getcwd(), self.get_cache_slug_filepath(), 'assets',
'i18n', filename)
os.path.join(os.getcwd(), self.get_static_asset_filepath(),
'assets', 'i18n', filename)
)).keys())

def test_i18n_keys(self):
Expand All @@ -283,8 +283,8 @@ def test_i18n_keys(self):
self.assertGreater(len(master_key_list), 0)

filenames = os.listdir(
os.path.join(os.getcwd(), self.get_cache_slug_filepath(), 'assets',
'i18n'))
os.path.join(os.getcwd(), self.get_static_asset_filepath(),
'assets', 'i18n'))
for filename in filenames:
if filename == 'en.json':
continue
Expand Down
6 changes: 3 additions & 3 deletions core/controllers/library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,12 @@ def test_thumbnail_icons_exist_for_each_category(self):
# Test that an icon exists for each default category.
for category in all_categories:
utils.get_file_contents(os.path.join(
self.get_cache_slug_filepath(), 'assets', 'images', 'subjects',
'%s.svg' % category.replace(' ', '')))
self.get_static_asset_filepath(), 'assets', 'images',
'subjects', '%s.svg' % category.replace(' ', '')))

# Test that the default icon exists.
utils.get_file_contents(os.path.join(
self.get_cache_slug_filepath(), 'assets', 'images', 'subjects',
self.get_static_asset_filepath(), 'assets', 'images', 'subjects',
'%s.svg' % feconf.DEFAULT_THUMBNAIL_ICON))


Expand Down
16 changes: 8 additions & 8 deletions core/domain/summary_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ def test_get_displayable_exp_summary_dicts_matching_ids(self):
'tags': [],
'thumbnail_bg_color': '#a33f40',
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Lightbulb.svg'),
'/images/subjects/Lightbulb.svg'),
'title': u'Exploration 2 Albert title',
}
self.assertIn('last_updated_msec', displayable_summaries[0])
Expand Down Expand Up @@ -280,7 +280,7 @@ def test_get_library_groups(self):
'title': u'The Lazy Magician',
'thumbnail_bg_color': '#d0982a',
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Algorithms.svg'),
'/images/subjects/Algorithms.svg'),
}
expected_group = {
'categories': ['Algorithms', 'Computing', 'Programming'],
Expand Down Expand Up @@ -357,7 +357,7 @@ def test_for_featured_explorations(self):
'community_owned': False,
'tags': [],
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Lightbulb.svg'),
'/images/subjects/Lightbulb.svg'),
'language_code': feconf.DEFAULT_LANGUAGE_CODE,
'id': self.EXP_ID_2,
'category': 'A category',
Expand Down Expand Up @@ -644,7 +644,7 @@ def test_at_most_eight_top_rated_explorations(self):
'community_owned': False,
'tags': [],
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Lightbulb.svg'),
'/images/subjects/Lightbulb.svg'),
'language_code': feconf.DEFAULT_LANGUAGE_CODE,
'id': self.EXP_ID_3,
'category': u'A category',
Expand Down Expand Up @@ -682,7 +682,7 @@ def test_only_explorations_with_ratings_are_returned(self):
'community_owned': False,
'tags': [],
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Lightbulb.svg'),
'/images/subjects/Lightbulb.svg'),
'language_code': feconf.DEFAULT_LANGUAGE_CODE,
'id': self.EXP_ID_2,
'category': u'A category',
Expand Down Expand Up @@ -764,7 +764,7 @@ def test_for_recently_published_explorations(self):
'community_owned': False,
'tags': [],
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Lightbulb.svg'),
'/images/subjects/Lightbulb.svg'),
'language_code': feconf.DEFAULT_LANGUAGE_CODE,
'id': self.EXP_ID_1,
'category': u'A category',
Expand All @@ -779,7 +779,7 @@ def test_for_recently_published_explorations(self):
'community_owned': False,
'tags': [],
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Lightbulb.svg'),
'/images/subjects/Lightbulb.svg'),
'language_code': feconf.DEFAULT_LANGUAGE_CODE,
'id': self.EXP_ID_2,
'category': u'A category',
Expand All @@ -794,7 +794,7 @@ def test_for_recently_published_explorations(self):
'community_owned': False,
'tags': [],
'thumbnail_icon_url': self.get_static_asset_url(
'/assets/images/subjects/Lightbulb.svg'),
'/images/subjects/Lightbulb.svg'),
'language_code': feconf.DEFAULT_LANGUAGE_CODE,
'id': self.EXP_ID_3,
'category': u'A category',
Expand Down
4 changes: 2 additions & 2 deletions core/domain/user_services_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_get_user_id_from_username(self):
def test_fetch_gravatar_success(self):
user_email = 'user@example.com'
expected_gravatar_filepath = os.path.join(
self.get_cache_slug_filepath(), 'assets', 'images', 'avatar',
self.get_static_asset_filepath(), 'assets', 'images', 'avatar',
'gravatar_example.png')
with open(expected_gravatar_filepath, 'r') as f:
gravatar = f.read()
Expand Down Expand Up @@ -197,7 +197,7 @@ def log_mock(message):

def test_default_identicon_data_url(self):
identicon_filepath = os.path.join(
self.get_cache_slug_filepath(), 'assets', 'images', 'avatar',
self.get_static_asset_filepath(), 'assets', 'images', 'avatar',
'user_blue_72px.png')
identicon_data_url = utils.convert_png_to_data_url(
identicon_filepath)
Expand Down
109 changes: 36 additions & 73 deletions core/templates/dev/head/domain/utilities/UrlInterpolationService.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,28 @@ oppia.factory('UrlInterpolationService', ['alertsService',
return (typeof value === 'string') || (value instanceof String);
};

var validateResourcePath = function(resourcePath) {
if (!resourcePath) {
alertsService.fatalWarning(
'Empty path passed in method.');
return null;
}

var RESOURCE_PATH_STARTS_WITH_FORWARD_SLASH = /^\//;
// Ensure that resourcePath starts with a forward slash.
if (!resourcePath.match(RESOURCE_PATH_STARTS_WITH_FORWARD_SLASH)) {
alertsService.fatalWarning(
'Path must start with \'\/\': \'' + new String(resourcePath) +
'\'.');
return null;
}
};

var getCachePrefixedUrl = function(resourcePath) {
validateResourcePath(resourcePath);
return GLOBALS.ASSET_DIR_PREFIX + resourcePath;
};

return {
/**
* Given a formatted URL, interpolates the URL by inserting values the URL
Expand Down Expand Up @@ -121,101 +143,42 @@ oppia.factory('UrlInterpolationService', ['alertsService',
* prefixing the appropriate cache_slug to it.
*/
getStaticResourceUrl: function(resourcePath) {
if (!resourcePath) {
alertsService.fatalWarning(
'Empty resource path passed in getStaticResourceUrl.');
return null;
}

var RESOURCE_PATH_STARTS_WITH_FORWARD_SLASH = /^\//;
// Ensure that resourcePath starts with a forward slash.
if (!resourcePath.match(RESOURCE_PATH_STARTS_WITH_FORWARD_SLASH)) {
alertsService.fatalWarning(
'Resource path passed into getStaticResourceUrl must start with' +
'\'\/\': \'' + new String(resourcePath) + '\'.');
return null;
}

return GLOBALS.ASSET_DIR_PREFIX + resourcePath;
},

/**
* Given a domain url and resource path, returns the complete url path
* to that resource prefixing the appropriate cache_slug to it.
*/
getCompleteStaticResourceUrl: function(domainUrl, resourcePath) {
if (!resourcePath) {
alertsService.fatalWarning(
'Empty resource path passed in getCompleteStaticResourceUrl.');
return null;
}

var RESOURCE_PATH_STARTS_WITH_FORWARD_SLASH = /^\//;
// Ensure that resourcePath starts with a forward slash.
if (!resourcePath.match(RESOURCE_PATH_STARTS_WITH_FORWARD_SLASH)) {
alertsService.fatalWarning(
'Resource path passed into getCompleteStaticResourceUrl must start' +
'with \'\/\': \'' + new String(resourcePath) + '\'.');
return null;
}

return domainUrl + GLOBALS.ASSET_DIR_PREFIX + resourcePath;
return getCachePrefixedUrl(resourcePath);
},

/**
* Given an image path, returns the complete url path to that image
* prefixing the appropriate cache_slug to it.
*/
getStaticImageUrl: function(imagePath) {
if (!imagePath) {
alertsService.fatalWarning(
'Empty image path passed in getStaticImageUrl.');
return null;
}

var IMAGE_PATH_STARTS_WITH_FORWARD_SLASH = /^\//;
// Ensure that imagePath starts with a forward slash.
if (!imagePath.match(IMAGE_PATH_STARTS_WITH_FORWARD_SLASH)) {
alertsService.fatalWarning(
'Image path passed into getStaticImageUrl must start with \'\/\': ' +
'\'' + new String(imagePath) + '\'.');
return null;
}

return GLOBALS.ASSET_DIR_PREFIX + '/assets/images' + imagePath;
validateResourcePath(imagePath);
return getCachePrefixedUrl('/assets/images' + imagePath);
},

/**
* Given an extension resource path, returns the complete url path to that
* extension resource, prefixing the appropriate cache_slug to it.
*/
getExtensionResourceUrl: function(extensionResourcePath) {
if (!extensionResourcePath) {
alertsService.fatalWarning(
'Empty extension resource path passed in getExtensionResourceUrl.');
return null;
}

var EXTENSION_RESOURCE_PATH_CHECK = /^\//;
// Ensure that extensionResourcePath starts with a forward slash.
if (!extensionResourcePath.match(EXTENSION_RESOURCE_PATH_CHECK)) {
alertsService.fatalWarning(
'Extension resource path passed into getExtensionResourceUrl' +
'must start with \'\/\': ' +
'\'' + new String(extensionResourcePath) + '\'.');
return null;
}
return getCachePrefixedUrl(extensionResourcePath);
},

return GLOBALS.ASSET_DIR_PREFIX + extensionResourcePath;
/**
* Given a gadget type, returns the complete url path to that
* gadget type image, prefixing the appropriate cache_slug to it.
*/
getGadgetImgUrl: function(gadgetType) {
return getCachePrefixedUrl('/extensions/gadgets/' + gadgetType +
'/static/images/' + gadgetType + '.png');
},

/**
* Given an interaction id, returns the complete url path to the thumbnail
* image for the interaction, prefixing the appropriate cache_slug to it.
*/
getInteractionThumbnailImageUrl: function(interactionId) {
return GLOBALS.ASSET_DIR_PREFIX + '/extensions/interactions/' +
interactionId + '/static/' + interactionId + '.png';
return getCachePrefixedUrl('/extensions/interactions/' +
interactionId + '/static/' + interactionId + '.png');
}
};
}]);
Original file line number Diff line number Diff line change
Expand Up @@ -208,29 +208,29 @@ describe('URL Interpolation Service', function() {

it('should interpolate correct imagePath', function() {
expect(uis.getStaticImageUrl('/test_url/')).toBe(
GLOBALS.ASSET_DIR_PREFIX + '/assets/images' + '/test_url/');
GLOBALS.ASSET_DIR_PREFIX + '/assets/images/test_url/');
expect(uis.getStaticImageUrl('/test_url/test.png')).toBe(
GLOBALS.ASSET_DIR_PREFIX + '/assets/images' + '/test_url/test.png');
GLOBALS.ASSET_DIR_PREFIX + '/assets/images/test_url/test.png');
});

it('should throw an error for empty imagePath', function() {
expect(uis.getStaticImageUrl.bind(null, null)).toThrow(
new Error(
'Empty image path passed in getStaticImageUrl.'));
'Empty path passed in method.'));
expect(uis.getStaticImageUrl.bind(null, '')).toThrow(
new Error(
'Empty image path passed in getStaticImageUrl.'));
'Empty path passed in method.'));
});

it('should throw an error for imagePath not beginning with forward slash',
function() {
expect(uis.getStaticImageUrl.bind(null, 'test_url_fail')).toThrow(
new Error(
'Image path passed into getStaticImageUrl must start with \'\/\': ' +
'\'' + new String('test_url_fail') + '\'.'));
'Path must start with \'\/\': \'' + new String('test_url_fail') +
'\'.'));
expect(uis.getStaticImageUrl.bind(null, 'test_url/fail.png')).toThrow(
new Error(
'Image path passed into getStaticImageUrl must start with \'\/\': ' +
'\'' + new String('test_url/fail.png') + '\'.'));
'Path must start with \'\/\': \'' + new String('test_url/fail.png') +
'\'.'));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@
// Controls adding, deleting and updating gadgets.
oppia.controller('GadgetEditor', [
'$scope', '$modal', '$log', 'editorContextService',
'explorationGadgetsService', 'GADGET_SPECS',
'explorationGadgetsService', 'GADGET_SPECS', 'UrlInterpolationService',
function(
$scope, $modal, $log, editorContextService,
explorationGadgetsService, GADGET_SPECS) {
explorationGadgetsService, GADGET_SPECS, UrlInterpolationService) {
$scope.GADGET_SPECS = GADGET_SPECS;

$scope.$on('gadgetsChangedOrInitialized', function() {
Expand All @@ -35,7 +35,7 @@ oppia.controller('GadgetEditor', [
$scope.activeStateName = currentStateName;
});

$scope.extensionResourcePrefix = GLOBALS.ASSET_DIR_PREFIX;
$scope.getGadgetImgUrl = UrlInterpolationService.getGadgetImgUrl;

$scope.refreshGadgetsInfo = function() {
$scope.gadgets = explorationGadgetsService.getGadgets();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ oppia.controller('StateEditor', [
$scope.oppiaBlackImgUrl = UrlInterpolationService.getStaticImageUrl(
'/avatar/oppia_black_72px.png');

$scope.extensionResourcePrefix = GLOBALS.ASSET_DIR_PREFIX;
$scope.getInteractionThumbnailImageUrl = (
UrlInterpolationService.getInteractionThumbnailImageUrl);

$scope.isCurrentStateInitialState = function() {
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ <h3 ng-if="!gadgetDict.gadget_type">
<div class="oppia-gadget-tile"
ng-repeat="(gadgetType, gadget) in ALLOWED_GADGETS"
ng-click="onChangeGadgetType(gadgetType)">
<img style="padding-top: 20px; padding-bottom: 10px;" ng-src="<[extensionResourcePrefix]>/extensions/gadgets/<[gadgetType]>/static/images/<[gadgetType]>.png">
<img style="padding-top: 20px; padding-bottom: 10px;" ng-src="<[getGadgetImgUrl(gadgetType)]>">
<div class="oppia-gadget-tile-name protractor-test-<[gadgetType]>-gadget-selection-modal">
<[GADGET_SPECS[gadgetType].short_description]>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ <h3 ng-if="stateInteractionIdService.displayed">
<div ng-repeat="interactionId in category.interaction_ids"
ng-click="onChangeInteractionId(interactionId)"
class="oppia-interaction-tile protractor-test-interaction-tile-<[interactionId]>">
<img ng-src="<[extensionResourcePrefix]>/extensions/interactions/<[interactionId]>/static/<[interactionId]>.png">
<img ng-src="<[getInteractionThumbnailImageUrl(interactionId)]>">
<div class="oppia-interaction-tile-name">
<[INTERACTION_SPECS[interactionId].name]>
</div>
Expand Down
2 changes: 1 addition & 1 deletion core/templates/dev/head/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ oppia.config([
.registerAvailableLanguageKeys(
availableLanguageKeys, availableLanguageKeysMap)
.useStaticFilesLoader({
prefix: GLOBALS.ASSET_DIR_PREFIX + '/assets/i18n' + '/',
prefix: GLOBALS.ASSET_DIR_PREFIX + '/assets/i18n/',
suffix: '.json'
})
// The use of default translation improves the loading time when English is
Expand Down
2 changes: 1 addition & 1 deletion core/templates/dev/head/pages/Splash.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ oppia.controller('Splash', [
function($scope, $timeout, $window, siteAnalyticsService,
UrlInterpolationService) {
$scope.getStaticImageUrl = UrlInterpolationService.getStaticImageUrl;
$scope.getSubjectsImageUrl = function(subjectName) {
$scope.getStaticSubjectImageUrl = function(subjectName) {
return UrlInterpolationService.getStaticImageUrl('/subjects/' +
subjectName + '.svg');
};
Expand Down
Loading

0 comments on commit 88c91e7

Please sign in to comment.