[CMF-checkins] CVS: CMF/CMFCalendar - Event.py:1.10

Sidnei da Silva sidnei@x3ng.com.br
Tue, 6 Aug 2002 19:55:36 -0400


Update of /cvs-repository/CMF/CMFCalendar
In directory cvs.zope.org:/tmp/cvs-serv4834

Modified Files:
	Event.py 
Log Message:
Implemented PUT and GET support via FTP/WebDAV.

=== CMF/CMFCalendar/Event.py 1.9 => 1.10 ===
 from Globals import InitializeClass
 from AccessControl import ClassSecurityInfo
 from Products.CMFDefault.DublinCore import DefaultDublinCoreImpl
-from Products.CMFCore.PortalContent import PortalContent
+from Products.CMFCore.PortalContent import PortalContent, NoWL, ResourceLockedError
 from Products.CMFCore.WorkflowCore import WorkflowAction
 
 # Import permission names
-from Products.CMFCore.CMFCorePermissions import View
+from Products.CMFCore.CMFCorePermissions import View, ModifyPortalContent
+from Products.CMFDefault.utils import formatRFC822Headers, html_headcheck
+from Products.CMFDefault.utils import SimpleHTMLParser, bodyfinder, parseHeadersBody
+from Products.CMFDefault.Document import Document
+from Products.CMFCore.utils import keywordsplitter
+
 import EventPermissions
 
 # Factory type information -- makes Events objects play nicely
@@ -107,6 +112,10 @@
     security = ClassSecurityInfo()
     security.declareObjectProtected(View)
     
+    __implements__ = ( PortalContent.__implements__
+                     , DefaultDublinCoreImpl.__implements__
+                     )
+
     def __init__(self
                  , id
                  , title=''
@@ -183,33 +192,42 @@
             ):
         """\
         """
+        
         if title is not None: 
             self.setTitle(title)
         if description is not None:
             self.setDescription(description)
         if eventType is not None:
             self.setSubject(eventType)
-        efdate = '%s/%s/%s %s %s' % (effectiveDay
-                                 , effectiveMo
-                                 , effectiveYear
-                                 , start_time
-                                 , startAMPM
-                                 )
-        start_date = DateTime( efdate )
-
-        exdate = '%s/%s/%s %s %s' % (expirationDay
-                                 , expirationMo
-                                 , expirationYear
-                                 , stop_time
-                                 , stopAMPM
-                                 )
-        end_date = DateTime( exdate )
 
-        if end_date < start_date:
-            end_date = start_date
+        start_date = end_date = None
+
+        if effectiveDay and effectiveMo and effectiveYear and start_time:
+            efdate = '%s/%s/%s %s %s' % (effectiveDay
+                                         , effectiveMo
+                                         , effectiveYear
+                                         , start_time
+                                         , startAMPM
+                                         )
+            start_date = DateTime( efdate )
+
+        if expirationDay and expirationMo and expirationYear and stop_time:
+
+            exdate = '%s/%s/%s %s %s' % (expirationDay
+                                         , expirationMo
+                                         , expirationYear
+                                         , stop_time
+                                         , stopAMPM
+                                         )
+            end_date = DateTime( exdate )
+        
+        if start_date and end_date:
+
+            if end_date < start_date:
+                end_date = start_date
  
-        self.setStartDate( start_date )
-        self.setEndDate( end_date )
+            self.setStartDate( start_date )
+            self.setEndDate( end_date )
 
         if location is not None:
             self.location = location
@@ -298,6 +316,108 @@
             Return our stop time as a string.
         """
         return self.end().AMPMMinutes() 
+
+    security.declarePrivate('handleText')
+    def handleText(self, text, format=None, stx_level=None):
+        """ Handles the raw text, returning headers, body, cooked, format """
+        headers = {}
+        if format == 'html':
+            parser = SimpleHTMLParser()
+            parser.feed(text)
+            headers.update(parser.metatags)
+            if parser.title:
+                headers['Title'] = parser.title
+            bodyfound = bodyfinder(text)
+            if bodyfound:
+                body = bodyfound
+        else:
+            headers, body = parseHeadersBody(text, headers)
+
+        return headers, body, format
+
+    security.declareProtected(ModifyPortalContent, 'setMetadata')
+    def setMetadata(self, headers):
+        headers['Format'] = self.Format()
+        new_subject = keywordsplitter(headers)
+        headers['Subject'] = new_subject or self.Subject()
+        haveheader = headers.has_key
+        for key, value in self.getMetadataHeaders():
+            if key != 'Format' and not haveheader(key):
+                headers[key] = value
+        self._editMetadata(title=headers['Title'],
+                          subject=headers['Subject'],
+                          description=headers['Description'],
+                          contributors=headers['Contributors'],
+                          effective_date=headers['Effective_date'],
+                          expiration_date=headers['Expiration_date'],
+                          format=headers['Format'],
+                          language=headers['Language'],
+                          rights=headers['Rights'],
+                          )
+ 
+    security.declarePublic( 'getMetadataHeaders' )
+    def getMetadataHeaders(self):
+        """Return RFC-822-style header spec."""
+        hdrlist = DefaultDublinCoreImpl.getMetadataHeaders(self)
+        hdrlist.append( ('StartDate', self.start().strftime("%Y-%m-%d %H:%M:%S") ) )
+        hdrlist.append( ('EndDate',  self.end().strftime("%Y-%m-%d %H:%M:%S") ) )
+        hdrlist.append( ('Location', self.location) )
+        hdrlist.append( ('ContactName', self.contact_name) )
+        hdrlist.append( ('ContactEmail', self.contact_email) )
+        hdrlist.append( ('ContactPhone', self.contact_phone) )
+        hdrlist.append( ('EventURL', self.event_url) )
+
+        return hdrlist
+
+    ## FTP handlers
+    security.declareProtected(ModifyPortalContent, 'PUT')
+
+    def PUT(self, REQUEST, RESPONSE):
+        """ Handle HTTP (and presumably FTP?) PUT requests """
+        if not NoWL:
+            self.dav__init(REQUEST, RESPONSE)
+            self.dav__simpleifhandler(REQUEST, RESPONSE, refresh=1)
+        body = REQUEST.get('BODY', '')
+        guessedformat = REQUEST.get_header('Content-Type', 'text/plain')
+        ishtml = (guessedformat == 'text/html') or html_headcheck(body)
+
+        if ishtml: self.setFormat('text/html')
+        else: self.setFormat('text/plain')
+
+        try:
+            headers, body, format = self.handleText(text=body)
+            self.setMetadata(headers)
+            self.setStartDate(headers['StartDate'])
+            self.setEndDate(headers['EndDate'])
+            self.edit( location=headers['Location']
+             , contact_name=headers['ContactName']
+             , contact_email=headers['ContactEmail']
+             , contact_phone=headers['ContactPhone']
+             , event_url=headers['EventURL']
+             )
+            
+        except ResourceLockedError, msg:
+            get_transaction().abort()
+            RESPONSE.setStatus(423)
+            return RESPONSE
+
+        RESPONSE.setStatus(204)
+        self.reindexObject()
+        return RESPONSE
+
+    security.declareProtected(View, 'manage_FTPget')
+    def manage_FTPget(self):
+        "Get the document body for FTP download (also used for the WebDAV SRC)"
+        hdrlist = self.getMetadataHeaders()
+        hdrtext = formatRFC822Headers( hdrlist )
+        bodytext = '%s\r\n\r\n%s' % ( hdrtext, self.Description() )
+
+        return bodytext
+
+    security.declareProtected(View, 'get_size')
+    def get_size( self ):
+        """ Used for FTP and apparently the ZMI now too """
+        return len(self.manage_FTPget())
 
 # Intialize the Event class, setting up security.
 InitializeClass(Event)