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	Mon May 24 07:28:39 2010 -0700
+++ b/taginthemiddle/handlers.py	Mon May 24 07:44:39 2010 -0700
@@ -98,6 +98,8 @@
 
     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'
--- a/taginthemiddle/model.py	Mon May 24 07:28:39 2010 -0700
+++ b/taginthemiddle/model.py	Mon May 24 07:44:39 2010 -0700
@@ -1,35 +1,57 @@
 """
- 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 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 __init__(self, path):
+  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()
 
@@ -41,10 +63,10 @@
       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):
@@ -54,18 +76,55 @@
                                               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=''
+    """