示例#1
0
文件: views.py 项目: dmdm/PySite
 def __init__(self, context, request):
     self.context = context
     self.request = request
     dsn = os.path.join(context.site.dir_, 'cache', 'blog', 'cache.sqlite3')
     self.cache = Cache(dsn, {})
示例#2
0
文件: views.py 项目: dmdm/PySite
class BlogView(object):

    def __init__(self, context, request):
        self.context = context
        self.request = request
        dsn = os.path.join(context.site.dir_, 'cache', 'blog', 'cache.sqlite3')
        self.cache = Cache(dsn, {})

    @view_config()
    @view_config(name='view')
    def view(self):
        self._sp = list(map(urllib.parse.unquote, self.context.slugparts))
        self._page = Page(self.context, self.request)
        self._page.jjglobals['authors'] = self.cache.list_authors()
        self._page.jjglobals['categories'] = self.cache.list_categories()
        self._page.jjglobals['tags'] = self.cache.list_tags()
        _init_plugins(self.context, self.request, self._page)
        _init_i18n(self.context, self.request, self._page)
        if self._sp[0] == 'index':
            return self._view_index()
        elif self._sp[0] == 'author':
            return self._view_author()
        elif self._sp[0] == 'category':
            return self._view_category()
        elif self._sp[0] == 'tag':
            return self._view_tag()
        else:
            return self._view_article()

    def _build_prev_next_links(self, sp):
        n = int(sp[-1])
        # prev
        if n <= 1:
            link_prev = None
        else:
            sp[-1] = n - 1
            link_prev = self._page.blog(['/'] + sp)
        # next
        sp[-1] = n + 1
        link_next = self._page.blog(['/'] + sp)
        return (link_prev, link_next)

    def _view_index(self):
        if len(self._sp) == 1:
            self._sp.append('1')
        n = int(self._sp[1])
        data = self.cache.get_index_page(n)
        self._render_data(data)
        fn = os.path.join(self.context.site.rc['blog.theme'],
            'index_page.jinja2')
        link_prev, link_next = self._build_prev_next_links(self._sp[:])
        html = self._page.get_page(fn, jjglobals={
            'data': data,
            'index_kind': 'Index',
            'index_name': None,
            'index_page': n,
            'link_prev': link_prev,
            'link_next': link_next
        })
        return Response(html)

    def _view_author(self):
        if len(self._sp) == 1:
            data = self.cache.list_authors()
            fn = os.path.join(safepath(self.context.site.rc['blog.theme']),
                'list_page.jinja2')
            html = self._page.get_page(fn, jjglobals={
                'data': data,
                'list_kind': 'Authors',
                'url_prefix': 'author/'
            })
            return Response(html)
        else:
            try:
                n = int(self._sp[-1])
            except ValueError:
                self._sp.append('1')
                n = 1
            author = '/'.join(self._sp[1:-1])
            data = self.cache.get_author_page(author, n)
            self._render_data(data)
            fn = os.path.join(safepath(self.context.site.rc['blog.theme']),
                'index_page.jinja2')
            link_prev, link_next = self._build_prev_next_links(self._sp[:])
            html = self._page.get_page(fn, jjglobals={
                'data': data,
                'index_kind': 'Author',
                'index_name': author,
                'index_page': n,
                'link_prev': link_prev,
                'link_next': link_next
            })
            return Response(html)

    def _view_category(self):
        if len(self._sp) == 1:
            data = self.cache.list_categories()
            fn = os.path.join(safepath(self.context.site.rc['blog.theme']),
                'list_page.jinja2')
            html = self._page.get_page(fn, jjglobals={
                'data': data,
                'list_kind': 'Categories',
                'url_prefix': 'category/'
            })
            return Response(html)
        else:
            try:
                n = int(self._sp[-1])
            except ValueError:
                self._sp.append('1')
                n = 1
            category = '/'.join(self._sp[1:-1])
            data = self.cache.get_category_page(category, n)
            self._render_data(data)
            fn = os.path.join(safepath(self.context.site.rc['blog.theme']),
                'index_page.jinja2')
            link_prev, link_next = self._build_prev_next_links(self._sp[:])
            html = self._page.get_page(fn, jjglobals={
                'data': data,
                'index_kind': 'Category',
                'index_name': category,
                'index_page': n,
                'link_prev': link_prev,
                'link_next': link_next
            })
            return Response(html)

    def _view_tag(self):
        if len(self._sp) == 1:
            data = self.cache.list_tags()
            fn = os.path.join(safepath(self.context.site.rc['blog.theme']),
                'list_page.jinja2')
            html = self._page.get_page(fn, jjglobals={
                'data': data,
                'list_kind': 'Tags',
                'url_prefix': 'tag/'
            })
            return Response(html)
        else:
            try:
                n = int(self._sp[-1])
            except ValueError:
                self._sp.append('1')
                n = 1
            tag = '/'.join(self._sp[1:-1])
            data = self.cache.get_tag_page(tag, n)
            self._render_data(data)
            fn = os.path.join(safepath(self.context.site.rc['blog.theme']),
                'index_page.jinja2')
            link_prev, link_next = self._build_prev_next_links(self._sp[:])
            html = self._page.get_page(fn, jjglobals={
                'data': data,
                'index_kind': 'Tag',
                'index_name': tag,
                'index_page': n,
                'link_prev': link_prev,
                'link_next': link_next
            })
            return Response(html)

    def _view_article(self):
        data = self.cache.get_article_by_slugparts(self._sp)
        self._render_data(data)
        fn = os.path.join(safepath(self.context.site.rc['blog.theme']),
            'article.jinja2')
        html = self._page.get_page(fn, jjglobals={'data': data})
        return Response(html)

    def _render_data(self, data):
        if isinstance(data, dict):
            # Render only the body, the summary is not necessary
            data['body'] = self._page.render_from_string(data['body'])
        else:
            for it in data:
                it['meta']['summary'] = self._page.render_from_string(
                    it['meta']['summary'])