<commit_before>// vim:ts=2:sw=2:softtabstop=2:smarttab:expandtab

/**
 * Client side code for general pycopia model access. 
 * MochiKit is used here  <http://www.mochikit.com/> (loaded seperately).
 *
 * Depends on:
 *   MochiKit.js
 *   proxy.js
 *   ui.js
 */

/////////////// DB objects. ////////////////////

/**
 * DBModel holds table metadata as defined in a .../pycopia/db/models.py file.
 * dbmodel.name is modelname
 * dbmodel._Meta is a list of metadata tuples:
        coltype, colname, default, m2m, nullable, uselist
 */
function DBModel(modelname) {
  this.initialized = false;
  this.name = modelname;
  this._meta = {};
  var d = window.db.get_table_metadata_map(modelname);
  d.addCallback(bind(this._fillModelMetadata, this));
};

DBModel.prototype._fillModelMetadata = function(metamap) {
  this._meta = metamap;
  this.initialized = true;
};

DBModel.prototype.get_column = function(fieldname) {
  return this._meta[fieldname];
};

DBModel.prototype.get_choices = function(fieldname) {
  return window.db.get_choices(this.name, fieldname)
};

DBModel.prototype.all = function() {
  return window.db.query(this.name, {})
};

DBModel.prototype.get = function(entry_id) {
  return window.db.get_row(this.name, entry_id);
};



/**
 * Getter for DBModel object. This object is cached, for
 * performance.
 */
function getDBModel(modelname) {
  var model = window.modelcache[modelname];
  if (typeof(model) == "undefined") {
    model = new DBModel(modelname);
    window.modelcache[modelname] = model;
  };
  return model;
};

/**
 * DBModelInstance represents an instance of a DBModel, which is a
 * table row.
 */
function DBModelInstance(model, str, row) {
  this.data = row;
  this._model = model
  this._str_ = str;
  this.isdeleted = false;
};

DBModelInstance.prototype.toString = function() {
  return this._str_;
};

DBModelInstance.prototype.__dom__ = function(node) {
  // TODO object view registry. This is the default, generic generator.
  var tbl = TABLE(null, createDOM("caption", null, this._model.name),
              THEAD(null,
                TR(null,
                  TH(null, "Field"), TH(null, "Value"))));
  var body = TBODY(null);
  tbl.appendChild(body);
  var metadata = this._model._meta;
  for (var colname in metadata) {
    var coldata = metadata[colname];
    if (coldata[0] == "RelationProperty") {
      var tr = TR(null,
                    TD(null, colname),
                    TD(null, 
                      UL(null, map(function (obj) {
                                return LI({id: obj.get_id()}, obj.toString());
                              },
                            this.data[colname])
                      )
                    )
               );
    } else {
      var tr = TR({"class": coldata[0]},
                    TD(null, colname),
                    TD(null, this.data[colname].toString()));
    }
    body.appendChild(tr);
  };
  return tbl;
};

/**
 * Return a useful Node id. 
 */
DBModelInstance.prototype.get_id = function() {
    return this._model.name + "_" + this.data.id
};

/**
 * Delete the instance in the database.
 */
DBModelInstance.prototype.deleterow = function() {
  if (!this.isdeleted) {
    var d = window.db.deleterow(this.model.name, this.data.id);
    d.addCallback(bind(this._delete_db, this));
  };
};<commit_msg>Fixup generic DB model DOM coercion.<commit_after>// vim:ts=2:sw=2:softtabstop=2:smarttab:expandtab

/**
 * Client side code for general pycopia model access. 
 * MochiKit is used here  <http://www.mochikit.com/> (loaded seperately).
 *
 * Depends on:
 *   MochiKit.js
 *   proxy.js
 *   ui.js
 */

/////////////// DB objects. ////////////////////

/**
 * DBModel holds table metadata as defined in a .../pycopia/db/models.py file.
 * dbmodel.name is modelname
 * dbmodel._Meta is a list of metadata tuples:
        coltype, colname, default, m2m, nullable, uselist
 */
function DBModel(modelname) {
  this.initialized = false;
  this.name = modelname;
  this._meta = {};
  var d = window.db.get_table_metadata_map(modelname);
  d.addCallback(bind(this._fillModelMetadata, this));
};

DBModel.prototype._fillModelMetadata = function(metamap) {
  this._meta = metamap;
  this.initialized = true;
};

DBModel.prototype.get_column = function(fieldname) {
  return this._meta[fieldname];
};

DBModel.prototype.get_choices = function(fieldname) {
  return window.db.get_choices(this.name, fieldname)
};

DBModel.prototype.all = function() {
  return window.db.query(this.name, {})
};

DBModel.prototype.get = function(entry_id) {
  return window.db.get_row(this.name, entry_id);
};



/**
 * Getter for DBModel object. This object is cached, for
 * performance.
 */
function getDBModel(modelname) {
  var model = window.modelcache[modelname];
  if (typeof(model) == "undefined") {
    model = new DBModel(modelname);
    window.modelcache[modelname] = model;
  };
  return model;
};


/**
 * Represents a NULL value.
 */

function NULL() {
};

NULL.prototype.toString = function() {
  return NBSP;
};


/**
 * DBModelInstance represents an instance of a DBModel, which is a
 * table row.
 */
function DBModelInstance(model, str, row) {
  this.data = row;
  this._model = model
  this._str_ = str;
  this.isdeleted = false;
};

DBModelInstance.prototype.toString = function() {
  return this._str_;
};

DBModelInstance.prototype.__dom__ = function(node) {
  // TODO object view registry. This is the default, generic generator.
  var tbl = TABLE(null, createDOM("caption", null, this._model.name),
              THEAD(null,
                TR(null,
                  TH(null, "Field"), TH(null, "Value"))));
  var body = TBODY(null);
  tbl.appendChild(body);
  var metadata = this._model._meta;
  for (var colname in metadata) {
    var colmetadata = metadata[colname];
    var coldata = this.data[colname];
    if (coldata === null) {
      coldata = new NULL();
    };
    if (colmetadata[0] == "RelationProperty") {
      if (colmetadata[5] == true) { // uselist
        var tr = TR(null,
                      TD(null, colname),
                      TD(null, 
                        UL(null, map(function (obj) {
                                  return LI({id: obj.get_id()}, obj.toString());
                                }, coldata)
                        )
                      )
                 );
      } else {
        var tr = TR(null,
                      TD(null, colname),
                      TD(null, coldata.toString())
                 );

      };
    } else {
      var tr = TR({"class": colmetadata[0]},
                    TD(null, colname),
                    TD(null, coldata.toString()));
    }
    body.appendChild(tr);
  };
  return tbl;
};

/**
 * Return a useful Node id. 
 */
DBModelInstance.prototype.get_id = function() {
    return this._model.name + "_" + this.data.id
};

/**
 * Delete the instance in the database.
 */
DBModelInstance.prototype.deleterow = function() {
  if (!this.isdeleted) {
    var d = window.db.deleterow(this.model.name, this.data.id);
    d.addCallback(bind(this._delete_db, this));
  };
};
{'fraction_non_alphanumeric': 0.14681362159106967, 'fraction_numerical': 0.0018369365550374452, 'mean_word_length': 2.417672621921777, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4403411', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2225, 'n_words': 502}