Commit a7309f8a authored by okhin's avatar okhin 🚴

Merge branch '38-l-ajout-d-un-article-deja-existant-en-cree-un-nouveau' into 'rp2'

Resolve "L'ajout d'un article déjà existant en crée un nouveau"

Closes #38

See merge request !28
parents 3e82812a a3d4cdd0
Pipeline #2557 passed with stages
in 2 minutes and 56 seconds
...@@ -35,3 +35,7 @@ class ArticleSerializer(serializers.ModelSerializer): ...@@ -35,3 +35,7 @@ class ArticleSerializer(serializers.ModelSerializer):
model = Article model = Article
fields = ('id', 'url', 'title', 'tags', 'extracts', 'status', fields = ('id', 'url', 'title', 'tags', 'extracts', 'status',
'und_score_up', 'und_score_down') 'und_score_up', 'und_score_down')
def create(self, **validated_data):
article = Article.add_new_url(**validated_data)
article.save()
...@@ -38,7 +38,6 @@ EXTRACTS_HELP_TEXT = """Please select short and helpful extracts from the ...@@ -38,7 +38,6 @@ EXTRACTS_HELP_TEXT = """Please select short and helpful extracts from the
article content. You should aim at around 500 characters. Use bracket ellipsis article content. You should aim at around 500 characters. Use bracket ellipsis
[…] to cut parts not required to understand the context.""" […] to cut parts not required to understand the context."""
class Article(VoteMixin): class Article(VoteMixin):
#: Logical state (eg. article submitted, published, or rejected) #: Logical state (eg. article submitted, published, or rejected)
# This is unprotected because superuser should be able to change # This is unprotected because superuser should be able to change
...@@ -172,7 +171,8 @@ class Article(VoteMixin): ...@@ -172,7 +171,8 @@ class Article(VoteMixin):
""" """
super(Article, self).downvote(by) super(Article, self).downvote(by)
def add_new_url(url, by=None): @classmethod
def add_new_url(url, by=None, **data):
""" Manually add a new article from its URL. """ Manually add a new article from its URL.
Verify if the article has not been submitted before and automatically Verify if the article has not been submitted before and automatically
upvote for the given user if applicable. upvote for the given user if applicable.
...@@ -180,20 +180,20 @@ class Article(VoteMixin): ...@@ -180,20 +180,20 @@ class Article(VoteMixin):
import requests import requests
url = cleanup_url(url) url = cleanup_url(url)
article, _ = Article.objects.get_or_create(url=url) article, created = Article.objects.get_or_create(url=url,
defaults=data)
if article.created_by is None:
article.created_by = str(by)
if by is not None: # Is the article was already there, we should upvote it
article.upvote(by) if not created:
article.upvote(str(by))
try: try:
r = requests.get(url, timeout=0.5) r = requests.get(url, timeout=0.5)
article.original_status = r.status_code article.original_status = r.status_code
except: except:
# If the name can't be found, we're not even getting into the HTTP protocol # If the domain name can't be found, we're not even getting into
# So, let's get a specific status for that, one that can be identified. # the HTTP protocol So, let's get a specific status for that,
# one that can be easily identified.
article.original_status = 600 article.original_status = 600
article.save() article.save()
return article return article
......
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