Source

graphalchemy / graphalchemy / models / elements.py

Full commit
# -*- coding: utf-8 -*-
#pylint: disable-msg=e0203,w0201
'''graph element models'''

from stuf.utils import lazy_class, selfname


from graphalchemy.core import octopus, app
from graphalchemy.mixins.elements import NodeMixin, ElementMixin

__all__ = ['Node']
# settings
conf = octopus.G
backends = conf.backends
db = conf.key.db


class Node(NodeMixin, ElementMixin, octopus.workflow.Thing):

    '''node model'''

    # links collection
    _links = app(conf.generic.collector.link, conf.managers)
    # node collection
    _nodes = app(conf.model.collector.node, conf.models)

    @lazy_class
    def C(self):
        '''local settings'''
        metas = dict()
        # model name
        metas['name'] = selfname(self)
        class_names = self.names
        # slug field
        try:
            metas['slug_from'] = class_names(slug_from=True)[0]
        except IndexError:
            metas['slug_from'] = ''
        # properties to index
        metas['indexed'] = set(k for k in class_names(indexed=True))
        # properties indexed for full text search
        metas['fts_indexed'] = set(k for k in class_names(full_text=True))
        # properties to HTML escape
        metas['escaped'] = set(k for k in class_names(escaped=True))
        existing = self.M.localize(self)
        existing.update(metas)
        return existing

    def _version(self):
        '''increment current snapshot number of node'''
        self.update({'_versions': self._current['_versions'] + 1})
        return self._current['_versions']

    def anchor(self, anchor, **kw):
        '''link node to anchor'''
        self.session.links.create(self.C.anchor_link, self, anchor, kw)

    class Meta:
        # anchor link label
        anchor_link = ''
        # fork link label
        fork_link = ''
        # full text index name
        fts_index = ''
        # index name
        index = ''
        # reference link label
        reference_link = ''
        # root index name
        root_index = 'roots'
        # slug field name
        slug_field = ''
        # snapshot link label
        version_link = ''
        # whether to track changes
        versioned = False