Commit bea08e7c authored by okhin's avatar okhin 🚴

Merge branch...

Merge branch '42-lorsqu-on-pousse-une-deuxieme-fois-le-meme-article-ces-tags-sont-perdus' into 'rp2'

Resolve "Lorsqu'on pousse une deuxième fois le même article, ces tags sont perdus"

Closes #42

See merge request !32
parents 6b5e39e3 b25bc58b
Pipeline #2576 passed with stages
in 4 minutes and 36 seconds
......@@ -30,11 +30,14 @@ 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
fields = ('id', 'url', 'title', 'tags', 'extracts', 'status',
'und_score_up', 'und_score_down')
'und_score_up', 'und_score_down', 'und_score')
def create(self, validated_data):
article = Article.add_new_url(**validated_data)
......
......@@ -13,6 +13,7 @@ class ArticleViewSet(ArticleMixin, viewsets.ModelViewSet):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
......@@ -22,6 +23,7 @@ class ArticleSearch(viewsets.ModelViewSet, mixins.ListModelMixin):
| Q(extracts__icontains=kwargs['search_keywords']))
return super().list(request, args, kwargs)
class ArticleTag(viewsets.ModelViewSet, mixins.ListModelMixin):
queryset = Article.objects.all()
serializer_class = ArticleSerializer
......
......@@ -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())
......
......@@ -116,8 +116,33 @@ class TestArticleApi(TestCase):
assert r.data['count'] == 0
def test_api_filter_search(self):
text = ' '.join(self.articles[0].extracts.split(' '))
# 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']
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment