<commit_before># Copyright (C) 2013 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>

import json
from collections import namedtuple
from flask import request, flash, session, url_for
from flask.views import View
from ggrc.app import app
from ggrc.rbac import permissions
from ggrc.login import get_current_user
from ggrc.utils import as_json
from ggrc.builder.json import publish
from werkzeug.exceptions import Forbidden
from . import filters
from .common import BaseObjectView, RedirectedPolymorphView
from .tooltip import TooltipView
from ggrc.models.task import Task, queued_task, create_task, make_task_response

"""ggrc.views
Handle non-RESTful views, e.g. routes which return HTML rather than JSON
"""

def get_permissions_json():
  permissions.permissions_for(permissions.get_user())
  return json.dumps(session['permissions'])

def get_config_json():
  return json.dumps(app.config.public_config)

def get_current_user_json():
  current_user = get_current_user()
  return as_json(current_user.log_json())

@app.context_processor
def base_context():
  from ggrc.models import get_model
  return dict(
      get_model=get_model,
      permissions_json=get_permissions_json,
      permissions=permissions,
      config_json=get_config_json,
      current_user_json=get_current_user_json,
      )

from flask import render_template

# Actual HTML-producing routes
#

@app.route("/")
def index():
  """The initial entry point of the app
  """
  return render_template("welcome/index.haml")

from ggrc.login import login_required

@app.route("/dashboard")
@login_required
def dashboard():
  """The dashboard page
  """
  return render_template("dashboard/index.haml")

def generate_query_chunks(query):
  CHUNK_SIZE = 100
  count = query.count()
  for offset in range(0, count, CHUNK_SIZE):
    yield query.order_by('id').limit(CHUNK_SIZE).offset(offset).all()

# Needs to be secured as we are removing @login_required
@app.route("/tasks/reindex", methods=["POST"])
@queued_task
def reindex(task):
  """
  Web hook to update the full text search index
  """
  
  from ggrc.fulltext import get_indexer
  from ggrc.fulltext.recordbuilder import fts_record_for, model_is_indexed

  indexer = get_indexer()
  indexer.delete_all_records(False)

  from ggrc.models import all_models
  from ggrc.app import db

  # Find all models then remove base classes
  #   (If we don't remove base classes, we get duplicates in the index.)
  inheritance_base_models = [
      all_models.Directive, all_models.SystemOrProcess, all_models.Response
      ]
  models = set(all_models.all_models) - set(inheritance_base_models)
  models = [model for model in models if model_is_indexed(model)]

  for model in models:
    mapper_class = model._sa_class_manager.mapper.base_mapper.class_
    query = model.query.options(
        db.undefer_group(mapper_class.__name__+'_complete'),
        )
    for query_chunk in generate_query_chunks(query):
      for instance in query_chunk:
        indexer.create_record(fts_record_for(instance), False)
      db.session.commit()

  return app.make_response((
    'success', 200, [('Content-Type', 'text/html')]))

@app.route("/admin/reindex", methods=["POST"])
@login_required
def admin_reindex():
  """Calls a webhook that reindexes indexable objects
  """
  from ggrc import settings
  if not permissions.is_allowed_read("/admin", 1):
    raise Forbidden()
  tq = create_task("reindex", reindex)   
  return tq.make_response(app.make_response(("scheduled %s" % tq.name, 
                                            200, 
                                            [('Content-Type', 'text/html')])))

@app.route("/admin")
@login_required
def admin():
  """The admin dashboard page
  """
  if not permissions.is_allowed_read("/admin", 1):
    raise Forbidden()
  return render_template("admin/index.haml")

@app.route("/design/with_new_layout")
@login_required
def styleguide_with_new_layout():
  """The style guide page with the new layout
  """
  return render_template("styleguide/styleguide_with_new_layout.haml")

@app.route("/design")
@login_required
def styleguide():
  """The style guide page
  """
  return render_template("styleguide/styleguide.haml")

def allowed_file(filename):
  return filename.rsplit('.',1)[1] == 'csv'

ADMIN_KIND_TEMPLATES = {
  "processes": "Process_Import_Template.csv",
  "systems": "System_Import_Template.csv",
  "admin": "People_Import_Template.csv",
}

@app.route("/<admin_kind>/import_template", methods=['GET'])
def process_import_template(admin_kind):
  from flask import current_app
  if admin_kind in ADMIN_KIND_TEMPLATES:
    filename = ADMIN_KIND_TEMPLATES[admin_kind]
    headers = [('Content-Type', 'text/csv'), ('Content-Disposition','attachment; filename="{}"'.format(filename))]
    body = render_template("csv_files/" + filename)
    return current_app.make_response((body, 200, headers))
  return current_app.make_response((
      "No template for that type.", 404, []))

@app.route("/programs/<program_id>/import_template", methods=['GET'])
def system_program_import_template(program_id):
  from flask import current_app
  from ggrc.models import Program
  program = Program.query.get(program_id)
  if program:
    template_name = "System_Program_Import_Template.csv"
    headers = [('Content-Type', 'text/csv'), ('Content-Disposition','attachment; filename="{}"'.format(template_name))]
    options = {"program_slug": program.slug}
    body = render_template("csv_files/" + template_name, **options)
    return current_app.make_response((body, 200, headers))
  return current_app.make_response((
      "No such program.", 404, []))

@app.route("/task/import_people", methods = ['POST'])
@queued_task
def import_people_task(task):
  from ggrc.converters.common import ImportException
  from ggrc.converters.people import PeopleConverter
  from ggrc.converters.import_helper import handle_csv_import
  from ggrc.models import Person
  import ggrc.views
  
  csv_file = task.parameters.get("csv_file")
  dry_run = task.parameters.get("dry_run")
  try:
    options = {}
    options['dry_run'] = dry_run
    converter = handle_csv_import(PeopleConverter, csv_file.splitlines(True), **options)
    if dry_run:
      options['converter'] = converter
      options['results'] = [o for i,o in enumerate(converter.objects) if i < 20 or o.errors or o.warnings or o.messages][:50]   # Limit displayed table to a small sample
      options['heading_map'] = converter.object_map
      return render_template("people/import_result.haml", **options)
    else:
      count = len(converter.objects)
      flash(u'Successfully imported {} person{}'.format(count, 's' if count > 1 else ''), 'notice')
      return import_redirect("/admin")

  except ImportException as e:
    if e.show_preview:
      converter = e.converter
      return render_template("people/import_result.haml", exception_message=e,<commit_msg>Showing all rows in import people preview<commit_after># Copyright (C) 2013 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>

import json
from collections import namedtuple
from flask import request, flash, session, url_for
from flask.views import View
from ggrc.app import app
from ggrc.rbac import permissions
from ggrc.login import get_current_user
from ggrc.utils import as_json
from ggrc.builder.json import publish
from werkzeug.exceptions import Forbidden
from . import filters
from .common import BaseObjectView, RedirectedPolymorphView
from .tooltip import TooltipView
from ggrc.models.task import Task, queued_task, create_task, make_task_response

"""ggrc.views
Handle non-RESTful views, e.g. routes which return HTML rather than JSON
"""

def get_permissions_json():
  permissions.permissions_for(permissions.get_user())
  return json.dumps(session['permissions'])

def get_config_json():
  return json.dumps(app.config.public_config)

def get_current_user_json():
  current_user = get_current_user()
  return as_json(current_user.log_json())

@app.context_processor
def base_context():
  from ggrc.models import get_model
  return dict(
      get_model=get_model,
      permissions_json=get_permissions_json,
      permissions=permissions,
      config_json=get_config_json,
      current_user_json=get_current_user_json,
      )

from flask import render_template

# Actual HTML-producing routes
#

@app.route("/")
def index():
  """The initial entry point of the app
  """
  return render_template("welcome/index.haml")

from ggrc.login import login_required

@app.route("/dashboard")
@login_required
def dashboard():
  """The dashboard page
  """
  return render_template("dashboard/index.haml")

def generate_query_chunks(query):
  CHUNK_SIZE = 100
  count = query.count()
  for offset in range(0, count, CHUNK_SIZE):
    yield query.order_by('id').limit(CHUNK_SIZE).offset(offset).all()

# Needs to be secured as we are removing @login_required
@app.route("/tasks/reindex", methods=["POST"])
@queued_task
def reindex(task):
  """
  Web hook to update the full text search index
  """
  
  from ggrc.fulltext import get_indexer
  from ggrc.fulltext.recordbuilder import fts_record_for, model_is_indexed

  indexer = get_indexer()
  indexer.delete_all_records(False)

  from ggrc.models import all_models
  from ggrc.app import db

  # Find all models then remove base classes
  #   (If we don't remove base classes, we get duplicates in the index.)
  inheritance_base_models = [
      all_models.Directive, all_models.SystemOrProcess, all_models.Response
      ]
  models = set(all_models.all_models) - set(inheritance_base_models)
  models = [model for model in models if model_is_indexed(model)]

  for model in models:
    mapper_class = model._sa_class_manager.mapper.base_mapper.class_
    query = model.query.options(
        db.undefer_group(mapper_class.__name__+'_complete'),
        )
    for query_chunk in generate_query_chunks(query):
      for instance in query_chunk:
        indexer.create_record(fts_record_for(instance), False)
      db.session.commit()

  return app.make_response((
    'success', 200, [('Content-Type', 'text/html')]))

@app.route("/admin/reindex", methods=["POST"])
@login_required
def admin_reindex():
  """Calls a webhook that reindexes indexable objects
  """
  from ggrc import settings
  if not permissions.is_allowed_read("/admin", 1):
    raise Forbidden()
  tq = create_task("reindex", reindex)   
  return tq.make_response(app.make_response(("scheduled %s" % tq.name, 
                                            200, 
                                            [('Content-Type', 'text/html')])))

@app.route("/admin")
@login_required
def admin():
  """The admin dashboard page
  """
  if not permissions.is_allowed_read("/admin", 1):
    raise Forbidden()
  return render_template("admin/index.haml")

@app.route("/design/with_new_layout")
@login_required
def styleguide_with_new_layout():
  """The style guide page with the new layout
  """
  return render_template("styleguide/styleguide_with_new_layout.haml")

@app.route("/design")
@login_required
def styleguide():
  """The style guide page
  """
  return render_template("styleguide/styleguide.haml")

def allowed_file(filename):
  return filename.rsplit('.',1)[1] == 'csv'

ADMIN_KIND_TEMPLATES = {
  "processes": "Process_Import_Template.csv",
  "systems": "System_Import_Template.csv",
  "admin": "People_Import_Template.csv",
}

@app.route("/<admin_kind>/import_template", methods=['GET'])
def process_import_template(admin_kind):
  from flask import current_app
  if admin_kind in ADMIN_KIND_TEMPLATES:
    filename = ADMIN_KIND_TEMPLATES[admin_kind]
    headers = [('Content-Type', 'text/csv'), ('Content-Disposition','attachment; filename="{}"'.format(filename))]
    body = render_template("csv_files/" + filename)
    return current_app.make_response((body, 200, headers))
  return current_app.make_response((
      "No template for that type.", 404, []))

@app.route("/programs/<program_id>/import_template", methods=['GET'])
def system_program_import_template(program_id):
  from flask import current_app
  from ggrc.models import Program
  program = Program.query.get(program_id)
  if program:
    template_name = "System_Program_Import_Template.csv"
    headers = [('Content-Type', 'text/csv'), ('Content-Disposition','attachment; filename="{}"'.format(template_name))]
    options = {"program_slug": program.slug}
    body = render_template("csv_files/" + template_name, **options)
    return current_app.make_response((body, 200, headers))
  return current_app.make_response((
      "No such program.", 404, []))

@app.route("/task/import_people", methods = ['POST'])
@queued_task
def import_people_task(task):
  from ggrc.converters.common import ImportException
  from ggrc.converters.people import PeopleConverter
  from ggrc.converters.import_helper import handle_csv_import
  from ggrc.models import Person
  import ggrc.views
  
  csv_file = task.parameters.get("csv_file")
  dry_run = task.parameters.get("dry_run")
  try:
    options = {}
    options['dry_run'] = dry_run
    converter = handle_csv_import(PeopleConverter, csv_file.splitlines(True), **options)
    if dry_run:
      options['converter'] = converter
      options['results'] = converter.objects
      options['heading_map'] = converter.object_map
      return render_template("people/import_result.haml", **options)
    else:
      count = len(converter.objects)
      flash(u'Successfully imported {} person{}'.format(count, 's' if count > 1 else ''), 'notice')
      return import_redirect("/admin")

  except ImportException as e:
    if e.show_preview:
      converter = e.converter
      return render_template("people/import_result.haml", exception_message=e,
{'fraction_non_alphanumeric': 0.11588476183633882, 'fraction_numerical': 0.005029096917882032, 'mean_word_length': 4.55023923444976, 'pattern_counts': {'":': 8, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4481967', 'n_tokens_mistral': 4457, 'n_tokens_neox': 4362, 'n_words': 1033}