def write_category_to_mongo(category, parent_pageid):
    category['title'] = category['title'].replace('Category:', '')
    if Category.objects(pageid=category['pageid']).count() == 0:
        this_category = Category(pageid=category['pageid'],
                                 title=category['title'])
        this_category.save()

    this_category = Category.objects(pageid=category['pageid']).first()

    if parent_pageid is not None:
        parent_category = Category.objects(pageid=parent_pageid).first()
        this_category.parent_pageid = parent_pageid
        this_category.parent_title = parent_category.title
        this_category.save()

    return this_category
Exemple #2
0
    def save_categories(self):

        if self.categories is None:
            return

        root_id = 1
        categories = [c.strip() for c in self.categories.split('>')]
        parent = None
        for category in categories:
            slug = self.create_slug(category)
            if parent is not None:
                slug = parent.slug + "-" + slug

            try:
                cc_node = Category.get(Category.slug == slug)
            except:
                try:
                    cc_node = Category()
                    cc_node.name = category
                    cc_node.slug = slug
                    cc_node.parent_id = parent.id if parent is not None else root_id
                    cc_node.save()

                    cc_node = Category.get(Category.slug == slug)
                except:
                    pass

            parent = cc_node

            try:
                cp = CategoryProduct()
                cp.asin = self.asin
                cp.category_id = cc_node.id
                cp.save()
            except:
                pass