Mailing List Archive

TAL and empty sql-list
I have an sql statement that given a username it returns a project.

So my first question is: Is it possible to let the statement return a
string instead of a list. i.e. 'project' instead of ['project'] ?

So, if call it from a page template, I can use

<div tal:content="python:here.project(uname=username)[0].project" />

But if the project method returns an empty list, this statement will
fail (because of [0]).

So what is a smart solution to this ? I've tried putting a repeat
statement around this, and tried defining a variable called projectExist
using len on the function. This however makes the page template very
complex when calling another sql method such as:

<div
tal:python:here.membersInSectionAndProject(section=sectionList[0].section,

project=projectList[0].project)" />

if I'm using, say, the Python Database API I would just call fetchone()
on the cursor object. It is that functionality I am looking for.




_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: TAL and empty sql-list [ In reply to ]
On 2006-03-20 at 12:01:55 [+0100], Jonas Nielsen <jonasn@mail.tele.dk>
wrote:
> I have an sql statement that given a username it returns a project.
>
> So my first question is: Is it possible to let the statement return a
> string instead of a list. i.e. 'project' instead of ['project'] ?
>
> So, if call it from a page template, I can use
>
> <div tal:content="python:here.project(uname=username)[0].project" />
>
> But if the project method returns an empty list, this statement will
> fail (because of [0]).

> if I'm using, say, the Python Database API I would just call fetchone()
> on the cursor object. It is that functionality I am looking for.

Yes, it's very frustrating.

You've got two choices: either use a tal:repeat around your content which
is not only more code but as you point out not always helpful. You can also
use a Python statement or PythonScript to help you out:

<block tal:define="result python:here.project(uname=username)[0].project or
''"></block>

Charlie
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: TAL and empty sql-list [ In reply to ]
sorry for the double post --
A python script would be your best bet. You would be able to use that
in other areas of your site in that case.

On 3/21/06, Charlie Clark <charlie@egenix.com> wrote:
>
> On 2006-03-20 at 12:01:55 [+0100], Jonas Nielsen <jonasn@mail.tele.dk>
> wrote:
> > I have an sql statement that given a username it returns a project.
> >
> > So my first question is: Is it possible to let the statement return a
> > string instead of a list. i.e. 'project' instead of ['project'] ?
> >
> > So, if call it from a page template, I can use
> >
> > <div tal:content="python:here.project(uname=username)[0].project" />
> >
> > But if the project method returns an empty list, this statement will
> > fail (because of [0]).
>
> > if I'm using, say, the Python Database API I would just call fetchone()
> > on the cursor object. It is that functionality I am looking for.
>
> Yes, it's very frustrating.
>
> You've got two choices: either use a tal:repeat around your content which
> is not only more code but as you point out not always helpful. You can also
> use a Python statement or PythonScript to help you out:
>
> <block tal:define="result python:here.project(uname=username)[0].project or
> ''"></block>
>
> Charlie
> _______________________________________________
> Zope-DB mailing list
> Zope-DB@zope.org
> http://mail.zope.org/mailman/listinfo/zope-db
>
Re: TAL and empty sql-list [ In reply to ]
On 2006-03-21 at 15:21:29 [+0100], Jimmy small <mallaice@gmail.com> wrote:
> sorry for the double post --
> A python script would be your best bet. You would be able to use that
> in other areas of your site in that case.

At least as long as TALES doesn't support this directly.

In a PythonScript you can use the .names() method of your results object to
create an empty tuple for your results.

Charlie
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db
Re: TAL and empty sql-list [ In reply to ]
If you really have to do it in TAL try:

<div tal:define="results python:here.project(uname=username)"
tal:content="python: results and results[0].project or '---' " />

--
Maciej Wisniowski
_______________________________________________
Zope-DB mailing list
Zope-DB@zope.org
http://mail.zope.org/mailman/listinfo/zope-db