Example #1
0
exports.user_page = function (head, req) {
    start({code: 200, headers: {'Content-Type': 'text/html'}});
    var row, rows = [];
    while (row = getRow()) {
        row.value.extra = 'Updated ' + datelib.prettify(row.value.modified);
        rows.push(row);
    }
    events.once('afterResponse', function (info, req, res) {
        ui.addCategories(req, '#categories');
        ui.addUserWebsite(req);
    });
    var last_update;
    _.forEach(rows, function (r) {
        var m = new Date(r.value.modified);
        if (!last_update || m > last_update) {
            last_update = m;
        }
    });
    return {
        title: req.query.name,
        content: templates.render('user_page.html', req, {
            rows: rows,
            total_packages: rows.length,
            last_update: last_update,
            name: req.query.name
        })
    };
};
Example #2
0
 .then(function( documents ) {
   console.log( documents );
   var pushDocument = [];
   _.forEach( documents, function( document ) {
     pushDocument.push( {name: document._id, url: request + document._id} );
   });
   res.json( pushDocument );
 });
Example #3
0
 .then(function( database ) {
   var pushCleanDatabase = [];
   _.forEach( database.databases, function( databaseName ) {
       if ( databaseName.name !== 'undefined' )
         pushCleanDatabase.push( {name:databaseName.name, url: 'database/' + databaseName.name} );
     })
   res.json( pushCleanDatabase );
 })
Example #4
0
 .then(function ( collNames ) {
   var pushCleanDatabaseCollection = [];
   _.forEach( collNames, function( collName ) {
       var cleanName = collName.name.replace( dbName + '.','' );
       if(cleanName !== 'system.indexes') {
         pushCleanDatabaseCollection.push( {name:cleanName, url: request + cleanName} );
       }
     });
   res.json( pushCleanDatabaseCollection );
 });
Example #5
0
function interpolate_msg(msg, name, value, compare_to, vars) {
  var interp_msg = msg.replace(/{{name}}/, name.humanize(true)).
                       replace(/{{value}}/, value).
                       replace(/{{compare_to}}/, compare_to);
  if (vars)
    _.forEach(vars, function(val, name) {
      interp_msg = interp_msg.replace('{{'+name+'}}', val); 
    });
  return interp_msg;
}
Example #6
0
Batch.prototype.execute = function(input, callback) {
  var self = this;

  if (typeof input === 'function') { // if input argument is omitted
    callback = input;
    input = null;
  }

  // if batch is already executed
  if (this._result) {
    throw new Error("Batch already executed.");
  }

  var rdeferred = Promise.defer();
  this._result = rdeferred.promise;
  this._result.then(function(res) {
    self._deferred.resolve(res);
  }, function(err) {
    self._deferred.reject(err);
  });
  this.once('response', function(res) {
    rdeferred.resolve(res);
  });
  this.once('error', function(err) {
    rdeferred.reject(err);
  });

  if (input instanceof Stream) {
    input.pipe(this.stream());
  } else {
    var data;
    if (_.isArray(input)) {
      _.forEach(input, function(record) { self.send(record); });
      self.end();
    } else if (_.isString(input)){
      data = input;
      var stream = this.stream();
      stream.write(data);
      stream.end();
    }
  }

  // return Batch instance for chaining
  return this.thenCall(callback);
};
Example #7
0
 }, function(err, data) {
   if (err) {
     self.emit("error", err);
     return;
   }
   self.emit("response", data, self);
   self.totalSize = data.totalSize;
   _.forEach(data.records, function(record, i) {
     if (!self._stop) {
       self.emit('record', record, i, self.totalFetched++, self);
       self._stop = self.totalFetched >= maxFetch;
     }
   });
   if (!data.done && autoFetch && !self._stop) {
     self._locator = data.nextRecordsUrl.split('/').pop();
     self.execute(options, callback);
   } else {
     self._stop = true;
     self.emit('end', self);
   }
 });
Example #8
0
function validate(obj, config) {  
  
  var errors = newErrors();
  var defaultMessages = config.defaultMessages || {};
  _.without(validation_types, 'required').forEach(function(validation_type) {
    if (!defaultMessages[validation_type]) defaultMessages[validation_type] = {};
  });

  function test_and_add_error_message(prop_name, prop_config, validation_type, value, subtype) {
    if (!prop_config[validation_type]) return;
    var valid_func, compare_to, interpolation_scope_extractor;
    if (subtype) {
      if (prop_config[validation_type][subtype] == undefined) return;
      valid_func = valid_funcs[validation_type][subtype];
      compare_to = prop_config[validation_type][subtype];
      if (interpolation_scope_extractors[validation_type]) 
        interpolation_scope_extractor = interpolation_scope_extractors[validation_type][subtype];
    } else { 
      valid_func = valid_funcs[validation_type];
      compare_to = prop_config[validation_type];
      interpolation_scope_extractor = interpolation_scope_extractors[validation_type];
    }
    if (!valid_func(value, compare_to)) {
      var msg = prop_config.message;
      if (subtype) {
        msg = msg  || defaultMessages[validation_type][subtype] || 
                globalDefaultMessages[validation_type][subtype];
      } else {
        msg = msg  || defaultMessages[validation_type] || 
                globalDefaultMessages[validation_type];
      }
      var vars;
      if (interpolation_scope_extractor)
        vars = interpolation_scope_extractor(value);
      errors.add(prop_name, interpolate_msg(msg, prop_name, value, compare_to, vars));
      return false;
    } else {
      return true;
    }
  }
  
  _.forEach(config.properties, function(prop_config, prop_name) {
    var value = obj[prop_name];
    if (prop_config.object) {
      // recurse
      errors.add(prop_name, validate(value, prop_config.object));
      return; // no other validation types should apply if validation type is 'object'
    }
    for (var i = 0; i < validation_types.length; i++) {
      var validation_type = validation_types[i];
      if (!prop_config[validation_type]) continue;
      if (validation_type != 'required' && 
          (value === undefined || value === null )) 
          continue;
      if (typeof valid_funcs[validation_type] == 'function') {
        var is_valid = test_and_add_error_message(prop_name, prop_config, 
                         validation_type, value);
        if (!is_valid && validation_type == 'required') break;
      } else {
        _.keys(valid_funcs[validation_type]).forEach(function(subtype) {
          test_and_add_error_message(prop_name, prop_config, validation_type, 
            value, subtype);
        });        
      }
    }
  });
  if (!errors.isEmpty()) return errors;
}