Skip to content

Commit

Permalink
[#1746] Add POST request to add organisations
Browse files Browse the repository at this point in the history
  • Loading branch information
KasperBrandt committed Oct 2, 2015
1 parent 59a5290 commit 7a4d3cf
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 35 deletions.
3 changes: 2 additions & 1 deletion akvo/rsr/models/organisation.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ def org_type_from_iati_type(cls, iati_type):
help_text=_(u'The main language of the organisation'),
)
organisation_type = ValidXMLCharField(
_(u'organisation type'), max_length=1, db_index=True, choices=ORG_TYPES
_(u'organisation type'), max_length=1, db_index=True, choices=ORG_TYPES, null=True,
blank=True
)
new_organisation_type = models.IntegerField(
_(u'IATI organisation type'), db_index=True,
Expand Down
70 changes: 53 additions & 17 deletions akvo/rsr/static/scripts-src/myrsr-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2129,12 +2129,41 @@ function addOrgModal() {
/* Submit the new org */
function submitModal() {
if (allInputsFilled()) {
console.log("Do something to create the new organisation");
var api_url, request, message, form, form_data;
// TODO: Check if name or long_name already exist
// Add organisation to DB
form = document.querySelector('#addOrganisation');
form_data = serialize(form);

api_url = '/rest/v1/organisation/?format=json';

request = new XMLHttpRequest();
request.open('POST', api_url, true);
request.setRequestHeader("X-CSRFToken", csrftoken);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var response;
response = JSON.parse(request.responseText);
// TODO: Add organisation to all organisation typeaheads

return false;
} else {
// We reached our target server, but it returned an error
}
};

request.onerror = function() {
// There was a connection error of some sort
message = '<div class="help-block-error"><span class="glyphicon glyphicon-remove-circle"></span> Connection error, check your internet connection</div>';

finishSave(step, message);
return false;
};

request.send(form_data);

/* We will need to populate the "Reporting organisation" typeahead
** with the new org if creation is successful. We can't do this
** until we get back the ID of the new org.
*/
cancelModal();
} else {
// call to allInputsFilled() shows error message
Expand Down Expand Up @@ -2185,32 +2214,39 @@ function addOrgModal() {
React.DOM.div( {className:"orgModal"},
React.DOM.div( {className:"modalContents"},
React.DOM.h4(null, "Add new organisation"),
React.DOM.form(null,
React.DOM.form( {id:"addOrganisation"},
React.DOM.div( {className:"row"},
React.DOM.div( {className:"inputContainer newOrgName col-md-4"},
React.DOM.input( {id:"newOrgName", type:"text", className:"form-control", maxLength:"25"}),
React.DOM.label( {for:"newOrgName", className:"control-label"}, "Name: " ),
React.DOM.div( {className:"inputContainer newOrgName col-md-6"},
React.DOM.input( {name:"name", id:"newOrgName", type:"text", className:"form-control", maxLength:"25"}),
React.DOM.label( {htmlFor:"newOrgName", className:"control-label"}, "Name: " ),
React.DOM.p( {className:"helpBlock"})
),
React.DOM.div( {className:"inputContainer newOrgLongName col-md-4"},
React.DOM.input( {id:"newOrgLongName", type:"text", className:"form-control", maxLength:"75"}),
React.DOM.label( {for:"newOrgLongName", className:"control-label"}, "Long name: " ),
React.DOM.div( {className:"inputContainer newOrgLongName col-md-6"},
React.DOM.input( {name:"long_name", id:"newOrgLongName", type:"text", className:"form-control", maxLength:"75"}),
React.DOM.label( {htmlFor:"newOrgLongName", className:"control-label"}, "Long name: " ),
React.DOM.p( {className:"helpBlock"})
)
),
React.DOM.div( {className:"row"},
React.DOM.div( {className:"inputContainer newOrgIatiId col-md-6"},
React.DOM.input( {name:"iati_org_id", id:"newOrgIatiId", type:"text", className:"form-control", maxLength:"75"}),
React.DOM.label( {htmlFor:"newOrgIatiId", className:"control-label"}, "Organisation IATI identifier: " ),
React.DOM.p( {className:"helpBlock"})
),
React.DOM.div( {className:"IATIOrgTypeContainer inputContainer col-md-4"},
React.DOM.select( {id:"newOrgIATIType", className:"form-control"},
React.DOM.option( {value:"10", selected:true}, "10 - Government"),
React.DOM.div( {className:"IATIOrgTypeContainer inputContainer col-md-6"},
React.DOM.select( {name:"new_organisation_type", id:"newOrgIATIType", className:"form-control"},
React.DOM.option( {value:"10"}, "10 - Government"),
React.DOM.option( {value:"15"}, "15 - Other Public Sector"),
React.DOM.option( {value:"21"}, "21 - International NGO"),
React.DOM.option( {value:"22"}, "22 - National NGO"),
React.DOM.option( {value:"22", selected:true}, "22 - National NGO"),
React.DOM.option( {value:"23"}, "23 - Regional NGO"),
React.DOM.option( {value:"30"}, "30 - Public Private Partnership"),
React.DOM.option( {value:"40"}, "40 - Multilateral"),
React.DOM.option( {value:"60"}, "60 - Foundation"),
React.DOM.option( {value:"70"}, "70 - Private Sector"),
React.DOM.option( {value:"80"}, "80 - Academic, Training and Research")
),
React.DOM.label( {for:"newOrgIATIType", className:"control-label"}, "IATI organisation type: " ),
React.DOM.label( {for:"newOrgIATIType", className:"control-label"}, "Organisation type: " ),
React.DOM.p( {className:"helpBlock"})
)
)
Expand Down
70 changes: 53 additions & 17 deletions akvo/rsr/static/scripts-src/myrsr-admin.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2129,12 +2129,41 @@ function addOrgModal() {
/* Submit the new org */
function submitModal() {
if (allInputsFilled()) {
console.log("Do something to create the new organisation");
var api_url, request, message, form, form_data;
// TODO: Check if name or long_name already exist
// Add organisation to DB
form = document.querySelector('#addOrganisation');
form_data = serialize(form);

api_url = '/rest/v1/organisation/?format=json';

request = new XMLHttpRequest();
request.open('POST', api_url, true);
request.setRequestHeader("X-CSRFToken", csrftoken);
request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

request.onload = function() {
if (request.status >= 200 && request.status < 400) {
var response;
response = JSON.parse(request.responseText);
// TODO: Add organisation to all organisation typeaheads

return false;
} else {
// We reached our target server, but it returned an error
}
};

request.onerror = function() {
// There was a connection error of some sort
message = '<div class="help-block-error"><span class="glyphicon glyphicon-remove-circle"></span> Connection error, check your internet connection</div>';

finishSave(step, message);
return false;
};

request.send(form_data);

/* We will need to populate the "Reporting organisation" typeahead
** with the new org if creation is successful. We can't do this
** until we get back the ID of the new org.
*/
cancelModal();
} else {
// call to allInputsFilled() shows error message
Expand Down Expand Up @@ -2185,32 +2214,39 @@ function addOrgModal() {
<div className="orgModal">
<div className="modalContents">
<h4>Add new organisation</h4>
<form>
<form id="addOrganisation">
<div className="row">
<div className="inputContainer newOrgName col-md-4">
<input id="newOrgName" type="text" className="form-control" maxLength="25"/>
<label for="newOrgName" className="control-label">Name: </label>
<div className="inputContainer newOrgName col-md-6">
<input name="name" id="newOrgName" type="text" className="form-control" maxLength="25"/>
<label htmlFor="newOrgName" className="control-label">Name: </label>
<p className="helpBlock"></p>
</div>
<div className="inputContainer newOrgLongName col-md-4">
<input id="newOrgLongName" type="text" className="form-control" maxLength="75"/>
<label for="newOrgLongName" className="control-label">Long name: </label>
<div className="inputContainer newOrgLongName col-md-6">
<input name="long_name" id="newOrgLongName" type="text" className="form-control" maxLength="75"/>
<label htmlFor="newOrgLongName" className="control-label">Long name: </label>
<p className="helpBlock"></p>
</div>
</div>
<div className="row">
<div className="inputContainer newOrgIatiId col-md-6">
<input name="iati_org_id" id="newOrgIatiId" type="text" className="form-control" maxLength="75"/>
<label htmlFor="newOrgIatiId" className="control-label">Organisation IATI identifier: </label>
<p className="helpBlock"></p>
</div>
<div className="IATIOrgTypeContainer inputContainer col-md-4">
<select id="newOrgIATIType" className="form-control">
<option value="10" selected>10 - Government</option>
<div className="IATIOrgTypeContainer inputContainer col-md-6">
<select name="new_organisation_type" id="newOrgIATIType" className="form-control">
<option value="10">10 - Government</option>
<option value="15">15 - Other Public Sector</option>
<option value="21">21 - International NGO</option>
<option value="22">22 - National NGO</option>
<option value="22" selected>22 - National NGO</option>
<option value="23">23 - Regional NGO</option>
<option value="30">30 - Public Private Partnership</option>
<option value="40">40 - Multilateral</option>
<option value="60">60 - Foundation</option>
<option value="70">70 - Private Sector</option>
<option value="80">80 - Academic, Training and Research</option>
</select>
<label for="newOrgIATIType" className="control-label">IATI organisation type: </label>
<label for="newOrgIATIType" className="control-label">Organisation type: </label>
<p className="helpBlock"></p>
</div>
</div>
Expand Down

0 comments on commit 7a4d3cf

Please sign in to comment.