def comment_submit(request, post_slug=""): if request.method == 'POST': form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user if post_slug: post = Post.objects.get(slug=post_slug) comment.post = post comment.save() # Notification body = "replied to your [post]("+comment.post.get_absolute_url()+")<br/>"\ + comment.body message = Message(from_user=comment.author, to_user=comment.post.author, body=body) message.save() return HttpResponseRedirect(post.get_absolute_url()) else: # If error prev_url = request.GET.get('next', '/') return HttpResponseRedirect(prev_url) else: # If not post prev_url = request.GET.get('next', '/') return HttpResponseRedirect(prev_url)
def create(self, validated_data): created_by = validated_data.get('created_by') random_password = ''.join( random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8)) email = validated_data.pop('email') #create user password = validated_data.pop('password') user = User.objects.create_user(email=email, password=password, **validated_data) if created_by: password = random_password #send email here . default gateway is gmail email_message = "Welcome %s . Please use %s as your password. Make sure you change it later. " % ( user.first_name, password) Message.create_email(message=email_message, recipient_address=user.email, subject="Registration") user.set_password(password) else: user.is_password_changed = True #creted by himself. doenot need to change password on login user.save() return user
def subscribe(request, username): profile = User.objects.get(username=username) if not request.user.is_anonymous(): user = request.user user.subscribed_to.add(profile) user.save() # Notification message = Message(from_user=user, to_user=profile, body="subscribed to your stories!") message.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: return HttpResponseRedirect('/login/')
def run(): while True: #get messages to send messages = Message.get_unprocessed() for m in messages: if m.is_email(): #send via mail subject = m.subject if m.subject else 'Email Subject ' try: send_mail( subject, m.message, m.sender_address, [m.recipient_address], fail_silently=False, ) m.done() #mark as sent and successful except: #@TODO know how to process this m.fail() #mark as sent but failed elif m.is_sms(): #send sms here #@TODO implement this in your way pass time.sleep(2) #wait 2 seconds before sending again
def subscribe(request, username): userprofile = User.objects.get(username=username) if not request.user.is_anonymous(): user = request.user user.subscribed_to.add(userprofile) user.save() # Notification message = Message(from_user=user, to_user=userprofile, message_type="subscriber") message.save() userprofile.new_notifications = True userprofile.save() return HttpResponseRedirect(request.META.get('HTTP_REFERER')) else: return HttpResponseRedirect('/login/')
def comment_submit(request, comment_id): if request.method == "POST": form = CommentForm(request.POST) if form.is_valid(): comment = form.save(commit=False) comment.author = request.user comment.parent = Comment.objects.get(id=comment_id) comment.post = comment.parent.post comment.save() # Send Email # send_comment_email_notification(comment) # if comment.parent.author.email_comments: # commentauthor = comment.author.username # topic = commentauthor + " has replied to your comment" # body = commentauthor + " has replied to your comment\n" +\ # "http://fictionhub.io"+comment.post.get_absolute_url()+ "\n" +\ # "'" + comment.body[:64] + "...'" # body += "\n\nYou can manage your email notifications in preferences:\n" +\ # "http://fictionhub.io/preferences/" # try: # email = comment.parent.author.email # send_mail(topic, body, '*****@*****.**', [email], fail_silently=False) # except: # pass # Notification message = Message( from_user=request.user, to_user=comment.parent.author, story=comment.post, comment=comment, message_type="reply", ) message.save() comment.parent.author.new_notifications = True comment.parent.author.save() comment_url = request.GET.get("next", "/") + "#id-" + str(comment.id) return HttpResponseRedirect(comment_url) else: # return HttpResponse("string") return render(request, "posts/create.html", {"form": form})
def send_template_sms(sender, receiver, template_name, context, autosave=True): message = Message() message.method = Message.SMS message.sender = sender message.receiver = receiver context['sender_address'] = sender context['receiver_address'] = receiver context.update(site_url(None)) content = render_to_string(template_name, context).strip() message.content = content if autosave: message.save() return message
def send_template_mail(sender, receiver, template_name, context, autosave=True): message = Message() message.sender = sender message.receiver = receiver context['sender_address'] = sender context['receiver_address'] = receiver context.update(site_url(None)) raw_content = render_to_string(template_name, context).strip() subject, _, content = raw_content.partition('\n=====\n') message.subject = subject message.content = content if autosave: message.save() return message
def send_sms(self, message, recipient): return Message.create_sms(message=message, recipient_address=recipient)
def send_email(self, message, recipient, template_id=None, subject=None): return Message.create_email(message=message, recipient_address=recipient, template_id=template_id, subject=subject)