[Zope3-checkins] CVS: Products3/NewsSite - newsindex.pt:1.5 newssite.py:1.6

Tres Seaver tseaver@zope.com
Thu, 27 Mar 2003 07:09:37 -0500


Update of /cvs-repository/Products3/NewsSite
In directory cvs.zope.org:/tmp/cvs-serv670

Modified Files:
	newsindex.pt newssite.py 
Log Message:


  - Move date presentation logic into the view class.

  - Modify the logic used to generate the list of news items:

    o Use a for loop instead of successive list comprehensions.

    o Use the decorate-sort-undecorate pattern to do the sorting.

    o Sort by effective date, if present, or else the creation date.

    o Reverse the sort.

    o Add additional keys to the dictionary to make the template
      simpler.


=== Products3/NewsSite/newsindex.pt 1.4 => 1.5 ===
--- Products3/NewsSite/newsindex.pt:1.4	Wed Mar 26 14:15:16 2003
+++ Products3/NewsSite/newsindex.pt	Thu Mar 27 07:09:36 2003
@@ -7,17 +7,16 @@
 <div metal:fill-slot="body">
   <ul>
     <span tal:repeat="item view/listPublicNewsItems">
-		<li>
-				<span tal:content="python: item['obj'].effective.strftime('%Y-%m-%d')"/>
-				&nbsp;&nbsp;&nbsp;&nbsp;
-				<span tal:content="item/obj/title"/>
-				&nbsp;&nbsp;&nbsp;&nbsp;
-				<a href="link" 
-				   tal:attributes= "href item/absolute_url">
-					<span tal:content="item/obj/lead"/>
-				</a>
-		</li>
-	</span>
+      <li>
+        <span tal:content="item/date"/>
+                &nbsp;&nbsp;&nbsp;&nbsp;
+        <span tal:content="item/dc/title"/>
+                &nbsp;&nbsp;&nbsp;&nbsp;
+        <a href="link" 
+           tal:attributes="href item/absolute_url"
+        ><span tal:content="item/obj/lead"/></a>
+      </li>
+    </span>
   </ul>
 
 </div>


=== Products3/NewsSite/newssite.py 1.5 => 1.6 ===
--- Products3/NewsSite/newssite.py:1.5	Wed Mar 26 12:48:34 2003
+++ Products3/NewsSite/newssite.py	Thu Mar 27 07:09:36 2003
@@ -40,7 +40,8 @@
     __used_for__ = INewsSite
 
     def listPublicNewsItems(self):
-        return self._list()
+        #return self._list()
+        return self._newList()
     
     def _list(self):
         site = [ ContextWrapper(self.context[x], self.context, name=x) 
@@ -50,9 +51,60 @@
                  for x in list ]
         list.sort(lambda x,y: cmp(x[1], y[1]))
         list = [ x[0] for x in list]
-        list = [ {'obj':x, 'absolute_url':getView(x, 'absolute_url', self.request)()}
+        list = [ {'obj':x,
+                  'absolute_url':getView(x, 'absolute_url', self.request)()}
                  for x in list]
         return list
+
+    def _newList(self):
+        """Return a sequence of mappings for our news items.
+
+        o Sort the list in descending order by date (either the effective
+          date, if set, or the creation date).
+
+        o Keys include:
+
+          'obj' -- the item itself, wrapped in us.
+
+          'dc' -- an adapter to ICMFDublinCore.
+
+          'date' -- the "effective" date (may be created) as a string.
+
+          'effective' -- the item's effective date, as a string.
+
+          'expires' -- the item's expiration date, as a string.
+
+          'absolute_url' -- the item's URL.
+        """
+        decorated = []
+        for k, v in self.context.items():
+
+            if INewsItem.isImplementedBy(v):
+
+                wrapped = ContextWrapper(v, self.context, name=k)
+                dc = getAdapter(wrapped, ICMFDublinCore)
+                date = dc.effective or dc.created
+                info = {'obj' : wrapped,
+                        'dc' : dc,
+                        'date' : self._dateString(date),
+                        'effective' : self._dateString(dc.effective),
+                        'expires' : self._dateString(dc.expires),
+                        'absolute_url' :
+                            getView(wrapped, 'absolute_url', self.request)()
+                       }
+                decorated.append( (date, info) )
+
+        decorated.sort()
+        decorated.reverse()
+
+        return [ x[1] for x in decorated ]
+
+    def _dateString(self, value):
+
+        if value is None:
+            return ''
+
+        return value.strftime('%Y-%m-%d')
 
 #
 #   Adapter for storing ISyndicationPolicies as annotations.