当前位置: 动力学知识库 > 问答 > 编程问答 >

python - how to send email confirmation mail, after post_save signal with djanago-allauth

问题描述:

I am using django-allauth in my webapp for account management.

I have User model and UserProfile Model.

When User gets signs up it creates a user(an instance in user model).

UserProfile is assosiated with model User.

Allauth by default sends an email when user signs Up,A confirmation email will be sent to user when he signs up.

Now I want to send that email only when he fills UserProfile.

I am thiking to use signals here.

When user fill UserProfile, it will call post_save signal , which calls another function user_signed_up_ but I am facing problem in passing request and user args to that function.

Here is my models.py

from django.db import models

from django.contrib.auth.models import User

from allauth.account.signals import user_signed_up

from allauth.account.utils import send_email_confirmation

from django.dispatch import receiver

import datetime

class UserProfile(models.Model):

user = models.ForeignKey(User, unique=True)

are_u_intrested = models.BooleanField(default=False)

#this signal will be called when user signs up.allauth will send this signal.

@receiver(user_signed_up, dispatch_uid="some.unique.string.id.for.allauth.user_signed_up")

def user_signed_up_(request, user, **kwargs):

send_email_confirmation(request, user, signup=True)

#this allauth util method, which sends confirmation email to user.

models.signals.post_save.connect(user_signed_up_, sender=UserProfile, dispatch_uid="update_stock_count")

#If user fills this userProfile , then I only send confirmation mail.

#If UserProfile model instace saved , then it send post_save signal.

网友答案:

You don't need to be using allauth's signals here, you need to be using Django's model signals. Something like:

from django.db.models.signals import post_save
from django.dispatch import receiver

@receiver(post_save, sender=UserProfile)
def userprofile_filled_out(sender, instance, created, raw, **kwargs):
    # Don't want to send it on creation or raw most likely 
    if created or raw:
        return

    # Test for existence of fields we want to be populated before
    # sending the email
    if instance.field_we_want_populated:
       send_mail(...)

Hope that helps!

分享给朋友:
您可能感兴趣的文章:
随机阅读: