<commit_before># Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: <EMAIL>
# Maintained By: <EMAIL>

import datetime
from sqlalchemy.ext.declarative import declared_attr
from ggrc import db
from ggrc.models.mixins import deferred, Base, Described


class Context(Described, Base, db.Model):
  __tablename__ = 'contexts'

  name = deferred(db.Column(db.String(128), nullable=True), 'Context')
  related_object_id = deferred(
      db.Column(db.Integer(), nullable=True), 'Context')
  related_object_type = deferred(
      db.Column(db.String(128), nullable=True), 'Context')

  @property
  def related_object_attr(self):
    return '{0}_related_object'.format(self.related_object_type)

  @property
  def related_object(self):
    return getattr(self, self.related_object_attr)

  @related_object.setter
  def related_object(self, value):
    self.related_object_id = value.id if value is not None else None
    self.related_object_type = value.__class__.__name__ if value is not None \
        else None
    return setattr(self, self.related_object_attr, value)

  _publish_attrs = ['name', 'related_object',]
  _sanitize_html = ['name',]
  _include_links = []


class HasOwnContext(object):
  """Mixin for models which may have their own context
  """

  @declared_attr
  def contexts(cls):
    joinstr = 'and_(foreign(Context.related_object_id) == {type}.id, '\
                   'foreign(Context.related_object_type) == "{type}")'
    joinstr = joinstr.format(type=cls.__name__)
    return db.relationship(
        'Context',
        primaryjoin=joinstr,
        #foreign_keys='Context.related_object_id',
        #cascade='all, delete-orphan',
        backref='{0}_related_object'.format(cls.__name__),
        order_by='Context.id',
        post_update=True)

  def build_object_context(self, context, name=None, description=None):
    if name is None:
      name='{object_type} Context {timestamp}'.format(
        object_type=self.__class__.__name__,
        timestamp=datetime.datetime.now()),
    if description is None:
      description = ''
    new_context = Context(
        name=name,
        description=description,<commit_msg>Removing fulltext from Context description (no longer using Described mixin)<commit_after># Copyright (C) 2013 Google Inc., authors, and contributors <see AUTHORS file>
# Licensed under http://www.apache.org/licenses/LICENSE-2.0 <see LICENSE file>
# Created By: <EMAIL>
# Maintained By: <EMAIL>

import datetime
from sqlalchemy.ext.declarative import declared_attr
from ggrc import db
from ggrc.models.mixins import deferred, Base, Described


class Context(Base, db.Model):
  __tablename__ = 'contexts'

  # This block and the 'description' attrs are taken from the Described mixin
  #  which we do not use for Context because indexing Context descriptions
  #  for fulltext search leads to undesirable results
  @declared_attr
  def description(cls):
    return deferred(db.Column(db.Text), cls.__name__)

  name = deferred(db.Column(db.String(128), nullable=True), 'Context')
  related_object_id = deferred(
      db.Column(db.Integer(), nullable=True), 'Context')
  related_object_type = deferred(
      db.Column(db.String(128), nullable=True), 'Context')

  @property
  def related_object_attr(self):
    return '{0}_related_object'.format(self.related_object_type)

  @property
  def related_object(self):
    return getattr(self, self.related_object_attr)

  @related_object.setter
  def related_object(self, value):
    self.related_object_id = value.id if value is not None else None
    self.related_object_type = value.__class__.__name__ if value is not None \
        else None
    return setattr(self, self.related_object_attr, value)

  _publish_attrs = ['name', 'related_object','description',]
  _sanitize_html = ['name','description',]
  _include_links = []


class HasOwnContext(object):
  """Mixin for models which may have their own context
  """

  @declared_attr
  def contexts(cls):
    joinstr = 'and_(foreign(Context.related_object_id) == {type}.id, '\
                   'foreign(Context.related_object_type) == "{type}")'
    joinstr = joinstr.format(type=cls.__name__)
    return db.relationship(
        'Context',
        primaryjoin=joinstr,
        #foreign_keys='Context.related_object_id',
        #cascade='all, delete-orphan',
        backref='{0}_related_object'.format(cls.__name__),
        order_by='Context.id',
        post_update=True)

  def build_object_context(self, context, name=None, description=None):
    if name is None:
      name='{object_type} Context {timestamp}'.format(
        object_type=self.__class__.__name__,
        timestamp=datetime.datetime.now()),
    if description is None:
      description = ''
    new_context = Context(
        name=name,
        description=description,
{'fraction_non_alphanumeric': 0.1132152311138261, 'fraction_numerical': 0.0057014864589696596, 'mean_word_length': 3.991869918699187, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '5186734', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1476, 'n_words': 357}