Fixes sending invite emails

Corrects the email sender and avoids integrity error on saves
This commit is contained in:
Mouse Reeve 2021-03-29 08:33:12 -07:00
parent fd97b167e1
commit f63b6fb325
4 changed files with 9 additions and 6 deletions

View file

@ -2,7 +2,7 @@
from django.core.mail import EmailMultiAlternatives from django.core.mail import EmailMultiAlternatives
from django.template.loader import get_template from django.template.loader import get_template
from bookwyrm import models from bookwyrm import models, settings
from bookwyrm.tasks import app from bookwyrm.tasks import app
from bookwyrm.settings import DOMAIN from bookwyrm.settings import DOMAIN
@ -59,6 +59,8 @@ def format_email(email_name, data):
@app.task @app.task
def send_email(recipient, subject, html_content, text_content): def send_email(recipient, subject, html_content, text_content):
""" use a task to send the email """ """ use a task to send the email """
email = EmailMultiAlternatives(subject, text_content, None, [recipient]) email = EmailMultiAlternatives(
subject, text_content, settings.DEFAULT_FROM_EMAIL, [recipient]
)
email.attach_alternative(html_content, "text/html") email.attach_alternative(html_content, "text/html")
email.send() email.send()

View file

@ -83,7 +83,7 @@ class InviteRequest(BookWyrmModel):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
""" don't create a request for a registered email """ """ don't create a request for a registered email """
if User.objects.filter(email=self.email).exists(): if not self.id and User.objects.filter(email=self.email).exists():
raise IntegrityError() raise IntegrityError()
super().save(*args, **kwargs) super().save(*args, **kwargs)

View file

@ -25,6 +25,7 @@ EMAIL_PORT = env("EMAIL_PORT", 587)
EMAIL_HOST_USER = env("EMAIL_HOST_USER") EMAIL_HOST_USER = env("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD") EMAIL_HOST_PASSWORD = env("EMAIL_HOST_PASSWORD")
EMAIL_USE_TLS = env("EMAIL_USE_TLS", True) EMAIL_USE_TLS = env("EMAIL_USE_TLS", True)
DEFAULT_FROM_EMAIL = "admin@{:s}".format(env("DOMAIN"))
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

View file

@ -107,7 +107,7 @@
{% trans 'Settings' %} {% trans 'Settings' %}
</a> </a>
</li> </li>
{% if perms.bookwyrm.create_invites or perms.bookwyrm.edit_instance_settings%} {% if perms.bookwyrm.create_invites or perms.moderate_users %}
<li class="navbar-divider" role="presentation"></li> <li class="navbar-divider" role="presentation"></li>
{% endif %} {% endif %}
{% if perms.bookwyrm.create_invites %} {% if perms.bookwyrm.create_invites %}
@ -117,9 +117,9 @@
</a> </a>
</li> </li>
{% endif %} {% endif %}
{% if perms.bookwyrm.edit_instance_settings %} {% if perms.bookwyrm.moderate_users %}
<li> <li>
<a href="{% url 'settings-reports' %}" class="navbar-item"> <a href="{% url 'settings-users' %}" class="navbar-item">
{% trans 'Admin' %} {% trans 'Admin' %}
</a> </a>
</li> </li>