Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subscribe/Unsubscribe from blog alerts #50

Merged
merged 4 commits into from
Dec 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions dscblog/migrations/0019_blog_is_subscribed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 3.1.1 on 2020-12-22 13:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dscblog', '0018_alert'),
]

operations = [
migrations.AddField(
model_name='blog',
name='is_subscribed',
field=models.BooleanField(default=True),
),
]
17 changes: 16 additions & 1 deletion dscblog/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ class Blog(models.Model):
modified_on = models.DateTimeField()
published_on = models.DateTimeField(null=True, default=None)
is_published = models.BooleanField(default=False)
is_subscribed = models.BooleanField(default=True)
score = models.FloatField(verbose_name='Engagement Score', default=0.0)
topics = models.ManyToManyField('Topic', related_name="blogs")

Expand Down Expand Up @@ -488,6 +489,20 @@ def unpublish(self):
self.modified_on = timezone.now()
self.save()

def subscribe(self):
if not self.is_subscribed:
self.is_subscribed = True
self.modified_on = timezone.now()
self.save()

def unsubscribe(self):
if self.is_subscribed:
self.is_subscribed = False
self.modified_on = timezone.now()
# Deleting Alerts
Alert.objects.filter(blog_id=self.id).delete()
self.save()

def has_topic(self, name):
try:
self.topics.get(name=name)
Expand Down Expand Up @@ -895,7 +910,7 @@ def create_alert(cls, ref_user, type, blog=None, user=None, comment=None, reacti
if type == cls.COMMENT or type == cls.REACTION:
user = Blog.objects.get(pk=blog.pk).author

if user.id != ref_user.id:
if user.id != ref_user.id and (blog and blog.is_subscribed):
cls.check_for_max_limit(user)
obj = cls.objects.update_or_create(user=user, ref_user=ref_user, type=type, blog=blog, comment=comment, reaction=reaction,
follow=follow)
Expand Down
64 changes: 61 additions & 3 deletions dscblog/pages/blogSettings.html
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,14 @@
</div>
<div class="hstack space-between opt">
<div class="ink-grey size-s base-regular">Public</div>
<div id="toggle"></div>
<div id="publishToggle"></div>
</div>
<br/>
<div class="hstack space-between opt">
<div class="ink-grey size-s base-regular">Alerts</div>
<div id="subscribeToggle"></div>
</div>
<br />
<hr/>
<div class="center">
<div id="delete" class="ink-red clickable">Delete blog</div>
Expand All @@ -55,7 +60,7 @@
</div>
</div>
<script>
var toggle=toggleSwitch("toggle",(isActive)=>{
var publishToggle=toggleSwitch("publishToggle",(isActive)=>{
var willActivate=!isActive
if(willActivate!=is_published){
if(willActivate){
Expand All @@ -66,11 +71,23 @@
}
}
})
var subscribeToggle=toggleSwitch("subscribeToggle",(isActive)=>{
var willActivate = !isActive
if(willActivate!==is_subscribed){
if(willActivate){
subscribe();
}
else{
unsubscribe();
}
}
})
var api = new window.Api();
const blog_id = {{blog.blog_id}}
var is_published = '{{blog.is_published}}'
var title = '{{blog.title}}'
var img_url = '{{blog.img_url}}'
var is_subscribed = '{{blog.is_subscribed}}'
{% autoescape off %}
var topics = JSON.parse('{{blog.topics}}')
{% endautoescape %}
Expand All @@ -79,10 +96,20 @@
else
is_published = false;

if(is_subscribed === 'True')
is_subscribed = true;
else
is_subscribed = false;

function renderPublishButton() {
toggle(is_published)
publishToggle(is_published)
}
renderPublishButton()
function renderSubUnsubButton() {
subscribeToggle(is_subscribed);
}
renderSubUnsubButton();

function publish () {
api.post('blog/publish', {
blog_id
Expand Down Expand Up @@ -111,6 +138,37 @@
}
})
}

function subscribe () {
api.post('blog/subscribe', {
blog_id
}, (code, res) => {
if (code == 201) {
is_subscribed = true;
renderSubUnsubButton()
} else if (code == 400) {
alert(res.msg);
} else if (code == 401) {
window.location.href = "/login";
}
})
};

function unsubscribe () {
api.post('blog/unsubscribe', {
blog_id
}, (code, res) => {
if (code == 201) {
is_subscribed = false;
renderSubUnsubButton();
} else if (code == 400) {
alert(res.msg);
} else if (code == 401) {
window.location.href = "/login";
}
})
};

$("#titleSave").on('click', () => {
api.post('blog/title/set', {
blog_id,
Expand Down
44 changes: 43 additions & 1 deletion dscblog/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ def blog_settings(request, id):
return page404(request)
else:
if request.user == b.author:
blog_opts = b.get_obj_min()
blog_opts['is_subscribed'] = b.is_subscribed
opts = {'header': {'is_loggedin': True, 'is_empty': False, 'float': True},
'blog': b.get_obj_min()}
'blog': blog_opts}
topics = []
for topic in b.get_topics():
topics.append(topic.name)
Expand Down Expand Up @@ -624,6 +626,46 @@ def unpublish_blog(request):
return apiRespond(401, msg='User not logged in')


@require_http_methods(["POST"])
def subscribe_blog(request):
if request.user.is_authenticated:
if 'blog_id' in request.POST:
try:
b = Blog.get_by_id(request.POST['blog_id'])
except:
return apiRespond(400, msg='Blog not found')
else:
if b.author == request.user:
b.subscribe()
return apiRespond(201, is_subscribe=b.is_subscribed)
else:
return apiRespond(400, msg='Access denied')
else:
return apiRespond(400, msg='Required fields missing')
else:
return apiRespond(401, msg='User not logged in')


@require_http_methods(["POST"])
def unsubscribe_blog(request):
if request.user.is_authenticated:
if 'blog_id' in request.POST:
try:
b = Blog.get_by_id(request.POST['blog_id'])
except:
return apiRespond(400, msg='Blog not found')
else:
if b.author == request.user:
b.unsubscribe()
return apiRespond(201, is_subscribe=b.is_subscribed)
else:
return apiRespond(400, msg='Access denied')
else:
return apiRespond(400, msg='Required fields missing')
else:
return apiRespond(401, msg='User not logged in')


@require_http_methods(["POST"])
def delete_blog(request):
if request.user.is_authenticated:
Expand Down
2 changes: 2 additions & 0 deletions dscblog/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@
path('api/blog/unreact', paths.blog_unreact),
path('api/blog/comment', paths.blog_comment),
path('api/blog/uncomment', paths.blog_uncomment),
path('api/blog/subscribe', paths.subscribe_blog),
path('api/blog/unsubscribe', paths.unsubscribe_blog),
path('api/blog/pingback', paths.pingback),
path('api/alerts/new', paths.get_new_alerts),
path('api/alerts/seen', paths.set_alerts_seen),
Expand Down