-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(push notification):send notification
- add follow model for user follower - add notification app - add notifications on installed apps - add notifications view [finishes #161254670]
- Loading branch information
Gidraf
committed
Nov 19, 2018
1 parent
615ace6
commit b64acfa
Showing
25 changed files
with
728 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
from ..profiles.models import UserProfile, Follow | ||
from django.template.loader import render_to_string | ||
from django.core.mail import send_mail | ||
from .models import Article, Favorite | ||
from authors.settings import DEFAULT_DOMAIN, EMAIL_HOST_USER | ||
from rest_framework.generics import CreateAPIView | ||
from .serializers import RateArticleSerializer | ||
from .models import RateArticle | ||
from rest_framework import status | ||
from rest_framework.response import Response | ||
from ..authentication.renderers import UserJSONRenderer | ||
from rest_framework.permissions import IsAuthenticated | ||
from ..notifications.models import NotificationsFollower, NotificationsArticle | ||
|
||
|
||
def send_article_notification(slug, data): | ||
""" | ||
send email to article favourites | ||
""" | ||
try: | ||
article = Article.objects.get(slug=slug) | ||
subscribers = Favorite.objects.filter(article=article) | ||
for subscriber in subscribers: | ||
message = request.user.username + " commented on this article " | ||
link = DEFAULT_DOMAIN + '/api/article/detail/' + \ | ||
article.slug | ||
msg = render_to_string('article_alert.html', { | ||
'user': subscriber.user.username, | ||
"link": link, | ||
"message": message, | ||
"comment": data["body"] | ||
}) | ||
send_mail('Authors Heaven New Article!', | ||
'New Comment!!!!', | ||
EMAIL_HOST_USER, | ||
[subscriber.user.email], | ||
html_message=msg, | ||
fail_silently=False) | ||
NotificationsArticle( | ||
subscriber_id=subscriber.pk, | ||
article_id=article.pk).save() | ||
return True | ||
except Exception as error: | ||
return False | ||
|
||
|
||
def send_followers_notification(instance, user): | ||
""" | ||
send email to user following the author | ||
""" | ||
try: | ||
profile = UserProfile.objects.get(user=user) | ||
followers = Follow.objects.filter(follower=profile) | ||
article = Article.objects.get(title=instance["title"]) | ||
for follower in followers: | ||
message = article.user.username + " published new article " | ||
link = DEFAULT_DOMAIN + '/api/article/detail/' + \ | ||
article.slug | ||
follower_message = render_to_string('followee_alert.html', { | ||
'user': follower.followee.user.username, | ||
"link": link, | ||
"message": message, | ||
"title": instance["title"] | ||
}) | ||
send_mail('Authors Heaven New Article!', | ||
'New Article!!!!', | ||
EMAIL_HOST_USER, | ||
[follower.followee.user.email], | ||
html_message=follower_message, | ||
fail_silently=False) | ||
NotificationsFollower( | ||
follower=profile, | ||
followee=follower.followee, | ||
article=article).save() | ||
return True | ||
except Exception as error: | ||
return False | ||
|
||
|
||
class Rate(CreateAPIView): | ||
permission_classes = (IsAuthenticated,) | ||
renderer_classes = (UserJSONRenderer,) | ||
serializer_class = RateArticleSerializer | ||
|
||
def post(self, request, **kwargs): | ||
""" | ||
rate user view | ||
""" | ||
data = request.data.get('user', {}) | ||
serializer = self.serializer_class(data=data) | ||
serializer.validate(data=data) | ||
serializer.is_valid(raise_exception=True) | ||
rate_article = None | ||
try: | ||
article = Article.objects.get(slug=data['slug']) | ||
except Exception: | ||
return Response({"response": "Article not found"}, | ||
status=status.HTTP_204_NO_CONTENT) | ||
rater = request.user | ||
try: | ||
rate_article = RateArticle.objects.get( | ||
rater=rater, article=article) | ||
rate_article.rate = data["rate"] | ||
rate_article.save() | ||
return Response({"response": "successfully rated"}, | ||
status=status.HTTP_200_OK) | ||
except Exception: | ||
RateArticle( | ||
rater=rater, article=article, rate=data["rate"]).save() | ||
return Response(data={"response": "sucessfully rated"}, | ||
status=status.HTTP_200_OK) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<style> | ||
.card { | ||
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); | ||
max-width: 600px; | ||
margin: auto; | ||
text-align: center; | ||
font-family: arial; | ||
} | ||
|
||
.title { | ||
color: grey; | ||
font-size: 13px; | ||
text-align: left; | ||
} | ||
|
||
button { | ||
border: none; | ||
outline: 0; | ||
display: inline-block; | ||
padding: 8px; | ||
color: #000;; | ||
background-color: #000; | ||
text-align: center; | ||
cursor: pointer; | ||
width: 100%; | ||
font-size: 18px; | ||
} | ||
|
||
a { | ||
text-decoration: none; | ||
font-size: 13px; | ||
color: black; | ||
} | ||
|
||
button:hover, a:hover { | ||
opacity: 0.7; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<div class="card"> | ||
<p class="title"> | ||
Hi {{user}},<br></p> | ||
<p class="title"> New Comment!!!! <a href={{ENVIRONMENT}}> | ||
Authors Heaven</a>, the platform for | ||
people with expressive ideas.</p> | ||
<p class="title">{{message}}<a href={{link}}> {{comment}} </a>view it on Authors Haven</p> | ||
<p><button></button>Authors Heavens. All Rights Reserved</p> | ||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<style> | ||
.card { | ||
box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); | ||
max-width: 600px; | ||
margin: auto; | ||
text-align: center; | ||
font-family: arial; | ||
} | ||
|
||
.title { | ||
color: grey; | ||
font-size: 13px; | ||
text-align: left; | ||
} | ||
|
||
button { | ||
border: none; | ||
outline: 0; | ||
display: inline-block; | ||
padding: 8px; | ||
color: #000;; | ||
background-color: #000; | ||
text-align: center; | ||
cursor: pointer; | ||
width: 100%; | ||
font-size: 18px; | ||
} | ||
|
||
a { | ||
text-decoration: none; | ||
font-size: 13px; | ||
color: black; | ||
} | ||
|
||
button:hover, a:hover { | ||
opacity: 0.7; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
|
||
<div class="card"> | ||
<p class="title"> | ||
Hi {{user}},<br></p> | ||
<p class="title"> New Article!!!! <a href={{ENVIRONMENT}}> | ||
Authors Heaven</a>, the platform for | ||
people with expressive ideas.</p> | ||
<p class="title">{{message}}<a href={{link}}> {{title}} </a>view it on Authors Haven</p> | ||
<p><button></button>Authors Heavens. All Rights Reserved</p> | ||
</div> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.