Skip to content

Commit

Permalink
feat(user notification): user should be able to recieve notification
Browse files Browse the repository at this point in the history
- create app notifications
- create directory tests
- update urls.py
- make migrations
- add tests
- update migrations
- add get notification on new follow
- add get notification on like or dislike

[finishes#165273483]
  • Loading branch information
3Nakajugo committed May 15, 2019
1 parent 227ddb2 commit a460414
Show file tree
Hide file tree
Showing 20 changed files with 568 additions and 22 deletions.
2 changes: 1 addition & 1 deletion authors/apps/articles/fixtures/article.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"dislikes_count": 0,
"favorited": false,
"favorite_count": 0,
"author_id": "jeanmarcus"
"author": "jeanmarcus"
}
}
]
17 changes: 17 additions & 0 deletions authors/apps/articles/tests/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,23 @@ def setUp(self):
super_test_token = self.super_login_response.data['token']
self.super_auth_header = 'Bearer {}'.format(super_test_token)

self.user3 = User.objects.create_superuser(
username="edina", email="edina@gmail.com", password="Edina1234")
setattr(self.user3, 'email_verified', True)
self.user3.save()

self.edna_login_data = {
"user": {
"email": "edina@gmail.com",
"password": "Edina1234"
}
}

self.edna_login_response = self.client.post(
self.login_url, self.edna_login_data, format='json')
edna_test_token = self.edna_login_response.data['token']
self.edna_auth_header = 'Bearer {}'.format(edna_test_token)

self.create_article_data = {
"title": "Fresh kid wonders on stage at lugogo",
"description": "he wows the kids",
Expand Down
40 changes: 23 additions & 17 deletions authors/apps/articles/tests/test_article_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ class ArticleCrudTest(BaseTestCase):
Class Test Case for Testing article crud functionality
"""
# Initialize fixture for the class Test Case
fixtures = ['authors/apps/articles/fixtures/article.json',
'authors/apps/articles/fixtures/user.json']
fixtures = ['authors/apps/articles/fixtures/user.json']


def test_user_create_article(self):
"""
Expand Down Expand Up @@ -74,12 +74,15 @@ def test_only_one_article_created(self):
url = reverse('articles-list-create')
response = self.client.post(
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
self.assertEqual(Article.objects.count(), 2)
self.assertEqual(Article.objects.count(), 1)

def test_get_all_articles(self):
"""
Method tests for getting all articles
"""
url = reverse('articles-list-create')
response = self.client.post(
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
url = '/api/articles/all/'
response = self.client.get(
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
Expand All @@ -89,7 +92,11 @@ def test_get_one_article(self):
"""
Method tests for getting one article
"""
url = '/api/articles/1/'
url = reverse('articles-list-create')
response = self.client.post(
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
article_id = response.data['article']['id']
url = '/api/articles/{}/'.format(article_id)
response = self.client.get(
url, HTTP_AUTHORIZATION=self.auth_header, format="json")
self.assertEqual(response.status_code, status.HTTP_200_OK)
Expand All @@ -98,19 +105,27 @@ def test_no_permision_to_update_article(self):
"""
Method tests for permision for updating one article
"""
url = '/api/articles/1/'
url = reverse('articles-list-create')
response = self.client.post(
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
article_id = response.data['article']['id']
url = '/api/articles/{}/'.format(article_id)
response = self.client.put(
url, self.update_article, HTTP_AUTHORIZATION=self.auth_header, format="json")
url, self.update_article, HTTP_AUTHORIZATION=self.edna_auth_header, format="json")
self.assertIn(
"You do not have permission to perform this action", str(response.data))

def test_no_permision_to_delete_article(self):
"""
Method tests for permision for deleting one article
"""
url = '/api/articles/1/'
url = reverse('articles-list-create')
response = self.client.post(
url, self.create_article_data, HTTP_AUTHORIZATION=self.auth_header, format="json")
article_id = response.data['article']['id']
url = '/api/articles/{}/'.format(article_id)
response = self.client.delete(
url, HTTP_AUTHORIZATION=self.auth_header, format="json")
url, self.update_article, HTTP_AUTHORIZATION=self.edna_auth_header, format="json")
self.assertEqual(
'You do not have permision to delete the article', str(response.data))

Expand All @@ -123,15 +138,6 @@ def test_get_article_wrong_id(self):
url, self.create_article_data, format="json")
self.assertIn("That article is not found", str(response.data))

def test_get_article(self):
"""
Method tests for getting one article with the wrong id
"""
url = '/api/articles/1/retrieve/'
response = self.client.get(
url, self.create_article_data, format="json")
self.assertEqual(200, response.status_code)

def test_user_create_article_blank_body(self):
"""
Method tests creating the article with blank body
Expand Down
12 changes: 8 additions & 4 deletions authors/apps/articles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,10 @@ def post(self, request, **kwargs):
return Response(
{"message": "Your like has been revoked"}, status=status.HTTP_200_OK,)
elif my_dislike_already:
Like_Dislike.objects.filter(
article_id=kwargs['article_id'], reviewer_id=request.user.username).update(like_or_dislike=1)
like_dislike = Like_Dislike.objects.filter(
article_id=kwargs['article_id'], reviewer_id=request.user.username).first()
like_dislike.like_or_dislike = 1
like_dislike.save()
Article.objects.filter(
id=kwargs['article_id']).update(
likes_count=article.likes_count + 1, dislikes_count=article.dislikes_count - 1)
Expand Down Expand Up @@ -239,8 +241,10 @@ def post(self, request, **kwargs):
return Response(
{"message": "Your dislike has been revoked"}, status=status.HTTP_200_OK,)
elif my_like_already:
Like_Dislike.objects.filter(
article_id=kwargs['article_id'], reviewer_id=request.user.username).update(like_or_dislike=-1)
like_dislike = Like_Dislike.objects.filter(
article_id=kwargs['article_id'], reviewer_id=request.user.username).first()
like_dislike.like_or_dislike = -1
like_dislike.save()
Article.objects.filter(
id=kwargs['article_id']).update(
likes_count=article.likes_count - 1, dislikes_count=article.dislikes_count + 1)
Expand Down
Empty file.
3 changes: 3 additions & 0 deletions authors/apps/notifications/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.contrib import admin

# Register your models here.
5 changes: 5 additions & 0 deletions authors/apps/notifications/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.apps import AppConfig


class NotificationsConfig(AppConfig):
name = 'notifications'
41 changes: 41 additions & 0 deletions authors/apps/notifications/fixtures/notification_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[
{
"model": "authentication.user",
"pk": 2,
"fields": {
"username": "ednan",
"email": "3dnamargarita@gmail.com",
"email_verified": true,
"password": "pbkdf2_sha256$120000$hnVeYt1oA4Z9$F64DTDJ9ChmjxhMvZbJm0WGYmzpqg2+7oib+ZE5MV8E=",
"created_at": "2013-03-23 00:03:00",
"updated_at": "2013-03-23 01:02:00"
}
},
{
"model": "articles.article",
"pk": 1,
"fields": {
"title": "Town hall at Andela",
"description": "There was lots of fun",
"body": "There was no TIA chant.",
"slug": "town-hall-at-andela",
"created_at": "2019-05-02T05:03:36.898328Z",
"updated_at": "2019-05-02T05:03:36.898341Z",
"favorited": false,
"favorite_count": 0,
"author": "ednan"
}
},
{
"model": "authentication.user",
"pk": 1,
"fields": {
"username": "joell",
"email": "joell@gmail.com",
"email_verified": true,
"password": "pbkdf2_sha256$120000$hnVeYt1oA4Z9$F64DTDJ9ChmjxhMvZbJm0WGYmzpqg2+7oib+ZE5MV8E=",
"created_at": "2013-03-23 00:03:00",
"updated_at": "2013-03-23 01:02:00"
}
}
]
27 changes: 27 additions & 0 deletions authors/apps/notifications/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 2.1 on 2019-05-09 07:37

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='UserNotification',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('message', models.CharField(max_length=255, null=True)),
('link', models.CharField(max_length=255, null=True)),
('created_at', models.DateTimeField(auto_now_add=True)),
('reciever', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='username')),
],
),
]
24 changes: 24 additions & 0 deletions authors/apps/notifications/migrations/0002_notificationsetting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 2.1 on 2019-05-10 01:30

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('notifications', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='NotificationSetting',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('get_notification', models.BooleanField(default=True)),
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL, to_field='username')),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 2.1 on 2019-05-14 11:17

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('notifications', '0002_notificationsetting'),
]

operations = [
migrations.AddField(
model_name='usernotification',
name='read',
field=models.BooleanField(default=False),
),
]
Empty file.
Loading

0 comments on commit a460414

Please sign in to comment.