<commit_before>'use strict';

const util = require('util');
const EventEmitter = require('events');

var extend = require('backbone-extend-standalone');
var forms = require('forms');
var cloudinary = require('cloudinary');
var ContentType;
var ensureAuthenticated;

const pubSubEvent = require('../utils/pubsubevent');

// Custom Middleware
ensureAuthenticated = function (req, res, next) {
  if (req.isAuthenticated()) { return next(); }
  res.redirect('/admin/login');
};

// Object definition

ContentType = function (options) {
  EventEmitter.call(this);

  options = options || {};

  this.name = options.name;

  if (options.id) {
    this.id = this.id || options.id.replace(/[\W_]/, '').toLowerCase();

    this.path = this.path || ('/' + this.id);
    this.hash = this.hash || ('#' + this.id);
    this.formName = this.formName || (this.id + 'Form');
  }

  this.on('updated', data => this.updatedEvent(data));
};

util.inherits(ContentType, EventEmitter);

ContentType.extend = extend;

// Object properties

Object.assign(ContentType.prototype, pubSubEvent);

ContentType.prototype.updatedEvent = function () {
  this.getData(true);
};

ContentType.prototype.config = function () {
  return this.page.config;
};

ContentType.prototype.setPage = function (page) {
  this.page = page;
  this.knex = page.knex;

  this.initPubSubEvent(page.pubsub, this.id);

  return this;
};

ContentType.prototype.setAdminPageAssemble = function (assembleAdminPage) {
  this.assembleAdminPage = assembleAdminPage;
  return this;
};

ContentType.prototype.addToAdmin = function (app) {
  app.get(this.path, function (req, res) {
    res.redirect('/admin');
  });
  app.post(this.path, ensureAuthenticated, this.handlePost.bind(this));
  if (this.getPath) {
    app.get(this.path + (this.getPath === true ? '/:id' : this.getPath), ensureAuthenticated, this.handleGet.bind(this));
  }
  if (this.deletePath) {
    app.delete(this.path + (this.deletePath === true ? '/:id' : this.deletePath), ensureAuthenticated, this.handleDelete.bind(this));
  }
  return this;
};

ContentType.prototype.getId = function () {
  return this.id;
};

ContentType.prototype.getName = function () {
  return this.name || '';
};

ContentType.prototype.getHelp = function () {
  return this.help;
};

ContentType.prototype.getRequiredPermission = function () {
  return this.permission || 'edit content';
};

ContentType.prototype.getRoutes = function () {
  return {};
};

ContentType.prototype.getDataQuery = function () {
  return Promise.resolve();
};

ContentType.prototype.getData = function (reset) {
  let cacheKey = 'contentType::' + this.id;
  let cache = this.page.cache(cacheKey);

  if (cache && !reset) {
    return cache;
  }

  let result = this.getDataQuery()
    .catch(err => {
      this.page.logger.error(err, 'Failed to load data for', this.id);

      let fallback = this.page.fallback(cacheKey);<commit_msg>fix(contenttype): some more detailed logging<commit_after>'use strict';

const util = require('util');
const EventEmitter = require('events');

var extend = require('backbone-extend-standalone');
var forms = require('forms');
var cloudinary = require('cloudinary');
var ContentType;
var ensureAuthenticated;

const pubSubEvent = require('../utils/pubsubevent');

// Custom Middleware
ensureAuthenticated = function (req, res, next) {
  if (req.isAuthenticated()) { return next(); }
  res.redirect('/admin/login');
};

// Object definition

ContentType = function (options) {
  EventEmitter.call(this);

  options = options || {};

  this.name = options.name;

  if (options.id) {
    this.id = this.id || options.id.replace(/[\W_]/, '').toLowerCase();

    this.path = this.path || ('/' + this.id);
    this.hash = this.hash || ('#' + this.id);
    this.formName = this.formName || (this.id + 'Form');
  }

  this.on('updated', data => this.updatedEvent(data));
};

util.inherits(ContentType, EventEmitter);

ContentType.extend = extend;

// Object properties

Object.assign(ContentType.prototype, pubSubEvent);

ContentType.prototype.updatedEvent = function () {
  this.getData(true);
};

ContentType.prototype.config = function () {
  return this.page.config;
};

ContentType.prototype.setPage = function (page) {
  this.page = page;
  this.knex = page.knex;

  this.initPubSubEvent(page.pubsub, this.id);

  return this;
};

ContentType.prototype.setAdminPageAssemble = function (assembleAdminPage) {
  this.assembleAdminPage = assembleAdminPage;
  return this;
};

ContentType.prototype.addToAdmin = function (app) {
  app.get(this.path, function (req, res) {
    res.redirect('/admin');
  });
  app.post(this.path, ensureAuthenticated, this.handlePost.bind(this));
  if (this.getPath) {
    app.get(this.path + (this.getPath === true ? '/:id' : this.getPath), ensureAuthenticated, this.handleGet.bind(this));
  }
  if (this.deletePath) {
    app.delete(this.path + (this.deletePath === true ? '/:id' : this.deletePath), ensureAuthenticated, this.handleDelete.bind(this));
  }
  return this;
};

ContentType.prototype.getId = function () {
  return this.id;
};

ContentType.prototype.getName = function () {
  return this.name || '';
};

ContentType.prototype.getHelp = function () {
  return this.help;
};

ContentType.prototype.getRequiredPermission = function () {
  return this.permission || 'edit content';
};

ContentType.prototype.getRoutes = function () {
  return {};
};

ContentType.prototype.getDataQuery = function () {
  return Promise.resolve();
};

ContentType.prototype.getData = function (reset) {
  let cacheKey = 'contentType::' + this.id;
  let cache = this.page.cache(cacheKey);

  if (cache && !reset) {
    this.page.logger.debug({ contentType: this.id }, 'Found cached data for content type');
    return cache;
  }

  this.page.logger.debug({ contentType: this.id }, 'Looking up new data for content type');

  let result = this.getDataQuery()
    .catch(err => {
      this.page.logger.error(err, 'Failed to load data for', this.id);

      let fallback = this.page.fallback(cacheKey);
{'fraction_non_alphanumeric': 0.1621217159960093, 'fraction_numerical': 0.0, 'mean_word_length': 4.163090128755365, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '609251', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1913, 'n_words': 388}