def build(self, table_id): """ Builds an article, written in HTML, the article includes the table, title, review and image for all the products. :param table_id: wordpress table id :return article: html string """ if self.article: return self.article self.article = self.TABLE_FORMAT.format(table_id) for index, product in enumerate(self.products): shorten_url = common.get_short_url(product.page_url) shorten_url = common.get_internal_url_redirect(shorten_url, product.title) if INTERNAL_REDIRECT_SHORTEN_URLS else shorten_url alignment = self.CLASS_FORMAT.format('alignleft' if index % 2 == 0 else 'alignright') title = self.TITLE_FORMAT.format(shorten_url, product.title) features = self.FEATURES_FORMAT.format(price=product.get_price(), score=product.get_rating(), num_of_reviews=product.get_num_of_reviews(), features=product.get_features()) img = self.IMG_FORMAT.format(shorten_url, alignment, product.get_img_url('LargeImage'), product.title) review = self.REVIEW_FORMAT.format(product.get_review(), review_url=common.get_short_url(product.five_stars_review_url), title=product.title) self.article += self.PRODUCT_FORMAT.format(title, features, img, review) return self.article
def test_build_sanity(self): table_id = 5 article = self.article_builder.build(table_id) our_article = TABLE_FORMAT.format(table_id) for index, product in enumerate(self.products): shorten_url = common.get_short_url(product.page_url) alignment = CLASS_FORMAT.format('alignleft' if index % 2 == 0 else 'alignright') title = TITLE_FORMAT.format(shorten_url, product.title) img = IMG_FORMAT.format(shorten_url, alignment, product.get_img_url('LargeImage'), product.title) review = REVIEW_FORMAT.format(product.get_review()) our_article += PRODUCT_FORMAT.format(title, img, review) self.assertTrue(our_article == article)
def build(self, products): full_path = path.join(self.article_dir, self.TABLE_PATH) with open(full_path, "wb") as f: writer = csv.DictWriter(f, fieldnames=self.fieldnames_list) writer.writeheader() fieldnames = {} for product in products: product_url = get_short_url(product.page_url) product_url = ( get_internal_url_redirect(product_url, product.title) if INTERNAL_REDIRECT_SHORTEN_URLS else product_url ) fieldnames["Picture"] = self.IMG_FORMAT.format(product_url, product.get_img_url("SmallImage")) fieldnames["Name"] = product.title fieldnames["Rating"] = product.get_rating() fieldnames["Price"] = product.get_price() writer.writerow(fieldnames) return self._upload_table(full_path)
def short_url(self): return get_short_url(self.get_absolute_url())