from bda.plone.orders.common import acquire_vendor_or_shop_root
from bda.plone.orders.common import get_bookings_soup
from bda.plone.orders.common import get_orders_soup
from bda.plone.orders.common import OrderData
from plone.app.uuid.utils import uuidToObject
from plone.uuid.interfaces import IUUID
from zope.component.hooks import getSite

import logging
import uuid

logger = logging.getLogger('bda.plone.orders UPGRADE')


def fix_bookings_vendor_uid(ctx=None):
    """Add vendor_uid attribute to booking records.
    """
    portal = getSite()
    soup = get_bookings_soup(portal)
    data = soup.storage.data
    need_reindex = False
    for item in data.values():
        if not 'vendor_uid' in item.attrs\
                or not isinstance(item.attrs['vendor_uid'], uuid.UUID):
            buyable_uid = item.attrs['buyable_uid']
            obj = uuidToObject(buyable_uid)
            shop = acquire_vendor_or_shop_root(obj)
            vendor_uid = uuid.UUID(IUUID(shop))
            item.attrs['vendor_uid'] = vendor_uid
            need_reindex = True
            logging.info(
                "Added vendor_uid to booking {0}".format(item.attrs['uid'])
            )
    if need_reindex:
        soup.reindex()
        logging.info("Reindexed bookings souper storage")


def fix_orders_vendor_uids(ctx=None):
    """Add vendor_uids attribute to order records.
    """
    portal = getSite()
    soup = get_bookings_soup(portal)
    data = soup.storage.data
    need_reindex = False
    for item in data.values():
        if not 'vendor_uids' in item.attrs\
                or not isinstance(item.attrs['vendor_uids'], list):
            order_data = OrderData(order=item)
            vendor_uids = set()
            for booking in order_data.bookings:
                vendor_uids.add(booking.attrs['vendor_uid'])
            item.attrs['vendor_uids'] = list(vendor_uids)
            need_reindex = True
            logging.info(
                "Added vendor_uids to order {0}".format(item.attrs['uid'])
            )
    if need_reindex:
        soup.reindex()
        logging.info("Reindexed orders souper storage")

@@ -613,37 +613,37 @@
 data%0A    need_re
-index
+build
  = False%0A    for
@@ -1038,37 +1038,37 @@
          need_re
-index
+build
  = True%0A        
@@ -1181,37 +1181,37 @@
 )%0A    if need_re
-index
+build
 :%0A        soup.r
@@ -1203,37 +1203,37 @@
 %0A        soup.re
-index
+build
 ()%0A        loggi
@@ -1243,23 +1243,21 @@
 info(%22Re
-indexed
+built
  booking
@@ -1258,30 +1258,23 @@
 ookings 
-souper storage
+catalog
 %22)%0A%0A%0Adef
@@ -1467,21 +1467,21 @@
  need_re
-index
+build
  = False
@@ -1889,21 +1889,21 @@
  need_re
-index
+build
  = True%0A
@@ -2031,21 +2031,21 @@
  need_re
-index
+build
 :%0A      
@@ -2053,21 +2053,21 @@
  soup.re
-index
+build
 ()%0A     
@@ -2089,23 +2089,21 @@
 (%22Re
-indexed
+built
  orders 
 soup
@@ -2102,21 +2102,14 @@
 ers 
-souper storage
+catalog
 %22)%0A

rebuild catalog instead of reindex in upgrade steps