def import_processes():
  from werkzeug import secure_filename
  from ggrc.converters.common import ImportException
  from ggrc.converters.systems import SystemsConverter
  from ggrc.converters.import_helper import handle_csv_import

  if not permissions.is_allowed_read("/admin", 1):
    raise Forbidden()

  if request.method == 'POST':
    if 'cancel' in request.form:
      return import_redirect('/admin')
    dry_run = not ('confirm' in request.form)
    csv_file = request.files['file']
    try:
      if csv_file and allowed_file(csv_file.filename):
        filename = secure_filename(csv_file.filename)
        converter = handle_csv_import(SystemsConverter, csv_file, dry_run = dry_run, is_biz_process='1')
        if dry_run:
          return render_template("systems/import_result.haml",
            converter = converter, results=converter.objects, heading_map=converter.object_map)
        else:
          count = len(converter.objects)
          flash(u'Successfully imported {} process{}'.format(count, 'es' if count > 1 else ''), 'notice')
          return import_redirect("/admin")
      else:
        file_msg = "Could not import: invalid csv file."
        return render_template("directives/import_errors.haml", exception_message = file_msg)
    except ImportException as e:
      return render_template("directives/import_errors.haml", exception_message = str(e))

  return render_template("systems/import.haml", import_kind = 'Processes')
def import_controls(directive_id):
  from werkzeug import secure_filename
  from ggrc.converters.common import ImportException
  from ggrc.converters.controls import ControlsConverter
  from ggrc.converters.import_helper import handle_csv_import
  from ggrc.models import Directive
  import ggrc.views

  directive = Directive.query.get(directive_id)
  directive_url =\
    getattr(ggrc.views, directive.__class__.__name__).url_for(directive)

  if request.method == 'POST':
    if 'cancel' in request.form:
      return import_redirect(directive_url + "#control_widget")
    dry_run = not ('confirm' in request.form)
    csv_file = request.files['file']
    try:
      if csv_file and allowed_file(csv_file.filename):
        filename = secure_filename(csv_file.filename)
        options = {}
        options['directive_id'] = int(directive_id)
        options['dry_run'] = dry_run
        converter = handle_csv_import(ControlsConverter, csv_file, **options)
        if dry_run:
          options['converter'] = converter
          options['results'] = converter.objects
          options['heading_map'] = converter.object_map
          return render_template("directives/import_controls_result.haml", **options)
        else:
          count = len(converter.objects)
          flash(u'Successfully imported {} control{}'.format(count, 's' if count > 1 else ''), 'notice')
          return import_redirect(directive_url + "#control_widget")
      else:
        file_msg = "Could not import: invalid csv file."
        return render_template("directives/import_errors.haml",
              directive_id = directive_id, exception_message = file_msg)

    except ImportException as e:
      return render_template("directives/import_errors.haml",
            directive_id = directive_id, exception_message = str(e))

  return render_template("directives/import.haml", directive_id = directive_id, import_kind = 'Controls')
def import_people():

  from werkzeug import secure_filename
  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

  if not permissions.is_allowed_read("/admin", 1):
    raise Forbidden()

  if request.method == 'POST':
    if 'cancel' in request.form:
      return import_redirect("/admin")
    dry_run = not ('confirm' in request.form)
    csv_file = request.files['file']
    try:
      if csv_file and allowed_file(csv_file.filename):
        filename = secure_filename(csv_file.filename)
        options = {}
        options['dry_run'] = dry_run
        converter = handle_csv_import(PeopleConverter, csv_file, **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")
      else:
        file_msg = "Could not import: invalid csv file."
        return render_template("directives/import_errors.haml",
              directive_id = "People", exception_message = file_msg)

    except ImportException as e:
      return render_template("directives/import_errors.haml",
            directive_id = "People", exception_message = str(e))

  return render_template("people/import.haml", import_kind = 'People')