diff --git a/api/pages/org/list.py b/api/pages/org/list.py index 28d6d6f5..bef67ed6 100644 --- a/api/pages/org/list.py +++ b/api/pages/org/list.py @@ -110,19 +110,32 @@ def run(API, environ, indata, session): orgname = indata.get('name', 'Foo') orgdesc = indata.get('desc', '') orgid = indata.get('id', str(int(time.time()))) + queryMethod = indata.get('querymethod', 'update') if session.DB.ES.exists(index=session.DB.dbname, doc_type='organisation', id = orgid): - raise API.exception(403, "Organisation ID already in use!") - - doc = { - 'id': orgid, - 'name': orgname, - 'description': orgdesc, - 'admins': [] - } - session.DB.ES.index(index=session.DB.dbname, doc_type='organisation', id = orgid, body = doc) - time.sleep(1.5) - yield json.dumps({"okay": True, "message": "Organisation created!"}) - return + if queryMethod == 'update': + doc = { + 'id': orgid, + 'name': orgname, + 'description': orgdesc, + 'admins': [] + } + session.DB.ES.index(index=session.DB.dbname, doc_type='organisation',id= orgid, body = doc) + time.sleep(1.5) + yield json.dumps({"okay": True, "message": "Organisation updated!"}) + return + else: + raise API.exception(403, "Organisation ID already in use!") + else: + doc = { + 'id': orgid, + 'name': orgname, + 'description': orgdesc, + 'admins': [] + } + session.DB.ES.index(index=session.DB.dbname, doc_type='organisation', id = orgid, body = doc) + time.sleep(1.5) + yield json.dumps({"okay": True, "message": "Organisation created!"}) + return else: raise API.exception(403, "Only administrators can create new organisations.") diff --git a/api/yaml/openapi.yaml b/api/yaml/openapi.yaml index 245ef57c..4c0312a5 100644 --- a/api/yaml/openapi.yaml +++ b/api/yaml/openapi.yaml @@ -78,10 +78,15 @@ components: description: The name of the organisation to create example: Kibble, Inc. type: string + querymethod: + description: Update or new + example: update + type: string required: - id - name - desc + - querymethod OrgMembers: properties: admins: diff --git a/api/yaml/openapi/components/schemas/NewOrg.yaml b/api/yaml/openapi/components/schemas/NewOrg.yaml index 58f8d6b1..d79a1017 100644 --- a/api/yaml/openapi/components/schemas/NewOrg.yaml +++ b/api/yaml/openapi/components/schemas/NewOrg.yaml @@ -14,7 +14,13 @@ properties: description: The name of the organisation to create example: Kibble, Inc. type: string + querymethod: + description: Update or new + example: update + type: string + required: - id - name - desc +- querymethod diff --git a/api/yaml/openapi/components/schemas/Organisation.yaml b/api/yaml/openapi/components/schemas/Organisation.yaml index abd66cc5..b3b47e87 100644 --- a/api/yaml/openapi/components/schemas/Organisation.yaml +++ b/api/yaml/openapi/components/schemas/Organisation.yaml @@ -26,7 +26,12 @@ properties: description: An array containing the IDs of the admins of this org type: array exammple: ['foo@bar', 'bar@foo'] + querymethod: + description: Update or new + example: update + type: string required: - id - name +- querymethod diff --git a/ui/js/coffee/kibble_organisation.coffee b/ui/js/coffee/kibble_organisation.coffee index f6a4edc7..d2b2bf22 100644 --- a/ui/js/coffee/kibble_organisation.coffee +++ b/ui/js/coffee/kibble_organisation.coffee @@ -40,21 +40,39 @@ defaultOrgChanged = (json, state) -> location.reload() , 1000) -makeOrg = () -> +makeOrg = (queryMethod) -> orgname = get('orgname').value orgdesc = get('orgdesc').value orgid = get('orgid').value - if orgid.length == 0 - orgid = parseInt(Math.random()*987654321).toString(16) + if queryMethod == 'new' + if orgid.length == 0 + orgid = parseInt(Math.random()*987654321).toString(16) if orgname.length == 0 alert("Please enter a name for the organisation!") return if orgdesc.length == 0 alert("Please enter a description of the organisation!") return - put('org/list', {id: orgid, name: orgname, desc: orgdesc}, {}, orgCreated) - + put('org/list', {id: orgid, name: orgname, desc: orgdesc, querymethod: queryMethod}, {}, orgCreated) +edit = (org) -> + document + .getElementById 'orgname' + .value = org.name + document + .getElementById 'orgdesc' + .value = org.desc + document + .getElementById 'orgid' + .value = org.id + document + .getElementById 'create_btn' + .disabled = true + document + .getElementById 'create_btn' + .scrollIntoView = false + window + .scrollTo(0,document.body.scrollHeight) orglist = (json, state) -> items = [] if json.organisations.length == 0 @@ -81,6 +99,8 @@ orglist = (json, state) -> if not isDefault dbtn = new HTML('input', { style: { marginTop: "10px", width: "120px"},class: "btn btn-primary btn-block", type: "button", onclick: "setDefaultOrg('#{org.id}');", value: "Set as current"}) div.inject(dbtn) + dbtn = new HTML('i', { style: { float: "right" }, class: "fa fa-edit", type: "icon", title: "Edit Organisation", onclick: "edit({name: '#{org.name}', desc: '#{org.description}', id: '#{org.id}'});",href: "#create_btn"}) + div.inject(dbtn) odiv.inject(new HTML('hr')) state.widget.inject(odiv, true) @@ -95,7 +115,9 @@ orglist = (json, state) -> fieldset.inject(new HTML('p', {}, "You'll be able to add users and owners once the organisation has been created.")) - btn = new HTML('input', { style: { width: "200px"},class: "btn btn-primary btn-block", type: "button", onclick: "makeOrg();", value: "Create organisation"}) + btn = new HTML('input', { style: { width: "200px"},class: "btn btn-primary btn-block", type: "button",id: 'create_btn', onclick: "makeOrg('new');", value: "Create organisation"}) + fieldset.inject(btn) + btn = new HTML('input', { style: { width: "200px"},class: "btn btn-primary btn-block", type: "button",id: 'edit_btn', onclick: "makeOrg('update');", value: "Edit organisation"}) fieldset.inject(btn) state.widget.inject(fieldset) diff --git a/ui/js/kibble.v1.js b/ui/js/kibble.v1.js index 42f02b3a..0fabc42f 100644 --- a/ui/js/kibble.v1.js +++ b/ui/js/kibble.v1.js @@ -3105,13 +3105,15 @@ defaultOrgChanged = function(json, state) { }, 1000); }; -makeOrg = function() { +makeOrg = function(queryMethod) { var orgdesc, orgid, orgname; orgname = get('orgname').value; orgdesc = get('orgdesc').value; orgid = get('orgid').value; - if (orgid.length === 0) { - orgid = parseInt(Math.random() * 987654321).toString(16); + if (queryMethod === 'new') { + if (orgid.length === 0) { + orgid = parseInt(Math.random() * 987654321).toString(16); + } } if (orgname.length === 0) { alert("Please enter a name for the organisation!"); @@ -3124,10 +3126,20 @@ makeOrg = function() { return put('org/list', { id: orgid, name: orgname, - desc: orgdesc + desc: orgdesc, + querymethod: queryMethod }, {}, orgCreated); }; +edit = function(org) { + document.getElementById('orgname').value = org.name; + document.getElementById('orgdesc').value = org.desc; + document.getElementById('orgid').value = org.id; + document.getElementById('create_btn').disabled = true; + document.getElementById('create_btn').scrollIntoView = false; + return window.scrollTo(0, document.body.scrollHeight); +}; + orglist = function(json, state) { var btn, dbtn, div, fieldset, isDefault, items, legend, len, obj, odiv, org, q, ref; items = []; @@ -3164,6 +3176,17 @@ orglist = function(json, state) { }); div.inject(dbtn); } + dbtn = new HTML('i', { + style: { + float: "right" + }, + "class": "fa fa-edit", + type: "icon", + title: "Edit Organisation", + onclick: "edit({name: '" + org.name + "', desc: '" + org.description + "', id: '" + org.id + "'});", + href: "#create_btn" + }); + div.inject(dbtn); odiv.inject(new HTML('hr')); } state.widget.inject(odiv, true); @@ -3187,10 +3210,22 @@ orglist = function(json, state) { }, "class": "btn btn-primary btn-block", type: "button", - onclick: "makeOrg();", + id: 'create_btn', + onclick: "makeOrg('new');", value: "Create organisation" }); fieldset.inject(btn); + btn = new HTML('input', { + style: { + width: "200px" + }, + "class": "btn btn-primary btn-block", + type: "button", + id: 'edit_btn', + onclick: "makeOrg('update');", + value: "Edit organisation" + }); + fieldset.inject(btn); return state.widget.inject(fieldset); } };