def keepData(self,arr): article=arr['article'] pubdate=arr['pubdate'] title=arr['title'] link=arr['link'] brief=arr['brief'] descr=arr['descr'] content=arr['content'] a1=Article(article=article,title=title,link=link,pubdate=pubdate,brief=brief,descr=descr,content=content) a1.save()
def setUp(self): self.factory = RequestFactory() self.outlet1 = Outlet.objects.create(name='Sample Feed', description="Description", url='http://example.org', language='en-US', updated=None) self.outlet2 = Outlet.objects.create(name='Sample Feed 2', description="Description 2", url='http://example2.org', language='en-GB', updated=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc)) self.author1 = Author(name='Author1') self.author2 = Author(name='Author2', profile='http://plus.google.com/Author2', twitter='@author2') self.outlet1.author_set.add(self.author1) self.outlet1.author_set.add(self.author2) self.article1 = Article(title='Title1', summary='Summary1', url='http://example.com/articles/1', pub_date=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc), content='Content1') self.article2 = Article(title='Title2', summary='Summary2', url='http://example.com/articles/2', pub_date=datetime.datetime( 2002, 9, 7, 0, 1, 1, tzinfo=timezone.utc), content='Content2') self.outlet1.article_set.add(self.article1) self.outlet1.article_set.add(self.article2) self.article1.authors.add(self.author1, self.author2) self.article2.authors.add(self.author1) self.tag1 = Tag.objects.create(term='Tag1') self.tag2 = Tag.objects.create(term='Tag2') self.article1.tags.add(self.tag1) self.article2.tags.add(self.tag2)
def article_add(request, site_id, article_id=None): site = get_object_or_404(Site, pk=site_id) user = request.user added = False changed = False if article_id: # article_id が指定されている (修正時 article = get_object_or_404(Article, pk=article_id) changed = True print("changed") else: # article_id が指定されていない (追加時) article = Article() added = True print("added") if request.method == 'POST': article_form = ArticleForm(request.POST, instance=article) if article_form.is_valid(): article = article_form.save(commit=False) article.title = request.POST['title'] url = request.POST['url'] article.url = url article.content = get_content(url) article.user = user article.site = site article.save() # TODO: 登録後のページ変える、登録or編集しましたというアラートつける context = { "site_id": site_id, "added": added, "changed": changed, } return redirect('result:article_list', site_id=site_id) elif request.method == 'GET': article_form = ArticleForm(instance=article) # site インスタンスからフォームを作成 context = { "site_id": site_id, "article_id": article_id, 'user': user, 'article_form': article_form, } return render( request, 'register/article_add.html', context )
class RestAPITestCase(TestCase): def setUp(self): self.factory = RequestFactory() self.outlet1 = Outlet.objects.create(name='Sample Feed', description="Description", url='http://example.org', language='en-US', updated=None) self.outlet2 = Outlet.objects.create(name='Sample Feed 2', description="Description 2", url='http://example2.org', language='en-GB', updated=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc)) self.author1 = Author(name='Author1') self.author2 = Author(name='Author2', profile='http://plus.google.com/Author2', twitter='@author2') self.outlet1.author_set.add(self.author1) self.outlet1.author_set.add(self.author2) self.article1 = Article(title='Title1', summary='Summary1', url='http://example.com/articles/1', pub_date=datetime.datetime( 2002, 9, 7, 0, 0, 1, tzinfo=timezone.utc), content='Content1') self.article2 = Article(title='Title2', summary='Summary2', url='http://example.com/articles/2', pub_date=datetime.datetime( 2002, 9, 7, 0, 1, 1, tzinfo=timezone.utc), content='Content2') self.outlet1.article_set.add(self.article1) self.outlet1.article_set.add(self.article2) self.article1.authors.add(self.author1, self.author2) self.article2.authors.add(self.author1) self.tag1 = Tag.objects.create(term='Tag1') self.tag2 = Tag.objects.create(term='Tag2') self.article1.tags.add(self.tag1) self.article2.tags.add(self.tag2) def test_outlets_api(self): expected = [self.outlet1.__data__(), self.outlet2.__data__()] # Ordered by name outlets_response = views.outlets(self.factory.get('/rss/outlets/')) self.assertEqual(outlets_response.status_code, 200) deserialized_data = json.loads(outlets_response.content) self.assertEqual(deserialized_data, expected) def test_outlet_api(self): expected = self.outlet1.__data__() outlets_response = views.outlet(self.factory.get('/rss/outlets/1/'), self.outlet1.id) self.assertEqual(outlets_response.status_code, 200) deserialized_data = json.loads(outlets_response.content) self.assertEqual(deserialized_data, expected) def test_authors_api(self): expected = [self.author1.__data__(), self.author2.__data__()] # Ordered by name response = views.authors(self.factory.get('/rss/outlets/1/authors/'), self.outlet1.id) self.assertEqual(response.status_code, 200) deserialized_data = json.loads(response.content) self.assertEqual(deserialized_data, expected) def test_author_api(self): expected = self.author1.__data__() response = views.author(self.factory.get('/rss/outlets/1/authors/1'), self.outlet1.id, self.author1.id) self.assertEqual(response.status_code, 200) deserialized_data = json.loads(response.content) self.assertEqual(deserialized_data, expected) def test_all_authors_api(self): expected = [self.author1.__data__(), self.author2.__data__()] # Ordered by name response = views.all_authors(self.factory.get('/rss/authors/')) self.assertEqual(response.status_code, 200) deserialized_data = json.loads(response.content) self.assertEqual(deserialized_data, expected) def test_articles_api(self): expected = [self.article2.__data__(), self.article1.__data__() ] # Ordered by publication date from newest to oldest response = views.articles(self.factory.get('/rss/outlets/1/articles/'), self.outlet1.id) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_article_api(self): expected = self.article1.__data__() response = views.article(self.factory.get('/rss/outlets/1/articles/1'), self.outlet1.id, self.article1.id) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_all_articles_api(self): expected = [self.article2.__data__(), self.article1.__data__() ] # Ordered by publication date from newest to oldest response = views.all_articles(self.factory.get('/rss/articles/')) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_tags_api(self): expected = [self.tag1.__data__(), self.tag2.__data__()] response = views.tags(self.factory.get('/rss/tags')) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content) self.assertEqual(deserialized_data, expected) def test_article_by_tags_api(self): expected = [self.article1.__data__()] response = views.articles_by_tag( self.factory.get('/rss/tags/Tag1/articles/'), 'Tag1') self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected) def test_articles_search(self): test_tuples = [ # Articles search should search in articles title ([self.article1.__data__()], 'title1'), ([self.article2.__data__()], 'title2'), ([self.article2.__data__(), self.article1.__data__()], 'title'), # Articles search should search in articles summary ([self.article1.__data__()], 'summary1'), ([self.article2.__data__()], 'summary2'), ([self.article2.__data__(), self.article1.__data__()], 'summary'), # Articles search should search in articles content ([self.article1.__data__()], 'content1'), ([self.article2.__data__()], 'content2'), ([self.article2.__data__(), self.article1.__data__()], 'content'), # Articles search should search in articles tags ([self.article1.__data__()], 'tag1'), ([self.article2.__data__()], 'tag2'), ([self.article2.__data__(), self.article1.__data__()], 'tag'), # Articles search should search in articles authors ([self.article2.__data__(), self.article1.__data__()], 'author1'), ([self.article1.__data__()], 'author2'), ([self.article2.__data__(), self.article1.__data__()], 'author') ] for expected, term in test_tuples: self.article_search_test(expected, term) def article_search_test(self, expected, term): endpoint = '/rss/articles/search/%s/' % term response = views.articles_search(self.factory.get(endpoint), term) self.assertEqual(response.status_code, 200) deserialized_data = parse(response.content, 'date') self.assertEqual(deserialized_data, expected)