Commits

Atsushi Odagiri committed dd4c161 Draft

refs #1: カテゴリ追加

  • Participants
  • Parent commits 0d58602

Comments (0)

Files changed (5)

File addressbook/__init__.py

     config.add_route('top', '/')
     config.add_route('new_person', '/new')
     config.add_route('person', '/people/{person_id}')
+    config.add_route('categories', '/categories')
 
 def skin(config):
     config.override_asset(

File addressbook/models.py

     Unicode,
     UnicodeText,
     Date,
+    ForeignKey,
     )
 
 from sqlalchemy.orm import (
     scoped_session,
     sessionmaker,
+    relationship,
     )
 
 from formalchemy import (
     email = Column(Unicode(1024))
     description = Column(UnicodeText,
                          renderer=RichTextFieldRenderer())
+    category_id = Column(Integer,
+                         ForeignKey('categories.id'))
+    category = relationship('Category',
+                            backref='persons')
+
+class Category(Base):
+    __tablename__ = 'categories'
+    query = DBSession.query_property()
+    id = Column(Integer, primary_key=True)
+    name = Column(Unicode(255), nullable=False,
+                  unique=True)
+    
+    def __unicode__(self):
+        return self.name

File addressbook/panels.py

 @panel_config("app_bar", renderer="app_bar.mako")
 def app_bar(context, request):
     return dict(app_name=u"Address Book",
-                menus=[(u'Add', request.route_url('new_person'))
+                menus=[(u'Add', request.route_url('new_person')),
+                       (u'Categories', request.route_url('categories'))
                        ])

File addressbook/templates/grid.mako

+<%inherit file="${context['main_template'].uri}" />
+<form action="${request.url}" method="post">
+  ${grid.render(request=request)}
+  <input type="submit" name="update" value="Update" class="btn"/>
+</form>
+
+<form action="${request.url}" method="post">
+  <fieldset>
+    <legend>New Category</legend>
+    ${fs.render(request=request)}
+    <input type="submit" name="new" value="Add" class="btn"/>
+  </fieldset>
+</form>

File addressbook/views.py

 from pyramid.view import view_config
 from pyramid.httpexceptions import HTTPFound
 from formalchemy import FieldSet, Grid
-from .models import DBSession, Person
+from .models import DBSession, Person, Category
 
 @view_config(route_name="top", renderer="index.mako")
 def index(request):
         fs.sync()
         return HTTPFound(request.route_url('top'))
     return dict(fs=fs)
+
+@view_config(route_name="categories", renderer="grid.mako")
+def categories(request):
+    fs = FieldSet(Category, session=DBSession)
+    fs.configure(exclude=[fs.persons])
+    grid = Grid(Category, Category.query)
+    grid.configure(exclude=[grid.persons])
+    
+    if "update" in request.POST:
+        grid.rebind(data=request.POST)
+        if grid.validate():
+            grid.sync()
+            grid = Grid(Category, Category.query)
+            grid.configure(exclude=[grid.persons])
+
+    if "new" in request.POST:
+        fs.rebind(data=request.POST)
+        if fs.validate():
+            fs.sync()
+            fs = FieldSet(Category, session=DBSession)
+            fs.configure(exclude=[fs.persons])
+
+
+    return dict(grid=grid,
+                fs=fs)