Beispiel #1
0
    def test_db(self):
        import webstruct
        class Application(webstruct.Application):
            db_url = 'sqlite:///'
            templates = [os.path.join(here, 'templates')]

            @webstruct.view(template='index.html')
            def index(request):
                return {'message': 'This is Top'}
            class users(webstruct.Application):
                @webstruct.view(template='show_user.html', pattern=r'(?P<username>\w+)')
                def show_user(request):
                    name = request.urlvars['username']
                    user = webstruct.query(User).one()
                    return dict(user=user)
        from sqlalchemy import Column, Integer, String, UnicodeText
        class User(webstruct.Model):
            __tablename__ = 'user'
            id = Column(Integer, primary_key=True)
            username = Column(String(255), unique=True)
            description = Column(UnicodeText)
        webstruct.metadata.create_all()
        user = webstruct.new_data(User, username='******', description=u"this is test")
        webstruct.transaction.commit()
        app = TestApp(Application)
        res = app.get('/users/aodag')
        self.assert_('aodag' in res.body)
        self.assert_('this is test' in res.body)
Beispiel #2
0
    @webstruct.view(template='index.html')
    def index(req):
        return dict()

    class users(webstruct.Application):
        @webstruct.view(template="users.html")
        def index(req):
            users = webstruct.query(User).all()
            return dict(users=users)

        @webstruct.view(template="show_user.html", pattern=r'(?P<username>\w+)')
        def show_user(req):
            username = req.urlvars['username']
            user = webstruct.query_one_or_404(User, username=username)
            return dict(user=user)

from sqlalchemy import *

class User(webstruct.Model):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    username = Column(String(255), unique=True)
    description = Column(UnicodeText)

webstruct.metadata.create_all()
webstruct.new_data(User, username='******', description=u"""aodag is creator of webstruct.
""")
webstruct.transaction.commit()
webstruct.run(application)