<commit_before>/*!
    Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
    Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
    Created By: <EMAIL>
    Maintained By: <EMAIL>
*/

(function (can, $) {
  'use strict';

  GGRC.Components('assessmentGeneratorButton', {
    tag: 'assessment-generator-button',
    template: '{{{> /static/mustache/base_objects/generate_assessments_button.mustache}}}',
    scope: {
      audit: null
    },
    events: {
      'a click': function (el, ev) {
        var instance = this.scope.attr('audit') || GGRC.page_instance();
        this._results = null;
        GGRC.Controllers.MapperModal.launch(el, {
          object: 'Audit',
          type: 'Control',
          'join-object-id': this.scope.audit.id,
          'join-mapping': 'program_controls',
          getList: true,
          useTemplates: true,
          relevantTo: [{
            type: instance.program.type,
            id: instance.program.id
          }],
          template: {
            title: '/static/mustache/assessments/generator_title.mustache',
            submitButton: 'Generate Assessments',
            count: 'assessment(s) will be generated'
          },
          callback: this.generateAssessments.bind(this)
        });
      },
      generateAssessments: function (list, options) {
        var que = new RefreshQueue();

        this._results = null;
        que.enqueue(list).trigger().then(function (items) {
          var results = _.map(items, function (item) {
            return this.generateModel(item, options.assessmentTemplate);
          }.bind(this));
          this._results = results;

          $.when.apply($, results)
            .always(options.context.closeModal.bind(options.context))
            .done(this.notify.bind(this))
            .fail(this.notify.bind(this));
        }.bind(this));
      },
      generateModel: function (object, template) {
        var assessmentTemplate = CMS.Models.AssessmentTemplate.findInCacheById(
          template);
        var title = object.title + ' assessment for ' + this.scope.audit.title;
        var data = {
          audit: this.scope.audit,
          object: object.stub(),
          context: this.scope.audit.context,
          template: assessmentTemplate && assessmentTemplate.stub(),
          title: title,
          owners: [CMS.Models.Person.findInCacheById(GGRC.current_user.id)]
        };

        if (assessmentTemplate) {
          if (_.exists(assessmentTemplate, 'procedure_description.length')) {
            data.test_plan = assessmentTemplate.procedure_description;
          }
          if (_.exists(assessmentTemplate, 'test_plan_procedure') &&
              _.exists(object, 'test_plan.length')) {
            data.test_plan = object.test_plan;
          }
        }
        return new CMS.Models.Assessment(data).save();
      },
      notify: function () {
        var success;
        var errors;
        var msg;

        if (!this._results) {
          return;
        }
        success = _.filter(this._results, function (assessment) {
          return !_.isNull(assessment) &&
            !(assessment.state && assessment.state() === 'rejected');
        }).length;
        errors = _.filter(this._results, function (assessment) {
          return assessment.state && assessment.state() === 'rejected';
        }).length;

        if (errors < 1) {
          if (success === 0) {
            msg = {
              success: 'Every Control already has an Assessment!'
            };
          } else {
            msg = {
              success: success + ' Assessments successfully created.'
            };
          }
        } else {
          msg = {
            error: 'An error occured when creating Assessments.'
          };
        }

        $(document.body).trigger('ajax:flash', msg);
      }
    }
  });
})(window.can, window.can.$);
<commit_msg>Reload page after asssessment creation<commit_after>/*!
    Copyright (C) 2015 Google Inc., authors, and contributors <see AUTHORS file>
    Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
    Created By: <EMAIL>
    Maintained By: <EMAIL>
*/

(function (can, $) {
  'use strict';

  GGRC.Components('assessmentGeneratorButton', {
    tag: 'assessment-generator-button',
    template: '{{{> /static/mustache/base_objects/generate_assessments_button.mustache}}}',
    scope: {
      audit: null
    },
    events: {
      'a click': function (el, ev) {
        var instance = this.scope.attr('audit') || GGRC.page_instance();
        this._results = null;
        GGRC.Controllers.MapperModal.launch(el, {
          object: 'Audit',
          type: 'Control',
          'join-object-id': this.scope.audit.id,
          'join-mapping': 'program_controls',
          getList: true,
          useTemplates: true,
          relevantTo: [{
            type: instance.program.type,
            id: instance.program.id
          }],
          template: {
            title: '/static/mustache/assessments/generator_title.mustache',
            submitButton: 'Generate Assessments',
            count: 'assessment(s) will be generated'
          },
          callback: this.generateAssessments.bind(this)
        });
      },
      generateAssessments: function (list, options) {
        var que = new RefreshQueue();

        this._results = null;
        que.enqueue(list).trigger().then(function (items) {
          var results = _.map(items, function (item) {
            return this.generateModel(item, options.assessmentTemplate);
          }.bind(this));
          this._results = results;
          $.when.apply($, results)
            .always(function () {
              // We reload page after creation of assessments
              // because currently is no way to get custom attributes dynamically
              setTimeout(function () {
                window.location.reload();
              }, 3000);
            })
            .done(this.notify.bind(this))
            .fail(this.notify.bind(this));
        }.bind(this));
      },
      generateModel: function (object, template) {
        var assessmentTemplate = CMS.Models.AssessmentTemplate.findInCacheById(
          template);
        var title = object.title + ' assessment for ' + this.scope.audit.title;
        var data = {
          audit: this.scope.audit,
          object: object.stub(),
          context: this.scope.audit.context,
          template: assessmentTemplate && assessmentTemplate.stub(),
          title: title,
          owners: [CMS.Models.Person.findInCacheById(GGRC.current_user.id)]
        };

        if (assessmentTemplate) {
          if (_.exists(assessmentTemplate, 'procedure_description.length')) {
            data.test_plan = assessmentTemplate.procedure_description;
          }
          if (_.exists(assessmentTemplate, 'test_plan_procedure') &&
              _.exists(object, 'test_plan.length')) {
            data.test_plan = object.test_plan;
          }
        }
        return new CMS.Models.Assessment(data).save();
      },
      notify: function () {
        var success;
        var errors;
        var msg;

        if (!this._results) {
          return;
        }
        success = _.filter(this._results, function (assessment) {
          return !_.isNull(assessment) &&
            !(assessment.state && assessment.state() === 'rejected');
        }).length;
        errors = _.filter(this._results, function (assessment) {
          return assessment.state && assessment.state() === 'rejected';
        }).length;

        if (errors < 1) {
          if (success === 0) {
            msg = {
              success: 'Every Control already has an Assessment!'
            };
          } else {
            msg = {
              success: success + ' Assessments successfully created.'
            };
          }
        } else {
          msg = {
            error: 'An error occured when creating Assessments.'
          };
        }

        $(document.body).trigger('ajax:flash', msg);
      }
    }
  });
})(window.can, window.can.$);

{'fraction_non_alphanumeric': 0.12801872843765402, 'fraction_numerical': 0.0024642681123706258, 'mean_word_length': 1.9072349570200573, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '416588', 'n_tokens_mistral': 2241, 'n_tokens_neox': 2200, 'n_words': 460}