Ignore:
Timestamp:
03/12/14 02:23:11 (11 years ago)
Author:
hendrikvanantwerpen
Message:
  • Server handles the new flat response format correctly.
  • Client widgets and survey rendering creates a flat structure.
  • Fixed logic error in checking if questions in survey are published.
  • Restrict accepted properties in answers and reject empty strings as properties.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Dev/trunk/src/server/app.js

    r507 r508  
    154154    }
    155155    function areDocsPublished(docs) {
    156         return _.every(docs,isDocPublished);
     156        return _.every(docs, isDocPublished);
    157157    }
    158158    function isDocPublished(doc) {
     
    592592            if ( !areDocsUnique(doc.questions) ) {
    593593                hr = new HTTPResult(400,{error:"Survey contains duplicate questions."});
    594             } else if ( !areDocsPublished(doc.questions) || isDocPublished(doc) ) {
     594            } else if ( isDocPublished(doc) && !areDocsPublished(doc.questions) ) {
    595595                hr = new HTTPResult(400,{error:"Cannot publish Survey with unpublished questions."});
    596596            } else {
     
    610610            var doc = req.body;
    611611            var rev = etags.parse(req.header('If-Match'))[0] || (doc && doc._rev);
    612             var hr;
    613612            if ( !areDocsUnique(doc.questions) ) {
    614613                new HTTPResult(400,{error:"Survey contains duplicate questions."})
    615614                .handle(res.send.bind(res));
    616             } else if ( !areDocsPublished(doc.questions) || isDocPublished(doc) ) {
    617                 hr = new HTTPResult(400,{error:"Cannot publish Survey with unpublished questions."});
     615            } else if ( isDocPublished(doc) && !areDocsPublished(doc.questions) ) {
     616                new HTTPResult(400,{error:"Cannot publish Survey with unpublished questions."})
     617                .handle(res.send.bind(res));
    618618            } else {
    619619                putDocument(id,rev,'Survey',doc)
     
    718718            .handle({
    719719                200: function(responses) {
    720                     var flatResponses = responsesToVariables(responses);
     720                    var answers = _.map(responses,function(response){
     721                        return response.answers;
     722                    });
    721723                    res.set({
    722724                        'Content-Disposition': 'attachment; filename=surveyRun-'+id+'-responses.csv'
    723725                    });
    724726                    res.status(200);
    725                     writeObjectsToCSVStream(flatResponses, res);
     727                    writeObjectsToCSVStream(answers, res);
    726728                    res.end();
    727729                },
     
    953955}
    954956
    955 function responsesToVariables(responses) {
    956     return _.map(responses, responseToVariables);
    957 }
    958 
    959 function responseToVariables(response) {
    960     var result = flattenObject(response.answers);
    961     return result;
    962 }
    963 
    964 function flattenObject(value) {
    965     var result = {};
    966     (function agg(val,key,res){
    967         if ( _.isObject(val) ) {
    968             var keys = _.keys(val);
    969             // FIXME : dirty hack for questions with only one input
    970             if ( keys.length === 1 ) {
    971                 agg(val[keys[0]],key,res);
    972             } else {
    973                 _.forEach(val, function(v,k){
    974                     agg(v,(key ? key+'.' : '')+k,res);
    975                 });
    976             }
    977         } else if ( _.isArray(val) ) {
    978             // FIXME : dirty hack for questions with only one input
    979             if ( val.length === 1 ) {
    980                 agg(val[0],key,res);
    981             } else {
    982                 _.forEach(val, function(v,i){
    983                     agg(v,(key ? key+'.' : '')+i,res);
    984                 });
    985             }
    986         } else {
    987             res[key] = val;
    988         }
    989     })(value,null,result);
    990     return result;
    991 }
    992 
    993957function writeObjectsToCSVStream(objects, stream, prelude) {
    994958    var keys = _.chain(objects)
Note: See TracChangeset for help on using the changeset viewer.