Puria Nafisi Azizi avatar Puria Nafisi Azizi committed e05bb29

Creating the list index, with polls partials

Comments (0)

Files changed (7)

tgpolls/controllers/root.py

 """Main Controller"""
 
 from tg import TGController
-from tg import expose, flash, require, url, lurl, request, redirect, validate
-from tg.i18n import ugettext as _, lazy_ugettext as l_
-from tgext.datahelpers.validators import SQLAEntityConverter, validated_handler
+from tg import expose, flash, validate
+from tg.i18n import ugettext as _
+from tgext.datahelpers.validators import validated_handler
 from tgext.pluggable import plug_redirect
 
 
 from tgpolls import model
-from tgpolls.model import DBSession
 from tgpolls.lib.forms import new_poll_form
+from tgpolls.model import DBSession, Poll
 
 class RootController(TGController):
     @expose('tgpolls.templates.index')
     def index(self):
-        return dict()
+        polls = DBSession.query(Poll).order_by(Poll.uid.desc()).all()
+        return dict(polls=polls)
 
     @expose('tgpolls.templates.poll.new')
     def new(self, **kw):
         model.DBSession.flush()
 
         flash(_('Poll successfully added'))
-        return plug_redirect('tgpolls', '/new')
+        return plug_redirect('tgpolls', '/')
 

tgpolls/lib/forms.py

-from tw.api import WidgetsList
-from tw.forms import TableForm, TextField, HiddenField, TextArea, SingleSelectField
-from tw.forms import validators
-from tw.forms.calendars import CalendarDateTimePicker
-from tgext.datahelpers.validators import SQLAEntityConverter
-from tg import config
 from tg.i18n import lazy_ugettext as l_
-from calendarevents import model
+from tgext.pluggable import plug_url
 
-import tw2.core
-import tw2.forms
+from tw2.core import DateTimeValidator, Required
+from tw2.forms import TextField, CalendarDateTimePicker, TextArea, FormPage, TableForm
 
-class NewPollForm(tw2.forms.FormPage):
+class NewPollForm(FormPage):
     title = l_("Poll")
 
-    class child(tw2.forms.TableForm):
+    class child(TableForm):
         action = '/tgpolls/save'
-        body = tw2.forms.TextField(validator=tw2.core.Required)
-        expire = tw2.forms.CalendarDateTimePicker(label_text=l_('Expiry date'), date_format='%d/%m/%Y %H:%M',
-                                                  validator=tw2.core.DateTimeValidator(required=True, format='%d/%m/%Y %H:%M')
+        body = TextField(validator=Required)
+        expire = CalendarDateTimePicker(label_text=l_('Expiry date'), date_format='%d/%m/%Y %H:%M',
+                                                  validator=DateTimeValidator(required=True, format='%d/%m/%Y %H:%M')
                                                   )
-        answers = tw2.forms.TextArea(label_text=l_('Answers separated by | (eg: First answer|Second answer)'), validator=tw2.core.Required)
+        answers = TextArea(label_text=l_('Answers separated by | (eg: First answer|Second answer)'), validator=Required)
 
 new_poll_form = NewPollForm()

tgpolls/model/models.py

 
     uid = Column(Integer, autoincrement=True, primary_key=True)
     text = Column(Unicode(255))
+    votes = Column(Integer)
 
     poll_id = Column(Integer, ForeignKey(primary_key(Poll)), nullable=True, index=True)
     poll = relation(Poll, backref=backref('answers'), lazy='joined')

tgpolls/partials.py

 from tg import expose
 
-@expose('tgpolls.templates.little_partial')
-def something(name):
-    return dict(name=name)
+@expose('tgpolls.templates.poll_partial')
+def poll_partial(poll):
+    return dict(poll=poll)

tgpolls/templates/index.html

 
 <head>
   <meta content="text/html; charset=UTF-8" http-equiv="Content-Type" py:if="False"/>
-  <title>Welcome to TurboGears pluggable Application</title>
+  <title>TGPolls pluggable Application</title>
   <link rel="stylesheet" type="text/css" media="screen" href="${tg.url('/_pluggable/tgpolls/css/style.css')}" />
 </head>
 
 <body>
-    <div py:if="request.identity">Hi ${request.identity['user']}</div>
-    <div id="hello_box">
-        <img src="${tg.url('/_pluggable/tgpolls/images/star.png')}"/>
+    <h1>Polls</h1>
+    <br />
+    <div py:for="poll in polls" class="tgpolls_poll_container">
+        <div class="tgpolls_poll">${h.call_partial('tgpolls.partials:poll_partial', poll=poll)}</div>
     </div>
-    <div>${h.call_partial('tgpolls.partials:something', name='Partial')}</div>
 </body>
 </html>

tgpolls/templates/little_partial.html

-<div xmlns="http://www.w3.org/1999/xhtml"
-      xmlns:py="http://genshi.edgewall.org/"
-      xmlns:xi="http://www.w3.org/2001/XInclude">
-    <div>This is from partial: ${h.tgpolls.bold(name)}</div>
-</div>

tgpolls/templates/poll_partial.html

+<div xmlns="http://www.w3.org/1999/xhtml"
+      xmlns:py="http://genshi.edgewall.org/">
+    <h3 class="tgpolls_poll_title">${poll.body}</h3>
+    <ul class="tgpolls_poll_answers_container" py:for="answer in poll.answers">
+        <li class="tgpolls_poll_answer">${answer.text} - ${answer.votes}</li>
+    </ul>
+</div>
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.