From c0a220abc9921a41b35f5746f0b03f712fc68154 Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Thu, 6 Oct 2016 11:52:05 +0800 Subject: [PATCH 1/2] allow synapses to be imported by topic name as well as id --- frontend/src/Metamaps/Import.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/frontend/src/Metamaps/Import.js b/frontend/src/Metamaps/Import.js index 6788335f..91da38cf 100644 --- a/frontend/src/Metamaps/Import.js +++ b/frontend/src/Metamaps/Import.js @@ -253,12 +253,16 @@ const Import = { parsedSynapses.forEach(function (synapse) { // only createSynapseWithParameters once both topics are persisted + // if there isn't a cidMapping, check by topic name instead var topic1 = Metamaps.Topics.get(self.cidMappings[synapse.topic1]) + if (!topic1) topic1 = Metamaps.Topics.findWhere({ name: synapse.topic1 }) var topic2 = Metamaps.Topics.get(self.cidMappings[synapse.topic2]) + if (!topic1) topic1 = Metamaps.Topics.findWhere({ name: synapse.topic1 }) + if (!topic1 || !topic2) { console.error("One of the two topics doesn't exist!") console.error(synapse) - return true + return // next } // ensure imported topics have a chance to get a real id attr before creating synapses @@ -407,6 +411,7 @@ const Import = { normalizeKeys: function(obj) { return _.transform(obj, (result, val, key) => { let newKey = key.toLowerCase() + newKey = newKey.replace(/\s/g, '') // remove whitespace if (newKey === 'url') key = 'link' if (newKey === 'title') key = 'name' if (newKey === 'description') key = 'desc' From b4d12509595e04aa3cb76cd557f74b2b15eb075b Mon Sep 17 00:00:00 2001 From: Devin Howard Date: Thu, 6 Oct 2016 12:02:14 +0800 Subject: [PATCH 2/2] share normalizeKey between TSV, CSV, and JSON --- frontend/src/Metamaps/Import.js | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/frontend/src/Metamaps/Import.js b/frontend/src/Metamaps/Import.js index 91da38cf..f70a1290 100644 --- a/frontend/src/Metamaps/Import.js +++ b/frontend/src/Metamaps/Import.js @@ -27,7 +27,7 @@ const Import = { 'id', 'name', 'metacode', 'x', 'y', 'description', 'link', 'permission' ], synapseWhitelist: [ - 'topic1', 'topic2', 'category', 'desc', 'description', 'permission' + 'topic1', 'topic2', 'category', 'direction', 'desc', 'description', 'permission' ], cidMappings: {}, // to be filled by import_id => cid mappings @@ -59,7 +59,7 @@ const Import = { console.warn(err) return topicsPromise.resolve([]) } - topicsPromise.resolve(data.map(row => self.normalizeKeys(row))) + topicsPromise.resolve(data) }) const synapsesPromise = $.Deferred() @@ -68,7 +68,7 @@ const Import = { console.warn(err) return synapsesPromise.resolve([]) } - synapsesPromise.resolve(data.map(row => self.normalizeKeys(row))) + synapsesPromise.resolve(data) }) $.when(topicsPromise, synapsesPromise).done((topics, synapses) => { @@ -83,8 +83,8 @@ const Import = { handle: function(results) { var self = Import - var topics = results.topics - var synapses = results.synapses + var topics = results.topics.map(topic => self.normalizeKeys(topic)) + var synapses = results.synapses.map(synapse => self.normalizeKeys(synapse)) if (topics.length > 0 || synapses.length > 0) { if (window.confirm('Are you sure you want to create ' + topics.length + @@ -149,7 +149,7 @@ const Import = { state = STATES.ABORT } topicHeaders = line.map(function (header, index) { - return header.toLowerCase().replace('description', 'desc') + return self.normalizeKey(header) }) state = STATES.TOPICS break @@ -160,7 +160,7 @@ const Import = { state = STATES.ABORT } synapseHeaders = line.map(function (header, index) { - return header.toLowerCase().replace('description', 'desc') + return self.normalizeKey(header) }) state = STATES.SYNAPSES break @@ -406,15 +406,20 @@ const Import = { .toLowerCase() }, + normalizeKey: function(key) { + let newKey = key.toLowerCase() + newKey = newKey.replace(/\s/g, '') // remove whitespace + if (newKey === 'url') newKey = 'link' + if (newKey === 'title') newKey = 'name' + if (newKey === 'description') newKey = 'desc' + if (newKey === 'direction') newKey = 'category' + return newKey + }, // thanks to http://stackoverflow.com/a/25290114/5332286 normalizeKeys: function(obj) { return _.transform(obj, (result, val, key) => { - let newKey = key.toLowerCase() - newKey = newKey.replace(/\s/g, '') // remove whitespace - if (newKey === 'url') key = 'link' - if (newKey === 'title') key = 'name' - if (newKey === 'description') key = 'desc' + const newKey = Import.normalizeKey(key) result[newKey] = val }) }