Skip to content

Commit

Permalink
Merge pull request #32 from andela/ft-share-articles-across-diff-chan…
Browse files Browse the repository at this point in the history
…nels-#161967028

#161967028 Share articles to other social sites
  • Loading branch information
kipropbrian committed Dec 21, 2018
2 parents af0c9d6 + 7e4630d commit 8c55917
Show file tree
Hide file tree
Showing 12 changed files with 217 additions and 70 deletions.
3 changes: 1 addition & 2 deletions authors/apps/articles/tests/endpoints/test_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ def test_wrong_request(self):
'articleSpecific', kwargs={
'slug': 'life_love_death_live'})
response = self.client.get(url)
self.assertNotEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

response.render()
self.assertIn(b"Article does not exist", response.content)
Expand Down
3 changes: 1 addition & 2 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ def get_paginated_response(self, data):
assert self.paginator is not None
return Response({'message': 'cannot return paginated data'})


@staticmethod
def post(request, **kwargs):
"""Create a new article"""
Expand Down Expand Up @@ -86,7 +85,7 @@ def get(request, slug):
except Exception as error:
print('Received error is : {}'.format(error))
return Response({"message": "Article does not exist"},
status.HTTP_400_BAD_REQUEST)
status.HTTP_404_NOT_FOUND)

@staticmethod
def put(request, slug):
Expand Down
Empty file.
Empty file.
Empty file.
Empty file.
132 changes: 132 additions & 0 deletions authors/apps/share_article/tests/test_share.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import json

from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase
from django.contrib.auth import get_user_model

from authors.apps.articles.models import Articles
from authors.apps.authentication.models import User


class TestShareEndpoint(APITestCase):

def setUp(self):

self.token = self.get_user_token()

self.data = {
"slug": "posting_test",
"title": "Posting Test",
"description": "this is a posting test",
"body": "The test was successful",
"tagList": "live again",
"author": "TestAuthor"
}
self.all_setup()

def test_get_facebook_link(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
self.client.post(url, self.data, format='json')
link_url = reverse(
'share_article',
kwargs={
'slug': 'life-love-death',
'provider': 'facebook'})
response = self.client.get(link_url, format='json')
self.assertEqual(response.data['provider'], 'facebook')

def test_get_twitter_link(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
self.client.post(url, self.data, format='json')
link_url = reverse(
'share_article',
kwargs={
'slug': 'life-love-death',
'provider': 'twitter'})
response = self.client.get(link_url, format='json')
self.assertEqual(response.data['provider'], 'twitter')

def test_get_email_link(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
self.client.post(url, self.data, format='json')
link_url = reverse(
'share_article',
kwargs={
'slug': 'life-love-death',
'provider': 'email'})
response = self.client.get(link_url, format='json')
self.assertEqual(response.data['provider'], 'email')

def test_using_unexisting_article_slug(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
self.client.post(url, self.data, format='json')
link_url = reverse(
'share_article',
kwargs={
'slug': 'life-love',
'provider': 'email'})
response = self.client.get(link_url, format='json')
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
self.assertEqual(response.data['message'], 'Article does not exist')

def test_using_invalid_provider(self):

self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
self.client.post(url, self.data, format='json')
link_url = reverse(
'share_article',
kwargs={
'slug': 'life-love-death',
'provider': 'reddit'})
response = self.client.get(link_url, format='json')
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertEqual(response.data['error'], 'provider link is invalid')

def all_setup(self):

self.slug = "life-love-death"
self.title = "Life Love and Death"
self.description = "What is life?"
self.body = "This is the real life body."
self.tagList = "life,love,death"
self.favorited = True
self.favoritesCount = 4
self.author = 'TestAuthor'

self.article = Articles(
slug=self.slug,
title=self.title,
description=self.description,
body=self.body,
tagList=self.tagList,
favorited=self.favorited,
favoritesCount=self.favoritesCount,
author=User.objects.get(username=self.author))

self.article.save()

def get_user_token(self):
user = {
"user": {
"username": "TestAuthor",
"email": "test_user@email.com",
"password": "test123user#Password"
}
}

response = self.client.post(
reverse('register'), data=user, format='json')
user = get_user_model()
user = user.objects.get(username="TestAuthor")
user.is_active = True
user.save()
response.render()
data = response.content
token = json.loads(data.decode('utf-8'))['user']['token']
return token
9 changes: 9 additions & 0 deletions authors/apps/share_article/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.urls import path
from .views import ShareArticleView


urlpatterns = [
path('articles/<str:slug>/share/<str:provider>',
ShareArticleView.as_view(),
name='share_article'),
]
68 changes: 68 additions & 0 deletions authors/apps/share_article/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
from django.urls import reverse
from django_social_share.templatetags import social_share

from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from authors.apps.articles.models import Articles
from authors.apps.authentication.models import User


class ShareArticleView(APIView):
permission_classes = (IsAuthenticated,)

def get(self, request, *args, **kwargs):

provider = kwargs['provider']
example_providers = ['facebook', 'email', 'twitter']

if provider not in example_providers:
return Response(
{
'error': 'provider link is invalid',
}, status.HTTP_400_BAD_REQUEST)

try:
article = Articles.objects.get(slug=kwargs['slug'])

except Exception as error:
print('Received error is : {}'.format(error))
return Response({"message": "Article does not exist"},
status.HTTP_404_NOT_FOUND)

article_link = request.build_absolute_uri(
reverse('articleSpecific', kwargs={'slug': article.slug})
)

context = {'request': request}
text = "Daily Digest from Authors Haven"
user = User.objects.get(id=article.author_id).username
subject = "Read: {} by {} from Authors Haven".format(
article.title,
user)
if provider == 'email':
link = social_share.send_email_url(
context,
subject,
text,
article_link)['mailto_url']
return Response(
{'link': link, 'provider': provider},
status=status.HTTP_200_OK)
return Response(
{
'link': self.set_link(
context,
provider,
article_link, args),
'provider': provider}, status.HTTP_200_OK)

def set_link(self, context, provider, article_link, *args):
providers = {
"twitter": [social_share.post_to_twitter_url, "tweet_url"],
"facebook": [social_share.post_to_facebook_url, "facebook_url"]
}
provider_link = providers.get(provider, providers['facebook'])
return provider_link[0](context, article_link)[provider_link[1]]
2 changes: 2 additions & 0 deletions authors/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
'django_filters',
'haystack',
'drf_haystack',
'django_social_share',

# External apps
'authors.apps.authentication',
Expand All @@ -70,6 +71,7 @@
'authors.apps.favourite',
'authors.apps.bookmark',
'authors.apps.ratings',
'authors.apps.share_article',
]

MIDDLEWARE = [
Expand Down
3 changes: 3 additions & 0 deletions authors/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,7 @@
path(
'api/',
include('authors.apps.ratings.urls')),
path(
'api/',
include('authors.apps.share_article.urls'))
]
67 changes: 1 addition & 66 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ django-filter==2.0.0
django-haystack==2.8.1
django-pytest==0.2.0
django-rest-swagger==2.1.0
django-social-share==1.3.0
djangorestframework==3.9.0
djangorestframework-jwt==1.11.0
docopt==0.6.2
Expand Down Expand Up @@ -65,69 +66,3 @@ urllib3==1.24.1
whitenoise==4.1.2
Whoosh==2.7.4
wrapt==1.10.11
astroid==2.1.0
atomicwrites==1.2.1
attrs==18.2.0
autopep8==1.4.3
certifi==2018.11.29
chardet==3.0.4
colorclass==2.2.0
coreapi==2.3.3
coreschema==0.0.4
coverage==4.5.2
coveralls==1.5.1
dj-database-url==0.5.0
Django==2.1.4
django-cors-middleware==1.3.1
django-environ==0.4.5
django-extensions==2.1.4
django-filter==2.0.0
django-haystack==2.8.1
django-pytest==0.2.0
django-rest-swagger==2.1.0
djangorestframework==3.9.0
djangorestframework-jwt==1.11.0
docopt==0.6.2
drf-haystack==1.8.4
factory-boy==2.11.1
Faker==1.0.1
flake8==3.6.0
gunicorn==19.9.0
idna==2.8
inflection==0.3.1
isort==4.3.4
itypes==1.1.0
Jinja2==2.10
jsonschema==2.6.0
lazy-object-proxy==1.3.1
MarkupSafe==1.1.0
mccabe==0.6.1
more-itertools==4.3.0
openapi==1.1.0
openapi-codec==1.3.2
packaging==18.0
pluggy==0.8.0
psycopg2-binary==2.7.6.1
py==1.7.0
pycodestyle==2.4.0
pyflakes==2.0.0
PyJWT==1.7.1
pylint==2.2.2
pylint-django==2.0.4
pylint-plugin-utils==0.4
pyparsing==2.3.0
pytest==4.0.1
pytest-cov==2.6.0
pytest-django==3.4.4
python-dateutil==2.7.5
pytz==2018.7
requests==2.21.0
simplejson==3.16.0
six==1.12.0
terminaltables==3.1.0
text-unidecode==1.2
uritemplate==3.0.0
urllib3==1.24.1
whitenoise==4.1.2
Whoosh==2.7.4
wrapt==1.10.11

0 comments on commit 8c55917

Please sign in to comment.