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

"""Roleable model"""

from sqlalchemy import orm
from sqlalchemy.ext.declarative import declared_attr
from ggrc import db


class Roleable(object):
  """Roleable"""

  _include_links = _publish_attrs = [
      'access_control_list'
  ]

  @declared_attr
  def access_control_list(self):
    """access_control_list"""
    joinstr = 'and_(remote(AccessControlList.object_id) == {type}.id, '\
        'remote(AccessControlList.object_type) == "{type}")'
    joinstr = joinstr.format(type=self.__name__)
    return db.relationship(
        'AccessControlList',
        primaryjoin=joinstr,
        foreign_keys='AccessControlList.object_id',
        backref='{0}_object'.format(self.__name__),
        cascade='all, delete-orphan')

  @classmethod
  def eager_query(cls):
    """Eager Query"""
    query = super(Roleable, cls).eager_query()
    return cls.eager_inclusions(query, Roleable._include_links).options(
        orm.subqueryload('access_control_list'))

  def log_json(self):
    """Log custom attribute values."""
    # pylint: disable=not-an-iterable
    res = super(Roleable, self).log_json()
    res["access_control_list"] = [
        value.log_json() for value in self.access_control_list]
    return res

@@ -208,16 +208,121 @@
 ed_attr%0A
+from sqlalchemy.ext.hybrid import hybrid_property%0Afrom ggrc.access_control.list import AccessControlList%0A
 from ggr
@@ -378,16 +378,30 @@
 le%22%22%22%0A%0A 
+ _update_raw =
  _includ
@@ -485,16 +485,17 @@
 r%0A  def 
+_
 access_c
@@ -955,16 +955,572 @@
 phan')%0A%0A
+  @hybrid_property%0A  def access_control_list(self):%0A    return self._access_control_list%0A%0A  @access_control_list.setter%0A  def access_control_list(self, values):%0A    %22%22%22Setter function for access control list.%0A%0A    Args:%0A      value: List of access control roles or dicts containing json%0A        representation of custom attribute values.%0A    %22%22%22%0A    if not values:%0A      return%0A    for value in values:%0A      AccessControlList(%0A          object=self,%0A          person_id=value.get('person').get('id'),%0A          ac_role_id=value.get('ac_role_id')%0A      )%0A%0A
   @class

Add access_control_list setter