开发者

Convert a function from many to single

开发者 https://www.devze.com 2023-03-10 12:14 出处:网络
I have a function from django-registration which re-sends an activation email to the given recipients. I am trying to convert the function from accepting multiple users for a given email to only one u

I have a function from django-registration which re-sends an activation email to the given recipients. I am trying to convert the function from accepting multiple users for a given email to only one user per email. However, it is throwing an AttributeError when I try and change it.

def resend_activation(self, email, site):   # for multiple emails -- this works
    sent = False
    users = User.objects.all().filter(email=email)

    if users:
        for user in users:
            registratio开发者_JAVA百科n_profiles = self.all().filter(user=user)
            for registration_profile in registration_profiles:
                if not registration_profile.activation_key_expired():
                    registration_profile.send_activation_email(site)
                    sent = True
    return sent

def resend_activation(self, email, site):   # for single email -- this does not work
    sent = False                            
    user = User.objects.all().filter(email=email)

    if user:
        registration_profile = self.all().get(user=user)
        if not registration_profile.activation_key_expired():
            registration_profile.send_activation_email(site)
            sent = True
    return sent

This latter function throws an AttributeError, but I can't figure out why the function won't 'work' without a for loop. What seems to be my problem here? Thank you.


try:

def resend_activation(self, email, site):
    sent = False
    # Get the user you are looking for
    try:
        single_user = User.objects.get(email=email)
    except User.DoesNotExist:
        return false

    # Get all the profiles for that single user
    registration_profiles = self.all().filter(user=user)
        # Loop through, and send an email to each of the profiles belonging to that user
        for registration_profile in registration_profiles:
            if not registration_profile.activation_key_expired():
                registration_profile.send_activation_email(site)
                sent = True
    return sent

In the original, User.object.filter(email=email) is returning a queryset which is a collection of objects from the database that were returned from the query filter(email=email). The for loop in the original is looping through each of these objects, and sending a corresponding email. You were trying to call the send_

0

精彩评论

暂无评论...
验证码 换一张
取 消