Send request mail

This commit is contained in:
Jannis Portmann 2023-03-31 23:56:19 +02:00
parent 79b29d0d95
commit 9d2b219a70
5 changed files with 45 additions and 8 deletions

25
pflaenzli/mail.py Normal file
View file

@ -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])}"

View file

@ -83,6 +83,6 @@
</ul>
{% endif %}
</div>
<a class="btn btn-pfl mb-3" href="{% url 'trade' offer.id %}">Offer trade</a>
<a class="btn btn-pfl mb-3" href="{% url 'offer_trade' offer.id %}">Offer trade</a>
{% endif %}
{% endblock %}

View file

@ -0,0 +1,5 @@
<p>Hello {{ recipient_user.username }}</p>
<h1>{{ sender_user.username }} wants to trade '{{ offer.title }}'!</h1>
<p>Checkout their offers:</p>
<a href="{{ request.scheme }}://{{ request.get_host }}{% url 'user_detail' sender_user.id %}">Link</a>
<p>Just reply to get in contact witj to start trading with {{ sender_user.username }}.</p>

View file

@ -1,5 +0,0 @@
<h1>{{ user.username }} wants to trade!</h1>
<p>Checkout {{ user.username}}'s offers:</p>
<a href="{{ url('user_public', {'urlId': urlId}) }}">Link</a>
<p>Reply to this email to start trading.</p>

View file

@ -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):