def post(self): subject = self.request.get("subject") content = self.request.get("content") verify_subject = len(subject) > 0 verify_content = len(content) > 0 if verify_content and verify_subject: entry = BlogEntry( subject = subject, content = content ) entry.put() self.redirect(self.app.config.get('url_base') + "/" + str(entry.key().id())) else: self.generate_newpost_page( subject, "" if verify_subject else "Missing subject.", content, "" if verify_content else "Missing content.")
def transform(collection): output_collection = list() for entry in collection: output_collection.append(BlogEntry(StripPunctuation.eliminate_punctuation(entry.title()), \ entry.date(), entry.url(), StripPunctuation.eliminate_punctuation(entry.text()), entry.source, entry.crawl_url)) return BlogEntryCollection(output_collection)
def get(self, post_id): entry = BlogEntry.get_by_id(int(post_id)) if entry: self.response.headers.add_header("Content-Type", "application/json") self.response.out.write(entry.to_json()) else: self.redirect(URL_BASE)
def get(self): entries = BlogEntry.get_last_10_json() if entries: self.response.headers.add_header("Content-Type", "application/json") self.response.out.write(entries) else: self.redirect(URL_BASE)
def get(self): entries = BlogEntry.get_last_10() self.render("mainpage.html", newpost_url=self.app.config.get('url_newpost'), single_url=self.app.config.get('url_base')+"/", title="My Blog", entries=entries)
def test_construct_with_nones(self): date = datetime.now() b = BlogEntry(None, date, None, None, None, None) self.assertEqual(b.title(), None) self.assertEqual(b.date(), date) self.assertEqual(b.url(), None) self.assertEqual(b.text(), None) self.assertEqual(b.source(), None) self.assertEqual(b.crawl_url(), None)
def test_construct_with_date(self): date = datetime.now() b = BlogEntry("title", date, "url", "raw_text", "source", "crawl_url") self.assertEqual(b.title(), "title") self.assertEqual(b.date(), date) self.assertEqual(b.url(), "url") self.assertEqual(b.text(), "raw_text") self.assertEqual(b.source(), "source") self.assertEqual(b.crawl_url(), "crawl_url")
def test_construct_with_invalid_date(self): error = False not_a_valid_date = "not a valid date" try: b = BlogEntry(None, not_a_valid_date, None, None, None, None) except ValueError: error = True self.assertTrue(error)
def transform(collection): output_collection = list() for entry in collection: output_collection.append(BlogEntry(TextUtil.to_lower_case(entry.title()), \ entry.date(), entry.url(), TextUtil.to_lower_case(entry.text()), entry.source, entry.crawl_url)) return BlogEntryCollection(output_collection)
def get(self): entries, cache_time = BlogEntry.get_last_10_posts() self.render("mainpage.html", newpost_url=self.app.config.get('url_newpost'), single_url=self.app.config.get('url_base')+"/", title="My Blog", entries=entries, cache_time=build_cache_time_str(cache_time) )
def get(self, post_id): entry = BlogEntry.get_by_id(int(post_id)) if entry: self.render("singlepost.html", subject=entry.subject, content=entry.content, main_url=self.app.config.get('url_base') ) else: self.redirect(self.app.config.get('url_base'))
def get(self, post_id): post, post_time = BlogEntry.get_post_by_id(int(post_id)) if post: self.render("singlepost.html", subject=post.subject, content=post.content, main_url=self.app.config.get('url_base'), cache_time=build_cache_time_str(post_time) ) else: self.redirect(self.app.config.get('url_base'))
class TestBlogEntryCollection(unittest.TestCase): blog_entry_1 = BlogEntry("title", datetime.now(), "url", "raw_text", "source", "crawl_url") blog_entry_2 = BlogEntry("title", datetime.now(), "url", "raw_text", "source", "crawl_url") def test_invalid_entries(self): error = False try: BlogEntryCollection(TestBlogEntryCollection.blog_entry_1) except ValueError: error = True self.assertTrue(error) def test_construct(self): entries = [TestBlogEntryCollection.blog_entry_1, TestBlogEntryCollection.blog_entry_2] bc = BlogEntryCollection(entries) self.assertEquals(2, bc.size()) for entry in bc: self.assertIn(entry, entries) for entry in entries: self.assertIn(entry, bc) def test_load_from_json(self): date = datetime.now() date_string = str(date) json_entry_string = '\ { \ "title": ["title"], \ "url": ["url"], \ "timestamp": ["' + date_string + '"], \ "raw_content": ["raw", "text"], \ "source": "source", \ "crawl_url": "crawl_url"\ }' json_collection_string = '[' + json_entry_string + ', ' + json_entry_string + ']' json_object = json.loads(json_collection_string) bc = BlogEntryCollection.from_json_object(json_object) self.assertEquals(2, bc.size())
def test_transform(self): date = datetime.now() b = BlogEntry("title", date, "url", "raw_text", "source", "crawl_url") collection = BlogEntryCollection([b]) collection1 = DiscardBody.transform(collection) collection1 = ToLowerCase.transform(collection1) collection1 = StripPunctuation.transform(collection1) transformed_entry = collection1[0] expected_entry = BlogEntry("title", date, "url", "", "source", "crawl_url") self.assertEquals(expected_entry.title(), transformed_entry.title()) self.assertEquals(expected_entry.date(), transformed_entry.date()) self.assertEquals(expected_entry.url(), transformed_entry.url())
def test_load_from_json(self): date = datetime.now() date_string = str(date) json_string = '\ { \ "title": ["title"], \ "url": ["url"], \ "timestamp": ["' + date_string + '"], \ "raw_content": ["raw", "text"], \ "source": "source", \ "crawl_url": "crawl_url"\ }' json_object = json.loads(json_string) b = BlogEntry.from_json_object(json_object) self.assertEqual(b.title(), "title,") self.assertEqual(b.date(), date) self.assertEqual(b.url(), "url,") self.assertEqual(b.text(), "raw,text,") self.assertEqual(b.source(), "source") self.assertEqual(b.crawl_url(), "crawl_url")
def transform(collection, foodwords_list, neighboring_words = 2): collection = ToLowerCase.transform(collection) collection = StripPunctuation.transform(collection) output_collection = list() for entry in collection: raw_words = TextUtil.make_lowercase_word_vector(entry.text()) #, unique = False, stemming = True tot_words = len(raw_words) index = neighboring_words raw_content_words_list = list() while index <= (tot_words - neighboring_words): if TextUtil.list_contains(raw_words[index], foodwords_list): sub_sentence = raw_words[(index-neighboring_words):(index+neighboring_words+1)] for word in sub_sentence: raw_content_words_list.append(word) index += neighboring_words else: index += 1 output_collection.append(BlogEntry(entry.title(), entry.date(), entry.url(), set(raw_content_words_list))) return output_collection
def transform(collection): output_collection = list() for entry in collection: output_collection.append(BlogEntry(entry.title(), entry.date(), entry.url(), '', entry.source(), entry.crawl_url())) return BlogEntryCollection(output_collection)