Commit a3d4cdd0 authored by okhin's avatar okhin 🚴

Slightly changing the signature for Article.add_new_url and overloading the...

Slightly changing the signature for Article.add_new_url and overloading the create method for the Serializers
parent 3e82812a
Pipeline #2555 passed with stages
in 3 minutes and 1 second
......@@ -35,3 +35,7 @@ class ArticleSerializer(serializers.ModelSerializer):
model = Article
fields = ('id', 'url', 'title', 'tags', 'extracts', 'status',
'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
article content. You should aim at around 500 characters. Use bracket ellipsis
[…] to cut parts not required to understand the context."""
class Article(VoteMixin):
#: Logical state (eg. article submitted, published, or rejected)
# This is unprotected because superuser should be able to change
......@@ -172,7 +171,8 @@ class Article(VoteMixin):
"""
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.
Verify if the article has not been submitted before and automatically
upvote for the given user if applicable.
......@@ -180,20 +180,20 @@ class Article(VoteMixin):
import requests
url = cleanup_url(url)
article, _ = Article.objects.get_or_create(url=url)
if article.created_by is None:
article.created_by = str(by)
article, created = Article.objects.get_or_create(url=url,
defaults=data)
if by is not None:
article.upvote(by)
# Is 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:
# If the 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 identified.
# 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.
article.original_status = 600
article.save()
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