From 5cad626f3e6658e8472d461a7953f32546caf690 Mon Sep 17 00:00:00 2001 From: Damien Allen Date: Mon, 27 Jun 2016 12:19:20 +0200 Subject: [PATCH] [Fixes #2223] Improved reliability of migration --- .../rsr/migrations/0079_auto_20160620_1418.py | 6 +- .../project-directory-typeahead.js | 206 ------------------ 2 files changed, 4 insertions(+), 208 deletions(-) diff --git a/akvo/rsr/migrations/0079_auto_20160620_1418.py b/akvo/rsr/migrations/0079_auto_20160620_1418.py index 21a33bbea0..f5ece3f33f 100644 --- a/akvo/rsr/migrations/0079_auto_20160620_1418.py +++ b/akvo/rsr/migrations/0079_auto_20160620_1418.py @@ -11,7 +11,9 @@ def sector_validation(apps, schema_editor): sector_validators = ['rsr_sector', 'rsr_sector.sector_code', 'rsr_sector.vocabulary'] for v in sector_validators: - ProjectEditorValidation.objects.filter(validation_set_id=1).get(validation__exact=v).delete() + validation = ProjectEditorValidation.objects.filter(validation_set_id=1, validation__exact=v) + if validation: + validation.delete() def undo_sector_validation(apps, schema_editor): """ Remove sector from RSR validation set """ @@ -20,7 +22,7 @@ def undo_sector_validation(apps, schema_editor): sector_validators = ['rsr_sector', 'rsr_sector.sector_code', 'rsr_sector.vocabulary'] for v in sector_validators: - ProjectEditorValidation(validation=v, action=1, validation_set_id = 1).save() + ProjectEditorValidation.objects.get_or_create(validation=v, action=1, validation_set_id=1) class Migration(migrations.Migration): diff --git a/akvo/rsr/static/scripts-src/project-directory-typeahead.js b/akvo/rsr/static/scripts-src/project-directory-typeahead.js index fc16d2a649..e69de29bb2 100644 --- a/akvo/rsr/static/scripts-src/project-directory-typeahead.js +++ b/akvo/rsr/static/scripts-src/project-directory-typeahead.js @@ -1,206 +0,0 @@ -/** @jsx React.DOM */ - -// Akvo RSR is covered by the GNU Affero General Public License. -// See more details in the license.txt file located at the root folder of the -// Akvo RSR module. For additional details on the GNU license please see -// < http://www.gnu.org/licenses/agpl.html >. - -var i18n, - Typeahead; - -function loadAsync(url, retryCount, retryLimit) { - var xmlHttp; - - xmlHttp = new XMLHttpRequest(); - - xmlHttp.onreadystatechange = function() { - if (xmlHttp.readyState == XMLHttpRequest.DONE) { - - if(xmlHttp.status == 200){ - processResponse(xmlHttp.responseText); - } else { - if (retryCount >= retryLimit) { - return; - } else { - retryCount = retryCount + 1; - loadAsync(url, retryCount, retryLimit); - } - } - } else { - return; - } - }; - - xmlHttp.open("GET", url, true); - xmlHttp.send(); -} - -function processResponse(response) { - var orgs, typeaheadOptions, typeaheadPlaceholder, typeaheadCallback, allEntry, currentFilter; - - orgs = JSON.parse(response); - orgs = orgs.results; - - // Add an "All" entry so user can reset filter - allEntry = {}; - allEntry.id = ""; - allEntry.name = i18n.all_text; - orgs.unshift(allEntry); - - currentFilter = getCurrentOrgFilter(orgs); - updateIdElement(currentFilter); - - typeaheadOptions = getTypeaheadOptions(orgs); - typeaheadPlaceholder = getPlaceholder(currentFilter); - typeaheadCallback = function(option) { - var id, idElement; - - id = getIdFromName(option, orgs); - idElement = document.getElementById('org-filter-input'); - - idElement.value = id; - }; - - buildReactComponents(typeaheadPlaceholder, typeaheadOptions, typeaheadCallback); -} - -function getTypeaheadOptions(orgs) { - var orgNames; - - orgNames = []; - - for (var i = 0; i < orgs.length; i++) { - orgNames[i] = orgs[i].name; - } - - return orgNames; -} - -function getCurrentOrgFilter(orgs) { - - var currentFilterId, currentFilterName, filter; - - currentFilterId = JSON.parse(document.getElementById("react-typeahead-org").innerHTML).currentOrg; - currentFilterName = getNameFromId(currentFilterId, orgs); - - filter = {}; - filter.id = currentFilterId; - filter.name = currentFilterName; - - return filter; -} - -function getNameFromId(id, orgs) { - for (var i = 0; i < orgs.length; i++) { - if (id.toString().toLowerCase() === orgs[i].id.toString().toLowerCase()) { - return orgs[i].name; - } - } - return "All"; -} - -function getIdFromName(name, orgs) { - for (var i = 0; i < orgs.length; i++) { - if (name.toString().toLowerCase() === orgs[i].name.toString().toLowerCase()) { - return orgs[i].id; - } - } - return ""; -} - -function updateIdElement(filter) { - var idElement, typeaheadPlaceholder; - - idElement = document.getElementById('org-filter-input'); - idElement.value = filter.id; -} - -function getPlaceholder(filter) { - return filter.name; -} - -function buildReactComponents(placeholder, typeaheadOptions, typeaheadCallback) { - var TypeaheadLabel = React.createClass({displayName: 'TypeaheadLabel', - render: function() { - return React.createElement('div', null, - React.createElement('label', {className: 'control-label'}, i18n.organisation_text) - ); - } - }); - - var TypeaheadContainer = React.createClass({displayName: 'TypeaheadContainer', - render: function() { - return React.createElement('div', null, - React.createElement(TypeaheadLabel), - React.createElement(Typeahead, { - placeholder: placeholder, - options: typeaheadOptions, - onOptionSelected: typeaheadCallback, - maxVisible: 10, - customClasses: { - typeahead: "", - input: "form-group form-control", - results: "", - listItem: "", - token: "", - customAdd: "" - }, - className: "partnerTest" - }) - ); - } - }); - - ReactDOM.render( - React.createElement(TypeaheadContainer), document.getElementById('org-filter-container') - ); -} - -function initReact() { - // Load globals - Typeahead = ReactTypeahead.Typeahead; - - loadAsync('/rest/v1/typeaheads/organisations?format=json', 0, 3); -} - -var loadJS = function(url, implementationCode, location){ - //url is URL of external file, implementationCode is the code - //to be called from the file, location is the location to - //insert the