Skip to content

Commit

Permalink
Merge pull request #29 from andela/ft-share-articles-164798217
Browse files Browse the repository at this point in the history
#164798217  User should be able to share articles
  • Loading branch information
ja-odur committed Apr 15, 2019
2 parents 5d5314d + 34dea98 commit a41f1c5
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .env_config_sample
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ EMAIL_PORT='587'

WORD_LENGTH='5'
WORD_PER_MINUTE='256'
BASE_ARTICLE_SHARE_URL='http://www.front/articles'


7 changes: 6 additions & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,19 @@
from .models import Article
from authors.apps.comments.models import Comment
from authors.apps.comments.serializers import CommentDetailSerializer
from authors.apps.helpers.share_articles import share_articles_links


class ArticleSerializer(serializers.ModelSerializer):
author = ProfileSerializer(required=False)
article_read_time = serializers.CharField(max_length=100, read_only=True)
favorite = serializers.SerializerMethodField()
share_article_links = serializers.SerializerMethodField()

class Meta:
fields = '__all__'
model = models.Article
read_only_fields = ['author', 'slug', 'article_read_time']
read_only_fields = ['author', 'slug', 'article_read_time', 'share_article_links']

def fetch_usernames(self, users):
"""
Expand All @@ -33,6 +35,9 @@ def get_favorite(self, obj):
article_fav_users = obj.favorite.all()
return self.fetch_usernames(article_fav_users)

def get_share_article_links(self, obj):
return share_articles_links(obj, self.context['request'])


class ArticleDetailSerializer(serializers.ModelSerializer):
author = ProfileSerializer(required=False)
Expand Down
17 changes: 17 additions & 0 deletions authors/apps/articles/tests/test_articles.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,20 @@ def test_favorite_list(self):
response1 = self.client.post(f'/api/articles/{article_slug}/favorite', None, HTTP_AUTHORIZATION=user_token, format='json')
response2 = self.client.get(f'/api/users/articles/favorites', HTTP_AUTHORIZATION=user_token, format='json')
self.assertEqual(response2.status_code, status.HTTP_200_OK)


def test_article_contains_share_links(self):
"""
Test article contains share links
"""
user_token = self.create_user(test_data.test_user_data)
resp = self.client.post('/api/articles/', article_data, HTTP_AUTHORIZATION=user_token, format='json')
response = self.client.get('/api/articles/', format='json')
self.assertEqual(response.data["results"][0]["share_article_links"]["facebook"],
'https://www.facebook.com/sharer/sharer.php?u=http%3A//testserver/api/articles/hello-slug-first')
self.assertEqual(response.data["results"][0]["share_article_links"]["twitter"],
'https://twitter.com/home?status=http%3A//testserver/api/articles/hello-slug-first')
self.assertEqual(response.data["results"][0]["share_article_links"]["googleplus"],
'https://plus.google.com/share?url=http%3A//testserver/api/articles/hello-slug-first')
self.assertEqual(response.data["results"][0]["share_article_links"]["email"],
'mailto:?&subject=hello%20slug&body=hello%20slug%0A%0Ahttp%3A//testserver/api/articles/hello-slug-first')
14 changes: 12 additions & 2 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@ class ListCreateArticle(generics.ListCreateAPIView):
serializer_class = serializers.ArticleSerializer
pagination_class = ArticlePageNumberPagination

def get_serializer_context(self):
"""
Extra context provided to the serializer class.
"""
return {
'request': self.request
}

def create(self, request):
article = request.data

serializer = self.serializer_class(data=article)
serializer = self.serializer_class(data=article, context={'request':self.request})

author = Profile.objects.get(username=self.request.user.username)
serializer.is_valid(raise_exception=True)
Expand All @@ -53,6 +61,8 @@ class RetrieveUpdateDestroyArticle(generics.RetrieveUpdateDestroyAPIView):
serializer_class = serializers.ArticleSerializer
lookup_field = 'slug'




class RetrieveArticleCommentDetails(generics.RetrieveAPIView):
"""
Expand Down Expand Up @@ -232,5 +242,5 @@ def post(self, request, *args, **kwargs):
returned_status = status.HTTP_201_CREATED

article.save()
serializer = self.serializer_class(article)
serializer = self.serializer_class(article, context={'request':self.request})
return Response(serializer.data, status=returned_status)
2 changes: 1 addition & 1 deletion authors/apps/comments/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class CommentSerializer(ModelSerializer):
class Meta:
model = Comment
fields = '__all__'
read_only_fields =('id', 'parent', 'reply_count', 'createdAt', 'author')
read_only_fields = ('id', 'parent', 'reply_count', 'createdAt', 'author')

def get_reply_count(self, obj):
if obj.is_parent:
Expand Down
Empty file.
34 changes: 34 additions & 0 deletions authors/apps/helpers/share_articles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import urllib.parse
from django.urls import reverse
from django.conf import settings


def share_articles_links(obj, request):
"""
Generates sharing links for popular plaforms
like facebook, googleplus, twitter and email
Args:
article_title(String): title of article to share
article_link(String): http link to article
returns
share_links(dict): collection of sharing links
"""
share_links = {}
article_title = obj.title
article_link = f'{settings.BASE_ARTICLE_SHARE_URL}/{obj.slug}' if settings.BASE_ARTICLE_SHARE_URL else \
request \
.build_absolute_uri(reverse('articles:auth-articles', \
kwargs={'slug': obj.slug}))

encoded_article_link = urllib.parse.quote(article_link)
encoded_article_title = urllib.parse.quote(article_title)

share_links['facebook'] = 'https://www.facebook.com/sharer/sharer.php?u=' + encoded_article_link
share_links['twitter'] = 'https://twitter.com/home?status=' + encoded_article_link
share_links['googleplus'] = 'https://plus.google.com/share?url=' + encoded_article_link
share_links['email'] = 'mailto:?&subject=' + encoded_article_title + '&body=' \
+ encoded_article_title + '%0A%0A' + encoded_article_link
return share_links
2 changes: 2 additions & 0 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,5 @@

WORD_LENGTH = config('WORD_LENGTH')
WORD_PER_MINUTE = config('WORD_PER_MINUTE')

BASE_ARTICLE_SHARE_URL= config('BASE_ARTICLE_SHARE_URL', False)

0 comments on commit a41f1c5

Please sign in to comment.