I am trying to make the Django-organization's custom invitation work. But it's throwing an error. I am unable to find the reason. Below is my code:
My urls.py
file
from django.urls import path, include
from organizations.backends import invitation_backend
urlpatterns = [
path('accounts/', include('organizations.urls')),
path('invitations/', include(invitation_backend().get_urls())),
]
In my settings.py
file I have added the 'organizations' app and my custom app 'integration' in the installed app section. There needs to be added a backend for the custom invitation which is guided in the following link: https://django-organizations.readthedocs.io/en/latest/custom_usage.html#creating-the-backend
Here is the specified backend.
INVITATION_BACKEND = 'integration.backends.CustomInvitations'
My backends.py
file
from organizations.backends.defaults import InvitationBackend
class CustomInvitations(InvitationBackend):
def invite_by_email(self, email, sender=None, request=None, **kwargs):
try:
user = self.user_model.objects.get(email=email)
except self.user_model.DoesNotExist:
user = self.user_model.objects.create(email=email,
password=self.user_model.objects.make_random_password())
user.is_active = False
user.save()
self.send_invitation(user, sender, **kwargs)
return user
And here is the error occurring:
Traceback (most recent call last):
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
response = get_response(request)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\contrib\auth\decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\mixins.py", line 111, in dispatch
return super(AdminRequiredMixin, self).dispatch(request, *args,
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\views.py", line 135, in post
return super(BaseOrganizationUserCreate, self).post(request, *args, **kwargs)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\edit.py", line 172, in post
return super().post(request, *args, **kwargs)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\edit.py", line 142, in post
return self.form_valid(form)
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\views\generic\edit.py", line 125, in form_valid
self.object = form.save()
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\forms.py", line 111, in save
invitation_backend().send_notification(user, **{
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\organizations\backends\defaults.py", line 317, in send_notification
self.email_message(user, self.notification_subject, self.notification_body, sender, **kwargs).send()
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\mail\message.py", line 276, in send
return self.get_connection(fail_silently).send_messages([self])
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\mail\backends\smtp.py", line 102, in send_messages
new_conn_created = self.open()
File "H:\Hobby\Project\django_org\orgenv\lib\site-packages\django\core\mail\backends\smtp.py", line 62, in open
self.connection = self.connection_class(self.host, self.port, **connection_params)
File "c:\users\acer\appdata\local\programs\python\python38\lib\smtplib.py", line 253, in __init__
(code, msg) = self.connect(host, port)
File "c:\users\acer\appdata\local\programs\python\python38\lib\smtplib.py", line 339, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "c:\users\acer\appdata\local\programs\python\python38\lib\smtplib.py", line 308, in _get_socket
return socket.create_connection((host, port), timeout,
File "c:\users\acer\appdata\local\programs\python\python38\lib\socket.py", line 808, in create_connection
raise err
File "c:\users\acer\appdata\local\programs\python\python38\lib\socket.py", line 796, in create_connection
sock.connect(sa)
Exception Type: ConnectionRefusedError at /accounts/1/people/add/
Exception Value: [WinError 10061] No connection could be made because the target machine actively refused it
I just ran into this error a few minutes ago myself. The error is telling you that it's trying to send an email using the configured SMTP server but the machine is refusing the connection. Short version - you need to point to a running SMTP server.
I was unable to find the proper settings to point to a remote SMTP server but what I was able to do was run a local fake SMTP server to get rid of the error and at least let the process run to completion.
I did this by installing and running nullsmtpd.
Install the package using:
Create a folder to store emails and logs:
Launch the process on Windows and point it at your output folder:
The listener defaults to localhost on port 25 which aligns with the default SMTP settings that your Django app should be using.