<commit_before>Indicator = require('../models/indicator').model
Theme = require('../models/theme').model
_ = require('underscore')
async = require('async')
Q = require('q')

exports.index = (req, res) ->
  Theme.getFatThemes( (err, themes) ->
    if err?
      console.error err
      return res.render(500, "Error fetching the themes")
    res.render "themes/index", themes: themes
  )

exports.show = (req, res) ->
  Theme
    .findOne(_id: req.params.id)
    .populate('owner')
    .exec( (err, theme) ->
      if err?
        console.error err
        return res.render(500, "Error fetching the theme")

      unless theme?
        error = "Could not find theme with ID #{req.params.id}"
        console.error error
        return res.send(404, error)

      theme.toObjectWithNestedPage().then( (themeObject) ->
        Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
          res.render "themes/show",
            theme: themeObject,
            themeJSON: JSON.stringify(themeObject),
            indicators: indicators
        )
      ).fail( (err) ->
        console.error err
        return res.render(500, "Error fetching theme page")
      )
    )

exports.showDraft = (req, res) ->
  Q.nsend(
    Theme.findOne(_id: req.params.id).populate('owner'),
    'exec'
  ).then( (theme) ->
    unless theme?
      error = "Could not find theme with ID #{req.params.id}"
      console.error error
      return res.send(404, error)

    theme = theme.toObjectWithNestedPage(draft: true)
    .then((themeObject) ->
      Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
        res.render "themes/show",
          theme: themeObject,
          themeJSON: JSON.stringify(themeObject),
          indicators: indicators
      )
    ).fail((err) ->
      console.error err
      return res.render(500, "Error fetching the theme page")
    )

  ).fail((err) ->
    console.error err
    return res.render(500, "Error fetching the theme")
  )

exports.publishDraft = (req, res) ->
  Q.nsend(
    Theme.findOne(_id: req.params.id).populate('owner'),
    'exec'
  ).then( (theme) ->
    unless theme?
      error = "Could not find theme with ID #{req.params.id}"
      console.error error
      return res.send(404, error)

    page = null
    theme.publishDraftPage()
    .then( (publishedPage) ->
      theme.toObjectWithNestedPage()
    ).then( (themeObject) ->
      Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
        res.render "themes/show",
          theme: themeObject,
          themeJSON: JSON.stringify(themeObject),
          indicators: indicators
      )
    ).fail((err) ->
      console.error err
      return res.render(500, "Error fetching the theme page")
    )

  ).fail((err) ->
    console.error err
    return res.render(500, "Error fetching the theme")
  )

exports.discardDraft = (req, res) ->
  Q.nsend(
    Theme.findOne(_id: req.params.id).populate('owner'),
    'exec'
  ).then( (theme) ->
    unless theme?
      error = "Could not find theme with ID #{req.params.id}"
      console.error error
      return res.send(404, error)

    page = null
    theme.discardDraft()
    .then( (publishedPage) ->
      theme.toObjectWithNestedPage()
    ).then( (themeObject) ->
      Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
        res.render "themes/show",
          theme: themeObject,
          themeJSON: JSON.stringify(themeObject),
          indicators: indicators
      )
    ).fail((err) ->
      console.error err
      return res.render(500, "Error fetching the theme page")
    )

  ).fail((err) ->
    console.error err
    return res.render(500, "Error fetching the theme")
  )
<commit_msg>Better promise usage for Theme controller<commit_after>Indicator = require('../models/indicator').model
Theme = require('../models/theme').model
_ = require('underscore')
async = require('async')
Q = require('q')

exports.index = (req, res) ->
  Theme.getFatThemes( (err, themes) ->
    if err?
      console.error err
      return res.render(500, "Error fetching the themes")
    res.render "themes/index", themes: themes
  )

exports.show = (req, res) ->
  Theme
    .findOne(_id: req.params.id)
    .populate('owner')
    .exec( (err, theme) ->
      if err?
        console.error err
        return res.render(500, "Error fetching the theme")

      unless theme?
        error = "Could not find theme with ID #{req.params.id}"
        console.error error
        return res.send(404, error)

      theme.toObjectWithNestedPage().then( (themeObject) ->
        Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
          res.render "themes/show",
            theme: themeObject,
            themeJSON: JSON.stringify(themeObject),
            indicators: indicators
        )
      ).fail( (err) ->
        console.error err
        return res.render(500, "Error fetching theme page")
      )
    )

exports.showDraft = (req, res) ->
  Q.nsend(
    Theme.findOne(_id: req.params.id).populate('owner'),
    'exec'
  ).then( (theme) ->
    unless theme?
      error = "Could not find theme with ID #{req.params.id}"
      console.error error
      return res.send(404, error)

    theme.toObjectWithNestedPage(draft: true)
  ).then( (themeObject) ->

    Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
      res.render "themes/show",
        theme: themeObject,
        themeJSON: JSON.stringify(themeObject),
        indicators: indicators
    )

  ).fail((err) ->
    console.error err
    return res.render(500, "Error fetching the theme")
  )

exports.publishDraft = (req, res) ->
  theTheme = null

  Q.nsend(
    Theme.findOne(_id: req.params.id).populate('owner'),
    'exec'
  ).then( (theme) ->
    theTheme = theme

    unless theme?
      error = "Could not find theme with ID #{req.params.id}"
      console.error error
      return res.send(404, error)

    theTheme.publishDraftPage()
  ).then( (publishedPage) ->
    theTheme.toObjectWithNestedPage()
  ).then( (themeObject) ->

    Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
      res.render "themes/show",
        theme: themeObject,
        themeJSON: JSON.stringify(themeObject),
        indicators: indicators
    )

  ).fail((err) ->
    console.error err
    return res.render(500, "Error fetching the theme")
  )

exports.discardDraft = (req, res) ->
  theTheme = null

  Q.nsend(
    Theme.findOne(_id: req.params.id).populate('owner'),
    'exec'
  ).then( (theme) ->
    theTheme = theme

    unless theme?
      error = "Could not find theme with ID #{req.params.id}"
      console.error error
      return res.send(404, error)

    theTheme.discardDraft()
  ).then( (publishedPage) ->
    theTheme.toObjectWithNestedPage()
  ).then( (themeObject) ->

    Theme.getIndicatorsByTheme( themeObject.externalId, (err, indicators) ->
      res.render "themes/show",
        theme: themeObject,
        themeJSON: JSON.stringify(themeObject),
        indicators: indicators
    )

  ).fail((err) ->
    console.error err
    return res.render(500, "Error fetching the theme")
  )

{'fraction_non_alphanumeric': 0.13503700600474794, 'fraction_numerical': 0.009635525764558023, 'mean_word_length': 2.5561072492552137, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 54, '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': '5689511', 'n_tokens_mistral': 2299, 'n_tokens_neox': 2210, 'n_words': 525}