def post_status_changed(sender, instance, **kwargs): """ Notify user when the status of his post changed """ if instance.pk: saved_post = Post.objects.get(pk=instance.pk) # Set current date as publication date instance.date_posted = timezone.now() if instance.status != saved_post.status: site = Site.objects.get_current() content = render_to_string( "blog/post_status_notification_email.html", { "name": instance.author.first_name, "post": instance, "link": f"{site.domain}/post/{instance.slug}/", "domain": site.domain, }) user = instance.author # Send a notification email send_email.delay(subject=str(_("Your post has been ")) + f"{instance.status}" + str(_(" to publication")), email=user.email, content=content)
def notify_about_comment(sender, instance, **kwargs): """ Notify user about a new comment on his post """ comment = Comment.objects.filter(pk=instance.pk) if comment: # Do not notify user if someone edited an existing comment return # Do not notify user about his comment on his own post if instance.author != instance.post.author: site = Site.objects.get_current() content = render_to_string( "blog/comment_notification_email.html", { "name": instance.post.author.first_name, "comment_author": instance.author.username, "post": instance.post, "link": f"{site.domain}/post/{instance.post.slug}/", "domain": site.domain, }) user = instance.post.author # Send a notification email send_email.delay(subject=str(_("New Comment on your post")), email=user.email, content=content)
def save(self, *args, **kwargs): if not self.id: send_email.delay( self.post.author.email, "New comment", f"Тew comment for your news {self.post.title}:\n{self.content}" ) super().save(*args, **kwargs)
def register_email(request): username = request.POST.get("email").strip() password = request.POST.get("passwordB").strip() re_password = request.POST.get("passwordB2").strip() tel = int(request.POST["tel"]) if username == "": return render(request, "users/register.html", {"msg": "用户名称不能为空"}) if len(password) < 3: return render(request, "users/register.html", {"msg": "用户密码长度不能小于3位"}) if re_password == password: if models.Passport.objects.filter(username=username): return render(request, "users/register.html", {"msg": "注册失败用户名已存在"}) else: # 保存用户 s=transaction.savepoint() try: password = utils.md5_by_hmac(password) user = models.Passport(username=username, email=username, password=password, tel=tel) user.save() """ 发送激活邮件 """ send_email.delay(user.id, username, username) transaction.savepoint_commit(s) except: transaction.rollback(s) return render(request, "users/register.html", {"msg": "邮件已发送"}) else: return render(request, "users/register.html", {"msg": "注册失败两次密码不一致"})
def send_mail(sender, instance, created, **kwargs): if created: email = instance.user.email name = instance.user.profile.name slack_id = instance.user.profile.slack_id badge = instance.badges.title description = instance.description awarded_by = instance.awarded_by timestamp = instance.timestamp image = 'https://sushiksha.konkanischolarship.com' + str( instance.badges.logo.url) array = [ email, timestamp, awarded_by, description, badge, name, image, slack_id ] profile = Profile.objects.get(user=instance.user) _badge = Badge.objects.get(title=badge) update_profile_points(profile, _badge) team = Teams.objects.filter(members__user=profile.user).first() if team is not None: team.points = team.points + _badge.points team.save() house = House.objects.filter(teams__members__user=profile.user).first() if house is not None: house.points = house.points + _badge.points house.save() send_email.delay(array) # comment during production to avoid unnecessary errors # uncomment above line only if you have celery, rabbitmq setup and know the implementation return True
def post(self, request, *args, **kwargs): email = request.POST.get("email") user = User.objects.filter(email=email).first() if not user: messages.error(request, _("User with this email does not exist")) return redirect("user:sign-up") content = render_to_string( "users/password_reset_email.html", { "user": user.first_name, "domain": get_current_site(request).domain, "uid": urlsafe_base64_encode(force_bytes(user.pk)), "token": PasswordResetTokenGenerator().make_token(user=user), }) # Send a confirmation email send_email.delay(subject=str(_("Password reset confirmation")), email=user.email, content=content) messages.success( request, _("Instructions to reset your password has been emailed to you")) return redirect("home")
def send_mail(sender, instance, created, **kwargs): if created: email = instance.user.email name = instance.user.profile.name badge = instance.badges.title description = instance.description awarded_by = instance.awarded_by timestamp = instance.timestamp image = 'https://sushiksha.konkanischolarship.com' + str(instance.badges.logo.url) array = [email, timestamp, awarded_by, description, badge, name, image] send_email.delay(array) # do not uncomment # uncomment above line only if you have celery, rabbitmq setup and know the implementation return True
def post(self, request, *args, **kwargs): if request.user.is_authenticated: return redirect("home") form = self.form_class(request.POST) if form.is_valid(): if User.objects.filter( email=request.POST["email"].lower()).first(): messages.warning(request, _("User with this credential already exists")) return redirect("user:sign-up") user = form.save() user.refresh_from_db() # Email is case-insensitive user.email = user.email.lower() # Email isn't confirmed user.is_active = False # Profile created in signals user.profile.birth_date = form.cleaned_data.get("birth_date") user.save() content = render_to_string( "users/activation_email.html", { "user": user.first_name, "domain": get_current_site(request).domain, "uid": urlsafe_base64_encode(force_bytes(user.pk)), "token": account_activation_token.make_token(user), }) # Send a confirmation email send_email.delay(subject=str(_("Please Activate Your Account")), email=user.email, content=content) messages.success( request, _("Instructions for activating your account has been emailed to you" )) return redirect("home") return redirect("user:sign-up")
def forgot(request): if request.method == "GET": if request.user.is_authenticated: return redirect(reverse('profile')) form = ForgotForm() return render(request, "forgot.html", {"form": form}) elif request.method == "POST": form = ForgotForm(request.POST) if form.is_valid(): try: user = User.objects.get(email=form.cleaned_data['email']) token = uuid.uuid4() user.confirm_code = token user.save() # test.delay() send_email.delay("Reset password", settings.DEFAULT_FROM_EMAIL, user.email, 'mail-reset.html', args=dict(code=user.confirm_code, name=user.first_name)) except User.DoesNotExist: return render( request, "forgot.html", {"form": form, "errors": ["This email is wrong"]}) return render(request, "forgot_success.html", {})