A method of a class is not calling up another method of same class

OK, so I have a class named SendEmail in my models.py, it has a method send_invite_user_email(), which calls another method of this class that is invite_user_email_details().

In my views.py, I am calling the first method that is send_invite_user_email(), but the second method is not being called by the first method. I wrote some print statements to test what is working and what is not, but print statement in second method is not being executed.

Here is my models.py:

class SendEmail():
    def invite_user_email_details(
        self, 
        user, 
        user_id, 
        receiver_email_address, 
        token
    ):
        print("STARTED - EMAIL DETAILS")  # THIS PRINT STATEMENT DOES NOT WORK
        email_details = {
                "email": receiver_email_address,
                'domain': settings.INVITE_USER_EMAIL_DOMAIN,
                'site_name': settings.INVITE_USER_EMAIL_SITE_NAME,
                "uid": user_id,
                "user": user,
                'token': token,
                'protocol': settings.INVITE_USER_EMAIL_PROTOCOL,
        }
        print(email_details)  # THIS ALSO DOES NOT WORK

        return email_details

    def invite_user_mail(self, email, receiver_email_address):
        send_mail(
            settings.INVITE_USER_EMAIL_SUBJECT,
            email,
            settings.INVITE_USER_EMAIL_SENDER,
            [receiver_email_address],
            fail_silently=False
        )

    def send_invite_user_email(
        self,
        user,
        user_id,
        receiver_email_address,
        token
    ):
        try:
            print("STARTED")  # THIS WORKS
            email_details = self.invite_user_email_details(
                self, 
                user, 
                user_id, 
                receiver_email_address, 
                token
            )  # THIS METHOD IS NOT BEING CALLED
            print(type(email_details))  # THIS DOES NOT WORK

            email = render_to_string(settings.PASSWORD_RESET_EMAIL_TEMPLATE, email_details)

            self.invite_user_mail(email, receiver_email_address)

            return True
        except:
            return False

In my views.py, I have:

user = request.user
  
email = SendEmail()
email_sent = email.send_invite_user_email(
    user,
    user.pk,
    user.email,
    user.token.key
)

What could be wrong here? I can’t see anything bad I am doing.

You are most likely swallowing an exception in the try/except block. Try removing the try/except and see what happens. Then, if you want to keep a try except, only wrap it around the minimal amount of code, and always specify the exceptions you want to catch (don’t use a bare except)

1 Like

You’re swallowing any exceptions in your try / except block. You’ll get a lot more information if you either print the exception that is happening or you run this code outside that wrapper.

But the fundamental problem here is that you’re explicitly passing self as a parameter. You don’t pass that in your parameter list - it’s implicitly added by Python when calling a method in a class. The exception you will see is that you’re passing 6 parameters to a method only expecting 5.