Skip to content

Commit

Permalink
Merge 2cfd55d into 1cbad8f
Browse files Browse the repository at this point in the history
  • Loading branch information
GransonO authored Dec 9, 2018
2 parents 1cbad8f + 2cfd55d commit 65b77a8
Show file tree
Hide file tree
Showing 24 changed files with 856 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@ ENV/
# SQLite3
db.sqlite3

# Migrations
authors/apps/*/migrations/*
.DS_Store

# vscode cache files and log files
.vscode/
logfile
File renamed without changes.
7 changes: 7 additions & 0 deletions authors/apps/articles/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.contrib import admin
from .models import Profiles, Articles

# Register your models here.

admin.site.register(Profiles)
admin.site.register(Articles)
5 changes: 5 additions & 0 deletions authors/apps/articles/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class ArticlesConfig(AppConfig):
name = 'articles'
34 changes: 34 additions & 0 deletions authors/apps/articles/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from django.db import models

# Create your models here.


class Profiles(models.Model):
username = models.CharField(max_length=50)
bio = models.CharField(max_length=150)
image = models.CharField(max_length=500)
following = models.CharField(max_length=10)

def __str__(self):
""" String representation of db object """
return ' {}: {}'.format(self.id, self.username)


class Articles(models.Model):
""" Model for all articles """
slug = models.SlugField(max_length=250, default='non')
title = models.CharField(max_length=50, default='non')
description = models.CharField(max_length=250, default='non')
body = models.CharField(max_length=550, default='non')
tagList = models.CharField(max_length=50,
default='non') # ["dragons", "training"],
createdAt = models.DateTimeField(auto_now_add=True)
updatedAt = models.DateTimeField(auto_now=True)
favorited = models.BooleanField(default=False)
favoritesCount = models.IntegerField(default=0)
readtime = models.IntegerField(default=0)
author = models.ForeignKey(Profiles, default=0, on_delete=models.CASCADE)

def __str__(self):
""" String representation of db object """
return ' {}: {}'.format(self.id, self.slug)
143 changes: 143 additions & 0 deletions authors/apps/articles/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
from rest_framework import serializers
from .models import Articles, Profiles


class ArticleSerializer(serializers.ModelSerializer):

class Meta:
model = Articles
fields = ("__all__")

@staticmethod
def get_all_objects():
articles = Articles.objects.all()
profiles = AuthorSerializer()

articles_list = []
for article in articles:
article = {
"slug": article.slug,
"title": article.title,
"description": article.description,
"body": article.body,
"tagList": article.tagList.split(","),
"createdAt": article.createdAt,
"updatedAt": article.updatedAt,
"favorited": article.favorited,
"favoritesCount": article.favoritesCount,
"author": profiles.get_all_objects(article.author.pk)
}
articles_list.append(article)

return (articles_list)

def get_specific_objects(self, slug):
self.article = Articles.objects.get(slug=slug)
profiles = AuthorSerializer()

article = {
"slug": self.article.slug,
"title": self.article.title,
"description": self.article.description,
"body": self.article.body,
"tagList": self.article.tagList.split(","),
"createdAt": self.article.createdAt,
"updatedAt": self.article.updatedAt,
"favorited": self.article.favorited,
"favoritesCount": self.article.favoritesCount,
"author": profiles.get_all_objects(self.article.author.pk)
}

return (article)

def posting_articles(self, value): # dictionary passed from view
author_details = self.get_authors_object(
value['author']) # get user object
article_slug = self.slugify_string(value['title'])
read_time = self.readTime(value['body'])

self.new_article = Articles(
slug=article_slug,
title=value['title'],
description=value['description'],
body=value['body'],
tagList=value['tagList'],
author=author_details,
readtime=read_time)

try:
self.new_article.save()
return (self.get_all_objects())

except Exception as What_is_this:
print('error {}'.format(What_is_this))
return ("Could not post")

def get_authors_object(self, name):
user_details = Profiles.objects.get(username=name)
return user_details

def slugify_string(self, string):
string = string.lower()
processed_slug = string.replace(" ", "-")

db_check = Articles.objects.filter(slug=processed_slug).count()
if db_check < 1: # Slug exists in DB
return processed_slug

else:
new_slug = '{}*'.format(processed_slug)
return self.slugify_string(new_slug)

def readTime(self, story):
story_list = story.split(" ")
resolved_time = (len(story_list)) / 200
read_time = round(resolved_time)
return read_time

def updateArticle(self, value, passed_slug):
try:
Articles.objects.filter(
slug=passed_slug).update(
slug=self.slugify_string(
value['title']),
title=value['title'],
description=value['description'],
body=value['body'],
tagList=value['tagList'],
readtime=self.readTime(
value['body']))
return ('Update was successful, Title: {}'.format(value['title']))

except Exception as What_is_this:
print('Received error is : {}'.format(What_is_this))
return ('error')

def deleteArticle(self, passed_slug):
try:
deleted_article = Articles.objects.get(slug=passed_slug)
title = deleted_article.title
deleted_article.delete()
return('Article title: {} deleted successfully'.format(title))

except Exception as What_is_this:
print('Received error is : {}'.format(What_is_this))
return ("Article does not exist")


class AuthorSerializer(serializers.ModelSerializer):

class Meta:
model = Profiles
fields = ("__all__")

def get_all_objects(self, id):
profile = Profiles.objects.get(pk=id)

author = {
'username': profile.username,
'bio': profile.bio,
'image': profile.image,
'following': profile.following
}
return (author)
74 changes: 74 additions & 0 deletions authors/apps/articles/tests/endpoints/test_delete.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APITestCase

import json
from authors.apps.articles.models import Articles, Profiles


class TestGetEndpoint(APITestCase):

def setUp(self):
self.profile = Profiles(
username='TestAuthor',
bio='This is my bio',
image='Thi is image url',
following=1)
self.profile.save()

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=True,
favoritesCount=self.favoritesCount,
author=Profiles.objects.get(username=self.author))
self.article.save()

self.token = self.get_user_token()

def test_delArticle_status(self):
url = reverse('articleSpecific', kwargs={'slug': 'life-love-death'})
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
response = self.client.delete(url)
response.render()
self.assertIn(
b'Article title: Life Love and Death deleted successfully',
response.content)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_wrong_status(self):
url = reverse('articleSpecific', kwargs={'slug': 'life-love'})
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
response = self.client.delete(url)
response.render()
self.assertIn(b'Article does not exist', response.content)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)

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')
response.render()
data = response.content
token = json.loads(data.decode('utf-8'))['user']['token']
return token
# Token returned after login
104 changes: 104 additions & 0 deletions authors/apps/articles/tests/endpoints/test_get.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import json

from rest_framework.test import APITestCase
from django.urls import reverse
from rest_framework import status

from authors.apps.articles.models import Articles, Profiles


class TestGetEndpoint(APITestCase):

def setUp(self):
""" Prepares table for tests """
self.profile = Profiles(
username='TestAuthor',
bio='Thi is my bio',
image='Thi is image url',
following=1)
self.profile.save()

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=Profiles.objects.get(username=self.author))
self.article.save()

self.token = self.get_user_token()

def test_getArticle_status(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articles')
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

# Test for specific article calls

def test_getArticle_content(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articles')
response = self.client.get(url)

response.render()
self.assertIn(b"life_love_death", response.content)
self.assertIn(b"Life Love and Death", response.content)
self.assertIn(b"What is life?", response.content)
self.assertIn(b"This is the real life body.", response.content)
self.assertIn(b"[\"life\",\"love\",\"death\"]", response.content)
self.assertIn(b"4", response.content)

def test_get_specific_article(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse('articleSpecific', kwargs={'slug': 'life_love_death'})
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)

response.render()
self.assertIn(b"life_love_death", response.content)
self.assertIn(b"Life Love and Death", response.content)
self.assertIn(b"What is life?", response.content)
self.assertIn(b"This is the real life body.", response.content)
self.assertIn(b"[\"life\",\"love\",\"death\"]", response.content)
self.assertIn(b"4", response.content)

def test_wrong_request(self):
self.client.credentials(HTTP_AUTHORIZATION='Token ' + self.token)
url = reverse(
'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)

response.render()
self.assertIn(b"Article does not exist", response.content)

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')
response.render()
data = response.content
token = json.loads(data.decode('utf-8'))['user']['token']
return token
Loading

0 comments on commit 65b77a8

Please sign in to comment.