db.py :  » Web-Frameworks » Nevow » Nevow-0.10.0 » examples » db » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Nevow 
Nevow » Nevow 0.10.0 » examples » db » db.py

from zope.interface import implements

from nevow import inevow
from nevow import loaders
from nevow import rend
from nevow import tags
from nevow.url import here

from formless import annotate
from formless import webform


whole = [(1, 'one'), (2, 'two'), (3, 'buckle'), (4, 'my'), (5, 'shoe')]


def doQuery(q, *args):
    """Pretend like we have a database and we are accessing it through this hypothetical interface.
    Ignore this. Use dbapi or adbapi instead, and build a real sql table. I hope that's obvious.
    """
    matchid = 'select * from foo where id ='
    setsql = 'update foo set subject = '
    insertsql = 'insert into foo values'
    if q == 'select * from foo':
        return whole
    elif q.startswith(matchid):
        theId = args[0]
        for dbid, subj in whole:
            if dbid == theId:
                return [(dbid, subj)]
        raise KeyError, theId
    elif q.startswith(setsql):
        newsubj, theId = args
        for index, (dbid, subj) in enumerate(whole):
            if dbid == theId:
                whole[index] = (dbid, newsubj)
    elif q.startswith(insertsql):
        max = whole[-1][0]
        subject, = args
        whole.append((max + 1, subject))


class IAddItem(annotate.TypedInterface):
    def addItem(newSubject=annotate.String()):
        pass
    addItem = annotate.autocallable(addItem)


class DBBrowser(rend.Page):
    implements(IAddItem)
    addSlash = True

    def addItem(self, newSubject):
        doQuery('insert into foo values subject = "%s"', newSubject)

    def data_queryDatabase(self, context, data):
        return doQuery('select * from foo')

    def render_row(self, context, data):
        theId, theSubj = data
        return context.tag[ # put our anchor in the li provided by the template
            tags.a(href=theId)[ theSubj ]
        ]

    docFactory = loaders.stan(
        tags.html[
            tags.body[
                tags.h1["Welcome, user"],
                tags.ul(data=tags.directive("queryDatabase"), render=tags.directive("sequence"))[
                    tags.li(pattern="item", render=render_row)
                    ],
                webform.renderForms()
                ]
            ]
        )

    def childFactory(self, ctx, name):
        """Since we created anchor tags linking to children of this resource
        directly by id, when the anchor is clicked, childFactory will be called
        with the appropriate id as the name argument."""
        try:
            ## Pass the id of the database item we want to be rendered on this page
            ## to the DBItem constructor. This integer will be used as the default data
            ## for this page.
            return DBItem(int(name))
        except ValueError:
            pass
            ## returning None results in a 404


class IItemWithSubject(annotate.TypedInterface):
    def setSubject(newSubject=annotate.String(label="Change Subject")):
        pass
    setSubject = annotate.autocallable(setSubject)


class DBItem(rend.Page):
    implements(IItemWithSubject)
    addSlash=True

    def setSubject(self, newSubject):
        ## Self.original is the data that was passed to the DBItem constructor above; the id of this record
        doQuery('update foo set subject = "%s" where id = %s', newSubject, self.original)

    def render_viewSelector(self, context, data):
        args = inevow.IRequest(context).args
        view = args.get('view', ['view'])[0]
        if view == 'view':
            selector = "View | ", tags.a(href=here.add('view','edit'))[ "Edit" ]
            editor = ''
        else:
            selector = tags.a(href=here.add('view','view'))["View"], " | Edit"
            editor = context.onePattern('edit')() # get one copy of the edit pattern
        viewer = context.onePattern('view')() # get one copy of the view pattern
        return selector, viewer, editor

    def render_itemDetail(self, context, data):
        theId, theSubject = doQuery('select * from foo where id = %s', self.original)[0]
        return tags.h2["Object ", theId], tags.span["Subject: ", theSubject]

    docFactory = loaders.stan(
        tags.html[
            tags.body[
                tags.p[tags.a(href=here.parent())["Up"]],
                tags.div(render=render_viewSelector)[
                    tags.p(pattern="edit")[webform.renderForms()],
                    tags.p(pattern="view")[render_itemDetail]
                    ]
                ]
            ]
        )

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.