<commit_before>from sqlalchemy import event

from datetime import datetime
from ggrc import db
from sqlalchemy.ext.declarative import declared_attr
from .mixins import deferred
from ggrc.login import get_current_user_id
from .reflection import PublishOnly

class HasObjectState(object):

  _publish_attrs = [
    PublishOnly('os_last_updated_by_user_id'),
    PublishOnly('os_last_updated'),
    PublishOnly('os_approved_on'),
    PublishOnly('os_state'),
  ]

  def __init__(self):
    self._skip_os_state_update = False;

  @declared_attr
  def os_state(cls):
    return deferred(db.Column(db.String, nullable=False), cls.__name__)

  @declared_attr
  def os_last_updated(cls):
    return deferred(db.Column(db.DateTime, nullable=False), cls.__name__)

  @declared_attr
  def os_approved_on(cls):
    return deferred(db.Column(db.DateTime), cls.__name__)

  @declared_attr
  def os_last_updated_by_user_id(cls):
    return deferred(db.Column(db.Integer), cls.__name__)

  def skip_os_state_update(self):
    self._skip_os_state_update = True

class ObjectStates:
  DRAFT = 'Draft'
  APPROVED = 'Approved'
  DECLINED = 'Declined'
  MODIFIED = 'Modified'

# This table
class ObjectStateTables:
  table_names = [
    'programs', 'objectives', 'controls', 'sections',
    'systems', 'data_assets', 'facilities',
    'markets', 'products', 'projects', 'directives'
  ]

def state_before_insert_listener(mapper, connection, target):
  if hasattr(target, 'os_state'):
    target.os_state = ObjectStates.DRAFT
    target.os_last_updated = datetime.now()
    target.os_last_updated_by_user_id = get_current_user_id()

def state_before_update_listener(mapper, connection, target):
  # import ipdb; ipdb.set_trace()
  if hasattr(target, 'os_state'):
    # import ipdb; ipdb.set_trace()
    if hasattr(target, 'skip_os_state_update'):
      if True == target.skip_os_state_update:
        return
    target.os_state = ObjectStates.MODIFIED
    target.os_last_updated = datetime.now()
    target.os_last_updated_by_user_id = get_current_user_id()
<commit_msg>Correctly check for automatic skip state setting.<commit_after>from sqlalchemy import event

from datetime import datetime
from ggrc import db
from sqlalchemy.ext.declarative import declared_attr
from .mixins import deferred
from ggrc.login import get_current_user_id
from .reflection import PublishOnly

class HasObjectState(object):

  _publish_attrs = [
    PublishOnly('os_last_updated_by_user_id'),
    PublishOnly('os_last_updated'),
    PublishOnly('os_approved_on'),
    PublishOnly('os_state'),
  ]

  def __init__(self):
    self._skip_os_state_update = False;

  @declared_attr
  def os_state(cls):
    return deferred(db.Column(db.String, nullable=False), cls.__name__)

  @declared_attr
  def os_last_updated(cls):
    return deferred(db.Column(db.DateTime, nullable=False), cls.__name__)

  @declared_attr
  def os_approved_on(cls):
    return deferred(db.Column(db.DateTime), cls.__name__)

  @declared_attr
  def os_last_updated_by_user_id(cls):
    return deferred(db.Column(db.Integer), cls.__name__)

  def skip_os_state_update(self):
    self._skip_os_state_update = True

class ObjectStates:
  DRAFT = 'Draft'
  APPROVED = 'Approved'
  DECLINED = 'Declined'
  MODIFIED = 'Modified'

# This table
class ObjectStateTables:
  table_names = [
    'programs', 'objectives', 'controls', 'sections',
    'systems', 'data_assets', 'facilities',
    'markets', 'products', 'projects', 'directives'
  ]

def state_before_insert_listener(mapper, connection, target):
  if hasattr(target, 'os_state'):
    target.os_state = ObjectStates.DRAFT
    target.os_last_updated = datetime.now()
    target.os_last_updated_by_user_id = get_current_user_id()

def state_before_update_listener(mapper, connection, target):
  if hasattr(target, 'os_state'):
    if hasattr(target, '_skip_os_state_update'):
      if True == target._skip_os_state_update:
        return
    target.os_state = ObjectStates.MODIFIED
    target.os_last_updated = datetime.now()
    target.os_last_updated_by_user_id = get_current_user_id()

{'fraction_non_alphanumeric': 0.10260826771653543, 'fraction_numerical': 0.0, 'mean_word_length': 5.031157270029674, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '5379896', 'n_tokens_mistral': 1385, 'n_tokens_neox': 1367, 'n_words': 268}