Example #1
0
    def migrate_legacy_categories(self):
        print cformat('%{white!}migrating legacy categories')

        indexes = [
            self.zodb_root['categories'],
            self.zodb_root['catalog']['categ_conf_sd']._container
        ]
        indexes += [
            self.zodb_root['indexes'][x]._idxCategItem
            for x in ('category', 'categoryDate', 'categoryDateAll')
        ]
        mapping = {}

        for categ in self._committing_iterator(self._get_categories()):
            if not hasattr(categ, '_old_id'):
                new_id = self.gen_categ_id()
                index_data = [idx.pop(categ.id, None) for idx in indexes]
                categ._old_id = categ.id
                categ.id = new_id
                for idx, data in zip(indexes, index_data):
                    assert categ.id not in idx
                    if data is not None:
                        idx[categ.id] = data
                self.zodb_root['categories'][categ.id] = categ
                IndexedCategory.find(id=categ._old_id).update(
                    {IndexedCategory.id: categ.id})
                FavoriteCategory.find(target_id=categ._old_id).update(
                    {FavoriteCategory.target_id: categ.id})
                db.session.add(
                    LegacyCategoryMapping(legacy_category_id=categ._old_id,
                                          category_id=int(categ.id)))
                print cformat('%{green}+++%{reset} '
                              '%{white!}{:6s}%{reset} %{cyan}{}').format(
                                  categ._old_id, int(categ.id))
            else:
                # happens if this importer was executed before but you want to add the mapping to your DB again
                db.session.add(
                    LegacyCategoryMapping(legacy_category_id=categ._old_id,
                                          category_id=int(categ.id)))
                msg = cformat(
                    '%{green}+++%{reset} '
                    '%{white!}{:6s}%{reset} %{cyan}{}%{reset} %{yellow}(already updated in zodb)'
                )
                print msg.format(categ._old_id, int(categ.id))
            mapping[categ._old_id] = categ.id

        print cformat('%{white!}fixing subcategory lists')
        for categ in self._committing_iterator(
                self.flushing_iterator(
                    self.zodb_root['categories'].itervalues())):
            for subcateg_id in categ.subcategories.keys():
                new_id = mapping.get(subcateg_id)
                if new_id is not None:
                    categ.subcategories[new_id] = categ.subcategories.pop(
                        subcateg_id)
                    categ._p_changed = True
Example #2
0
 def initialize(self, items):
     for i, categ in enumerate(items, 1):
         cat = IndexedCategory(id=categ.getId(), title=categ.getTitle())
         db.session.add(cat)
         if i % 1000 == 0:
             db.session.commit()
     db.session.commit()
Example #3
0
 def index(self, obj):
     if not obj.getId():  # newly created root category has id=''
         return
     self.unindex(obj)
     category = IndexedCategory(id=obj.getId(), title=obj.getTitle())
     db.session.add(category)
     with retry_request_on_conflict():
         db.session.flush()
    def migrate_legacy_categories(self):
        print cformat('%{white!}migrating legacy categories')

        indexes = [self.zodb_root['categories'], self.zodb_root['catalog']['categ_conf_sd']._container]
        indexes += [self.zodb_root['indexes'][x]._idxCategItem for x in ('category', 'categoryDate', 'categoryDateAll')]
        mapping = {}

        for categ in self._committing_iterator(self._get_categories()):
            if not hasattr(categ, '_old_id'):
                new_id = self.gen_categ_id()
                index_data = [idx.pop(categ.id, None) for idx in indexes]
                categ._old_id = categ.id
                categ.id = new_id
                for idx, data in zip(indexes, index_data):
                    assert categ.id not in idx
                    if data is not None:
                        idx[categ.id] = data
                self.zodb_root['categories'][categ.id] = categ
                IndexedCategory.find(id=categ._old_id).update({IndexedCategory.id: categ.id})
                FavoriteCategory.find(target_id=categ._old_id).update({FavoriteCategory.target_id: categ.id})
                db.session.add(LegacyCategoryMapping(legacy_category_id=categ._old_id, category_id=int(categ.id)))
                print cformat('%{green}+++%{reset} '
                              '%{white!}{:6s}%{reset} %{cyan}{}').format(categ._old_id, int(categ.id))
            else:
                # happens if this importer was executed before but you want to add the mapping to your DB again
                db.session.add(LegacyCategoryMapping(legacy_category_id=categ._old_id, category_id=int(categ.id)))
                msg = cformat('%{green}+++%{reset} '
                              '%{white!}{:6s}%{reset} %{cyan}{}%{reset} %{yellow}(already updated in zodb)')
                print msg.format(categ._old_id, int(categ.id))
            mapping[categ._old_id] = categ.id

        print cformat('%{white!}fixing subcategory lists')
        for categ in self._committing_iterator(self.flushing_iterator(self.zodb_root['categories'].itervalues())):
            for subcateg_id in categ.subcategories.keys():
                new_id = mapping.get(subcateg_id)
                if new_id is not None:
                    categ.subcategories[new_id] = categ.subcategories.pop(subcateg_id)
                    categ._p_changed = True
Example #5
0
 def unindex(self, obj):
     if not obj.getId():  # newly created root category has id=''
         return
     IndexedCategory.find(id=obj.getId()).delete()
     db.session.flush()
Example #6
0
 def unindex(self, obj):
     if not obj.getId():  # newly created root category has id=''
         return
     IndexedCategory.find(id=obj.getId()).delete()
     db.session.flush()
Example #7
0
 def unindex(self, obj):
     IndexedCategory.find(id=obj.getId()).delete()
     db.session.flush()