# Path: beavy/models/object.py
# class Object(db.Model):
#     """
#     This is the primary base class for all kind of objects
#     we know of inside the system
#     """
#     @unique
#     class Capabilities(Enum):
#         # This type is to be shown in default lists
#         # like 'top', 'latest' etc
#         listed = 'listed'
# 
#         # If the type isn't listed but has `listed_for_activity`
#         # it can show up in lists about activitys, for example
#         # when an object got liked
#         listed_for_activity = 'a_listable'
# 
#         # This can be searched for
#         searchable = 'searchable'
# 
#     __tablename__ = "objects"
#     query_class = ObjectQuery
# 
#     id = db.Column(db.Integer, primary_key=True)
#     discriminator = db.Column('type', db.String(100), nullable=False)
#     created_at = db.Column('created_at', db.DateTime(), nullable=False,
#                            server_default=func.now())
#     payload = db.Column('payload', JSONB, nullable=True)
#     owner_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
#     belongs_to = db.Column(db.Integer, db.ForeignKey("objects.id"),
#                            nullable=True)
#     # children = db.relationship("Object", backref=db.backref('belongs_to',
#     #                                                         remote_side=id))
# 
#     __mapper_args__ = {'polymorphic_on': discriminator}
# 
#     owner = db.relationship(User, backref=db.backref('objects'))
# 
# Path: beavy/common/rendered_text_mixin.py
# class RenderedTextMixin(object):
#     # to be used on activity or object models
# 
#     raw = PayloadProperty('raw', 'text')
#     chopped = PayloadProperty('chopped', 'text')
#     cooked = PayloadProperty('cooked', 'text')
# 
# Path: beavy/common/payload_property.py
# class PayloadProperty(object):
# 
#     def __init__(self, key, path=[], attribute='payload', force=True):
#         self.key = key
#         self.attribute = attribute
#         self.force = force
#         self.path = isinstance(path, str) and path.split('.') or path
#         self.flag_modified = sa_flag_modified
# 
#     def _findBase(self, obj):
#         base = getattr(obj, self.attribute)
#         if base is None and self.force:
#             base = {}
#             setattr(obj, self.attribute, base)
#             self.flag_modified(obj, self.attribute)
# 
#         items = self.path[:]
#         while items:
#             cur = items.pop(0)
#             try:
#                 base = base[cur]
#             except KeyError:
#                 base[cur] = {}
#                 base = base[cur]
#                 while items:
#                     cur = items.pop(0)
#                     base[cur] = {}
#                     base = base[cur]
# 
#                 self.flag_modified(obj, self.attribute)
#                 break
# 
#         return base
# 
#     def __get__(self, obj, _):
#         if not obj:
#             # someone asked on the class
#             return self
#         base = self._findBase(obj)
#         key = self.key
# 
#         return base.get(key, None)
# 
#     def __set__(self, obj, value):
#         self._findBase(obj)[self.key] = value
#         self.flag_modified(obj, self.attribute)
# 
#     def __delete__(self, obj):
#         raise NotImplemented()


from beavy.models.object import Object
from beavy.common.rendered_text_mixin import RenderedTextMixin
from beavy.common.payload_property import PayloadProperty

# from slugify import slugify_url

TOPIC_ID = "topic"
LINK_ID = "link"


class Topic(Object, RenderedTextMixin):
    __mapper_args__ = {
        'polymorphic_identity': TOPIC_ID
    }


# Path: beavy/models/object.py
# class Object(db.Model):
#     """
#     This is the primary base class for all kind of objects
#     we know of inside the system
#     """
#     @unique
#     class Capabilities(Enum):
#         # This type is to be shown in default lists
#         # like 'top', 'latest' etc
#         listed = 'listed'
# 
#         # If the type isn't listed but has `listed_for_activity`
#         # it can show up in lists about activitys, for example
#         # when an object got liked
#         listed_for_activity = 'a_listable'
# 
#         # This can be searched for
#         searchable = 'searchable'
# 
#     __tablename__ = "objects"
#     query_class = ObjectQuery
# 
#     id = db.Column(db.Integer, primary_key=True)
#     discriminator = db.Column('type', db.String(100), nullable=False)
#     created_at = db.Column('created_at', db.DateTime(), nullable=False,
#                            server_default=func.now())
#     payload = db.Column('payload', JSONB, nullable=True)
#     owner_id = db.Column(db.Integer, db.ForeignKey(User.id), nullable=False)
#     belongs_to = db.Column(db.Integer, db.ForeignKey("objects.id"),
#                            nullable=True)
#     # children = db.relationship("Object", backref=db.backref('belongs_to',
#     #                                                         remote_side=id))
# 
#     __mapper_args__ = {'polymorphic_on': discriminator}
# 
#     owner = db.relationship(User, backref=db.backref('objects'))
# 
# Path: beavy/common/rendered_text_mixin.py
# class RenderedTextMixin(object):
#     # to be used on activity or object models
# 
#     raw = PayloadProperty('raw', 'text')
#     chopped = PayloadProperty('chopped', 'text')
#     cooked = PayloadProperty('cooked', 'text')
# 
# Path: beavy/common/payload_property.py
# class PayloadProperty(object):
# 
#     def __init__(self, key, path=[], attribute='payload', force=True):
#         self.key = key
#         self.attribute = attribute
#         self.force = force
#         self.path = isinstance(path, str) and path.split('.') or path
#         self.flag_modified = sa_flag_modified
# 
#     def _findBase(self, obj):
#         base = getattr(obj, self.attribute)
#         if base is None and self.force:
#             base = {}
#             setattr(obj, self.attribute, base)
#             self.flag_modified(obj, self.attribute)
# 
#         items = self.path[:]
#         while items:
#             cur = items.pop(0)
#             try:
#                 base = base[cur]
#             except KeyError:
#                 base[cur] = {}
#                 base = base[cur]
#                 while items:
#                     cur = items.pop(0)
#                     base[cur] = {}
#                     base = base[cur]
# 
#                 self.flag_modified(obj, self.attribute)
#                 break
# 
#         return base
# 
#     def __get__(self, obj, _):
#         if not obj:
#             # someone asked on the class
#             return self
#         base = self._findBase(obj)
#         key = self.key
# 
#         return base.get(key, None)
# 
#     def __set__(self, obj, value):
#         self._findBase(obj)[self.key] = value
#         self.flag_modified(obj, self.attribute)
# 
#     def __delete__(self, obj):
#         raise NotImplemented()


# Path: beavy_apps/hacker_news/models.py
from beavy.models.object import Object
from beavy.common.rendered_text_mixin import RenderedTextMixin
from beavy.common.payload_property import PayloadProperty


# from slugify import slugify_url

TOPIC_ID = "topic"
LINK_ID = "link"


class Topic(Object, RenderedTextMixin):
    __mapper_args__ = {
        'polymorphic_identity': TOPIC_ID
    }


    title = PayloadProperty('title')