On 1/22/13 1:16 PM, Carey Gagnon wrote:
> def getCurrentProject(self):
>         if self.Record.projectname:
>             projectName = self.Record.projectname
>             projectNameSearch = projectName.split()
>             projectSearchName = projectNameSearch[0]
>             crs = self.getTempCursor()
>             crs.execute("select builderfk, projectname, buildername from
> projects inner join builders on projects.builderfk = builders.id where
> projectname like %s", ('%%%s%%' % projectSearchName))
>             ds = crs.getDataSet()
>             if ds:
>                 cnt = len(ds)
>                 recs = ds
>                 return ("There are %s project similar to %s already in the
> database as,\n%s.\n\nDo you "
>                         "want to add it anyway?" % (cnt, projectName, ", "
> .join(str(rec["projectrname"]) for rec in recs))

Try this, and ask me about where I've optimized the code for you in places.

{{{
def getCurrentProject(self):
  """Return information about similar projects; Called before saving."""
  rec = self.Record
  if rec.projectname:
    projectSearchName = rec.projectname.split()[0]
    crs = self.getTempCursor("""
select builderfk,
       projectname,
       buildername
  from projects
 inner join builders
    on builders.id = projects.builderfk
 where projectname like ?""", (projectSearchName,))

  names = []
  for i in range(crs.RowCount):
    crs.RowNumber = i
    names.append(crs.Record.projectname)
  if names:
    plural = ("is", "")
    if len(names) == 1:
      plural = ("are", "s")
    ret = "There %s %s project%s similar to %s already in the database, as:\n" \
        % (plural[0], len(names), plural[1], rec.projectname)
    for name in sorted(names):
      ret += " -> %s\n" % name
    ret += "\nDo you want to add it anyway?"
    return ret
}}}

It is untested an so may have name or syntax errors, but you should get the 
idea.

Paul

_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://mail.leafe.com/mailman/listinfo/dabo-users
Searchable Archives: http://leafe.com/archives/search/dabo-users
This message: http://leafe.com/archives/byMID/[email protected]

Reply via email to