Commits

Sam Hart committed 5cf6edd

quite a bit of work to get the event log storing information for playback, also work on the various DB classes and add a pickler

Comments (0)

Files changed (8)

src/noink/dataModels.py

     description = mainDB.Column(mainDB.String(500))
     date = mainDB.Column(mainDB.DateTime())
     user = mainDB.Column(mainDB.Integer)
+    blob = mainDB.Column(mainDB.String(500))
     processed = mainDB.Column(mainDB.Boolean())
     processedDate = mainDB.Column(mainDB.DateTime())
 
-    def __init__(self, event, description, date, user):
+
+    def __init__(self, event, description, date, user, blob):
         self.event = event
         self.description = description
         self.date = date
         self.user = user
+        self.blob = blob
 
     def __repr__(self):
         return "<Event %s>" % self.event

src/noink/entryDB.py

 from types import IntType, StringType
 
 from noink import mainDB
+from noink.urlDB import UrlDB
 from noink.dataModels import Entry
+from noink.pickler import PEntry, pickle, depickle
 from noink.eventLog import EventLog
+from noink.exceptions import DuplicateURL
 
 class EntryDB:
     __borg_state = {}
 
         if not self._setup:
             self.eventLog = EventLog()
+            self.urlDB = UrlDB()
             self._setup = True
 
     def add(self, title, entry, author, url=None):
 
         Will not perform any checks, it will just add this entry. It's not
         this method's responsibility to check whether or not your entry is a
-        duplicate.
+        duplicate. The one check it does do is to verify whether a URL is
+        unique or not.
 
         @param title: The title of the post.
         @param entry: The entry of the post.
         now = datetime.datetime.now()
 
         e = Entry(title, author, now, entry)
-        if type(url) is StringType:
-            #
-        mainDB.session.add(e)
-        mainDB.session.commit()
 
-        self.eventLog.add('add_entry', author.id, False, entry.title)
+       if type(url) is StringType:
+            if self.urlDB.findByName(url):
+                raise DuplicateURL('The URL "%s" was already found in the UrlDB!' % url)
+            else:
+                mainDB.session.add(e)
+                mainDB.session.commit()
+                self.urlDB.add(url, e)
+        else:
+            mainDB.session.add(e)
+            mainDB.session.commit()
+        self.eventLog.add('add_entry', author.id, False, str(entry.id), entry.title)
         return e
 
     def findByTitle(self, title):
         @param e: An entry to delete. Can be an integer for the entry id or an
                   entry object.
         '''
-
         entry = e
         if type(e) is IntType:
             entry = Entry.query.filter_by(id=e).first()
-        mainDB.session.delete(e)
+
+        url = self.urlDB.findByEntry(e)
+        pe = PEntry(e, url)
+        if url:
+            self.urlDB.delete(url)
+        mainDB.session.delete(entry)
         mainDB.session.commit()
+        self.eventLog.add('del_entry', 0, False, pickle(pe), entry.title)
 

src/noink/eventLog.py

     def __init__(self):
         self.__dict__ = self.__borg_state
 
-    def add(self, name, user, processed=False, *args):
+    def add(self, name, user, processed=False, blob='', *args):
         '''
         Adds an event to the log.
 
         if eventTable.has_key(name):
             now = datetime.datetime.now()
             if len(args) > 0:
-                e = Event(name, eventTable[name] % args, now, user)
+                e = Event(name, eventTable[name] % args, now, user, blob)
             else:
-                e = Event(name, eventTable[name], now, user)
+                e = Event(name, eventTable[name], now, user, blob)
 
             e.processed = processed
             if processed:

src/noink/eventsTable.py

     'add_user' : "User '%s' added",
     'add_group' : "Group '%s' added",
     'add_entry' : "Entry '%s' added",
-    'add_url' : "URL '%s' added for entry '%s'"
+    'add_url' : "URL '%s' added for entry '%s'",
+    'del_url' : "URL '%s' deleted",
+    'del_entry' : "Entry '%s' deleted",
+    'del_user' : "User '%s' deleted"
 }

src/noink/exceptions.py

 
     def __str__(self):
         return repr(self.value)
+
+class DuplicateURL(Exception):
+
+    def __init__(self, value):
+        self.value = value
+
+    def __str__(self):
+        return repr(self.value)
+

src/noink/pickler.py

+'''
+Common pickle objects for Noink event logs
+
+##BOILERPLATE_COPYRIGHT
+##BOILERPLATE_COPYRIGHT_END
+'''
+
+from copy import copy, deepcopy
+from pickle import dumps, loads
+
+# Entries
+class PEntry:
+    def __init__(self, entry, url):
+        self.id = copy(entry.id)
+        self.title = copy(entry.title)
+        if url:
+            self.url = copy(url.name)
+
+def pickle(self, obj):
+    '''
+    Takes an object to be pickled, will return the pickled string.
+
+    @param obj: Object to be pickled.
+
+    @return String of pickled object.
+    '''
+    return dumps(objs)
+
+def depickle(self, string):
+    '''
+    Takes a pickled string, and returns the de-pickled object.
+
+    @param string: Pickled string.
+
+    @return De-pickled object.
+    '''
+    return loads(objs)

src/noink/urlDB.py

         @param name: The URL of the post.
         @param entry: The entry itself. Can be an entry ID or an entry object.
 
-        @returns New URL object just added
+        @return New URL object just added
         '''
 
         e = entry
             mainDB.session.add(u)
             mainDB.session.commit()
 
-            self.eventLog.add('add_url', e.author_id, True, (name, e.id))
+            self.eventLog.add('add_url', e.author_id, False, str(u.id), (name, e.id))
             return u
         else:
             raise EntryNotFound('The entry could not be found! Entry "%e"' % e)
+
+    def findByName(self, url):
+        '''
+        Attempts to find a URL by the name.
+
+        @param url: The URL to find.
+
+        @rerturn The URL object or None on failure.
+        '''
+
+        return URL.query.filter_by(name=url).first()
+
+    def findByEntry(self, e):
+        '''
+        Attempts to find a URL by an entry given.
+
+        @param e: The entry. Can either be an entry id or and entry object.
+
+        @return The URL object or None on failure.
+        '''
+        if type(e) is IntType:
+            return URL.query.filter_by(entry_id=e).first()
+        return URL.query.filter_by(entry_id=e.id).first()
+
+    def delete(self, u):
+        '''
+        Deletes an entry from the database.
+
+        @param u: A url to delete. Can be an integer for the URL id or a URL
+                  object.
+        '''
+        url = u
+        if type(u) is IntType:
+            url = URL.query.filter_by(id=u).first()
+        uid = int(u.entry.author_id)
+        name = str(u.name)
+        mainDB.session.delete(url)
+        mainDB.session.commit()
+        self.eventLog.add('del_url', uid, False, name, name)

src/noink/userDB.py

             u = User(username, fullname, bio)
             mainDB.session.add(u)
             mainDB.session.commit()
-            self.eventLog.add('add_user', u.id, True, username)
+            self.eventLog.add('add_user', u.id, True, None, username)
             return u
 
     def addGroup(self, groupName, userId=None):
                     raise UserNotFound("%s not found in database to match with new group" % userId)
 
             mainDB.session.commit()
-            self.eventLog.add('add_group', (groupName))
+            self.eventLog.add('add_group', 0, True, None, groupName)
             return g
 
     def delete(self, u):
         user = u
         if type(u) is IntType:
             user = User.query.filter_by(id=u).first()
+        uid = int(user.id)
+        uname = str(uder.name)
         mainDB.session.delete(user)
         mainDB.session.commit()
+        self.eventLog.add('del_user', uid, True, None, uname)