[CalendarServer-changes] [5558] CalendarServer/trunk/calendarserver/tools

source_changes at macosforge.org source_changes at macosforge.org
Mon May 3 16:54:34 PDT 2010


Revision: 5558
          http://trac.macosforge.org/projects/calendarserver/changeset/5558
Author:   sagen at apple.com
Date:     2010-05-03 16:54:33 -0700 (Mon, 03 May 2010)
Log Message:
-----------
Centralize the logic that sets a default autoSchedule value

Modified Paths:
--------------
    CalendarServer/trunk/calendarserver/tools/gateway.py
    CalendarServer/trunk/calendarserver/tools/principals.py
    CalendarServer/trunk/calendarserver/tools/test/test_principals.py

Modified: CalendarServer/trunk/calendarserver/tools/gateway.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/gateway.py	2010-05-03 22:35:39 UTC (rev 5557)
+++ CalendarServer/trunk/calendarserver/tools/gateway.py	2010-05-03 23:54:33 UTC (rev 5558)
@@ -33,7 +33,10 @@
 from twext.web2.dav import davxml
 
 from calendarserver.tools.util import loadConfig, getDirectory, setupMemcached, setupNotifications
-from calendarserver.tools.principals import principalForPrincipalID, proxySubprincipal, addProxy, removeProxy, ProxyError, ProxyWarning
+from calendarserver.tools.principals import (
+    principalForPrincipalID, proxySubprincipal, addProxy, removeProxy,
+    ProxyError, ProxyWarning, updateRecord
+)
 
 from twext.python.log import StandardIOObserver
 
@@ -155,6 +158,7 @@
     'ZIP' : { 'extras' : True, 'attr' : 'zip', },
     'Country' : { 'extras' : True, 'attr' : 'country', },
     'Phone' : { 'extras' : True, 'attr' : 'phone', },
+    'AutoSchedule' : { 'attr' : 'autoSchedule', },
 }
 
 class Runner(object):
@@ -208,22 +212,11 @@
                 kwargs[info['attr']] = command[key]
 
         try:
-            record = self.dir.createRecord("locations", **kwargs)
+            (yield updateRecord(True, self.dir, "locations", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return
 
-        kwargs['guid'] = record.guid
-
-        principal = self.dir.principalCollection.principalForRecord(record)
-        (yield principal.setAutoSchedule(command.get('AutoSchedule', True)))
-
-        try:
-            self.dir.updateRecord("locations", **kwargs)
-        except DirectoryError, e:
-            respondWithError(str(e))
-            return
-
         respondWithRecordsOfType(self.dir, command, "locations")
 
     def command_getLocationAttributes(self, command):
@@ -254,7 +247,7 @@
             if command.has_key(key):
                 kwargs[info['attr']] = command[key]
         try:
-            self.dir.updateRecord("locations", **kwargs)
+            (yield updateRecord(False, self.dir, "locations", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return
@@ -286,22 +279,11 @@
                 kwargs[info['attr']] = command[key]
 
         try:
-            record = self.dir.createRecord("resources", **kwargs)
+            (yield updateRecord(True, self.dir, "resources", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return
 
-        kwargs['guid'] = record.guid
-
-        principal = self.dir.principalCollection.principalForRecord(record)
-        (yield principal.setAutoSchedule(command.get('AutoSchedule', True)))
-
-        try:
-            self.dir.updateRecord("resources", **kwargs)
-        except DirectoryError, e:
-            respondWithError(str(e))
-            return
-
         respondWithRecordsOfType(self.dir, command, "resources")
 
     def command_getResourceAttributes(self, command):
@@ -321,7 +303,7 @@
             if command.has_key(key):
                 kwargs[info['attr']] = command[key]
         try:
-            self.dir.updateRecord("resources", **kwargs)
+            (yield updateRecord(False, self.dir, "resources", **kwargs))
         except DirectoryError, e:
             respondWithError(str(e))
             return

Modified: CalendarServer/trunk/calendarserver/tools/principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/principals.py	2010-05-03 22:35:39 UTC (rev 5557)
+++ CalendarServer/trunk/calendarserver/tools/principals.py	2010-05-03 23:54:33 UTC (rev 5558)
@@ -35,9 +35,15 @@
 
 from twistedcaldav.config import config, ConfigurationError
 from twistedcaldav.directory.directory import UnknownRecordTypeError, DirectoryError
+from twistedcaldav.directory import augment
 
 from calendarserver.tools.util import loadConfig, getDirectory, setupMemcached, setupNotifications, booleanArgument
 
+__all__ = [
+    "principalForPrincipalID", "proxySubprincipal", "addProxy", "removeProxy",
+    "ProxyError", "ProxyWarning", "updateRecord"
+]
+
 def usage(e=None):
     if e:
         if isinstance(e, UnknownRecordTypeError):
@@ -263,16 +269,12 @@
             print e
             return
 
-        try:
-            record = config.directory.createRecord(addType, guid=guid,
-                shortNames=[shortName], fullName=fullName)
-        except DirectoryError, e:
-            print e
-            return
+        if shortName is not None:
+            shortNames = [shortName]
+        else:
+            shortNames = ()
 
-        print "Added '%s' (%s) %s %s" % (record.fullName, addType,
-            record.shortNames[0], record.guid)
-        return
+        params = (runAddPrincipal, addType, guid, shortNames, fullName)
 
 
     elif listPrincipals:
@@ -386,7 +388,23 @@
         #
         reactor.stop()
 
+ at inlineCallbacks
+def runAddPrincipal(addType, guid, shortNames, fullName):
+    try:
+        try:
+            yield updateRecord(True, config.directory, addType, guid=guid,
+                shortNames=shortNames, fullName=fullName)
+            print "Added '%s'" % (fullName,)
+        except DirectoryError, e:
+            print e
 
+    finally:
+        #
+        # Stop the reactor
+        #
+        reactor.stop()
+
+
 def principalForPrincipalID(principalID, checkOnly=False, directory=None):
     
     # Allow a directory parameter to be passed in, but default to config.directory
@@ -441,7 +459,8 @@
         if checkOnly:
             return None
 
-        return directory.principalCollection.principalForUID(principalID)
+        x = directory.principalCollection.principalForUID(principalID)
+        return x
     except ValueError:
         pass
 
@@ -591,15 +610,16 @@
             { True: "true", False: "false" }[autoSchedule],
             principal,
         )
-        (yield principal.setAutoSchedule(autoSchedule))
+        # (yield principal.setAutoSchedule(autoSchedule))
 
-        # Invalidate the directory cache by updating this record
-        config.directory.updateRecord(principal.record.recordType,
+        (yield updateRecord(False, config.directory,
+            principal.record.recordType,
             guid=principal.record.guid,
             shortNames=principal.record.shortNames,
             fullName=principal.record.fullName,
+            autoSchedule=autoSchedule,
             **principal.record.extras
-        )
+        ))
 
 def action_getAutoSchedule(principal):
     autoSchedule = principal.getAutoSchedule()
@@ -700,5 +720,36 @@
     for fullName, shortName, guid in results:
         print format % (fullName, shortName, guid)
 
+
+ at inlineCallbacks
+def updateRecord(create, directory, recordType, **kwargs):
+    """
+    Create/update a record, including the extra work required to set the
+    autoSchedule bit in the augment record.
+
+    If C{create} is true, the record is created, otherwise update the record
+    matching the guid in kwargs.
+    """
+
+    if kwargs.has_key("autoSchedule"):
+        autoSchedule = kwargs["autoSchedule"]
+        del kwargs["autoSchedule"]
+    else:
+        autoSchedule = recordType in ("locations", "resources")
+
+    if create:
+        record = directory.createRecord(recordType, **kwargs)
+        kwargs['guid'] = record.guid
+    else:
+        directory.updateRecord(recordType, **kwargs)
+
+    augmentRecord = (yield augment.AugmentService.getAugmentRecord(kwargs['guid']))
+    augmentRecord.autoSchedule = autoSchedule
+    (yield augment.AugmentService.addAugmentRecords([augmentRecord]))
+    directory.updateRecord(recordType, **kwargs)
+
+
+
+
 if __name__ == "__main__":
     main()

Modified: CalendarServer/trunk/calendarserver/tools/test/test_principals.py
===================================================================
--- CalendarServer/trunk/calendarserver/tools/test/test_principals.py	2010-05-03 22:35:39 UTC (rev 5557)
+++ CalendarServer/trunk/calendarserver/tools/test/test_principals.py	2010-05-03 23:54:33 UTC (rev 5558)
@@ -21,9 +21,11 @@
 from twisted.internet.defer import inlineCallbacks, Deferred, returnValue
 
 from twistedcaldav.config import config
+from twistedcaldav.directory.directory import DirectoryError
 from twistedcaldav.test.util import TestCase, CapturingProcessProtocol
 
-from calendarserver.tools.principals import parseCreationArgs, matchStrings
+from calendarserver.tap.util import getRootResource
+from calendarserver.tools.principals import parseCreationArgs, matchStrings, updateRecord
 
 
 class ManagePrincipalsTestCase(TestCase):
@@ -31,6 +33,8 @@
     def setUp(self):
         super(ManagePrincipalsTestCase, self).setUp()
 
+        config.GlobalAddressBook.Enabled = False
+
         testRoot = os.path.join(os.path.dirname(__file__), "principals")
         templateName = os.path.join(testRoot, "caldavd.plist")
         templateFile = open(templateName)
@@ -120,6 +124,10 @@
             "newresource", "edaa6ae6-011b-4d89-ace3-6b688cdd91d9")
         self.assertTrue("Added 'New Resource'" in results)
 
+        results = yield self.runCommand("--get-auto-schedule",
+            "resources:newresource")
+        self.assertTrue(results.startswith("Autoschedule for (resources)newresource is true"))
+
         results = yield self.runCommand("--list-principals=resources")
         self.assertTrue("newresource" in results)
 
@@ -217,3 +225,38 @@
         results = yield self.runCommand("--get-auto-schedule",
             "locations:location01")
         self.assertTrue(results.startswith("Autoschedule for (locations)location01 is true"))
+
+    @inlineCallbacks
+    def test_updateRecord(self):
+        directory = getRootResource(config).getDirectory()
+        guid = "eee28807-a8c5-46c8-a558-a08281c558a7"
+
+        (yield updateRecord(True, directory, "locations",
+            guid=guid, fullName="Test User", shortNames=["testuser",],)
+        )
+        try:
+            (yield updateRecord(True, directory, "locations",
+                guid=guid, fullName="Test User", shortNames=["testuser",],)
+            )
+        except DirectoryError:
+            # We're expecting an error for trying to create a record with
+            # an existing GUID
+            pass
+        else:
+            raise self.failureException("Duplicate guid expected")
+
+        record = directory.recordWithGUID(guid)
+        self.assertTrue(record is not None)
+        self.assertEquals(record.fullName, "Test User")
+        self.assertTrue(record.autoSchedule)
+
+        (yield updateRecord(False, directory, "locations",
+            guid=guid, fullName="Changed", shortNames=["testuser",],)
+        )
+        record = directory.recordWithGUID(guid)
+        self.assertTrue(record is not None)
+        self.assertEquals(record.fullName, "Changed")
+
+        directory.destroyRecord("locations", guid=guid)
+        record = directory.recordWithGUID(guid)
+        self.assertTrue(record is None)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20100503/c03f6300/attachment-0001.html>


More information about the calendarserver-changes mailing list