# Copyright (C) 2016 Google Inc.
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>

"""Handlers specific for Assessment templates."""


from ggrc.converters.handlers import handlers
from ggrc import converters


class TemplateObjectColumnHandler(handlers.ColumnHandler):
  """Handler for template object type field."""

  def parse_item(self):
    """Parse object type field for assessment templates."""
    exportables = converters.get_exportables()
    object_type = exportables.get(self.raw_value.strip().lower())
    if not object_type:
      self.add_error("invalid template object type")
      return

    return object_type.__name__


class TemplateCaColumnHandler(handlers.ColumnHandler):
  pass

@@ -158,16 +158,121 @@
 s.%22%22%22%0A%0A%0A
+from ggrc import db%0Afrom ggrc import converters%0Afrom ggrc.models import CustomAttributeDefinition as CAD%0A
 from ggr
@@ -313,36 +313,8 @@
 ers%0A
-from ggrc import converters%0A
 %0A%0Acl
@@ -800,11 +800,1868 @@
 r):%0A
-  pass
+%0A  %22%22%22Handler for Template custom attributes column.%22%22%22%0A%0A  TYPE_MAP = %7Bk.lower(): v for k, v in CAD.VALID_TYPES.iteritems()%7D%0A%0A  @staticmethod%0A  def _get_ca_type(definition_str):%0A    %22%22%22Get type and mandatory flag for custom attribute.%22%22%22%0A    ca_type = definition_str.strip().lower()%0A    mandatory = False%0A    if ca_type.lower().startswith(%22mandatory %22):%0A      mandatory = True%0A      ca_type = ca_type%5B10:%5D.strip()%0A    return ca_type, mandatory%0A%0A  def _handle_ca_line(self, line):%0A    %22%22%22Parse single custom attribute definition line.%22%22%22%0A    parts = %5Bpart.strip() for part in line.split(%22,%22)%5D%0A%0A    ca_type, mandatory = self._get_ca_type(parts%5B0%5D)%0A    ca_title = parts%5B1%5D%0A%0A    if len(parts) %3E 2:%0A      self.add_error(%22line %7Bline%7D: Dropdown is currently not supported.%22)%0A      return%0A%0A    if ca_type not in self.TYPE_MAP:%0A      self.add_error(%22line %7Bline%7D: Invalid custom attribute type.%22)%0A      return%0A%0A    if ca_type == %22person%22:%0A      self.add_error(%22line %7Bline%7D: Person custom attribute currently not %22%0A                     %22supported.%22)%0A      return%0A%0A    attribute = CAD()%0A    attribute.attribute_type = self.TYPE_MAP%5Bca_type%5D%0A    attribute.mandatory = mandatory%0A    attribute.title = ca_title%0A    attribute.definition_type = %22assessment_template%22%0A    return attribute%0A%0A  def parse_item(self):%0A    %22%22%22Parse a list of custom attributes.%22%22%22%0A    lines = self.raw_value.splitlines()%0A    lines = %5Bline for line in lines if line%5D%0A    custom_attributes = %5B%5D%0A    for line in lines:%0A      custom_attributes.append(self._handle_ca_line(line))%0A    return custom_attributes%0A%0A  def set_obj_attr(self):%0A    pass%0A%0A  def insert_object(self):%0A    %22%22%22Add custom attributes to db session.%22%22%22%0A    if not self.value or self.row_converter.ignore:%0A      return%0A%0A    for attribute in self.value:%0A      attribute.definition_id = self.row_converter.obj.id%0A      db.session.add(attribute)
 %0A

Update custom attribute handler