1. Alessandro Molina
  2. tgapp-smallpress

Commits

Alessandro Molina  committed 22597b3

more steps forwards, missing search, archive and RSS

  • Participants
  • Parent commits e7674fb
  • Branches default

Comments (0)

Files changed (9)

File smallpress/controllers/root.py

View file
 """Main Controller"""
 import os
 from tg import TGController
-from tg import expose, flash, require, url, lurl, request, redirect, validate
+from tg import expose, flash, require, url, lurl, request, redirect, validate, tmpl_context
+from tg.decorators import before_render
 from tg.i18n import ugettext as _, lazy_ugettext as l_
 from repoze.what import predicates
 
 from tgext.datahelpers.validators import SQLAEntityConverter
 from tgext.datahelpers.fields import AttachedFile
 
+from tw.api import CSSLink
 from tw.forms import DataGrid
 from datetime import datetime
 from tgext.ajaxforms.ajaxform import formexpose, spinner_icon
+from tgext.tagging import TaggingController, TagCloud
 
 article_form = ArticleForm()
 upload_form = UploadForm()
                                      (l_('Name'), 'name'),
                                      (l_('Url'), lambda row:format_link(row.url))])
 
+def inject_css(*args, **kw):
+    CSSLink(link='/_pluggable/smallpress/css/style.css').inject()
+
 class RootController(TGController):
-    @expose('smallpress.templates.index')
-    def index(self):
-        sample = DBSession.query(model.Sample).first()
-        return dict(sample=sample)
+    tagging = TaggingController(model=Article, session=DBSession, allow_edit=None)
+    tagging.search = before_render(inject_css)(tagging.search)
+
+    def _before(self, *args, **kw):
+        tmpl_context.smallpress_mount_point = self.mount_point
+
+    @expose('genshi:smallpress.templates.index')
+    def index(self, *args, **kw):
+        articles = Article.get_published().all()
+        tags = Tagging.tag_cloud_for_set(Article, articles)
+        return dict(articles=articles, tags=tags, mount_point=self.mount_point,
+                    tagcloud=TagCloud(tagging_url=self.mount_point+'/tagging'))
+
+    @expose('genshi:smallpress.templates.article')
+    @validate(dict(article=SQLAEntityConverter(Article)), error_handler=index)
+    def view(self, article):
+        tags = Tagging.tag_cloud_for_set(Article)
+        return dict(article=article, tags=tags, mount_point=self.mount_point,
+                    tagcloud=TagCloud(tagging_url=self.mount_point+'/tagging'))
 
     @require(predicates.in_group('smallpress'))
-    @expose('smallpress.templates.manage')
+    @expose('genshi:smallpress.templates.manage')
     def manage(self, *args, **kw):
         articles = DBSession.query(Article).order_by(Article.publish_date.desc())
         return dict(table=articles_table, articles=articles,
                     create_action=self.mount_point+'/new')
 
     @require(predicates.in_group('smallpress'))
-    @expose('smallpress.templates.edit')
+    @expose('genshi:smallpress.templates.edit')
     def new(self, **kw):
         attachments_table.register_resources()
 
                     upload_form=upload_form, upload_action=url(self.mount_point+'/attach'))
 
     @require(predicates.in_group('smallpress'))
-    @expose('smallpress.templates.edit')
-    @validate(dict(uid=SQLAEntityConverter(Article)))
-    def edit(self, uid, *args, **kw):
+    @expose('genshi:smallpress.templates.edit')
+    def edit(self, *args, **kw):
         attachments_table.register_resources()
 
-        article = uid
-
+        article = DBSession.query(Article).get(kw['uid'])
         value = {
             'uid':article.uid,
             'title':article.title,
 
     @require(predicates.in_group('smallpress'))
     @validate(upload_form, error_handler=upload_form_show)
-    @expose('smallpress.templates.attachments')
+    @expose('genshi:smallpress.templates.attachments')
     def attach(self, **kw):
         article = DBSession.query(Article).get(kw['article'])
         attachment = Attachment(name=kw['name'], article=article,

File smallpress/model/models.py

View file
+from tg import request
+
 from sqlalchemy import Table, ForeignKey, Column
 from sqlalchemy.types import Unicode, Integer, DateTime
 from sqlalchemy.orm import backref, relation
 
 from datetime import datetime
-from smallpress.model import DeclarativeBase
+from smallpress.model import DeclarativeBase, DBSession
 from tgext.pluggable import app_model, primary_key
 from tgext.datahelpers.fields import Attachment as DataHelpersAttachment
+from tgext.pluggable import call_partial
 
 class Article(DeclarativeBase):
     __tablename__ = 'smallpress_articles'
     description = Column(Unicode(150), nullable=False, default='Empty article, edit or delete this')
     content = Column(Unicode(32000), nullable=False, default='')
 
+    @staticmethod
+    def get_published():
+        now = datetime.now()
+        articles = DBSession.query(Article).filter_by(published=True)\
+                                           .filter(Article.publish_date<=now)\
+                                           .order_by(Article.publish_date.desc())
+        return articles
+
+    def tagging_display(self, mount_point=None):
+        if not mount_point:
+            mount_point=request.controller_state.controller.mount_steps[-2][1].mount_point
+        return call_partial('smallpress.partials:article_preview', article=self, mount_point=mount_point)
+
+    def is_owner(self, identity):
+        if not identity:
+            return False
+
+        return identity['user'] == self.author
+
 class Attachment(DeclarativeBase):
     __tablename__ = 'smallpress_attachments'
 

File smallpress/partials.py

View file
 from tg import expose
+from model import Article, Tagging
+
+@expose('genshi:smallpress.templates.articles')
+def articles(articles=None, mount_point=None):
+    if articles is None:
+        articles=Article.get_published()
+    return dict(articles=articles, mount_point=mount_point)
+
+@expose('genshi:smallpress.templates.article_preview')
+def article_preview(article, mount_point=None):
+    return dict(article=article, mount_point=mount_point)

File smallpress/public/css/style.css

View file
 #attachments_table a:hover {
     text-decoration: underline;
 }
+
+#smallpress_articles_box {
+    width: 67%;
+    margin-right: 3%;
+}
+
+#smallpress_rightbar {
+    width: 30%;
+    float: right;
+}
+
+#smallpress_rightbar h3 {
+    font: bold 12px Verdana, Tahoma, sans-serif;
+    background-color: #F0F0F0;
+    border-bottom: medium none;
+    border-top: 1px solid #CCC;
+    padding: 2px;
+    padding-left: 5px;
+}
+
+.smallpress_article h2 {
+    font: bold 20px/24px Georgia, "Times New Roman", Times, serif;
+    background-color: #F0F0F0;
+    border-bottom: medium none;
+    border-top: 1px solid #CCC;
+    padding: 2px;
+    padding-left: 5px;
+}
+
+.smallpress_article h4 {
+    color: #999;
+    font: 11px/11px Verdana, Tahoma, sans-serif;
+}
+
+.smallpress_article_content {
+    font: 11px/18px Verdana, Tahoma, sans-serif;
+    color: #606060;
+}
+
+#smallpress_tags .tagcloud {
+    font-size: 12px;
+}
+
+.smallpress_article h2 a {
+    text-decoration: none;
+    color: black;
+}

File smallpress/templates/article.html

View file
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/"
+      xmlns:xi="http://www.w3.org/2001/XInclude">
+
+<xi:include href="master.html" />
+
+<head>
+    <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" py:if="False"/>
+    <title>Articles</title>
+    <link rel="stylesheet" type="text/css" media="screen" href="${tg.url('/_pluggable/smallpress/css/style.css')}" />
+</head>
+
+<body>
+    <div id="smallpress_rightbar">
+        <div id="smallpress_search">
+            <h3>Search</h3>
+        </div>
+        <div id="smallpress_tags">
+            <h3>Tags</h3>
+            ${tagcloud(tags)}
+        </div>
+        <div id="smallpress_history">
+            <h3>Archive</h3>
+        </div>
+    </div>
+    <div id="smallpress_articles_box">
+        <div>${h.call_partial('smallpress.partials:article_preview', mount_point=mount_point, article=article)}</div>
+    </div>
+    <div style="clear:right;"></div>
+</body>
+</html>

File smallpress/templates/article_preview.html

View file
+<div xmlns:py="http://genshi.edgewall.org/" class="smallpress_article">
+    <h2><a href="${tg.url(mount_point+'/view/%s'%article.uid)}">${article.title}</a></h2>
+    <h4>
+        <span py:if="article.is_owner(request.identity)">
+            <a href="${tg.url(mount_point+'/edit/%s'%article.uid)}">edit</a> |
+        </span>
+        ${article.author} | ${article.publish_date} | ${h.smallpress.comma_separated_tags(article)}
+    </h4>
+    <div class="smallpress_article_content">
+        ${Markup(article.content)}
+    </div>
+</div>

File smallpress/templates/articles.html

View file
+<div xmlns:py="http://genshi.edgewall.org/" id="smallpress_articles">
+    <py:for each="article in articles">
+        ${article.tagging_display(mount_point=mount_point)}
+    </py:for>
+</div>

File smallpress/templates/edit.html

View file
 
 <body>
     <div id="smallpress_new_article" class="smallpress">
+        <a href="${tg.url(tmpl_context.smallpress_mount_point+'/manage')}">back to management</a>
         <h1>Edit Article</h1>
         ${form(value=value, action=action)}
         <div id="smallpress_attachments">

File smallpress/templates/index.html

View file
 
 <head>
   <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" py:if="False"/>
-  <title>Welcome to TurboGears pluggable Application</title>
+  <title>Articles</title>
   <link rel="stylesheet" type="text/css" media="screen" href="${tg.url('/_pluggable/smallpress/css/style.css')}" />
 </head>
 
 <body>
-    <div py:if="request.identity">Hi ${request.identity['user']}</div>
-    <div id="hello_box">
-        <img src="${tg.url('/_pluggable/smallpress/images/star.png')}"/>
-        Hello from, ${h.smallpress.bold(sample.name)}
-        owned by ${sample.user}
+    <div id="smallpress_rightbar">
+        <div id="smallpress_search">
+            <h3>Search</h3>
+        </div>
+        <div id="smallpress_tags">
+            <h3>Tags</h3>
+            ${tagcloud(tags)}
+        </div>
+        <div id="smallpress_history">
+            <h3>Archive</h3>
+        </div>
     </div>
-    <div>${h.call_partial('smallpress.partials:something', name='Partial')}</div>
+    <div id="smallpress_articles_box">
+        <div>${h.call_partial('smallpress.partials:articles', mount_point=mount_point, articles=articles)}</div>
+    </div>
+    <div style="clear:right;"></div>
 </body>
 </html>