def blog_comment(request): if request.user.is_authenticated: if 'blog_id' in request.POST and 'text' in request.POST and len( request.POST['text'].strip()) > 2: ref = None if 'ref_comment_id' in request.POST: ref_id = request.POST['ref_comment_id'] try: ref = Comment.get_by_id(ref_id) except: return apiRespond(400, msg='Reference comment not found') try: b = Blog.get_by_id(request.POST['blog_id']) except: return apiRespond(400, msg='Target blog not found') else: comment = request.user.comment( blog=b, text=request.POST['text'].strip(), reference=ref) Alert.create_alert( ref_user=request.user, type=Alert.COMMENT_REPLY if ref else Alert.COMMENT, blog=b) return apiRespond(201, comment=comment.get_obj()) else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def get_new_alerts(request): if request.user.is_authenticated: try: new_alerts = Alert.get_new_alerts(request.user) old_alerts = Alert.get_old_alerts(request.user) except: return apiRespond(400, msg=f'Something went wrong.') else: return apiRespond(201, new_alerts=new_alerts, old_alerts=old_alerts) else: return apiRespond(401, msg='User not logged in')
def follow_user(request): if request.user.is_authenticated: if 'user_id' in request.POST: try: target = User.get_by_id(request.POST['user_id']) except: return apiRespond(400, msg='Target user not found') else: result, obj = request.user.follow(target) Alert.create_alert(user=target, ref_user=request.user, type=Alert.FOLLOW, follow=obj) return apiRespond(201, result=result) else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def unfollow_user(request): if request.user.is_authenticated: if 'user_id' in request.POST: try: target = User.get_by_id(request.POST['user_id']) except: return apiRespond(400, msg='Target user not found') else: result = request.user.unfollow(target) Alert.delete_user_unfollows(ref_user=request.user, user=target) return apiRespond(201, result=result) else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def blog_react(request): if request.user.is_authenticated: if 'blog_id' in request.POST and 'reaction' in request.POST and request.POST['reaction'] in Reaction.CODES: reaction = request.POST['reaction'] try: b = Blog.get_by_id(request.POST['blog_id']) except: return apiRespond(400, msg='Target blog not found') else: obj = request.user.react(blog=b, reaction=reaction) Alert.create_alert(ref_user=request.user, blog=b, type=Alert.REACTION, reaction=obj) return apiRespond(201, result=True) else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def email_alert_preview(request): user=request.user msg = Alert.get_new_alerts(user=user) heading='You have '+str(len(msg))+' new notification' if len(msg)>1: heading+='s' html = email_html({'alerts':msg,'heading':heading},'alerts',user) return HttpResponse(html)
def unpublish_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.unpublish() Alert.alerts_for_new_blog(blog=b, delete=True) return apiRespond(201, is_published=b.is_published) else: return apiRespond(400, msg='Access denied') else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def set_alerts_seen(request): if request.user.is_authenticated: if 'alerts_id' in request.POST: try: is_success = Alert.mark_group_seen(request.POST['alerts_id'].split(',')) except: return apiRespond(400, msg=f'Something went wrong.') else: if is_success: return apiRespond(201, msg='Success') else: return apiRespond(400, msg='Required fields missing') else: return apiRespond(401, msg='User not logged in')
def send_alerts(): users = get_users_batch() for user in users: msg = Alert.get_new_alerts(user=user) heading = 'You have ' + str(len(msg)) + ' new notification' if len(msg) > 1: heading += 's' html = get_html({'alerts': msg, 'heading': heading}, 'alerts', user) success = send(to=user.email, subject=alert_subject(user), message=heading, html=html) if success: user.last_alert_email_date = timezone.now() user.save()