This is great, I was having trouble going from the tutorial to my own
project, getting some funky session errors, I am going to give your code
a try and see if that makes it work
Jose

Waldemar Osuch wrote:
> Joe wrote:
>   
>> Hi,
>>
>> I'm trying to update the QuickWiki tutorial code by following the
>> recommendation from Mike Bayer to use sessions instead of
>> mods.threadlocal (and so that I can use it in my own app).
>>
>> I modified the BaseController as follows:
>>
>> ---
>> from sqlalchemy import *
>>
>> class BaseController(WSGIController):
>>     def __call__(self, environ, start_response):
>>         conn = model.meta.connect(
>>             request.environ['paste.config']['app_conf']['dsn']
>>         )
>>         sess = create_session(bind_to=conn)
>>         sess.clear()
>>         response = WSGIController.__call__(self, environ,
>> start_response)
>>         sess.flush()
>>         return response
>> ---
>>
>>     
>
> The approach I have taken when going trought tutorial recently is to
> define database connection in my model pages.py like this
> ===================================================
> from sqlalchemy import *
> import re
>
> from docutils.core import publish_parts
> import quickwiki.lib.helpers as h
>
> wikiwords = re.compile(r"\b([A-Z]\w+[A-Z]+\w+)")
> meta = BoundMetaData('sqlite:///qwicki.db')
>
> page = Table('page', meta,
>         Column('title', String(40), primary_key=True),
>         Column('content', String(), default='')
>         )
>
> meta.create_all()
>
> class Page(object):
>     content = None
>
>     def __str__(self):
>         return self.title
>
>     def get_wiki_content(self):
>         content = publish_parts(self.content,
> writer_name="html")["html_body"]
>         titles = wikiwords.findall(content)
>         if titles:
>             for title in titles:
>                 content = content.replace(
>                     title,
>                     h.link_to(
>                          title,
>                          h.url_for(
>                              controller='page',
>                              action='index',
>                              title=title
>                          ),
>                      ),
>                  )
>         return content
>
> mapper(Page, page)
> --------------------------------------------------------------------------------
>
> and the controller pages.py looks like this
> ==============================================
> from qwiki.lib.base import *
>
> from sqlalchemy import create_session
>
> class PageController(BaseController):
>     def __before__(self):
>         db = self.db = create_session()
>         self.qry = db.query(model.Page)
>
>     def __after__(self):
>         self.db.close()
>
>     def index(self, title):
>         pg = self.qry.get_by(title=title)
>         if pg:
>             c.content = pg.get_wiki_content()
>             return render_response('/page.myt')
>         elif model.wikiwords.match(title):
>             return render_response('/new_page.myt')
>         abort(404)
>
>     def edit(self, title):
>         pg = self.qry.get_by(title=title)
>         if pg:
>             c.content = pg.content
>         return render_response('/edit.myt')
>
>     def save(self, title):
>         pg = self.qry.get_by(title=title)
>         if not pg:
>             pg = model.Page()
>             pg.title = title
>         pg.content = request.params['content']
>         c.title = pg.title
>         c.content = pg.get_wiki_content()
>         c.message = 'Content Saved'
>         self.db.save_or_update(pg)
>         self.db.flush()
>         return render_response('/page.myt')
>
>     def list(self):
>         c.titles = (pg.title for pg in self.qry.select())
>         return render_response('/titles.myt')
>
>     def delete(self):
>         title = request.params['id'][:5]
>         pg = self.qry.get_by(title)
>         self.db.delete(pg)
>         self.db.flush()
>         c.titles = (pg.title for pg in self.qry.select())
>         return render_response('/list.myt', fragment=True)
> =================================================
>
> I did not touch BaseController prefering to keep database session
> locally where I can see it :-)
>
> It did seem to work OK. At least for the tutorial purposes.
> The only bad part is that the path to the database is hardcoded in
> models/page.py.
> I would prefer to get it from configuration file but I could not figure
> out
> where all the settings live.  Should be somwhere in paste I think.
>
> Waldemar
>
>
> >
>
>   


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss
-~----------~----~----~----~------~----~------~--~---

Reply via email to