[xiph-commits] r3263 - in arkaiv/trunk/arkaiv: controllers model

dcrowdy at svn.annodex.net dcrowdy at svn.annodex.net
Mon Oct 29 01:06:25 PDT 2007


Author: dcrowdy
Date: 2007-10-29 01:06:24 -0700 (Mon, 29 Oct 2007)
New Revision: 3263

Modified:
   arkaiv/trunk/arkaiv/controllers/page.py
   arkaiv/trunk/arkaiv/controllers/page.pyc
   arkaiv/trunk/arkaiv/model/__init__.py
   arkaiv/trunk/arkaiv/model/__init__.py.old
   arkaiv/trunk/arkaiv/model/__init__.pyc
Log:
Now allows "and" search for multiple terms - searching still pretty basic


Modified: arkaiv/trunk/arkaiv/controllers/page.py
===================================================================
--- arkaiv/trunk/arkaiv/controllers/page.py	2007-10-29 07:58:10 UTC (rev 3262)
+++ arkaiv/trunk/arkaiv/controllers/page.py	2007-10-29 08:06:24 UTC (rev 3263)
@@ -36,7 +36,8 @@
 
 myhostname = gethostname()
 url_base = "http://" + myhostname + ":5000/"
-media_url_base = "http://" + myhostname + "/"
+#media_url_base = "http://" + myhostname + "/"
+media_url_base = "http://127.0.0.1/"
 
 class PageController(BaseController):
 
@@ -418,7 +419,6 @@
         if not os.path.isdir(imagedirname):
             os.makedirs(imagedirname, mode=0755)
 
-                
         # Get the title for the item (head title)
         parser = cmmlParser()
         parser.setcmmlfilename(newcmmlname)
@@ -461,13 +461,9 @@
                 clipdict['img_src'] = ""
             model.addclip(clipdict)
 
-        #print cliplist
-
         # Check whether the ogg file has any existing vorbis comments/tags
         # and import them as meta tags
 
-                #metalist=[]
-
         parser = cmmlParser()
         parser.setcmmlfilename(newcmmlname)
         metalist = parser.getheadinfofromcmml()
@@ -488,7 +484,6 @@
             
         # Extract meta info from the head and add to database
 
-
         for metadict in metalist:
             metadict['headindex'] = headid
             metadict['itemid'] = newitemid
@@ -514,7 +509,13 @@
 
     def search(self):
         searchterms = request.params['searchterms']
-        # actually we can only search for a single search term at the moment
+
+        # actually we can only search for a single search term at the moment To
+        # search for more terms, we should split the searchterms strings here
+        # and send the model a list of search terms it can search through.  If,
+        # at this stage, we assume that the process is "and", that should be
+        # reasonably straightforward.
+
         c.clips = model.searchforclips(searchterms)
         c.items = model.searchforitems(searchterms)
         c.metas = model.searchformetas(searchterms)
@@ -549,9 +550,9 @@
         files = os.listdir(directory)
         for file in files:
             try:
-                if os.path.splitext(file)[1] == ".cmml":
-                    oggfilename = directory + os.path.splitext(file)[0] + ".ogg"
-                    cmmlfilename = directory + file
+                if os.path.splitext(file)[1] == ".ogg":
+                    cmmlfilename = directory + os.path.splitext(file)[0] + ".cmml"
+                    oggfilename = directory + file
                     print "importing: " + cmmlfilename + " " + oggfilename
                     self.__localdirimport(cmmlfilename, oggfilename)
                 else:
@@ -559,22 +560,68 @@
             except:
                 print " problem with " + file
         return
- 
+#    def addannodexindirmedia(self):
+#        directory = request.params['mediadir']
+#        files = os.listdir(directory)
+#        for file in files:
+#            try:
+#                if os.path.splitext(file)[1] == ".cmml":
+#                    oggfilename = directory + os.path.splitext(file)[0] + ".ogg"
+#                    cmmlfilename = directory + file
+#                    print "importing: " + cmmlfilename + " " + oggfilename
+#                    self.__localdirimport(cmmlfilename, oggfilename)
+#                else:
+#                    print "moving right past: " + file
+#            except:
+#                print " problem with " + file
+#        return
+  
     def __localdirimport(self, cmmlfilename, oggfilename): 
-        newcmmlname = os.path.join(archive_loc, os.path.split(cmmlfilename)[1])
-        permanent_file = open(newcmmlname, 'w')
-        sourcefile = open(cmmlfilename, 'r')
-        shutil.copyfileobj(sourcefile, permanent_file)
-        sourcefile.close()
-        permanent_file.close()
+        print "well we get here"
+        print os.path.split(cmmlfilename)
+#        if os.path.split(cmmlfilename)[1] == "":
+        if (not os.path.isfile(cmmlfilename)):
+            # create new cmmlfile
+            print "going to create a new cmmlfile"
+            baseoggname = os.path.split(oggfilename)[1]
+            basecmmlname = os.path.splitext(baseoggname)[0].replace(' ',"") + ".cmml"
+            print "basecmmlname: " + basecmmlname
+            print "archive_loc: " + archive_loc
+            newcmmlname = os.path.join(archive_loc, basecmmlname)
+            print "newcmmlname: " + newcmmlname
+            parser = cmmlParser()
+            fulloggpath = os.path.join(archive_loc, baseoggname)
+            if os.path.isfile(newcmmlname):
+                print "File exists - bail time"
+                return "file exists" 
+            parser.createnewfile(newcmmlname, fulloggpath)
+
+        else:
+            newcmmlname = os.path.join(archive_loc, os.path.split(cmmlfilename)[1])
+            permanent_file = open(newcmmlname, 'w')
+            sourcefile = open(cmmlfilename, 'r')
+            shutil.copyfileobj(sourcefile, permanent_file)
+            sourcefile.close()
+            permanent_file.close()
        
         # Now the ogg file...
-        newoggname = os.path.join(archive_loc, os.path.split(oggfilename)[1])
+        baseoggname = os.path.split(oggfilename)[1]
+        baseoggname = baseoggname.replace(' ',"")
+        newoggname = os.path.join(archive_loc, baseoggname)
+#        newoggname = os.path.join(archive_loc, os.path.split(oggfilename)[1])
         permanent_file = open(newoggname, 'w')
         sourcefile = open(oggfilename, 'r')
         shutil.copyfileobj(sourcefile, permanent_file)
         sourcefile.close()
         permanent_file.close()
+
+        # shouldn't the image directory stuff happen here?
+        basename = os.path.splitext(newoggname)[0]
+        imagedirname = basename + "_clipimages"
+        # Create a directory for clip images
+#        print imagedirname
+        if not os.path.isdir(imagedirname):
+            os.makedirs(imagedirname, mode=0755)
  
         # Get the title for the item (head title)
         parser = cmmlParser()
@@ -601,11 +648,30 @@
         for clipdict in cliplist:
             clipdict['itemid'] = newitemid
             model.addclip(clipdict)
- 
-        # Extract meta info from the head and add to database
+
+        # Check whether the ogg file has any existing vorbis comments/tags
+        # and import them as meta tags
+
         parser = cmmlParser()
         parser.setcmmlfilename(newcmmlname)
         metalist = parser.getheadinfofromcmml()
+
+        cmd = "vorbiscomment -l " + newoggname
+        print "cmd: " + cmd
+        runit = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE , stderr=subprocess.PIPE)
+        out, err = runit.communicate()
+
+        for line in out.splitlines():
+            metadict={}
+            print line
+            tag = line.split("=")
+            metadict['name'] = tag[0]
+            metadict['content'] = tag[1]
+            metadict['id'] = tag[0]
+            metadict['scheme'] = ""
+            metalist.append(metadict)
+  
+        # Extract meta info from the head and add to database
         for metadict in metalist:
             metadict['headindex'] = headid
             metadict['itemid'] = newitemid

Modified: arkaiv/trunk/arkaiv/controllers/page.pyc
===================================================================
(Binary files differ)

Modified: arkaiv/trunk/arkaiv/model/__init__.py
===================================================================
--- arkaiv/trunk/arkaiv/model/__init__.py	2007-10-29 07:58:10 UTC (rev 3262)
+++ arkaiv/trunk/arkaiv/model/__init__.py	2007-10-29 08:06:24 UTC (rev 3263)
@@ -9,7 +9,7 @@
 # history and logs.
 #
 # Author: Denis Crowdy <Denis.Crowdy at mq.edu.au>
-
+import string
 from pylons import config
 from sqlalchemy import Column, MetaData, Table, ForeignKey, types
 from sqlalchemy.sql import and_, or_, not_
@@ -378,7 +378,7 @@
     return
 
 def additemtoplaylist(list, itemid):
-    """ Adds and item to a playlist
+    """ Adds an item to a playlist
 
     """
     p_q = Session.query(Playlist)
@@ -444,7 +444,7 @@
     p = p_q.filter_by(name=listname).first()
     # then get all items in the playlist_items table
     pi_q = Session.query(Playlistitems)
-    pi = pi_q.filter_by(ixp=p.ixp).first()
+    pi = pi_q.filter_by(ixp=p.ixp).all()
     for item in pi:
         Session.delete(item)
 
@@ -772,16 +772,24 @@
 
 # testing new search functions
 
-def searchforclips(searchterm):
+def searchforclips(searchterms):
     # search for clips within the database, and return a list of dictionaries
     # with keys "clipindex", "itemindex", "itemname", "clipdesc", "clipid"
-    searchstring = "%" + searchterm + "%"
-    clip_q = Session.query(Clip)
-    c = clip_q.select(clips_table.c.desc.like(searchstring))
+    
+    searchlist = string.split(searchterms, " ")
+    session = Session()
+    clip_q = session.query(Clip)
+    for term in searchlist:
+        searchstring = "%" + term + "%"
+        clip_q = clip_q.filter(Clip.desc.like(searchstring))
+
+#   Notes on more complex searching...
+#    (clips_table.c.desc.like(a) AND clips_table.c.desc.like(b) AND...
+
     clips = []
     ic_q = Session.query(Itemclips)
     it_q = Session.query(Item)
-    for i in c:
+    for i in clip_q:
         clip = {}
         ic = ic_q.filter_by(ixc=i.ixc).first()
         item = it_q.filter_by(ixi=ic.ixi).first() 
@@ -795,7 +803,7 @@
 
     return clips
 
-def searchforitems(searchterm):
+def oldsearchforitems(searchterm):
     searchstring = "%" + searchterm + "%"
     item_q = Session.query(Item)
     i = item_q.select(items_table.c.name.like(searchstring))
@@ -809,23 +817,48 @@
 
     return items
 
-def searchformetas(searchterm):
+def searchforitems(searchterms):
+    searchlist = string.split(searchterms, " ")
+    session = Session()
+    item_q = session.query(Item)
+    for term in searchlist:
+        searchstring = "%" + term + "%"
+        item_q = item_q.filter(Item.name.like(searchstring))
+
+    items = []
+    for n in item_q:
+        item = {}
+        item['index'] = n.ixi
+        item['name'] = n.name
+        item['cmmlfile'] = n.cmmlfile
+        items.append(item)
+
+    return items
+
+
+def searchformetas(searchterms):
     """ searches in the meta name and content fields of the database
 
         returns a list of dictionaries with keys:
             index, name, content, itemindex, itemname
     """
-    searchstring = "%" + searchterm + "%"
-    meta_q = Session.query(Meta)
-    s = or_((metas_table.c.name.like(searchstring)), (metas_table.c.content.like(searchstring)))
-    m = meta_q.select(s)
-    #m = meta_q.select((metas_table.c.name.like(searchstring))
-#    m = meta_q.select((metas_table.c.name.like(searchstring)), or_(metas_table.c.content.like(searchstring)))
+    # split search string at whitespace:
+    searchlist = string.split(searchterms, " ")
+#    searchstring = "%" + searchterm + "%"
+    session = Session()
+#    meta_q = session.query(Meta)
+#    statement = metas_table.select()
+    meta_q = session.query(Meta)
+    for term in searchlist:
+        searchstring = "%" + term + "%"
+        #meta_q = meta_q.filter(Meta.name.like(searchstring))
+        meta_q = meta_q.filter(or_(Meta.name.like(searchstring), Meta.content.like(searchstring)))
+
     hm_q = Session.query(Headmetas)
     ih_q = Session.query(Itemheads)
     i_q = Session.query(Item)
     metas = []
-    for n in m:
+    for n in meta_q:
         meta = {}
         hm = hm_q.filter_by(ixm=n.ixm).first()
         ih = ih_q.filter_by(ixh=hm.ixh).first()

Modified: arkaiv/trunk/arkaiv/model/__init__.py.old
===================================================================
--- arkaiv/trunk/arkaiv/model/__init__.py.old	2007-10-29 07:58:10 UTC (rev 3262)
+++ arkaiv/trunk/arkaiv/model/__init__.py.old	2007-10-29 08:06:24 UTC (rev 3263)
@@ -10,148 +10,153 @@
 #
 # Author: Denis Crowdy <Denis.Crowdy at mq.edu.au>
 
-import sqlalchemy as sqla
-from sqlalchemy.orm import mapper
-from sacontext import PylonsSAContext
+from pylons import config
+from sqlalchemy import Column, MetaData, Table, ForeignKey, types
+from sqlalchemy.sql import and_, or_, not_
+from sqlalchemy.orm import mapper, relation
+from sqlalchemy.orm import scoped_session, sessionmaker
+#from sacontext import PylonsSAContext
 import arkaiv.lib.helpers as h
 from operator import itemgetter
 #from arkaiv.model.oggzinfo import OggzInfo
 
-sac = PylonsSAContext()
-sac.add_engine_from_config("default")
+#sac = PylonsSAContext()
+#sac.add_engine_from_config("default")
+Session = scoped_session(sessionmaker(autoflush=True, transactional=True, bind=config['pylons.g'].sa_engine))
 
-system_table = sqla.Table('system', sac.metadata,
-        sqla.Column('db_version', sqla.Integer)
+metadata = MetaData()
+
+system_table = Table('system', metadata,
+        Column('db_version', types.Integer)
 )
 
-collections_table = sqla.Table('collections', sac.metadata,
-	sqla.Column('ixk', sqla.Integer, primary_key=True),
-	sqla.Column('name', sqla.String())
+collections_table = Table('collections', metadata,
+	Column('ixk', types.Integer, primary_key=True),
+	Column('name', types.String())
 )
 
-items_table = sqla.Table('items', sac.metadata,
-	sqla.Column('ixi', sqla.Integer, primary_key=True),
-	sqla.Column('name', sqla.String()),
-	sqla.Column('cmmlfile', sqla.String())
+items_table = Table('items', metadata,
+	Column('ixi', types.Integer, primary_key=True),
+	Column('name', types.String()),
+	Column('cmmlfile', types.String())
 )
 
-collection_items_table = sqla.Table('collection_items', sac.metadata,
-	sqla.Column('ixci', sqla.Integer, primary_key=True),
-	sqla.Column('ixk', sqla.Integer, sqla.ForeignKey('collections.ixk')),
-	sqla.Column('ixi', sqla.Integer, sqla.ForeignKey('items.ixi'))
+collection_items_table = Table('collection_items', metadata,
+	Column('ixci', types.Integer, primary_key=True),
+	Column('ixk', types.Integer, ForeignKey('collections.ixk')),
+	Column('ixi', types.Integer, ForeignKey('items.ixi'))
 )
 
-playlists_table = sqla.Table('playlists', sac.metadata,
-    sqla.Column('ixp', sqla.Integer, primary_key=True),
-    sqla.Column('name', sqla.String())
+playlists_table = Table('playlists', metadata,
+    Column('ixp', types.Integer, primary_key=True),
+    Column('name', types.String())
 )
 
-playlist_items_table = sqla.Table('playlist_items', sac.metadata,
-    sqla.Column('ixpi', sqla.Integer, primary_key=True),
-    sqla.Column('ixp', sqla.Integer(), sqla.ForeignKey('playlists.ixp')),
-    sqla.Column('ixi', sqla.Integer(), sqla.ForeignKey('items.ixi')),
-    sqla.Column('ixc', sqla.Integer(), sqla.ForeignKey('clips.ixc'))
+playlist_items_table = Table('playlist_items', metadata,
+    Column('ixpi', types.Integer, primary_key=True),
+    Column('ixp', types.Integer(), ForeignKey('playlists.ixp')),
+    Column('ixi', types.Integer(), ForeignKey('items.ixi')),
+    Column('ixc', types.Integer(), ForeignKey('clips.ixc'))
 )
 
-
-sources_table = sqla.Table('sources', sac.metadata,
-	sqla.Column('ixs', sqla.Integer, primary_key=True),
-	sqla.Column('path', sqla.String())
+sources_table = Table('sources', metadata,
+	Column('ixs', types.Integer, primary_key=True),
+	Column('path', types.String())
 )
 
-item_sources_table = sqla.Table('item_sources', sac.metadata,
-	sqla.Column('ixis', sqla.Integer, primary_key=True),
-	sqla.Column('ixi', sqla.Integer, sqla.ForeignKey('items.ixi')),
-	sqla.Column('ixs', sqla.Integer, sqla.ForeignKey('sources.ixs')
+item_sources_table = Table('item_sources', metadata,
+	Column('ixis', types.Integer, primary_key=True),
+	Column('ixi', types.Integer, ForeignKey('items.ixi')),
+	Column('ixs', types.Integer, ForeignKey('sources.ixs')
 ))
 
-item_clips_table = sqla.Table('item_clips', sac.metadata,
-	sqla.Column('ixic', sqla.Integer, primary_key=True),
-	sqla.Column('ixi', sqla.Integer, sqla.ForeignKey('items.ixi')),
-	sqla.Column('ixc', sqla.Integer, sqla.ForeignKey('clips.ixc')
+item_clips_table = Table('item_clips', metadata,
+	Column('ixic', types.Integer, primary_key=True),
+	Column('ixi', types.Integer, ForeignKey('items.ixi')),
+	Column('ixc', types.Integer, ForeignKey('clips.ixc')
 ))
 
-item_heads_table = sqla.Table('item_heads', sac.metadata,
-	sqla.Column('ixih', sqla.Integer, primary_key=True),
-	sqla.Column('ixi', sqla.Integer, sqla.ForeignKey('items.ixi')),
-	sqla.Column('ixh', sqla.Integer, sqla.ForeignKey('heads.ixh')
+item_heads_table = Table('item_heads', metadata,
+	Column('ixih', types.Integer, primary_key=True),
+	Column('ixi', types.Integer, ForeignKey('items.ixi')),
+	Column('ixh', types.Integer, ForeignKey('heads.ixh')
 ))
 
-metas_table = sqla.Table('metas', sac.metadata,
-	sqla.Column('ixm', sqla.Integer, primary_key=True),
-	sqla.Column('id', sqla.String()),
-	sqla.Column('name', sqla.String()),
-	sqla.Column('content', sqla.String()),
-	sqla.Column('scheme', sqla.String())
+metas_table = Table('metas', metadata,
+	Column('ixm', types.Integer, primary_key=True),
+	Column('id', types.String()),
+	Column('name', types.String()),
+	Column('content', types.String()),
+	Column('scheme', types.String())
 )
 
-head_metas_table = sqla.Table('head_metas', sac.metadata,
-	sqla.Column('ixhm', sqla.Integer, primary_key=True),
-	sqla.Column('ixh', sqla.Integer, sqla.ForeignKey('heads.ixh')),
-	sqla.Column('ixm', sqla.Integer, sqla.ForeignKey('metas.ixm')
+head_metas_table = Table('head_metas', metadata,
+	Column('ixhm', types.Integer, primary_key=True),
+	Column('ixh', types.Integer, ForeignKey('heads.ixh')),
+	Column('ixm', types.Integer, ForeignKey('metas.ixm')
 ))
 
-clip_metas_table = sqla.Table('clip_metas', sac.metadata,
-	sqla.Column('ixcm', sqla.Integer, primary_key=True),
-	sqla.Column('ixc', sqla.Integer, sqla.ForeignKey('clips.ixc')),
-	sqla.Column('ixm', sqla.Integer, sqla.ForeignKey('metas.ixm')
+clip_metas_table = Table('clip_metas', metadata,
+	Column('ixcm', types.Integer, primary_key=True),
+	Column('ixc', types.Integer, ForeignKey('clips.ixc')),
+	Column('ixm', types.Integer, ForeignKey('metas.ixm')
 ))
 
-heads_table = sqla.Table('heads', sac.metadata,
-	sqla.Column('ixh', sqla.Integer, primary_key=True),
-	sqla.Column('id', sqla.String()),
-	sqla.Column('title', sqla.String())
+heads_table = Table('heads', metadata,
+	Column('ixh', types.Integer, primary_key=True),
+	Column('id', types.String()),
+	Column('title', types.String())
 )
 
-clips_table = sqla.Table('clips', sac.metadata,
-	sqla.Column('ixc', sqla.Integer, primary_key=True ),
-	sqla.Column('start_time', sqla.Float ),
-	sqla.Column('end_time', sqla.Float ),
-	sqla.Column('id', sqla.String()),
-	sqla.Column('a_href', sqla.String()),
-	sqla.Column('a_text', sqla.String()),
-	sqla.Column('img_src', sqla.String()),
-	sqla.Column('desc', sqla.String()),
+clips_table = Table('clips', metadata,
+	Column('ixc', types.Integer, primary_key=True ),
+	Column('start_time', types.Float ),
+	Column('end_time', types.Float ),
+	Column('id', types.String()),
+	Column('a_href', types.String()),
+	Column('a_text', types.String()),
+	Column('img_src', types.String()),
+	Column('desc', types.String()),
 )
 
-rename_table = sqla.Table('rename', sac.metadata,
-	sqla.Column('ixr', sqla.Integer, primary_key=True),
-	sqla.Column('oldname', sqla.String()),
-	sqla.Column('newname', sqla.String())
+rename_table = Table('rename', metadata,
+	Column('ixr', types.Integer, primary_key=True),
+	Column('oldname', types.String()),
+	Column('newname', types.String())
 )
 
-channels_table = sqla.Table('channels', sac.metadata,
-	sqla.Column('ixch', sqla.Integer, primary_key=True ),
-	sqla.Column('title', sqla.String()),
-	sqla.Column('link', sqla.String()),
-	sqla.Column('description', sqla.String()),
-	sqla.Column('language', sqla.String()),
-	sqla.Column('copyright', sqla.String()),
-	sqla.Column('managingEditor', sqla.String()),
-	sqla.Column('webMaster', sqla.String()),
-	sqla.Column('image_path', sqla.String())
+channels_table = Table('channels', metadata,
+	Column('ixch', types.Integer, primary_key=True ),
+	Column('title', types.String()),
+	Column('link', types.String()),
+	Column('description', types.String()),
+	Column('language', types.String()),
+	Column('copyright', types.String()),
+	Column('managingEditor', types.String()),
+	Column('webMaster', types.String()),
+	Column('image_path', types.String())
 )
 
-stories_table = sqla.Table('stories', sac.metadata,
-	sqla.Column('ixst', sqla.Integer, primary_key=True ),
-	sqla.Column('id', sqla.String()),
-	sqla.Column('title', sqla.String()),
-	sqla.Column('pubdate', sqla.String()),
-	sqla.Column('link', sqla.String()),
-	sqla.Column('description', sqla.String()),
-	sqla.Column('author', sqla.String()),
-	sqla.Column('category', sqla.String())
+stories_table = Table('stories', metadata,
+	Column('ixst', types.Integer, primary_key=True ),
+	Column('id', types.String()),
+	Column('title', types.String()),
+	Column('pubdate', types.String()),
+	Column('link', types.String()),
+	Column('description', types.String()),
+	Column('author', types.String()),
+	Column('category', types.String())
 )
 
-source_info_table = sqla.Table('source_info', sac.metadata,
-	sqla.Column('ixs', sqla.Integer, primary_key=True ),
-	sqla.Column('duration', sqla.String()),
-	sqla.Column('bitrate', sqla.String()),
-	sqla.Column('resolution', sqla.String()),
-	sqla.Column('framerate', sqla.String()),
-	sqla.Column('has_video', sqla.Integer),
-	sqla.Column('has_audio', sqla.Integer),
-	sqla.Column('audio', sqla.String())
+source_info_table = Table('source_info', metadata,
+	Column('ixs', types.Integer, primary_key=True ),
+	Column('duration', types.String()),
+	Column('bitrate', types.String()),
+	Column('resolution', types.String()),
+	Column('framerate', types.String()),
+	Column('has_video', types.Integer),
+	Column('has_audio', types.Integer),
+	Column('audio', types.String())
 )
 
 class System(object):
@@ -230,159 +235,221 @@
     def __str__(self):
         return self.name
 
-sqla.mapper(Collection, collections_table, extension=sac.ext)
-sqla.mapper(Collectionitems, collection_items_table, extension=sac.ext)
+mapper(Collection, collections_table)
+mapper(Collectionitems, collection_items_table)
 
-sqla.mapper(Item, items_table, properties = {
-        'my_collections' : sqla.relation(Collection,
-                           secondary = collection_items_table)},
-                           extension=sac.ext)
+mapper(Item, items_table, properties = {
+        'my_collections' : relation(Collection,
+                           secondary = collection_items_table)})
 
+mapper(Playlist, playlists_table)
 
-sqla.mapper(Playlist, playlists_table, extension=sac.ext)
+mapper(Playlistitems, playlist_items_table)
 
-sqla.mapper(Playlistitems, playlist_items_table, extension=sac.ext)
+#mapper(Collection, collections_table)
 
-#sqla.mapper(Collection, collections_table, extension=sac.ext)
+mapper(Source, sources_table, properties = {
+	'my_items' : relation(Item, secondary = item_sources_table)
+	})
+mapper(Sourceinfo, source_info_table)
+mapper(Itemsources, item_sources_table)
+mapper(Head, heads_table, properties = {
+	'my_items' : relation(Item, secondary = item_heads_table)
+	})
 
-sqla.mapper(Source, sources_table, properties = {
-	'my_items' : sqla.relation(Item, secondary = item_sources_table)
-	},
-	extension=sac.ext)
-sqla.mapper(Sourceinfo, source_info_table, extension=sac.ext)
-sqla.mapper(Itemsources, item_sources_table, extension=sac.ext)
-sqla.mapper(Head, heads_table, properties = {
-	'my_items' : sqla.relation(Item, secondary = item_heads_table)
-	},
-	extension=sac.ext)
-sqla.mapper(Itemheads, item_heads_table, extension=sac.ext)
-sqla.mapper(Meta, metas_table, properties = {
-	'my_heads' : sqla.relation(Head, secondary = head_metas_table)
-	},
-	extension=sac.ext)
-sqla.mapper(Headmetas, head_metas_table, extension=sac.ext)
-sqla.mapper(Clip, clips_table, properties = {
-	'my_items' : sqla.relation(Item, secondary = item_clips_table)
-	},
-	extension=sac.ext)
-sqla.mapper(Itemclips, item_clips_table, extension=sac.ext)
+mapper(Itemheads, item_heads_table)
+mapper(Meta, metas_table, properties = {
+	'my_heads' : relation(Head, secondary = head_metas_table)
+	})
 
+mapper(Headmetas, head_metas_table)
+mapper(Clip, clips_table, properties = {
+	'my_items' : relation(Item, secondary = item_clips_table)
+	})
 
+mapper(Itemclips, item_clips_table)
+
 def getallitems():
-    item = items_table
-    i = item.select().execute().fetchall()
-    return i
+    """ get a list of all items in the database
 
+        returns a list of dicts with keys "index" and "name"
+    """
+    item_q = Session.query(Item)
+    i = item_q.all()
+    items = []
+    for n in i:
+        item = {}
+        item['index'] = n.ixi
+        item['name'] = n.name
+        items.append(item)
+
+    return items
+
 def addnewcollection(name):
+    """ Adds a new collection to the database given a name
+
+    """
     newcollection = Collection()
     newcollection.name = name
-    sac.session.flush()
+    Session.save(newcollection)
+    Session.commit()
     return
 
 def getcollections():
-    coll = collections_table
-    c = coll.select().execute().fetchall()
-    return c
+    """ gets a list of all collections in the database
 
+        returns a list of dicts with keys "index" and "name"
+    """
+    coll_q = Session.query(Collection)
+    c = coll_q.all()
+    collections=[]
+    for n in c:
+        collection = {}
+        collection['index'] = n.ixk
+        collection['name'] = n.name
+        collections.append(collection)
+
+    return collections
+
 def getitemsincollection(id):
-    coll_items_q = sac.query(Collectionitems)
+    """ Gets all items from a collection given a collection index value
+
+        returns a list of dictionaries with keys "index" and "name"
+    """
+    coll_items_q = Session.query(Collectionitems)
     # get a list of ids 
     cilist = coll_items_q.filter_by(ixk=id)
-    itemlist = []
+    items = []
     for ci in cilist:
-        item_q = sac.query(Item)
-        i = item_q.get_by(ixi=ci.ixi) 
-        itemlist.append(i)
+        item={}
+        item_q = Session.query(Item)
+        i = item_q.filter_by(ixi=ci.ixi).first()
+        item['index'] = i.ixi
+        item['name'] = i.name
+        items.append(item)
 
-    sac.session.flush()
-    return itemlist
+    Session.commit()
 
+    return items
+
 def getplaylists():
-    """ Return a list of all current playlists
+    """ get all current playlists from database
 
+        returns a list of dicts with keys "index" and "name"
     """
-    play = playlists_table
-    p = play.select().execute().fetchall()
-    return p
+    play_q = Session.query(Playlist)
+    p = play_q.all()
+    playlists=[]
+    for n in p:
+        playlist={}
+        playlist['index']=n.ixp
+        playlist['name']=n.name
+        playlists.append(playlist)
 
+    return playlists
+
 def addnewplaylist(name):
     """ Add a new named playlist to the database
 
     """
     newplaylist = Playlist()
     newplaylist.name = name
-    sac.session.flush()
+    Session.save(newplaylist)
+    Session.commit()
     return
 
 def addcliptoplaylist(list, clipid):
+    """ Adds a clip to a playlist
+
+    """
     # get the id for the list
-    p_q = sac.query(Playlist)
-    p = p_q.get_by(name=list)
+    p_q = Session.query(Playlist)
+    p = p_q.filter_by(name=list).first()
     # get the item id:
-    ic_q = sac.query(Itemclips)
-    ic = ic_q.get_by(ixc=clipid)
+    ic_q = Session.query(Itemclips)
+    ic = ic_q.filter_by(ixc=clipid).first()
     # add an entry to playlist_items
     pi = Playlistitems()
     pi.ixc = clipid
     pi.ixi = ic.ixi
     pi.ixp = p.ixp
-
-    sac.session.flush()
+    Session.save(pi)
+    Session.commit()
     return
 
 def additemtoplaylist(list, itemid):
-    p_q = sac.query(Playlist)
-    p = p_q.get_by(name=list)
+    """ Adds and item to a playlist
+
+    """
+    p_q = Session.query(Playlist)
+    p = p_q.filter_by(name=list).first()
     pi = Playlistitems()
     #pi.ixc = clipid
     pi.ixi = itemid
     pi.ixp = p.ixp
 
-    sac.session.flush()
+    Session.save(pi)
+    Session.commit()
     return
 
 
 def getclipsfromplaylist(listid):
-    pi_q = sac.query(Playlistitems)
-    pi = pi_q.filter_by(ixp=listid)
-    c_q = sac.query(Clip)
-    cliplist = []
+    pi_q = Session.query(Playlistitems)
+    pi = pi_q.filter_by(ixp=listid).all()
+    print pi
+    c_q = Session.query(Clip)
+    clips = []
     for item in pi:
-        c = c_q.get_by(ixc=item.ixc)
-        cliplist.append(c)
+        if item.ixc:
+            print item.ixc
+            clip = {}
+            c = c_q.filter_by(ixc=item.ixc).first()
+            clip['index'] = c.ixc
+            clip['start'] = c.start_time
+            clip['end'] = c.end_time
+            clip['id'] = c.id
+            clip['a_href'] = c.a_href
+            clip['a_text'] = c.a_text
+            clip['img_src'] = c.img_src
+            clip['desc'] = c.desc
+            clips.append(clip)
         
-    return cliplist
+    return clips
 
 def getitemsfromplaylist(listid):
     """ This gets items in the playlist that have no clip info
 
     """
-    pi_q = sac.query(Playlistitems)
-    pi = pi_q.filter_by(ixp=listid)
-    i_q = sac.query(Item)
-    itemlist = []
-    for item in pi:
+    pi_q = Session.query(Playlistitems)
+    pi = pi_q.filter_by(ixp=listid).all()
+    i_q = Session.query(Item)
+    items = []
+    for n in pi:
         #print item.ixc
-        if (not item.ixc):
-            i = i_q.get_by(ixi=item.ixi)
-            itemlist.append(i)
+        if (not n.ixc):
+            item = {}
+            i = i_q.filter_by(ixi=n.ixi).first()
+            item['index'] = i.ixi
+            item['name'] = i.name
+            item['cmmlfile'] = i.cmmlfile
+            items.append(item)
 
-    return itemlist
+    return items
 
 def deleteplaylist(listname):
     """ deletes a playlist and items in the playlist_items table
         given the playlist name
     """
-    p_q = sac.query(Playlist)
-    p = p_q.get_by(name=listname)
+    p_q = Session.query(Playlist)
+    p = p_q.filter_by(name=listname).first()
     # then get all items in the playlist_items table
-    pi_q = sac.query(Playlistitems)
-    pi = pi_q.filter_by(ixp=p.ixp)
+    pi_q = Session.query(Playlistitems)
+    pi = pi_q.filter_by(ixp=p.ixp).first()
     for item in pi:
-        sac.session.delete(item)
+        Session.delete(item)
 
-    sac.session.delete(p)
-    sac.session.flush()
+    Session.delete(p)
+    Session.commit()
 
 def addsource(itemname, source, cmmlfile):
     new_item = Item()
@@ -392,16 +459,20 @@
     new_source.path = source
     new_source.my_items.append(new_item) 
 	
-    sac.session.flush()
+    Session.save(new_item)
+    Session.save(new_source)
+    Session.commit()
     return new_item.ixi
 
 def additemtocollection(itemid, collection):
     new_coll_item = Collectionitems()
-    coll_q = sac.query(Collection)
-    c = coll_q.get_by(name=collection)
+    coll_q = Session.query(Collection)
+    #c = coll_q.filter_by(name=collection)
+    c = coll_q.filter_by(name=collection).first()
     new_coll_item.ixk = c.ixk
     new_coll_item.ixi = itemid
-    sac.session.flush()
+    Session.save(new_coll_item)
+    Session.commit()
     return
     
 def edititemname(itemid, text):
@@ -409,66 +480,64 @@
 
 
     """
-    item_q = sac.query(Item)
-    i = item_q.get_by(ixi=itemid)
+    item_q = Session.query(Item)
+    #i = item_q.filter_by(ixi=itemid)
+    i = item_q.filter_by(ixi=itemid).first()
     i.name = text
-    sac.session.flush()
+    Session.commit()
 
     return
      
 def getitemname(id):
-    item_q = sac.query(Item)
-    i = item_q.get_by(ixi=id)
+    item_q = Session.query(Item)
+    #i = item_q.filter_by(ixi=id)
+    i = item_q.filter_by(ixi=id).first()
     name = i.name
     return name
 
 def getitemforid(id):
-    item_q = sac.query(Item)
-    i = item_q.get_by(ixi=id)
+    item_q = Session.query(Item)
+    i = item_q.filter_by(ixi=id).first()
     return i
 
 def getitemforclip(clipid):
-    ic_q = sac.query(Itemclips)
-    ic = ic_q.get_by(ixc=clipid)
-    i_q = sac.query(Item)
-    i = i_q.get_by(ixi=ic.ixi)
+    ic_q = Session.query(Itemclips)
+    ic = ic_q.filter_by(ixc=clipid).first()
+    i_q = Session.query(Item)
+    i = i_q.filter_by(ixi=ic.ixi).first()
     return i
 
 def getclip(clipid):
-    c_q = sac.query(Clip)
-    c = c_q.get_by(ixc=clipid)
+    c_q = Session.query(Clip)
+    c = c_q.filter_by(ixc=clipid).first()
     return c
 
 def getheadmeta(id):
-    meta_q = sac.query(Meta)
-    m = meta_q.get_by(ixm=id)
-    print m.ixm
+    meta_q = Session.query(Meta)
+    m = meta_q.filter_by(ixm=id).first()
     return m
 
 def getitemforheadmeta(metaid):
     #first get the head
-    hm_q = sac.query(Headmetas)
-    h = hm_q.get_by(ixm=metaid)
-    ih_q = sac.query(Itemheads)
-    ih = ih_q.get_by(ixh=h.ixh)
-    i_q = sac.query(Item)
-    i = i_q.get_by(ixi=ih.ixi)
-    print "i is: " 
-    print i
-    print "i.ixi is" + str(i.ixi)
+    hm_q = Session.query(Headmetas)
+    h = hm_q.filter_by(ixm=metaid).first()
+    ih_q = Session.query(Itemheads)
+    ih = ih_q.filter_by(ixh=h.ixh).first()
+    i_q = Session.query(Item)
+    i = i_q.filter_by(ixi=ih.ixi).first()
     return i
 
 
 def getitemcmmlfile(id):
-    item_q = sac.query(Item)
-    i = item_q.get_by(ixi=id)
+    item_q = Session.query(Item)
+    i = item_q.filter_by(ixi=id).first()
     cmmlfile = i.cmmlfile
     return cmmlfile
 
 def getsourcesforitem(id):
     # get the results from item_sources for given item id
-    item_sources_q = sac.query(Itemsources)
-    s = item_sources_q.get_by(ixi=id)
+    item_sources_q = Session.query(Itemsources)
+    s = item_sources_q.filter_by(ixi=id).first()
     sid = s.ixs 
     return sid 
 
@@ -477,37 +546,38 @@
 
         Returns the source path
      """
-    source_q = sac.query(Source)
-    s = source_q.get_by(ixs=id)
+    source_q = Session.query(Source)
+    s = source_q.filter_by(ixs=id).first()
     path = s.path
     return path
 
 def addhead(info):
     # get the item this refers to:
-    item_q = sac.query(Item)
-    i = item_q.get_by(ixi=info['itemid'])
+    item_q = Session.query(Item)
+    i = item_q.filter_by(ixi=info['itemid']).first()
     new_head = Head()    
     new_head.id = info['id']
     new_head.title = info['title']
     new_head.my_items.append(i)
-    sac.session.flush() 
+    Session.save(new_head)
+    Session.commit() 
     return new_head.ixh
 
 def edithead(info):
-    head_q = sac.query(Head)
-    itemhead_q = sac.query(Itemheads)
-    ih = itemhead_q.get_by(ixi=info['itemid'])
-    h = head_q.get_by(ixh=ih.ixh)
+    head_q = Session.query(Head)
+    itemhead_q = Session.query(Itemheads)
+    ih = itemhead_q.filter_by(ixi=info['itemid']).first()
+    h = head_q.filter_by(ixh=ih.ixh).first()
     h.id = info['id']
     h.title = info['title']
-    sac.session.flush() 
+    Session.commit() 
     return   
 
 def getclipinfoforitem(id):
-    item_clip_q = sac.query(Itemclips)
+    item_clip_q = Session.query(Itemclips)
     iclist = item_clip_q.filter_by(ixi=id).all()
     cliplist = []
-    clip_q = sac.query(Clip)
+    clip_q = Session.query(Clip)
 
     # FIXME - why not just return a list of the objects?  Why the dictionary
     # step?  Madness.
@@ -515,7 +585,7 @@
     for ic in iclist:
         # get individual meta info
         clipinfo = {}
-        c = clip_q.get_by(ixc=ic.ixc)
+        c = clip_q.filter_by(ixc=ic.ixc).first()
         clipinfo['ixc'] = c.ixc
         clipinfo['start_time'] = c.start_time
         clipinfo['end_time'] = c.end_time
@@ -527,82 +597,83 @@
         cliplist.append(clipinfo)
     # great way to sort a list of dictionaries:
     sortedcliplist = sorted(cliplist, key=itemgetter('start_time'))
-    sac.session.flush()
+    Session.commit()
     return sortedcliplist
 
 def getheadinfoforitem(id):
     headdict = {}
-    head_q = sac.query(Head)
-    itemhead_q = sac.query(Itemheads)
-    ih = itemhead_q.get_by(ixi=id)
-    h = head_q.get_by(ixh=ih.ixh)
-    headdict['ixh'] = h.ixh
+    head_q = Session.query(Head)
+    itemhead_q = Session.query(Itemheads)
+    ih = itemhead_q.filter_by(ixi=id).first()
+    h = head_q.filter_by(ixh=ih.ixh).first()
+    headdict['index'] = h.ixh
     headdict['id'] = h.id
     headdict['title'] = h.title
     return headdict
 
 def getmetasforhead(headid):
-    head_meta_q = sac.query(Headmetas)
+    head_meta_q = Session.query(Headmetas)
     hmlist = head_meta_q.filter_by(ixh=headid).all()
     metalist = []
-    meta_q = sac.query(Meta)
+    meta_q = Session.query(Meta)
     for hm in hmlist:
         metainfo = {}
-        m = meta_q.get_by(ixm=hm.ixm)
+        m = meta_q.filter_by(ixm=hm.ixm).first()
         metalist.append(m)
 
-    sac.session.flush()
+    Session.commit()
     return metalist
 
 def getmetainfoforhead(id):
+    """ Get meta info for an item's head
 
-# This returns a dictionary - deprecated by getmetasforhead, but used in
-# displayitem  FIXME
-
-    # now return a list of metas with that id in head_metas table
-    head_meta_q = sac.query(Headmetas)
+        Returns a list of dicts with keys:
+            index, id, name, content, scheme
+    """
+    head_meta_q = Session.query(Headmetas)
     hmlist = head_meta_q.filter_by(ixh=id).all()
-    metalist = []
-    meta_q = sac.query(Meta)
+    metas = []
+    meta_q = Session.query(Meta)
     for hm in hmlist:
         # get individual meta info
-        metainfo = {}
-        m = meta_q.get_by(ixm=hm.ixm)
-        metainfo['ixm'] = m.ixm
-        metainfo['id'] = m.id
-        metainfo['name'] = m.name
-        metainfo['content'] = m.content
-        metainfo['scheme'] = m.scheme
-        metalist.append(metainfo)
+        meta= {}
+        m = meta_q.filter_by(ixm=hm.ixm).first()
+        meta['index'] = m.ixm
+        meta['id'] = m.id
+        meta['name'] = m.name
+        meta['content'] = m.content
+        meta['scheme'] = m.scheme
+        metas.append(meta)
 
-    sac.session.flush()
-    return metalist
+    Session.commit()
+    return metas
 
 def addmeta(info):
     """ Given a dictionary of meta info, adds a meta item to database
 
         returns the newly created meta item
     """ 
-    head_q = sac.query(Head)
-    h  = head_q.get_by(ixh=info['headindex'])
+    head_q = Session.query(Head)
+    h  = head_q.filter_by(ixh=info['headindex']).first()
     new_meta = Meta() 
     new_meta.id = info['id']
     new_meta.name = info['name']
     new_meta.content = info['content']
     new_meta.scheme = info['scheme']
     new_meta.my_heads.append(h)
-    sac.session.flush()
-    return new_meta
+    Session.save(new_meta)
+    Session.commit()
+    return new_meta.ixm
 
 def syncmetaid(metaid):
     """ Given the meta primary key, it sets id to the same value
 
     """
 
-    meta_q = sac.query(Meta)
-    m = meta_q.get_by(ixm=metaid)
+    meta_q = Session.query(Meta)
+    m = meta_q.filter_by(ixm=metaid).first()
     m.id = metaid
-    sac.session.flush()
+    Session.commit()
     return
 
 def editmeta(metaid, field, text):
@@ -614,126 +685,156 @@
 
         returns nothing yet... 
     """
-    meta_q = sac.query(Meta)
-    m = meta_q.get_by(ixm=metaid)
+    meta_q = Session.query(Meta)
+    m = meta_q.filter_by(ixm=metaid).first()
     if field=="name":
         m.name = text
     elif field=="content":
         m.content = text
-    sac.session.flush()
+    Session.commit()
     return
 
 def addclip(info):
-    item_q = sac.query(Item)
-    i = item_q.get_by(ixi=info['itemid'])
+    item_q = Session.query(Item)
+    i = item_q.filter_by(ixi=info['itemid']).first()
     new_clip = Clip()
     new_clip.start_time = info['starttime']
-    try:
+    if info['endtime'] == "":
+        new_clip.end_time = 0
+    else:
         new_clip.end_time = info['endtime']
-    except:
-        pass
     new_clip.id = info['id']
     new_clip.a_href = info['a_href']
     new_clip.a_text = info['a_text']
     new_clip.img_src = info['img_src']
     new_clip.desc = info['desc']
     new_clip.my_items.append(i)
-    sac.session.flush()
+    Session.save(new_clip)
+    Session.commit()
     return
 
 def deleteclip(id):
-    clip_q = sac.query(Clip)
-    c = clip_q.get_by(ixc=id)
-    sac.session.delete(c)
-    sac.session.flush()
+    clip_q = Session.query(Clip)
+    c = clip_q.filter_by(ixc=id).first()
+    Session.delete(c)
+    Session.commit()
     return
     
 def deleteitem(id):
-    item_q = sac.query(Item)
-    i = item_q.get_by(ixi=id)
-    sac.session.delete(i)
+    item_q = Session.query(Item)
+    i = item_q.filter_by(ixi=id).first()
+    Session.delete(i)
 
     # remove head info
-    ih_q = sac.query(Itemheads)
-    ih = ih_q.get_by(ixi=id)
+    ih_q = Session.query(Itemheads)
+    ih = ih_q.filter_by(ixi=id).first()
 
     # remove meta tags from this head
-    hm_q = sac.query(Headmetas)
+    hm_q = Session.query(Headmetas)
     hm = hm_q.filter_by(ixh=ih.ixh)
-    m_q = sac.query(Meta)
+    m_q = Session.query(Meta)
     for headmeta in hm:
-        m = m_q.get_by(ixm=headmeta.ixm)
-#        sac.session.delete(headmeta)
-        sac.session.delete(m)
+        m = m_q.filter_by(ixm=headmeta.ixm).first()
+#        Session.delete(headmeta)
+        Session.delete(m)
 
     # Remove all clips
-    ic_q = sac.query(Itemclips)
-    c_q = sac.query(Clip)
+    ic_q = Session.query(Itemclips)
+    c_q = Session.query(Clip)
     ic = ic_q.filter_by(ixi=id)
     for itemclip in ic:
-        c = c_q.get_by(ixc=itemclip.ixc)
-        sac.session.delete(c)
+        c = c_q.filter_by(ixc=itemclip.ixc).first()
+        Session.delete(c)
     
     # remove the head entry
-    h_q = sac.query(Head)
-    h = h_q.get_by(ixh=ih.ixh)
-    sac.session.delete(h)
+    h_q = Session.query(Head)
+    h = h_q.filter_by(ixh=ih.ixh).first()
+    Session.delete(h)
 
     # remove the source information as well
-    is_q = sac.query(Itemsources)
-    i_s = is_q.get_by(ixi=id)
-    s_q = sac.query(Source)
-    s = s_q.get_by(ixs=i_s.ixs)
-    #sac.session.delete(i_s)
-    sac.session.delete(s)
+    is_q = Session.query(Itemsources)
+    i_s = is_q.filter_by(ixi=id).first()
+    s_q = Session.query(Source)
+    s = s_q.filter_by(ixs=i_s.ixs).first()
+    #Session.delete(i_s)
+    Session.delete(s)
 
-    sac.session.flush()
+    Session.commit()
     return
 
-def search(searchterms):
-    # All very rough - done in a hurry; lots of code that can be combined in a
-    # single function here; need to be able to cope with multiple search words
-    # too
 
-    resultsdict={}
-    searchstring = "%" + searchterms + "%"
-    # look for item names:
-    item_q = sac.query(Item)
-    items = item_q.select(items_table.c.name.like(searchstring))
-    itemnames = []
-    for item in items:
-        itemnames.append(item.ixi)
+def deletemeta(metaid):
+    meta_q = Session.query(Meta)
+    m = meta_q.filter_by(ixm=metaid).first()
+    Session.delete(m)
+    Session.commit()
+    return
 
-    # look for meta names:
-    meta_q = sac.query(Meta)
-    metas = meta_q.select(metas_table.c.name.like(searchstring))
-    metanames = []
-    for meta in metas:
-        metanames.append(meta.ixm)
+# testing new search functions
 
-    metas = meta_q.select(metas_table.c.content.like(searchstring))
-    # keep appending to the metanames list
-    for meta in metas:
-        metanames.append(meta.ixm)
+def searchforclips(searchterm):
+    # search for clips within the database, and return a list of dictionaries
+    # with keys "clipindex", "itemindex", "itemname", "clipdesc", "clipid"
+    searchstring = "%" + searchterm + "%"
+    clip_q = Session.query(Clip)
+    c = clip_q.select(clips_table.c.desc.like(searchstring))
+    clips = []
+    ic_q = Session.query(Itemclips)
+    it_q = Session.query(Item)
+    for i in c:
+        clip = {}
+        ic = ic_q.filter_by(ixc=i.ixc).first()
+        item = it_q.filter_by(ixi=ic.ixi).first() 
+        clip['itemindex'] =  item.ixi
+        clip['itemname'] = item.name
+        clip['index'] = i.ixc
+        clip['id'] = i.id
+        clip['desc'] = i.desc
+        clips.append(clip)
+        #clipnames.append(clip.ixc)
 
-    # And search clips now:
-    clip_q = sac.query(Clip)
-    clips = clip_q.select(clips_table.c.desc.like(searchstring))
-    clipnames = []
-    for clip in clips:
-        clipnames.append(clip.ixc)
+    return clips
 
-    resultsdict['items'] = itemnames
-    resultsdict['metas'] = metanames
-    resultsdict['clips'] = clipnames
-    sac.session.flush()
+def searchforitems(searchterm):
+    searchstring = "%" + searchterm + "%"
+    item_q = Session.query(Item)
+    i = item_q.select(items_table.c.name.like(searchstring))
+    items = []
+    for n in i:
+        item = {}
+        item['index'] = n.ixi
+        item['name'] = n.name
+        item['cmmlfile'] = n.cmmlfile
+        items.append(item)
 
-    return resultsdict
+    return items
 
-def deletemeta(metaid):
-    meta_q = sac.query(Meta)
-    m = meta_q.get_by(ixm=metaid)
-    sac.session.delete(m)
-    sac.session.flush()
-    return
+def searchformetas(searchterm):
+    """ searches in the meta name and content fields of the database
 
+        returns a list of dictionaries with keys:
+            index, name, content, itemindex, itemname
+    """
+    searchstring = "%" + searchterm + "%"
+    meta_q = Session.query(Meta)
+    s = or_((metas_table.c.name.like(searchstring)), (metas_table.c.content.like(searchstring)))
+    m = meta_q.select(s)
+    #m = meta_q.select((metas_table.c.name.like(searchstring))
+#    m = meta_q.select((metas_table.c.name.like(searchstring)), or_(metas_table.c.content.like(searchstring)))
+    hm_q = Session.query(Headmetas)
+    ih_q = Session.query(Itemheads)
+    i_q = Session.query(Item)
+    metas = []
+    for n in m:
+        meta = {}
+        hm = hm_q.filter_by(ixm=n.ixm).first()
+        ih = ih_q.filter_by(ixh=hm.ixh).first()
+        i = i_q.filter_by(ixi=ih.ixi).first()
+        meta['index'] = n.ixm
+        meta['name'] = n.name
+        meta['content'] = n.content
+        meta['itemindex'] = i.ixi
+        meta['itemname'] = i.name
+        metas.append(meta)
+
+    return metas

Modified: arkaiv/trunk/arkaiv/model/__init__.pyc
===================================================================
(Binary files differ)



More information about the commits mailing list