diff --git a/apps/rp/api/serializers.py b/apps/rp/api/serializers.py index 2100d2763d0a16e532ac357e70a9f2e1c48c3d95..f67a72edc8bdcf0c1eb6c075b51283bafcd08f61 100644 --- a/apps/rp/api/serializers.py +++ b/apps/rp/api/serializers.py @@ -30,6 +30,9 @@ class ArticleSerializer(serializers.ModelSerializer): und_score_down = serializers.IntegerField( required=False, help_text="This is used to decrease the vote count by this value") + und_score = serializers.IntegerField( + required=False, + help_text="This is the actual computed score for an Article") class Meta: model = Article diff --git a/apps/rp/models.py b/apps/rp/models.py index e9370e799f04863e289ddaab9398c895c71d642a..dece655f277af19eefd18036b9a5fbc88916f1f3 100644 --- a/apps/rp/models.py +++ b/apps/rp/models.py @@ -180,17 +180,21 @@ class Article(VoteMixin): import requests url = cleanup_url(data.pop('url', None)) - article, created = Article.objects.get_or_create(url=url, - defaults=data) - - # Is the article was already there, we should upvote it + tags = data.pop('tags', None) + (article, created) = Article.objects.get_or_create(url=url, + defaults=data) + + # Let's add the tags + if tags: + article.tags.add(','.join([t for t in tags if len(t) > 0])) + # If the article was already there, we should upvote it if not created: article.upvote(str(by)) try: r = requests.get(url, timeout=0.5) article.original_status = r.status_code - except: + except Exception: # If the domain name can't be found, we're not even getting into # the HTTP protocol So, let's get a specific status for that, # one that can be easily identified. @@ -199,7 +203,6 @@ class Article(VoteMixin): return article # Content extraction - def fetch_content(self): if self.lang != "NA": article = ArticleParser(url=self.url, language=self.lang.lower()) diff --git a/apps/rp/tests/test_article.py b/apps/rp/tests/test_article.py index ddc1183d7253a6c9201b48156e6d4fa9f52edd71..1a64bcb07adf873eb9d021916603e9bed844b572 100644 --- a/apps/rp/tests/test_article.py +++ b/apps/rp/tests/test_article.py @@ -116,9 +116,33 @@ class TestArticleApi(TestCase): assert r.data['count'] == 0 def test_api_filter_search(self): - #text = ' '.join(self.articles[0].extracts.split(' ')[:10]) + # text = ' '.join(self.articles[0].extracts.split(' ')[:10]) text = self.articles[0].title r = self.client.get('/api/articles-search/{}/'.format(text)) assert r.status_code == 200 assert r.data['count'] == 1 assert r.data['results'][0]['id'] == self.articles[0].id + + def test_api_tag_push_unauth(self): + a = ArticleFactory(tags=['ZogZog'],) + r = self.client.post('/api/articles/', + {'url': a.url, 'title': a.title, + 'tags': ','.join([t.name for t in a.tags.all()]) + }) + assert r.status_code == 401 + + def test_api_tag_push_auth(self): + self.client.force_login(user=self.user) + a = ArticleFactory(tags=['ZogZog'], status='NEW') + r = self.client.post('/api/articles/', + {'url': a.url, 'title': a.title, + 'tags': ','.join([t.name for t in a.tags.all()]) + }) + assert r.status_code == 201 + + # Need to test if we keep the tags + r = self.client.post('/api/articles/', + {'url': a.url, 'title': a.title, + 'tags': '' + }) + assert [t.name for t in a.tags.all()] == r.data['tags']