From 4deab0e4779217dd0f82ba9beaea18b40ed31933 Mon Sep 17 00:00:00 2001 From: Aurélien Bompard Date: Mon, 3 Sep 2012 12:11:41 +0200 Subject: Rename hash_id to message_id_hash to implement MM's IMessage --- kittystore/storm/model.py | 11 ++++++++--- kittystore/storm/schema/__init__.py | 8 ++++---- kittystore/storm/store.py | 38 +++++++++++++++++-------------------- 3 files changed, 29 insertions(+), 28 deletions(-) diff --git a/kittystore/storm/model.py b/kittystore/storm/model.py index 469c5fa..a99f742 100644 --- a/kittystore/storm/model.py +++ b/kittystore/storm/model.py @@ -14,10 +14,12 @@ license. import datetime +from zope.interface import implements from storm.locals import * -from .hack_datetime import DateTime +from mailman.interfaces.messages import IMessage from kittystore.utils import get_message_id_hash +from .hack_datetime import DateTime __all__ = ("List", "Email",) @@ -34,6 +36,7 @@ class List(object): class Email(object): + implements(IMessage) __storm_table__ = "email" __storm_primary__ = "list_name", "message_id" @@ -45,12 +48,14 @@ class Email(object): content = Unicode() date = DateTime() in_reply_to = Unicode() - hash_id = Unicode() + message_id_hash = Unicode() thread_id = Unicode() full = RawStr() archived_date = DateTime(default_factory=datetime.datetime.now) + # path is required by IMessage, but it makes no sense here + path = None def __init__(self, list_name, message_id): self.list_name = unicode(list_name) self.message_id = unicode(message_id) - self.hash_id = unicode(get_message_id_hash(self.message_id)) + self.message_id_hash = unicode(get_message_id_hash(self.message_id)) diff --git a/kittystore/storm/schema/__init__.py b/kittystore/storm/schema/__init__.py index ece8df8..3e23256 100644 --- a/kittystore/storm/schema/__init__.py +++ b/kittystore/storm/schema/__init__.py @@ -17,7 +17,7 @@ CREATES = { content TEXT NOT NULL, date DATETIME NOT NULL, in_reply_to VARCHAR(255), -- How about replies from another list ? - hash_id VARCHAR(255) NOT NULL, + message_id_hash VARCHAR(255) NOT NULL, thread_id VARCHAR(255) NOT NULL, "full" BLOB NOT NULL, archived_date DATETIME DEFAULT CURRENT_TIMESTAMP, @@ -26,7 +26,7 @@ CREATES = { 'CREATE INDEX "ix_email_list_name" ON "email" (list_name);', 'CREATE UNIQUE INDEX "ix_email_message_id" ON "email" (message_id);', 'CREATE INDEX "ix_email_date" ON "email" (date);', - 'CREATE UNIQUE INDEX "ix_email_hash_id" ON "email" (hash_id);', + 'CREATE UNIQUE INDEX "ix_email_message_id_hash" ON "email" (message_id_hash);', 'CREATE INDEX "ix_email_subject" ON "email" (subject);', 'CREATE INDEX "ix_email_thread_id" ON "email" (thread_id);', ], @@ -45,7 +45,7 @@ CREATES = { content TEXT NOT NULL, date TIMESTAMP WITH TIME ZONE NOT NULL, in_reply_to VARCHAR(255), -- How about replies from another list ? - hash_id VARCHAR(255) NOT NULL, + message_id_hash VARCHAR(255) NOT NULL, thread_id VARCHAR(255) NOT NULL, "full" BYTEA NOT NULL, archived_date TIMESTAMP WITHOUT TIME ZONE DEFAULT CURRENT_TIMESTAMP, @@ -54,7 +54,7 @@ CREATES = { 'CREATE INDEX "ix_email_list_name" ON "email" USING btree (list_name);', 'CREATE UNIQUE INDEX "ix_email_message_id" ON "email" USING btree (message_id);', 'CREATE INDEX "ix_email_date" ON "email" USING btree (date);', - 'CREATE UNIQUE INDEX "ix_email_hash_id" ON "email" USING btree (hash_id);', + 'CREATE UNIQUE INDEX "ix_email_message_id_hash" ON "email" USING btree (message_id_hash);', 'CREATE INDEX "ix_email_subject" ON "email" USING btree (subject);', 'CREATE INDEX "ix_email_thread_id" ON "email" USING btree (thread_id);', ], diff --git a/kittystore/storm/store.py b/kittystore/storm/store.py index 167ac1e..4d98105 100644 --- a/kittystore/storm/store.py +++ b/kittystore/storm/store.py @@ -27,12 +27,6 @@ from storm.locals import * from .model import List, Email -#from kittystore.sa.kittysamodel import get_class_object -#from sqlalchemy import create_engine, distinct, MetaData, and_, desc, or_ -#from sqlalchemy.ext.declarative import declarative_base -#from sqlalchemy.orm import sessionmaker -#from sqlalchemy.orm.exc import NoResultFound - class StormStore(object): """ @@ -50,6 +44,8 @@ class StormStore(object): """ self.db = db + # IMessageStore methods + def add(self, message): """Add the message to the store. @@ -93,13 +89,13 @@ class StormStore(object): if self.is_message_in_list(list_name, email.message_id): print ("Duplicate email from %s: %s" % (message['From'], message.get('Subject', '""'))) - return email.hash_id + return email.message_id_hash # Find thread id ref, thread_id = get_ref_and_thread_id(message, list_name, self) if thread_id is None: # make up the thread_id if not found - thread_id = email.hash_id + thread_id = email.message_id_hash email.thread_id = thread_id email.in_reply_to = ref @@ -121,7 +117,7 @@ class StormStore(object): self.db.add(email) self.flush() - return email.hash_id + return email.message_id_hash def delete_message(self, message_id): """Remove the given message from the store. @@ -176,7 +172,7 @@ class StormStore(object): :returns: The message, or None if no matching message was found. """ return self.db.find(Email, - Email.hash_id == unicode(message_id_hash)).one() + Email.message_id_hash == unicode(message_id_hash)).one() def get_message_by_id(self, message_id): """Return the message with a matching Message-ID. @@ -199,17 +195,6 @@ class StormStore(object): Email.message_id == unicode(message_id)).one() return msg - def is_message_in_list(self, list_name, message_id): - """Return the number of messages with a matching Message-ID in the list. - - :param list_name: The fully qualified list name to which the - message should be added. - :param message_id: The Message-ID header contents to search for. - :returns: The message, or None if no matching message was found. - """ - return self.db.find(Email.message_id, - Email.message_id == unicode(message_id)).count() - def search_list_for_content(self, list_name, keyword): """ Returns a list of email containing the specified keyword in their content. @@ -270,7 +255,18 @@ class StormStore(object): """An iterator over all messages in this message store.""" raise NotImplementedError + # Other methods (not in IMessageStore) + + def is_message_in_list(self, list_name, message_id): + """Return the number of messages with a matching Message-ID in the list. + :param list_name: The fully qualified list name to which the + message should be added. + :param message_id: The Message-ID header contents to search for. + :returns: The message, or None if no matching message was found. + """ + return self.db.find(Email.message_id, + Email.message_id == unicode(message_id)).count() def get_list_names(self): -- cgit