diff --git a/pflaenzli/mail.py b/pflaenzli/mail.py new file mode 100644 index 0000000..d2a8504 --- /dev/null +++ b/pflaenzli/mail.py @@ -0,0 +1,25 @@ +from django.core.mail import EmailMessage, EmailMultiAlternatives +from django.urls import reverse +from django.template.loader import render_to_string + + +def send_offer_email(request, offer, sender_user, recipient_user): + html_content = render_to_string('user/trade/offer_email.html', + {'request': request, 'offer': offer, 'sender_user': sender_user, 'recipient_user': recipient_user}) + plain_text = get_offer_text(request, offer, sender_user, recipient_user) + + message = EmailMultiAlternatives( + f'{sender_user.username} wants to trade', + plain_text, + 'no-reply@pflaenz.li', + [recipient_user.email], + reply_to=[sender_user.email], + ) + + message.attach_alternative(html_content, 'text/html') + + message.send() + + +def get_offer_text(request, offer, sender_user, recipient_user): + return f"Hello {recipient_user.username},\n\nThe user {sender_user.username} would like to trade '{offer.title}' with you!\n\nIf you would like to trade with {sender_user.username}, just reply to this email to get in touch with them.\n\nYou can also view their offers here: {request.scheme}://{request.get_host()}{reverse('user_detail', args=[sender_user.id])}" diff --git a/pflaenzli/templates/offer/detail.html b/pflaenzli/templates/offer/detail.html index b15400b..00417df 100644 --- a/pflaenzli/templates/offer/detail.html +++ b/pflaenzli/templates/offer/detail.html @@ -83,6 +83,6 @@ {% endif %} - Offer trade + Offer trade {% endif %} {% endblock %} diff --git a/pflaenzli/templates/user/trade/offer_email.html b/pflaenzli/templates/user/trade/offer_email.html new file mode 100644 index 0000000..1042d07 --- /dev/null +++ b/pflaenzli/templates/user/trade/offer_email.html @@ -0,0 +1,5 @@ +

Hello {{ recipient_user.username }}

+

{{ sender_user.username }} wants to trade '{{ offer.title }}'!

+

Checkout their offers:

+Link +

Just reply to get in contact witj to start trading with {{ sender_user.username }}.

diff --git a/pflaenzli/templates/user/trade/offer_email.html.twig b/pflaenzli/templates/user/trade/offer_email.html.twig deleted file mode 100644 index f23e814..0000000 --- a/pflaenzli/templates/user/trade/offer_email.html.twig +++ /dev/null @@ -1,5 +0,0 @@ -

{{ user.username }} wants to trade!

- -

Checkout {{ user.username}}'s offers:

-Link -

Reply to this email to start trading.

\ No newline at end of file diff --git a/pflaenzli/views.py b/pflaenzli/views.py index b19d033..91d2c9d 100644 --- a/pflaenzli/views.py +++ b/pflaenzli/views.py @@ -2,10 +2,12 @@ from django.shortcuts import render, get_object_or_404, redirect from django.utils import timezone from django.contrib import messages from django.contrib.auth.decorators import login_required +from django.contrib.auth.models import User from django.http import HttpResponseForbidden from .forms import CreateOfferForm from .models import Offer, Wish +from .mail import send_offer_email from .upload import generate_unique_filename @@ -75,9 +77,19 @@ def offer_edit(request, offer_id): return render(request, "basic_form.html", {"form": form, "button_label": "Update"}) -@ login_required +@login_required def offer_trade(request, offer_id): - return 0 + offer = get_object_or_404(Offer, id=offer_id) + sender = request.user + recipient = offer.user + + if sender != recipient: + send_offer_email(request, offer, sender, recipient) + messages.success(request, f"{recipient.username} was successfully notified") + else: + messages.error(request, "You can't trade with yourself!") + + return redirect("offer_detail", offer_id) def user_detail(request, user_id):