Commit 9dbf26eb authored by Romain Courteaud's avatar Romain Courteaud

ERP5.get: returns properties based on document default form calculation.

parent 4e0e4b7f
......@@ -62,6 +62,59 @@
});
}
var allowed_field_dict = {
"StringField": null,
"IntegerField": null,
"FloatField": null,
"TextAreaField": null
};
function extractPropertyFromForm(context, id) {
return context.getAttachment(id, "view")
.push(function (blob) {
return jIO.util.readBlobAsText(blob);
})
.push(function (evt) {
return JSON.parse(evt.target.result);
})
.push(function (json) {
var form = json._embedded._view,
converted_json = {
portal_type: json.portal_type
},
form_data_json = {},
field,
key;
form_data_json.form_id = {
"key": [form.form_id.key],
"default": form.form_id["default"]
};
// XXX How to store datetime
for (key in form) {
if (form.hasOwnProperty(key)) {
field = form[key];
if ((key.indexOf('my_') === 0) &&
(field.editable) &&
(allowed_field_dict.hasOwnProperty(field.type))) {
form_data_json[key.substring(3)] = {
"default": field["default"],
"key": field.key
};
converted_json[key.substring(3)] = field["default"];
}
}
}
return {
action_href: form._actions.put.href,
data: converted_json,
form_data: form_data_json
};
});
}
// XXX docstring
function ERP5Storage(spec) {
if (typeof spec.url !== "string" || !spec.url) {
......@@ -73,22 +126,18 @@
}
ERP5Storage.prototype.get = function (id) {
return getDocumentAndHateoas(this, id)
.push(function (response) {
var result = JSON.parse(response.target.responseText),
key;
// action_type;
result.portal_type = result._links.type.name;
return extractPropertyFromForm(this, id)
.push(function (result) {
var key;
result = result.data;
// Remove all ERP5 hateoas links / convert them into jIO ID
for (key in result) {
if (result.hasOwnProperty(key)) {
if (key.indexOf("_") === 0) {
if (!result[key]) {
delete result[key];
}
}
}
return result;
});
};
......
......@@ -55,7 +55,8 @@
this.jio = jIO.createJIO({
type: "erp5",
url: domain
url: domain,
default_view_reference: "bar_view"
});
},
teardown: function () {
......@@ -93,10 +94,73 @@
});
});
test("get ERP5 document with empty form", function () {
var id = "person_module/20150119_azerty",
traverse_url = domain + "?mode=traverse&relative_url=" +
encodeURIComponent(id) + "&view=bar_view",
document_hateoas = JSON.stringify({
// Kept property
"title": "foo",
// Remove all _ properties
"_bar": "john doo",
"_links": {
type: {
name: "Person"
}
},
"_embedded": {
"_view": {
form_id: {
key: "form_id",
"default": "Base_view"
},
"_actions": {
put: {
href: "one erp5 url"
}
}
}
}
}),
server = this.server;
this.server.respondWith("GET", domain, [200, {
"Content-Type": "application/hal+json"
}, root_hateoas]);
this.server.respondWith("GET", traverse_url, [200, {
"Content-Type": "application/hal+json"
}, document_hateoas]);
stop();
expect(10);
this.jio.get(id)
.then(function (result) {
deepEqual(result, {
portal_type: "Person"
}, "Check document");
equal(server.requests.length, 2);
equal(server.requests[0].method, "GET");
equal(server.requests[0].url, domain);
equal(server.requests[0].requestBody, undefined);
equal(server.requests[0].withCredentials, true);
equal(server.requests[1].method, "GET");
equal(server.requests[1].url, traverse_url);
equal(server.requests[1].requestBody, undefined);
equal(server.requests[1].withCredentials, true);
})
.fail(function (error) {
ok(false, error);
})
.always(function () {
start();
});
});
test("get ERP5 document", function () {
var id = "person_module/20150119_azerty",
traverse_url = domain + "?mode=traverse&relative_url=" +
encodeURIComponent(id),
encodeURIComponent(id) + "&view=bar_view",
document_hateoas = JSON.stringify({
// Kept property
"title": "foo",
......@@ -106,6 +170,55 @@
type: {
name: "Person"
}
},
"_embedded": {
"_view": {
form_id: {
key: "form_id",
"default": "Base_view"
},
my_title: {
key: "field_my_title",
"default": "foo",
editable: true,
type: "StringField"
},
my_id: {
key: "field_my_id",
"default": "",
editable: true,
type: "StringField"
},
my_title_non_editable: {
key: "field_my_title_non_editable",
"default": "foo",
editable: false,
type: "StringField"
},
my_start_date: {
key: "field_my_start_date",
"default": "foo",
editable: true,
type: "DateTimeField"
},
your_reference: {
key: "field_your_title",
"default": "bar",
editable: true,
type: "StringField"
},
sort_index: {
key: "field_sort_index",
"default": "foobar",
editable: true,
type: "StringField"
},
"_actions": {
put: {
href: "one erp5 url"
}
}
}
}
}),
server = this.server;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment