Merge branch 'optimize_storage_collection'

This commit is contained in:
Florian Mounier 2016-07-08 15:35:43 +02:00
commit 6f9e9f21dd

View File

@ -35,7 +35,8 @@ import time
from contextlib import contextmanager from contextlib import contextmanager
from hashlib import md5 from hashlib import md5
from importlib import import_module from importlib import import_module
from uuid import uuid4 from itertools import groupby
from random import getrandbits
import vobject import vobject
@ -381,25 +382,26 @@ class Collection(BaseCollection):
items = [] items = []
for content in ("vevent", "vtodo", "vjournal"): for content in ("vevent", "vtodo", "vjournal"):
items.extend(getattr(collection, "%s_list" % content, [])) items.extend(getattr(collection, "%s_list" % content, []))
processed_uids = []
for i, item in enumerate(items): def get_uid(item):
uid = getattr(item, "uid", None) return hasattr(item, 'uid') and item.uid.value
if uid in processed_uids:
continue items_by_uid = groupby(
sorted(items, key=get_uid), get_uid)
for uid, items in items_by_uid:
new_collection = vobject.iCalendar() new_collection = vobject.iCalendar()
new_collection.add(item) for item in items:
if uid: new_collection.add(item)
processed_uids.append(uid) file_name = hex(getrandbits(32))[2:]
# search for items with same UID self.upload(file_name, new_collection)
for oitem in items[i+1:]:
if getattr(oitem, "uid", None) == uid:
new_collection.add(oitem)
self.upload(uuid4().hex, new_collection)
elif tag == "VCARD": elif tag == "VCARD":
self.set_meta("tag", "VADDRESSBOOK") self.set_meta("tag", "VADDRESSBOOK")
if collection: if collection:
for card in collection: for card in collection:
self.upload(uuid4().hex, card) file_name = hex(getrandbits(32))[2:]
self.upload(file_name, card)
return self return self
def list(self): def list(self):