[CalendarServer-changes] [9465] CalendarServer/branches/users/gaya/inviteclean

source_changes at macosforge.org source_changes at macosforge.org
Thu Jul 19 15:35:06 PDT 2012


Revision: 9465
          http://trac.macosforge.org/projects/calendarserver/changeset/9465
Author:   gaya at apple.com
Date:     2012-07-19 15:35:05 -0700 (Thu, 19 Jul 2012)
Log Message:
-----------
remove Invitations from sql.py. Add attrs to CommonHomeChild for invite info. Add asInvited() and use it in sharing.py.

Modified Paths:
--------------
    CalendarServer/branches/users/gaya/inviteclean/twistedcaldav/sharing.py
    CalendarServer/branches/users/gaya/inviteclean/txdav/caldav/datastore/sql.py
    CalendarServer/branches/users/gaya/inviteclean/txdav/carddav/datastore/sql.py
    CalendarServer/branches/users/gaya/inviteclean/txdav/common/datastore/sql.py

Modified: CalendarServer/branches/users/gaya/inviteclean/twistedcaldav/sharing.py
===================================================================
--- CalendarServer/branches/users/gaya/inviteclean/twistedcaldav/sharing.py	2012-07-19 20:50:26 UTC (rev 9464)
+++ CalendarServer/branches/users/gaya/inviteclean/twistedcaldav/sharing.py	2012-07-19 22:35:05 UTC (rev 9465)
@@ -55,6 +55,26 @@
     
     Invitation is needed now for _invitationFromLegecyInvite and _legacyInviteFromInvitation
 '''
+
+from txdav.common.datastore.sql_tables import _BIND_MODE_OWN, \
+    _BIND_MODE_READ, _BIND_MODE_WRITE, _BIND_STATUS_INVITED, \
+    _BIND_STATUS_ACCEPTED, _BIND_STATUS_DECLINED, _BIND_STATUS_INVALID
+
+
+invitationStateToBindStatusMap = {
+    "NEEDS-ACTION": _BIND_STATUS_INVITED,
+    "ACCEPTED": _BIND_STATUS_ACCEPTED,
+    "DECLINED": _BIND_STATUS_DECLINED,
+    "INVALID": _BIND_STATUS_INVALID,
+}
+invitationStateFromBindStatusMap = dict((v,k) for k, v in invitationStateToBindStatusMap.iteritems())
+invitationShareeAccessToBindModeMap = {
+    "own": _BIND_MODE_OWN,
+    "read-only": _BIND_MODE_READ,
+    "read-write": _BIND_MODE_WRITE,
+    }
+invitationShareeAccessFromBindModeMap = dict((v,k) for k, v in invitationShareeAccessToBindModeMap.iteritems())
+
 class Invitation(object):
     """
     """
@@ -570,9 +590,9 @@
         returnValue("%s:%s" % (hosturl, userid))
         
         
-    def _invitationFromLegecyInvite(self, legacyInvite):
+    def _invitationFromLegecyInvite(self, legacyInvite, sharerUID=None):
         return Invitation(uid=legacyInvite.inviteuid,
-                      sharerUID=None,
+                      sharerUID=sharerUID,
                       shareeUID=legacyInvite.principalUID, 
                       shareeAccess=legacyInvite.access,
                       state=legacyInvite.state,
@@ -616,18 +636,50 @@
 
 
     @inlineCallbacks
-    def _allInvitations(self):
+    def _oallInvitations(self):
         """
         replaces self.invitesDB().allRecords()
         """
-        ''' OLD
         legecyInvites = yield self.invitesDB().allRecords()
-        invitations = [self._invitationFromLegecyInvite(legecyInvite) for legecyInvite in legecyInvites]
-        '''
-        invitations = yield self._newStoreObject.invitations()
+        invitations = [self._invitationFromLegecyInvite(legecyInvite, sharerUID=self._newStoreObject._home.uid()) for legecyInvite in legecyInvites]
         returnValue(invitations)
 
     @inlineCallbacks
+    def _allInvitations(self):
+        """
+        replaces self.invitesDB().allRecords()
+        """
+        invited = yield self._newStoreObject.asInvited()
+        
+        invitations = []
+        for homeChildInvited in invited:
+            state = invitationStateFromBindStatusMap[homeChildInvited.shareStatus()]
+            shareeAccess = invitationShareeAccessFromBindModeMap[homeChildInvited.shareMode()]
+            invitation = Invitation(uid=homeChildInvited.inviteUID(),
+                          sharerUID=self._newStoreObject._home.uid(),
+                          shareeUID= homeChildInvited._home.uid(),
+                          shareeAccess=shareeAccess,
+                          state=state,
+                          summary=homeChildInvited.shareMessage(), )
+            invitations.append(invitation)
+        invitations.sort(key=lambda invitation:invitation.shareeUID())
+        
+        oinvitations = yield self._oallInvitations()
+        if len(oinvitations) != len(invitations):
+            print("len(oinvitations)=%s != len(invitations)=%s" % (len(oinvitations),len(invitations),))
+        else:
+            for i in range(len(oinvitations)):
+                new = invitations[i]
+                nstr = "uid=%s, sharerUID=%s, shareeUID=%s, shareeAccess=%s, state=%s, summary=%s" % (new.uid(), new.sharerUID(), new.shareeUID(), new.shareeAccess(), new.state(), new.summary(), )
+                old = oinvitations[i]
+                ostr = "uid=%s, sharerUID=%s, shareeUID=%s, shareeAccess=%s, state=%s, summary=%s" % (old.uid(), old.sharerUID(), old.shareeUID(), old.shareeAccess(), old.state(), old.summary(), )
+                if nstr != ostr:
+                    print("UNEQUAL new[%s] uid=%s, sharerUID=%s, shareeUID=%s, shareeAccess=%s, state=%s, summary=%s" % (i, new.uid(), new.sharerUID(), new.shareeUID(), new.shareeAccess(), new.state(), new.summary(), ))
+                    print("UNEQUAL old[%s] uid=%s, sharerUID=%s, shareeUID=%s, shareeAccess=%s, state=%s, summary=%s" % (i, old.uid(), old.sharerUID(), old.shareeUID(), old.shareeAccess(), old.state(), old.summary(), ))
+
+        returnValue(invitations)
+
+    @inlineCallbacks
     def _invitationForShareeUID(self, shareeUID):
         """
         replaces self.invitesDB().recordForPrincipalUID(principalUID)
@@ -742,21 +794,19 @@
                 yield self.sendInviteNotification(invitation, request)
         
         # use new API
-        if hasattr(self._newStoreObject, "unshareWith"):
+        from twistedcaldav.directory.util import transactionFromRequest
+        transaction = transactionFromRequest(request, self._newStoreObject)
+        
+        if self.isCalendarCollection():
+            shareeHome = yield transaction.calendarHomeWithUID(invitation.shareeUID(), create=True)
+        elif self.isAddressBookCollection():
+            shareeHome = yield transaction.addressbookHomeWithUID(invitation.shareeUID(), create=True)
 
-            from twistedcaldav.directory.util import transactionFromRequest
-            transaction = transactionFromRequest(request, self._newStoreObject)
-            
-            if self.isCalendarCollection():
-                shareeHome = yield transaction.calendarHomeWithUID(invitation.shareeUID(), create=True)
-            elif self.isAddressBookCollection():
-                shareeHome = yield transaction.addressbookHomeWithUID(invitation.shareeUID(), create=True)
+        yield self._newStoreObject.unshareWith(shareeHome)
     
-            yield self._newStoreObject.unshareWith(shareeHome)
+        #old code
+        #  yield self.invitesDB().removeRecordForInviteUID(invitation.uid())
         
-        else:
-            yield self.invitesDB().removeRecordForInviteUID(invitation.uid())
-        
         returnValue(True)            
 
     def inviteSingleUserUpdateToShare(self, userid, commonName, acesOLD, aceNEW, summary, request):

Modified: CalendarServer/branches/users/gaya/inviteclean/txdav/caldav/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/gaya/inviteclean/txdav/caldav/datastore/sql.py	2012-07-19 20:50:26 UTC (rev 9464)
+++ CalendarServer/branches/users/gaya/inviteclean/txdav/caldav/datastore/sql.py	2012-07-19 22:35:05 UTC (rev 9465)
@@ -397,7 +397,6 @@
     implements(ICalendar)
 
     # structured tables.  (new, preferred)
-    _homeSchema = schema.CALENDAR_HOME
     _bindSchema = schema.CALENDAR_BIND
     _homeChildSchema = schema.CALENDAR
     _homeChildMetaDataSchema = schema.CALENDAR_METADATA

Modified: CalendarServer/branches/users/gaya/inviteclean/txdav/carddav/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/gaya/inviteclean/txdav/carddav/datastore/sql.py	2012-07-19 20:50:26 UTC (rev 9464)
+++ CalendarServer/branches/users/gaya/inviteclean/txdav/carddav/datastore/sql.py	2012-07-19 22:35:05 UTC (rev 9465)
@@ -148,7 +148,6 @@
     implements(IAddressBook)
 
     # structured tables.  (new, preferred)
-    _homeSchema = schema.ADDRESSBOOK_HOME
     _bindSchema = schema.ADDRESSBOOK_BIND
     _homeChildSchema = schema.ADDRESSBOOK
     _homeChildMetaDataSchema = schema.ADDRESSBOOK_METADATA

Modified: CalendarServer/branches/users/gaya/inviteclean/txdav/common/datastore/sql.py
===================================================================
--- CalendarServer/branches/users/gaya/inviteclean/txdav/common/datastore/sql.py	2012-07-19 20:50:26 UTC (rev 9464)
+++ CalendarServer/branches/users/gaya/inviteclean/txdav/common/datastore/sql.py	2012-07-19 22:35:05 UTC (rev 9465)
@@ -56,9 +56,7 @@
 
 from txdav.common.datastore.sql_tables import schema
 from txdav.common.datastore.sql_tables import _BIND_MODE_OWN, \
-    _BIND_MODE_READ, _BIND_MODE_WRITE, _BIND_STATUS_INVITED, \
-    _BIND_STATUS_ACCEPTED, _BIND_STATUS_DECLINED, _BIND_STATUS_INVALID, \
-    NOTIFICATION_OBJECT_REVISIONS_TABLE
+    _BIND_STATUS_ACCEPTED, NOTIFICATION_OBJECT_REVISIONS_TABLE
 from txdav.common.icommondatastore import HomeChildNameNotAllowedError, \
     HomeChildNameAlreadyExistsError, NoSuchHomeChildError, \
     ObjectResourceNameNotAllowedError, ObjectResourceNameAlreadyExistsError, \
@@ -1878,89 +1876,7 @@
         """
 
 
-from txdav.common.iinvitation import IInvitation
 
-invitationStateToBindStatusMap = {
-    "NEEDS-ACTION": _BIND_STATUS_INVITED,
-    "ACCEPTED": _BIND_STATUS_ACCEPTED,
-    "DECLINED": _BIND_STATUS_DECLINED,
-    "INVALID": _BIND_STATUS_INVALID,
-}
-invitationStateFromBindStatusMap = dict((v,k) for k, v in invitationStateToBindStatusMap.iteritems())
-invitationShareeAccessToBindModeMap = {
-    "own": _BIND_MODE_OWN,
-    "read-only": _BIND_MODE_READ,
-    "read-write": _BIND_MODE_WRITE,
-    }
-invitationShareeAccessFromBindModeMap = dict((v,k) for k, v in invitationShareeAccessToBindModeMap.iteritems())
-
-class Invitation(object):
-    implements(IInvitation)
-    """
-    """
-    def __init__(self, uid, sharerUID, shareeUID, shareeAccess, state, summary):
-        self._uid = uid
-        self._sharerUID = sharerUID
-        self._shareeUID = shareeUID
-        self._shareeAccess = shareeAccess
-        self._state = state
-        self._summary = summary
-
-    def uid(self):
-        """
-        Unique identifier for this record.  Randomly generated.
-    
-        @return: the invite unique identifier
-        @rtype: C{str}
-        """
-        return self._uid
-    
-    def sharerUID(self):
-        """
-        Sharer's unique identifier.
-    
-        @return: the Sharer's unique identifier.
-        @rtype: C{str}
-        """
-        return self._sharerUID
-    
-    def shareeUID(self):
-        """
-        Sharee's unique identifier.
-    
-        @return: the Sharee's unique identifier.
-        @rtype: C{str}
-        """
-        return self._shareeUID
-   
-    def shareeAccess(self):
-        """
-        Sharee's access.  Currently, one of "own", "read-only", or "read-write".
-    
-        @return: the Sharee's access to the shared resource
-        @rtype: C{str}
-        """
-        return self._shareeAccess
-    
-    def state(self):
-        """
-        Invitation or bind state.  Currently, one of "NEEDS-ACTION","ACCEPTED", "DECLINED", "INVALID".
-    
-        @return: the record state
-        @rtype: C{str}
-        """
-        return self._state
-   
-    def summary(self):
-        """
-        The shared resource's name, purpose, or description.
-    
-        @return: the summary
-        @rtype: C{str}
-        """
-        return self._summary
-
-
 class CommonHomeChild(LoggingMixIn, FancyEqMixin, _SharedSyncLogic):
     """
     Common ancestor class of AddressBooks and Calendars.
@@ -1979,6 +1895,7 @@
     _objectResourceClass = None
 
     _bindSchema              = None
+    _homeSchema              = None
     _homeChildSchema         = None
     _homeChildMetaDataSchema = None
     _revisionsSchema         = None
@@ -1992,7 +1909,7 @@
     _objectTable         = None
 
 
-    def __init__(self, home, name, resourceID, owned, mode):
+    def __init__(self, home, name, resourceID, owned, mode, status=None, message=None, inviteUID=None):
 
         if home._notifiers:
             childID = "%s/%s" % (home.uid(), name)
@@ -2006,6 +1923,9 @@
         self._resourceID        = resourceID
         self._owned             = owned
         self._bindMode          = mode
+        self._bindStatus        = status
+        self._bindMessage       = message
+        self._inviteUID         = inviteUID
         self._created           = None
         self._modified          = None
         self._objects           = {}
@@ -2252,7 +2172,24 @@
         """
         return self._bindMode
 
+    def shareStatus(self):
+        """
+        @see: L{ICalendar.shareStatus}
+        """
+        return self._bindStatus
 
+    def shareMessage(self):
+        """
+        @see: L{ICalendar.shareMessage}
+        """
+        return self._bindMessage
+
+    def inviteUID(self):
+        """
+        @see: L{ICalendar.inviteUID}
+        """
+        return self._inviteUID
+
     @inlineCallbacks
     def unshare(self, homeType):
         """
@@ -2316,50 +2253,80 @@
 
 
     @classproperty
-    def _allInvitationsQuery(cls): #@NoSelf
+    def _allInvitedQuery(cls): #@NoSelf
+        
+        #FIXME performance: change schema to get rid if INVITE table -> no join
+        #
+        '''
+        # similar to sql_legacy.py
         inv = schema.INVITE
         home = cls._homeSchema
         bind = cls._bindSchema
         return Select(
-            [inv.INVITE_UID,
-             home.OWNER_UID,
-             bind.BIND_MODE,
+            [bind.BIND_MODE, 
+             bind.HOME_RESOURCE_ID,
+             bind.RESOURCE_NAME,    # same as inv.INVITE_UID when not accepted
              bind.BIND_STATUS,
-             bind.MESSAGE],
+             bind.MESSAGE,
+             inv.INVITE_UID],
             From=inv.join(home).join(bind),
             Where=(
                 (inv.RESOURCE_ID == Parameter("resourceID"))
                 .And(inv.RESOURCE_ID == bind.RESOURCE_ID)
                 .And(inv.HOME_RESOURCE_ID == home.RESOURCE_ID)
-                .And(inv.HOME_RESOURCE_ID == bind.HOME_RESOURCE_ID)),
-            OrderBy=home.OWNER_UID, Ascending=True
+                .And(inv.HOME_RESOURCE_ID == bind.HOME_RESOURCE_ID))
         )
+        '''
+        # This seems to work so far and is simpler. home table is not needed
+        inv = schema.INVITE
+        bind = cls._bindSchema
+        return Select(
+            [bind.BIND_MODE, 
+             bind.HOME_RESOURCE_ID,
+             bind.RESOURCE_NAME,    # same as inv.INVITE_UID when not accepted
+             bind.BIND_STATUS,
+             bind.MESSAGE,
+             inv.INVITE_UID],
+            From=inv.join(bind),
+            Where=(bind.RESOURCE_ID == Parameter("resourceID"))
+                .And(inv.HOME_RESOURCE_ID == bind.HOME_RESOURCE_ID)
+                .And(bind.BIND_MODE != _BIND_MODE_OWN)
+        )
 
-
     @inlineCallbacks
-    def invitations(self):
+    def asInvited(self):
         """
-        Get a list of invitations for sharing of this item
-        @return: a L{Deferred} which will fires with a L{list} of L{IInvitation}s.
+        Retrieve all the versions of this L{CommonHomeChild} as it is invited to
+        everyone.
+
+        @see: L{ICalendarHome.asInvited}
+
+        @return: L{CommonHomeChild} objects that represent this
+            L{CommonHomeChild} as a child of different L{CommonHome}s
+        @rtype: a L{Deferred} which fires with a L{list} of L{ICalendar}s.
         """
-        values = []
-        rows = yield self._allInvitationsQuery.on(
+        rows = yield self._allInvitedQuery.on(
             self._txn, resourceID=self._resourceID
         )
-        for row in rows:
-            [uid, shareeUID, bindMode, bindStatus, summary] = row
-            state = invitationStateFromBindStatusMap[bindStatus]
-            shareeAccess = invitationShareeAccessFromBindModeMap[bindMode]
+        cls = self.__class__ # for ease of grepping...
+        
+        result = []
+        for bindMode, homeResourceID, sharedResourceName, bindStatus, bindMessage, inviteUID in rows:
+            # TODO: this could all be issued in parallel; no need to serialize
+            # the loop.
+            new = cls(
+                (yield self._txn.homeWithResourceID(self._home._homeType,
+                                                    homeResourceID)),
+                sharedResourceName, self._resourceID, False, bindMode
+            )
+            yield new.initFromStore()
+            new._bindStatus = bindStatus
+            new._bindMessage = bindMessage
+            new._inviteUID = inviteUID
+            
+            result.append(new)
+        returnValue(result)
 
-            #FIXME: get sharerUID == this items's home.OWNER_UID 
-            invitation = Invitation(uid=uid,
-                      sharerUID=None,
-                      shareeUID=shareeUID,
-                      shareeAccess=shareeAccess,
-                      state=state,
-                      summary=summary, )
-            values.append(invitation)
-        returnValue(values)
 
     @classmethod
     @inlineCallbacks
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20120719/dacb0e9e/attachment-0001.html>


More information about the calendarserver-changes mailing list