changeset 17:c85d42296c06

make the model almost be real
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 24 May 2010 07:44:39 -0700
parents 5f492a6fbdd1
children 3bf478cb3166
files taginthemiddle/handlers.py taginthemiddle/model.py
diffstat 2 files changed, 85 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/taginthemiddle/handlers.py
+++ b/taginthemiddle/handlers.py
@@ -93,16 +93,18 @@ class ViewTags(GenshiHandler):
 
     /<resource>/tags/mozilla+workflow :
     all paths under /<resource> tagged with 'mozilla' and 'workflow'
     (or should it be /<resource>/tags/mozilla/workflow , which would be
     identical to /<resource>/tags/workflow/mozilla ?)
 
     Also handles RSS:
     /<resource>/tags/mozilla?format=rss
+
+    Maybe this takes care of the TagCloud too?  And/or view as most recent?
     """
     
     template = 'tags.html'
     methods = set(['GET'])
 
     def __init__(self, app, request):
         GenshiHandler.__init__(self, app, request)
         path = request.path_info.split('/')
--- a/taginthemiddle/model.py
+++ b/taginthemiddle/model.py
@@ -1,71 +1,130 @@
 """
- tags have:
+Tags models. Tags have:
  - a list of resources they're applied to
  - a time they're applied
- - for multi-user tags, the author should be passed (fudge for now)
- for now just assume  auth is on and that anyone authenticated may tag
- e.g. a tags file
-
-foo = /bar:1273023556 /fleem/baz:1273023556 etc
-cats = /cats:1273023556 /blog/mycat:1273023556
-
-Ideally, the form of tags would be something like a table
-
-Tag | URL | Author | Date [seconds since epoch]
+ - someone that tagged the resource
 """
 
 import os
 import time
 
 class Tags(object):
-  """file (e.g. .ini) based tagging scheme"""
+  """
+  abstract base class for tagging
+  """
 
-  def __init__(self, path):
+  def add(self, tag, url, author):
+    """add a tag to a URL"""
+    raise NotImplementedError
+
+  def remove(self, tag, url):
+    """remove a tag from a URL"""
+    raise NotImplementedError
+
+  def tags(self, url):
+    """
+    get the tags for a URL
+    """
+
+  def urls(self, *tags):
+    """
+    get the URLs for a set of tags
+    """
+
+class FileTags(Tags):
+  """
+  file (e.g. .ini) based tagging scheme
+
+  File format:
+  foo = /bar:1273023556 /fleem/baz:1273023556 etc
+  cats = /cats:1273023556 /blog/mycat:1273023556
+  """
+  # XXX TODO: store user;  currently this is not stored
+
+  def __init__(self, path, author):
     """
     - path: path to file
+    - author: who tagged the resource (fudge for now)
     """
     self.path = path
     if not os.path.exists(path):
       f = file(path, 'w')
       f.close()
-    self.tags = {}
-    self.urls = {}
+    self._tags = {}
+    self._urls = {}
     self.times = {}
     self.read()
 
   def read(self):
     for line in file(self.path).readlines():
       line = line.strip()
       if not line:
         continue
       tag, urls = line.split('=', 1) # asserts '=' in line
       tag = tag.strip()
       urls = urls.strip()
-      self.tags[tag] = set([urls])
+      self._tags[tag] = set([urls])
       for url in urls:
         url, time = url.rsplit(':', 1)
-        self.urls.setdefault(url, set()).add(tag)
+        self._urls.setdefault(url, set()).add(tag)
         self.times[(tag, url)] = int(time)
 
   def write(self):
     f = file(self.path, 'w')
     for tag in sorted(self.tags.keys()):
       print >> f, '%s = %s' % (tag, ' '.join(['%s:%s' % (url, self.times(tag, url))
                                               for url in self.tags[tag]]))
     f.close()
 
-  def add(self, tag, url):
-    if url in self.tags[tag]:
+  ### implementation of Tags interface
+
+  def add(self, tag, url, author):
+    if url in self._tags[tag]:
       return
-    self.tags[tag].add(url)
-    self.urls[url].add(tag)
+    self._tags[tag].add(url)
+    self._urls[url].add(tag)
     self.times[(tag, url)] = int(time.time())
     self.write()
 
   def remove(self, tag, url):
-    if url not in self.tags[tag]:
+    if url not in self._tags[tag]:
       return
-    self.tags[tag].remove(url)
-    self.urls[url].remove(tag)
+    self._tags[tag].remove(url)
+    self._urls[url].remove(tag)
     del self.times[(tag, url)]
     self.write()
+
+  def tags(self, url):
+    raise NotImplementedError
+
+  def urls(self, *tags):
+    raise NotImplementedError
+
+class SQLiteTags(Tags):
+  """
+  Tags model using SQLite
+
+  Tags are relational data stored in a table
+  Tag | URL | Author | Date [seconds since epoch]
+  """
+
+  def __init__(self, path):
+    """
+    - path: path to SQLite file
+    """
+
+  def add(self, tag, url, author):
+    raise NotImplementedError
+
+  def remove(self, tag, url):
+    raise NotImplementedError
+
+  def tags(self, url):
+    """
+    select * from tags where url='url'
+    """
+
+  def urls(self, *tags):
+    """
+    select * from tags where tag=''
+    """