<commit_before># 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 sqlalchemy.ext.hybrid import hybrid_property
from ggrc.access_control.list import AccessControlList
from ggrc import db


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

  _update_raw = _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')

  @hybrid_property
  def access_control_list(self):
    return self._access_control_list

  def _set_value(self, value):
    """Helper function for setting value of acl"""
    for itm in self.access_control_list:
      acr_id = value['ac_role_id']
      person_id = value['person']['id']
      if itm.ac_role_id == acr_id and itm.person_id == person_id:
        return
    AccessControlList(
        object=self,
        person_id=value.get('person').get('id'),
        ac_role_id=value.get('ac_role_id')
    )

  @access_control_list.setter
  def access_control_list(self, values):
    """Setter function for access control list.

    Args:
      value: List of access control roles or dicts containing json
        representation of custom attribute values.
    """
    if not values:
      return
    for value in values:
      self._set_value(value)

  @classmethod<commit_msg>Add code for removing items from access control list<commit_after># 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 sqlalchemy.ext.hybrid import hybrid_property
from ggrc.access_control.list import AccessControlList
from ggrc import db


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

  _update_raw = _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')

  @hybrid_property
  def access_control_list(self):
    return self._access_control_list

  def _remove_existing(self, existing, values):
    """Helper function for removing existing items in acl"""
    for value in values:
      acr_id = value['ac_role_id']
      person_id = value['person']['id']
      if existing.ac_role_id == acr_id and existing.person_id == person_id:
        return
    self.access_control_list.remove(existing)

  def _set_value(self, value):
    """Helper function for setting value of acl"""
    for itm in self.access_control_list:
      acr_id = value['ac_role_id']
      person_id = value['person']['id']
      if itm.ac_role_id == acr_id and itm.person_id == person_id:
        return
    AccessControlList(
        object=self,
        person_id=value.get('person').get('id'),
        ac_role_id=value.get('ac_role_id')
    )

  @access_control_list.setter
  def access_control_list(self, values):
    """Setter function for access control list.

    Args:
      value: List of access control roles or dicts containing json
        representation of custom attribute values.
    """
    if values is None:
      return
    for existing in self.access_control_list:
      self._remove_existing(existing, values)
    for value in values:
      self._set_value(value)

  @classmethod
{'fraction_non_alphanumeric': 0.10505002382086708, 'fraction_numerical': 0.0033349213911386373, 'mean_word_length': 3.6193619361936196, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '2214554', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1280, 'n_words': 302}