1. Chris Perkins
  2. c5t


c5t / c5t / core / model / content.py

import os
from tg import config
from ming import schema as s
from ming.orm import FieldProperty, MappedClass
from datetime import datetime
from c5t.core.model.session import main_orm_session

class Content(MappedClass):
    class __mongometa__:
        session = main_orm_session
        name = 'content'

        custom_indexes = [
                    dict(fields=('title',), unique=False, sparse=False),

    _id = FieldProperty(s.ObjectId)
    title = FieldProperty(s.String) # aka name, aka filename
    text = FieldProperty(s.String)
    excerpt = FieldProperty(s.String)
    format = FieldProperty(s.String, if_missing="text")
    tags = FieldProperty(s.Array(s.String), if_missing=[])
    acl = FieldProperty(s.Array(s.String), if_missing=[])
    parent_id = FieldProperty(s.ObjectId, if_missing=None)
    status = FieldProperty(s.OneOf('draft', 'reviewed', 'published', 'removed', 'public'), if_missing='published')

    def parent(self):
        return Page.query.get(_id=self.parent_id)

    def children(self):
        return self.query.find_by(parent_id=self._id)

    def delete_children(self):
        for child in self.children:

class Page(Content):
    class __mongometa__:
        session = main_orm_session
        name = 'page'
        custom_indexes = [
                    dict(fields=('slug',), unique=False, sparse=False),

    slug = FieldProperty(s.String)
    docstring = FieldProperty(s.String) # What is this for? If you know, please tell me. -chbrown
    template = FieldProperty(s.String) # This is a "python dotted notation path" that is package.templates.template_name
    created_datetime = FieldProperty(s.DateTime)
    expires_datetime = FieldProperty(s.DateTime)

    def path(self):
        path = []
        current = self
        found = []
        while current is not None:
            if getattr(current, 'parent_id', None):
                next = Page.query.get(_id=current.parent_id)
                # stop the possibility of infinite loop
                if next is None or next._id in found:
                current = next
        return '/'.join(path)

    def files(self):
        the_files = File.query.find_by(parent_id=self._id)
        def key(k):
            if k.text is not None:
                return k.text.lower()
                return k.title.lower()
        return sorted(the_files, key=key)

#    @property
#    def news(self):
#        return News.query.find_by(parent_id=self._id)

    def slug_or_id(self):
        return self.slug or str(self._id)

    # @classmethod
    # def by_slug(cls, slug):
    #     return cls.query.get(slug=slug)

class File(Content):
    class __mongometa__:
        session = main_orm_session
        name = 'file'

        custom_indexes = [
                    dict(fields=('fileurl',), unique=False, sparse=False),

    # file_name = FieldProperty(s.String) -> title
    # file_type = FieldProperty(s.String) -> format
    description = FieldProperty(s.String)
    fileurl = FieldProperty(s.String)
    # :attr:`public` may look like it's a duplicate of Page.status,
    # but it actually controls where the file is put in the file system
    public = FieldProperty(s.Bool, if_missing=True)

    def path(self):
        return (self.public and '/pages/%s/%s' or '/%s/files/%s') % (self.parent.path(), self.title)
    def private_path(self):
        return '/%s/files/%s'%(self.parent.path(), self.title)
    def filepath(self):
        short_path = '%s/%s' % (self.parent.path(), self.title)

        if self.public:
            return os.path.join(config['files.public.path'], short_path)
            return os.path.join(config['files.private.path'], short_path)

class Image(File):
    class __mongometa__:
        session = main_orm_session
        name = 'image'