def fetch_items(self): print('Fetching items...', file=sys.stdout) # Get parent categories parent_categories = self.get_parent_categories_from_db() for parent_category in parent_categories: # For every parent category get its leaves categories leaf_categories = Category.query.filter( Category.has_no_children(), Category.path.descendant_of(parent_category.path)).all() for leaf_category in leaf_categories: # For every leaf category get corresponding items print('Parsing category:', leaf_category.name, file=sys.stdout) url = self.get_full_url(self.BASE_URL, leaf_category.url) items = self.parser.get_items(url) # SAVE ITEMS TO .JSON FILE if self.save_to_json: print('Saving to .json file...', file=sys.stdout) self.save_to_jsonfile( 'items_{}'.format(leaf_category.slug), items, self.result_save_directory) # SAVE ITEMS TO DATABASE if self.save_to_db: print('Saving to database...', file=sys.stdout) self.save_items_to_database(items, leaf_category)
def get(self, slug): category = Category.query.get_or_404(slug) children_categories = Category.query.filter( Category.has_no_children(), Category.path.descendant_of(category.path)).all() results = category_schema.dump(children_categories, many=True) return results
def test_has_no_children(self): '''Ensure has_no_children method works as expected. ''' category1 = CategoryFactory.create(name='electronics') category2 = CategoryFactory.create(name='tv', parent=category1) categories = Category.query.filter(Category.has_no_children()).all() self.assertEqual(len(categories), 1) self.assertIn(category2, categories)
def get(self): args = parser.parse_args() category_slug = args['category'] # Get category category = Category.query.get_or_404(category_slug) # Get items referred to categories without children items_query = Item.query.join(Category).filter( Category.has_no_children(), Category.path.descendant_of(category.path)) pagination_helper = PaginationHelper( request, query=items_query, resource_for_url='api.itemlistresource', params={'category': category_slug}, key_name='results', schema=items_wo_prices_schema) results = pagination_helper.paginate_query() return results