本文整理汇总了Python中amcat.models.Article类的典型用法代码示例。如果您正苦于以下问题:Python Article类的具体用法?Python Article怎么用?Python Article使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Article类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_elastic_hash
def test_elastic_hash(self):
"""Can we reproduce a hash from elastic data alone?"""
article = Article(**{
"date": datetime.date(2015, 1, 1),
"section": "\u6f22\u5b57",
"pagenr": 1928390,
"headline": "Headline hier.",
"byline": "byline..",
"length": 1928,
"metastring": "Even more strange characters.. \x0C ..",
"url": "https://example.com",
"externalid": None,
"author": None,
"addressee": "Hmm",
"text": "Contains invalid char \x08 woo",
"medium": create_test_medium(name="abc."),
"project": create_test_project()
})
article.save()
es = ES()
es.add_articles([article.id])
hash = get_article_dict(article)["hash"]
es.flush()
es_articles = es.query_all(filters={"ids": [article.id]}, fields=HASH_FIELDS + ["hash"])
es_article = list(es_articles)[0]
self.assertEqual(article.id, es_article.id)
self.assertEqual(hash, es_article.hash)
self.assertEqual(_get_hash(es_article.to_dict()), hash)
示例2: test_aggregate
def test_aggregate(self):
"""Can we make tables per medium/date interval?"""
from amcat.models import Article
m1 = amcattest.create_test_medium(name="De Nep-Krant")
m2, m3 = [amcattest.create_test_medium() for _ in range(2)]
s1 = amcattest.create_test_set()
s2 = amcattest.create_test_set()
unused = amcattest.create_test_article(text='aap noot mies', medium=m3, articleset=s2)
a = amcattest.create_test_article(text='aap noot mies', medium=m1, date='2001-01-01', create=False)
b = amcattest.create_test_article(text='noot mies wim zus', medium=m2, date='2001-02-01', create=False)
c = amcattest.create_test_article(text='mies bla bla bla wim zus jet', medium=m2, date='2002-01-01', create=False)
d = amcattest.create_test_article(text='noot mies wim zus', medium=m2, date='2001-02-03', create=False)
Article.create_articles([a,b,c,d], articleset=s1, check_duplicate=False, create_id=True)
ES().flush()
self.assertEqual(dict(ES().aggregate_query(filters=dict(sets=s1.id), group_by="mediumid")),
{m1.id : 1, m2.id : 3})
self.assertEqual(dict(ES().aggregate_query(filters=dict(sets=s1.id), group_by="date", date_interval="year")),
{datetime(2001,1,1) : 3, datetime(2002,1,1) : 1})
self.assertEqual(dict(ES().aggregate_query(filters=dict(sets=s1.id), group_by="date", date_interval="month")),
{datetime(2001,1,1) : 1, datetime(2002,1,1) : 1, datetime(2001,2,1) : 2})
# set statistics
stats = ES().statistics(filters=dict(sets=s1.id))
self.assertEqual(stats.n, 4)
self.assertEqual(stats.start_date, datetime(2001,1,1))
self.assertEqual(stats.end_date, datetime(2002,1,1))
# media list
self.assertEqual(set(ES().list_media(filters=dict(sets=s1.id))),
{m1.id, m2.id})
示例3: test_get_articles
def test_get_articles(self):
from amcat.models import Sentence
_get_articles = lambda a,s : list(get_articles(a,s))
# Should raise exception if sentences not in article
article, sentences = self.create_test_sentences()
s1 = Sentence.objects.filter(id=amcattest.create_test_sentence().id)
self.assertRaises(ValueError, _get_articles, article, s1)
# Should raise an exception if we try to split on headline
self.assertRaises(ValueError, _get_articles, article, sentences.filter(parnr=1))
# Should return a "copy", with byline in "text" property
arts = _get_articles(article, Sentence.objects.none())
Article.create_articles(arts)
self.assertEquals(len(arts), 1)
sbd.create_sentences(arts[0])
self.assertEquals(
[s.sentence for s in sentences[1:]],
[s.sentence for s in arts[0].sentences.all()[1:]]
)
self.assertTrue("foo" in arts[0].text)
# Should be able to split on byline
self.assertEquals(2, len(_get_articles(article, sentences[1:2])))
a, b = _get_articles(article, sentences[4:5])
# Check if text on splitted articles contains expected
self.assertTrue("Einde" not in a.text)
self.assertTrue("Einde" in b.text)
示例4: create
def create(self, validated_data):
children = validated_data.pop("children")
article = Article(**validated_data)
if article.length is None:
article.length = word_len(article.text)
return (article, map(self.create, children))
示例5: scrape_unit
def scrape_unit(self, unit):
date = iso8601.iso8601.parse_date(unit["datum"], default_timezone=None)
hostname = urlparse(unit["url"]).hostname
publisher = ".".join(hostname.split(".")[-2:])
title = unit["titel"].strip() or "[No title]"
article = Article(title=title, text=unit["bericht tekst"], url=unit["url"], date=date)
article.set_property("author", unit["auteur"])
article.set_property("publisher", publisher)
return article
示例6: _parse_comment
def _parse_comment(self, comment, base_title, base_url):
text = html2text(comment.cssselect("p"))
article_id = comment.get("id")
title = "{base_title}#{article_id}".format(**locals())
url = "{base_url}#{article_id}".format(**locals())
author, timestamp = _parse_comment_footer(comment.cssselect("footer")[0].text_content())
article = Article(date=timestamp, title=title, text=text.strip() or ".", url=url)
article.set_property("author", author.strip())
article.set_property("medium", "GeenStijl Comments")
return article
示例7: test_create
def test_create(self):
"""Can we create/store/index an article object?"""
a = amcattest.create_test_article(create=False, date='2010-12-31', headline=u'\ua000abcd\u07b4')
Article.create_articles([a], create_id=True)
db_a = Article.objects.get(pk=a.id)
amcates.ES().flush()
es_a = list(amcates.ES().query(filters={'ids': [a.id]}, fields=["date", "headline"]))[0]
self.assertEqual(a.headline, db_a.headline)
self.assertEqual(a.headline, es_a.headline)
self.assertEqual('2010-12-31T00:00:00', db_a.date.isoformat())
self.assertEqual('2010-12-31T00:00:00', es_a.date.isoformat())
示例8: test_create_order
def test_create_order(self):
"""Is insert order preserved in id order?"""
articles = [amcattest.create_test_article(create=False) for _i in range(25)]
random.shuffle(articles)
Article.create_articles(articles)
ids = [a.id for a in articles]
# is order preserved?
self.assertEqual(ids, sorted(ids))
# do the right articles have the right title?
for saved in articles:
indb = Article.objects.get(pk=saved.id)
self.assertEqual(indb.title, saved.title)
示例9: create
def create(self, validated_data):
articleset = self.get_articleset()
if 'id' in validated_data:
_check_read_access(self.context['request'].user, [validated_data['id']])
article = Article.objects.get(pk=validated_data['id'])
articleset.add_articles([article])
else:
article = json_to_article(validated_data, articleset.project)
Article.create_articles([article], articleset=articleset)
return article
示例10: setup
def setup(self):
s1 = amcattest.create_test_set()
s2 = amcattest.create_test_set()
a = amcattest.create_test_article(text='aap noot mies', title='m1', date='2001-01-01', create=False)
b = amcattest.create_test_article(text='noot mies wim zus', title='m2', date='2001-02-01', create=False)
c = amcattest.create_test_article(text='mies bla bla bla wim zus jet', title='m2', date='2002-01-01', create=False)
d = amcattest.create_test_article(text='noot mies wim zus', title='m2', date='2001-02-03', create=False)
e = amcattest.create_test_article(text='aap noot mies', title='m3', articleset=s2)
Article.create_articles([a, b, c, d], articleset=s1)
ES().refresh()
return s1, s2, a, b, c, d, e
示例11: test_create
def test_create(self):
"""Can we create/store/index an article object?"""
a = amcattest.create_test_article(create=False, date='2010-12-31', title=u'\ua000abcd\u07b4')
Article.create_articles([a])
db_a = Article.objects.get(pk=a.id)
amcates.ES().refresh()
es_a = list(amcates.ES().query(filters={'ids': [a.id]}, _source=["date", "title", "hash"]))[0]
self.assertEqual(a.hash, db_a.hash)
self.assertEqual(a.hash, es_a.hash)
self.assertEqual(a.title, db_a.title)
self.assertEqual(a.title, es_a.title)
self.assertEqual('2010-12-31T00:00:00', db_a.date.isoformat())
self.assertEqual('2010-12-31T00:00:00', es_a.date.isoformat())
示例12: test_query_all
def test_query_all(self):
"""Test that query_all works"""
from amcat.models import Article
arts = [amcattest.create_test_article(create=False) for _ in range(20)]
s = amcattest.create_test_set()
Article.create_articles(arts, articleset=s, check_duplicate=False, create_id=True)
ES().flush()
r = ES().query(filters=dict(sets=s.id), size=10)
self.assertEqual(len(list(r)), 10)
r = ES().query_all(filters=dict(sets=s.id), size=10)
self.assertEqual(len(list(r)), len(arts))
示例13: setup
def setup(self):
m1 = amcattest.create_test_medium(name="De Nep-Krant")
m2, m3 = [amcattest.create_test_medium() for _ in range(2)]
s1 = amcattest.create_test_set()
s2 = amcattest.create_test_set()
a = amcattest.create_test_article(text='aap noot mies', medium=m1, date='2001-01-01', create=False)
b = amcattest.create_test_article(text='noot mies wim zus', medium=m2, date='2001-02-01', create=False)
c = amcattest.create_test_article(text='mies bla bla bla wim zus jet', medium=m2, date='2002-01-01', create=False)
d = amcattest.create_test_article(text='noot mies wim zus', medium=m2, date='2001-02-03', create=False)
e = amcattest.create_test_article(text='aap noot mies', medium=m3, articleset=s2)
Article.create_articles([a, b, c, d], articleset=s1)
ES().flush()
return m1, m2, m3, s1, s2, a, b, c, d, e
示例14: test_add_many
def test_add_many(self):
"""Can we add a large number of articles from one set to another?"""
s = amcattest.create_test_set()
s2 = amcattest.create_test_set()
p = amcattest.create_test_project()
arts = [amcattest.create_test_article(project=p, create=False) for _x in range(1213)]
Article.create_articles(arts, s)
ES().refresh()
self.assertEqual(len(arts), s.get_count())
s2.add_articles(arts)
ES().refresh()
self.assertEqual(len(arts), s2.get_count())
print(s2.get_count())
示例15: test_add_many
def test_add_many(self):
"""Can we add a large number of articles from one set to another?"""
s = amcattest.create_test_set()
s2 = amcattest.create_test_set()
m = amcattest.create_test_medium()
p = amcattest.create_test_project()
arts = [amcattest.create_test_article(project=p, medium=m, create=False) for _x in range(1213)]
Article.create_articles(arts, s, create_id=True)
ES().flush()
self.assertEqual(len(arts), s.get_count())
s2.add_articles(arts, monitor=ProgressMonitor())
ES().flush()
self.assertEqual(len(arts), s2.get_count())
print(s2.get_count())