From 707f90daa6d0780cb621e63d3697baaf0996dd6d Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Thu, 12 Jun 2008 20:36:12 +0000 Subject: Add a last_visit field --- diff --git a/globalhistory.py b/globalhistory.py index a0ad1b7..811de7e 100644 --- a/globalhistory.py +++ b/globalhistory.py @@ -14,6 +14,8 @@ # along with this program; if not, write to the Free Software # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +from datetime import datetime + from xpcom import components from xpcom.components import interfaces from xpcom.server.factory import Factory @@ -42,6 +44,7 @@ class GlobalHistory: place = self._store.lookup_place(uri.spec) if place: place.visits += 1 + place.last_visit = datetime.now() self._store.update_place(place) else: place = places.Place(uri.spec) diff --git a/places.py b/places.py index 00c6be0..35e2dc0 100644 --- a/places.py +++ b/places.py @@ -16,6 +16,7 @@ import os import sqlite3 +from datetime import datetime from sugar.activity import activity @@ -27,6 +28,7 @@ class Place(object): self.title = None self.gecko_flags = 0 self.visits = 0 + self.last_visit = datetime.now() class SqliteStore(object): def __init__(self): @@ -39,10 +41,11 @@ class SqliteStore(object): cur.execute('select * from sqlite_master where name == "places"') if cur.fetchone() == None: cur.execute("""create table places ( - uri TEXT, - title TEXT, - gecko_flags INTEGER, - visits INTEGER + uri text, + title text, + gecko_flags integer, + visits integer, + last_visit timestamp ); """) @@ -53,15 +56,21 @@ class SqliteStore(object): cur.execute('select * from places where uri like ? or title like ? ' \ 'order by visits desc limit 0, 30', (text, text)) - return [_place_from_row(row) for row in cur] + result = [self._place_from_row(row) for row in cur] + + cur.close() + + return result def add_place(self, place): cur = self._con.cursor() - cur.execute('insert into places values (?, ?, ?, ?)', \ - (place.uri, place.title, place.gecko_flags, place.visits)) + cur.execute('insert into places values (?, ?, ?, ?, ?)', \ + (place.uri, place.title, place.gecko_flags, + place.visits, place.last_visit)) self._con.commit() + cur.close() def lookup_place(self, uri): cur = self._con.cursor() @@ -69,23 +78,28 @@ class SqliteStore(object): row = cur.fetchone() if row: - return _place_from_row(row) + return self._place_from_row(row) else: return None + cur.close() + def update_place(self, place): cur = self._con.cursor() - cur.execute('update places set title=?, gecko_flags=?, visits=? ' \ - 'where uri=?', (place.title, place.gecko_flags, - place.visits, place.uri)) + cur.execute('update places set title=?, gecko_flags=?, ' + 'visits=?, last_visit=? where uri=?', + (place.title, place.gecko_flags, place.visits, + place.last_visit, place.uri)) self._con.commit() + cur.close() -def _place_from_row(row): - place = Place() - place.uri, place.title, place.gecko_flags, place.visits = row - return place + def _place_from_row(self, row): + place = Place() + place.uri, place.title, place.gecko_flags, \ + place.visits, place.last_visit = row + return place def get_store(): global _store -- cgit v0.9.1