Преглед изворни кода

Merge branch 'master' of github.com:Kozea/radicale

Guillaume Ayoub пре 9 година
родитељ
комит
5b6f0784d0
2 измењених фајлова са 43 додато и 33 уклоњено
  1. 20 13
      radicale/__init__.py
  2. 23 20
      radicale/storage.py

+ 20 - 13
radicale/__init__.py

@@ -533,7 +533,9 @@ class Application:
         content_type = environ.get("CONTENT_TYPE")
         content_type = environ.get("CONTENT_TYPE")
         if content_type:
         if content_type:
             tags = {value: key for key, value in storage.MIMETYPES.items()}
             tags = {value: key for key, value in storage.MIMETYPES.items()}
-            collection.set_meta("tag", tags[content_type.split(";")[0]])
+            tag = tags.get(content_type.split(";")[0])
+            if tag:
+                collection.set_meta("tag", tag)
         headers = {}
         headers = {}
         item_name = xmlutils.name_from_path(environ["PATH_INFO"], collection)
         item_name = xmlutils.name_from_path(environ["PATH_INFO"], collection)
         item = collection.get(item_name)
         item = collection.get(item_name)
@@ -546,21 +548,26 @@ class Application:
             # Case 1: No item and no ETag precondition: Add new item
             # Case 1: No item and no ETag precondition: Add new item
             # Case 2: Item and ETag precondition verified: Modify item
             # Case 2: Item and ETag precondition verified: Modify item
             # Case 3: Item and no Etag precondition: Force modifying item
             # Case 3: Item and no Etag precondition: Force modifying item
-            items = list(vobject.readComponents(content))
-            if items:
-                if item:
-                    # PUT is modifying an existing item
+            items = list(vobject.readComponents(content or ""))
+            if item:
+                # PUT is modifying an existing item
+                if items:
                     new_item = collection.update(item_name, items[0])
                     new_item = collection.update(item_name, items[0])
-                elif item_name:
-                    # PUT is adding a new item
+                else:
+                    new_item = None
+            elif item_name:
+                # PUT is adding a new item
+                if items:
                     new_item = collection.upload(item_name, items[0])
                     new_item = collection.upload(item_name, items[0])
                 else:
                 else:
-                    # PUT is replacing the whole collection
-                    collection.delete()
-                    new_item = self.Collection.create_collection(
-                        environ["PATH_INFO"], items)
-                if new_item:
-                    headers["ETag"] = new_item.etag
+                    new_item = None
+            else:
+                # PUT is replacing the whole collection
+                collection.delete()
+                new_item = self.Collection.create_collection(
+                    environ["PATH_INFO"], items)
+            if new_item:
+                headers["ETag"] = new_item.etag
             status = client.CREATED
             status = client.CREATED
         else:
         else:
             # PUT rejected in all other cases
             # PUT rejected in all other cases

+ 23 - 20
radicale/storage.py

@@ -578,23 +578,26 @@ class Collection(BaseCollection):
                     except OSError:
                     except OSError:
                         cls.logger.debug("Locking not supported")
                         cls.logger.debug("Locking not supported")
                 cls._lock_file_locked = True
                 cls._lock_file_locked = True
-        yield
-        with cls._lock:
-            if mode == "r":
-                cls._readers -= 1
-            else:
-                cls._writer = False
-            if cls._readers == 0:
-                if os.name == "nt":
-                    handle = msvcrt.get_osfhandle(cls._lock_file.fileno())
-                    overlapped = Overlapped()
-                    if not unlock_file_ex(handle, 0, 1, 0, overlapped):
-                        cls.logger.debug("Unlocking not supported")
-                elif os.name == "posix":
-                    try:
-                        fcntl.lockf(cls._lock_file.fileno(), fcntl.LOCK_UN)
-                    except OSError:
-                        cls.logger.debug("Unlocking not supported")
-                cls._lock_file_locked = False
-            if cls._waiters:
-                cls._waiters[0].notify()
+        try:
+            yield
+        finally:
+            with cls._lock:
+                if mode == "r":
+                    cls._readers -= 1
+                else:
+                    cls._writer = False
+                if cls._readers == 0:
+                    if os.name == "nt":
+                        handle = msvcrt.get_osfhandle(cls._lock_file.fileno())
+                        overlapped = Overlapped()
+                        if not unlock_file_ex(handle, 0, 1, 0, overlapped):
+                            cls.logger.debug("Unlocking not supported")
+                    elif os.name == "posix":
+                        try:
+                            fcntl.lockf(cls._lock_file.fileno(),
+                                        fcntl.LOCK_UN)
+                        except OSError:
+                            cls.logger.debug("Unlocking not supported")
+                    cls._lock_file_locked = False
+                if cls._waiters:
+                    cls._waiters[0].notify()