[Zope-CVS] CVS: Products/NotifyList - NotifyList.py:1.3 __init__.py:1.3

Shane Hathaway shane@cvs.zope.org
Tue, 22 Jan 2002 15:55:11 -0500


Update of /cvs-repository/Products/NotifyList
In directory cvs.zope.org:/tmp/cvs-serv9342

Modified Files:
	NotifyList.py __init__.py 
Log Message:
Implemented setting list by text and getting the list of all email addresses.


=== Products/NotifyList/NotifyList.py 1.2 => 1.3 ===
 _www = os.path.join(os.path.dirname(__file__), 'www')
 
-from Products.CMFCore.DirectoryView import registerDirectory
-registerDirectory('skins', globals())
-
 
 ManageNotifyList = 'Manage Notification Lists'
 
@@ -36,6 +33,12 @@
     name = ''
     email = ''
 
+    def description(self):
+        if self.name and self.name != self.email:
+            return "%s <%s>" % (self.name, self.email)
+        else:
+            return "<%s>" % self.email
+
 
 
 class GroupRecord (Persistent):
@@ -44,6 +47,9 @@
     name = ''
     ids = ()
 
+    def description(self):
+        return self.name
+
 
 
 class NotifyDatabase (SimpleItem):
@@ -80,6 +86,11 @@
     def deleteRecord(self, id):
         del self._contents[id]
 
+    security.declarePrivate('listAllItems')
+    def listAllItems(self):
+        return self._contents.items()
+        
+
 Globals.InitializeClass(NotifyDatabase)
 
 
@@ -104,26 +115,23 @@
             record = db.getRecordById(id)
             if record is None:
                 continue
+            desc = record.description()
             if record.is_group:
-                lst.append(('0' + record.name.lower(), record.name, id, 1))
+                lst.append(('0' + desc.lower(), desc, id, 1))
             else:
-                if record.email:
-                    desc = '%s <%s>' % (record.name, record.email)
-                else:
-                    desc = record.name
                 lst.append(('1' + desc.lower(), desc, id, 0))
         lst.sort()
         res = map(lambda (sort_key, desc, id, is_group): {
             'id': id, 'description': desc, 'is_group': is_group}, lst)
         return res
 
-    security.declarePublic(ManageNotifyList, 'listContents')
+    security.declareProtected(ManageNotifyList, 'listContents')
     def listContents(self):
         """Returns a list of dictionaries containing id, description, is_group
         """
         return self._idsToMappings(self.ids)
 
-    security.declarePublic(ManageNotifyList, 'listGroupContents')
+    security.declareProtected(ManageNotifyList, 'listGroupContents')
     def listGroupContents(self, group_id):
         """Returns a list of dictionaries similar to listContents()
         """
@@ -132,7 +140,7 @@
         if record is not None and record.is_group:
             return self._idsToMappings(record.ids)
 
-    security.declarePublic(ManageNotifyList, 'listAllEmailsAndGroups')
+    security.declareProtected(ManageNotifyList, 'listAllEmailsAndGroups')
     def listAllEmailsAndGroups(self, exclude_my_ids=0):
         """Returns a list of dictionaries similar to listContents()
         """
@@ -142,7 +150,7 @@
             ids = filter(lambda id, exclude_ids=self.ids:
                          id not in exclude_ids, ids)
         return self._idsToMappings(ids)
-        
+
     def _addIds(self, container, ids):
         if not ids:
             # No changes.
@@ -158,11 +166,11 @@
                 raise ValueError, 'Id %s not found.' % id
         container.ids = tuple(new_ids.keys())
 
-    security.declarePublic(ManageNotifyList, 'addToList')
+    security.declareProtected(ManageNotifyList, 'addToList')
     def addToList(self, ids):
         self._addIds(self, ids)
 
-    security.declarePublic(ManageNotifyList, 'addToGroupList')
+    security.declareProtected(ManageNotifyList, 'addToGroupList')
     def addToGroupList(self, group_id, ids):
         db = self.getDatabase()
         record = db.getRecordById(group_id)
@@ -178,39 +186,39 @@
                 del new_ids[id]
         container.ids = tuple(new_ids.keys())
 
-    security.declarePublic(ManageNotifyList, 'removeFromList')
+    security.declareProtected(ManageNotifyList, 'removeFromList')
     def removeFromList(self, ids):
         self._removeIds(self, ids)
         
-    security.declarePublic(ManageNotifyList, 'removeFromGroupList')
+    security.declareProtected(ManageNotifyList, 'removeFromGroupList')
     def removeFromGroupList(self, group_id, ids):
         db = self.getDatabase()
         record = db.getRecordById(group_id)
         if record is not None and record.is_group:
             self._removeIds(record, ids)
 
-    security.declarePublic(ManageNotifyList, 'addEmail')
+    security.declareProtected(ManageNotifyList, 'addEmail')
     def addEmail(self, name, email):
         """Returns the id of the new email.
         """
         db = self.getDatabase()
         ei = EmailRecord()
-        ei.name = str(name)
-        ei.email = str(email)
+        ei.name = str(name).strip()
+        ei.email = str(email).strip()
         id = db.addRecord(ei)
         return id
 
-    security.declarePublic(ManageNotifyList, 'addGroup')
+    security.declareProtected(ManageNotifyList, 'addGroup')
     def addGroup(self, name):
         """Returns the id of the new group.
         """
         db = self.getDatabase()
         ei = GroupRecord()
-        ei.name = str(name)
+        ei.name = str(name).strip()
         id = db.addRecord(ei)
         return id
 
-    security.declarePublic(ManageNotifyList, 'getGroupName')
+    security.declareProtected(ManageNotifyList, 'getGroupName')
     def getGroupName(self, id):
         """Returns the name of a group.
         """
@@ -220,7 +228,7 @@
             raise KeyError, 'Group %s not found' % id
         return record.name
 
-    security.declarePublic(ManageNotifyList, 'getEmailInfo')
+    security.declareProtected(ManageNotifyList, 'getEmailInfo')
     def getEmailInfo(self, id):
         """Returns a mapping containing name and email.
         """
@@ -230,7 +238,7 @@
             raise KeyError, 'Record %s not found or wrong type' % id
         return {'name': record.name, 'email': record.email}
 
-    security.declarePublic(ManageNotifyList, 'changeEmail')
+    security.declareProtected(ManageNotifyList, 'changeEmail')
     def changeEmail(self, id, name, email):
         """Changes a name and email address.
         """
@@ -238,10 +246,10 @@
         record = db.getRecordById(id)
         if record is None or record.is_group:
             raise KeyError, 'Record %s not found or wrong type' % id
-        record.name = str(name)
-        record.email = str(email)
+        record.name = str(name).strip()
+        record.email = str(email).strip()
 
-    security.declarePublic(ManageNotifyList, 'changeGroup')
+    security.declareProtected(ManageNotifyList, 'changeGroup')
     def changeGroup(self, id, name):
         """Changes a name and email address.
         """
@@ -249,29 +257,81 @@
         record = db.getRecordById(id)
         if record is None or not record.is_group:
             raise KeyError, 'Record %s not found or wrong type' % id
-        record.name = str(name)
+        record.name = str(name).strip()
 
-    security.declarePublic(ManageNotifyList, 'deleteRecord')
+    security.declareProtected(ManageNotifyList, 'deleteRecord')
     def deleteRecord(self, id):
         """Deletes an email or group record.
         """
         db = self.getDatabase()
         db.deleteRecord(id)
 
-    security.declarePublic(ManageNotifyList, 'getListAsText')
+    security.declareProtected(ManageNotifyList, 'getListAsText')
     def getListAsText(self):
         """
         """
+        mappings = self._idsToMappings(self.ids)
+        return '\n'.join(map(lambda dict: dict['description'], mappings))
 
-    security.declarePublic(ManageNotifyList, 'setListAsText')
+    security.declareProtected(ManageNotifyList, 'setListAsText')
     def setListAsText(self, text):
+        """Changes the list by parsing multiline text.
+
+        Automatically creates a new email if not found and there is
+        an @ symbol.
         """
-        """
+        # This method uses guessing, but it's conservative guessing. :-)
+        descs = text.split('\n')
+        ids = []
+        db = self.getDatabase()
+        db_items = db.listAllItems()
+
+        for s in descs:
+            desc = s.strip().lower()
+            # Search by full description first.
+            for id, record in db_items:
+                if record.description().lower() == desc:
+                    break
+            else:
+                # Search by name or email.
+                for id, record in db_items:
+                    if not record.is_group:
+                        if record.email.lower() == desc:
+                            break
+                    if record.name.lower() == desc:
+                        break
+                else:
+                    if '@' in s and ' ' not in s:
+                        # Create a new email.
+                        email = s
+                        if email.startswith('<') and email.endswith('>'):
+                            email = email[1:-1]
+                        id = self.addEmail(email, email)
+                    else:
+                        raise ValueError, s
+            ids.append(id)
+        self.ids = tuple(ids)
 
-    security.declarePublic(ManageNotifyList, 'getEmailsForNotification')
+    security.declareProtected(ManageNotifyList, 'getEmailsForNotification')
     def getEmailsForNotification(self):
+        """Returns a list containing email addresses to notify.
         """
-        """
+        db = self.getDatabase()
+        to_visit = list(self.ids)
+        visited = {}
+        while to_visit:
+            id = to_visit.pop()
+            if visited.has_key(id):
+                continue
+            r = db.getRecordById(id)
+            visited[id] = r
+            if r.is_group:
+                to_visit.extend(r.ids)
+        res = []
+        for r in visited.values():
+            if not r.is_group:
+                res.append(r.email)
+        return res
 
 
 Globals.InitializeClass(NotifyList)


=== Products/NotifyList/__init__.py 1.2 => 1.3 ===
 def initialize(context):
 
+    from Products.CMFCore.DirectoryView import registerDirectory
+    registerDirectory('skins', globals())
+
     context.registerClass(
         NotifyList.NotifyList,
         constructors=(NotifyList.manage_addNotifyListForm,