Source

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'

    _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')

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

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

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

class Page(Content):
    class __mongometa__:
        session = main_orm_session
        name = 'page'

    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:
            path.append(current.slug)
            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:
                    path.pop()
                    break
                found.append(next._id)
                current = next
            else:
                break
        path.reverse()
        return '/'.join(path)

    @property
    def files(self):
        the_files = File.query.find_by(parent_id=self._id)
        return sorted(the_files, key=lambda k: (k['text'].lower(), k['title'].lower()))

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

    @property
    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'

    # 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)
        else:
            return os.path.join(config['files.private.path'], short_path)

class Image(File):
    class __mongometa__:
        session = main_orm_session
        name = 'image'
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.