Skip to content

Commit

Permalink
Merge f2bd2fd into c287e80
Browse files Browse the repository at this point in the history
  • Loading branch information
tesh254 committed Feb 13, 2019
2 parents c287e80 + f2bd2fd commit 4a5d395
Show file tree
Hide file tree
Showing 24 changed files with 406 additions and 62 deletions.
4 changes: 0 additions & 4 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ class Article(models.Model):

objects = models.Manager()

def __str__(self):
return self.title


class Tags(models.Model):
tag = models.CharField(max_length=120)

Expand Down
1 change: 1 addition & 0 deletions authors/apps/articles/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ def render(self, data, media_type=None, renderer_context=None):
return json.dumps({
'articles': data
})

2 changes: 1 addition & 1 deletion authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.template.defaultfilters import slugify
from django.contrib.contenttypes.models import ContentType

from ..authentication.serializers import RegistrationSerializer
from authors.apps.authentication.serializers import RegistrationSerializer
from .messages import error_msgs
from .models import Article, Tags
from ..authentication.serializers import RegistrationSerializer
Expand Down
7 changes: 6 additions & 1 deletion authors/apps/authentication/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from django.contrib.auth import authenticate
from django.core.exceptions import ValidationError
from rest_framework import serializers

from .backends import JWTokens
from .models import User
from .validations import UserValidation
from authors.apps.favorite.serializers import FavoriteSerializer


class RegistrationSerializer(serializers.ModelSerializer):
Expand All @@ -21,12 +23,15 @@ class RegistrationSerializer(serializers.ModelSerializer):
# The client should not be able to send a token along with a registration
# request. Making `token` read-only handles that for us.
token = serializers.CharField(read_only=True)
favorites = FavoriteSerializer(many=True, read_only=True, required=False)

class Meta:
model = User
# List all of the fields that could possibly be included in a request
# or response, including fields specified explicitly above.
fields = ['email', 'username', 'password', 'token']
fields = (
"username", "email", "favorites", "password", "token"
)

def validate(self, data):
"""Validates the data"""
Expand Down
22 changes: 6 additions & 16 deletions authors/apps/authentication/tests/test_password_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,17 @@ def setUp(self):
}
self.user_email = [
{
"user": {
"email": ""
}
},
"email": ""
},
{
"user": {
"email": "newuser@gmail.com"
}
},
},
{
"user": {
"email": "alpha@andela"
}
},
{
"user": {
},
{
"email": "alphaandela@gmail.com"
}
},
},
]

def user_registration(self):
Expand All @@ -56,10 +48,8 @@ def get_email(self):
"""Get email of the registered user"""
email = self.user_registration().data['email']
mail = {
"user": {
"email": email
}
}
return mail

def submit_email(self, data):
Expand Down
30 changes: 15 additions & 15 deletions authors/apps/authentication/tests/test_social_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,18 @@ def setUp(self):
"access_token": "invalid-token"
}

def test_social_auth_api(self):
facebookresponse = self.client.post(
self.social_auth_url, self.test_social_body, format='json')
twitterresponse = self.client.post(
self.social_auth_url, self.test_twitter_body, format='json')
invalidtoken = self.client.post(
self.social_auth_url, self.test_invalid_token, format='json')
invalidprovider = self.client.post(
self.social_auth_url, self.test_invalid_provider, format='json')

self.assertEqual(facebookresponse.status_code, status.HTTP_201_CREATED)
self.assertEqual(twitterresponse.status_code, status.HTTP_201_CREATED)
self.assertTrue(invalidtoken.status_code, status.HTTP_400_BAD_REQUEST)
self.assertTrue(invalidprovider.status_code,
status.HTTP_400_BAD_REQUEST)

# def test_social_auth_api(self):
# facebookresponse = self.client.post(self.social_auth_url, self.test_social_body, format='json')
# twitterresponse = self.client.post(self.social_auth_url, self.test_twitter_body, format='json')
# googleresponse =self.client.post(self.social_auth_url, self.test_google_body, format='json')
# invalidtoken = self.client.post(self.social_auth_url, self.test_invalid_token, format= 'json')
# invalidprovider = self.client.post(self.social_auth_url, self.test_invalid_provider, format='json')

# # self.assertEqual(facebookresponse.status_code, status.HTTP_201_CREATED)
# # self.assertEqual(twitterresponse.status_code, status.HTTP_201_CREATED)
# # self.assertEqual(googleresponse.status_code, status.HTTP_201_CREATED)
# # self.assertTrue(invalidtoken.status_code, status.HTTP_400_BAD_REQUEST)
# # self.assertTrue(invalidprovider.status_code, status.HTTP_400_BAD_REQUEST)


2 changes: 1 addition & 1 deletion authors/apps/authentication/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class PasswordResetRequestAPIView(GenericAPIView):
)
def post(self, request):
user_data = request.data
email = user_data['user']['email']
email = user_data['email']

# confirms if an eamil has been provided
# if email is not given then an error message is thrown
Expand Down
Empty file.
9 changes: 9 additions & 0 deletions authors/apps/favorite/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.contrib import admin
from authors.apps.favorite import models

# Register your models here.

admin.site.register(models.Favorite)
Empty file added authors/apps/favorite/apps.py
Empty file.
8 changes: 8 additions & 0 deletions authors/apps/favorite/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error_msgs = {
"article_favorited":"This article is in your favorites",
"favorite_not_found":"That article is not in your favorites"
}

success_msg = {
"favorite_deleted":"Removed article from your favorites"
}
Empty file.
17 changes: 17 additions & 0 deletions authors/apps/favorite/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.db import models
from cloudinary.models import CloudinaryField

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


class Favorite(models.Model):
"""
Favorite model
"""
article_url = models.TextField(null=True)
article_title = models.CharField(max_length=255, null=True)
article_slug = models.CharField(max_length=255, null=True)
user = models.ForeignKey(
User, related_name="favorites", on_delete=models.CASCADE)
favorited_date = models.DateTimeField(auto_now_add=True)
55 changes: 55 additions & 0 deletions authors/apps/favorite/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from rest_framework import serializers
from django.contrib.sites.shortcuts import get_current_site
from django.db.models import Q

from authors.apps.favorite.models import Favorite


class FavoriteSerializer(serializers.ModelSerializer):
"""
Favorite model serializer
"""
article_url = serializers.CharField(required=True)

class Meta:
model = Favorite
fields = "__all__"

def create_article_url(self, request, article_slug):
"""
Create the article url
"""
base_url = "http://" + get_current_site(request).domain + "/"
article_url = base_url + "api/v1/articles/" + article_slug + "/"
return article_url

def check_article(self, slug, user_id):
"""
Check if that article was favorited
"""
favorite = Favorite.objects.filter(
Q(article_slug=slug) & Q(user=user_id)
)
if favorite:
return False
else:
return True

def get_that_one_favorite(self, slug, user_id):
"""
Get one article from favorites
"""
favorite = Favorite.objects.filter(
Q(article_slug=slug) & Q(user=user_id)
)
if favorite:
return favorite
else:
return False

def get_all_favorites(self, user):
"""
Get all articles
"""
favorites = Favorite.objects.filter(user=user).all()
return favorites
Empty file.
Loading

0 comments on commit 4a5d395

Please sign in to comment.