preview email
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
repos:
|
||||
- repo: https://github.com/psf/black
|
||||
rev: stable
|
||||
rev: 25.1.0
|
||||
hooks:
|
||||
- id: black
|
||||
|
||||
@@ -5,26 +5,32 @@ from django.shortcuts import render, get_object_or_404
|
||||
from django.urls import path
|
||||
from django.template.loader import get_template
|
||||
from django.core.mail import EmailMultiAlternatives
|
||||
from django.template.response import TemplateResponse
|
||||
|
||||
# Register your models here.
|
||||
|
||||
|
||||
@admin.register(Contact, site=admin.site)
|
||||
class ContactAdmin(admin.ModelAdmin):
|
||||
list_display = ("email", "name", "contacted")
|
||||
list_filter = ("email", "name", "contacted")
|
||||
search_fields = ("email", "name")
|
||||
|
||||
@admin.action(description='Send seelcted emails')
|
||||
|
||||
@admin.action(description="Send seelcted emails")
|
||||
def send_emails(modeladmin, request, queryset):
|
||||
for email in queryset:
|
||||
success_count: int = 0
|
||||
try:
|
||||
from_email="info@aimloperations.com"
|
||||
d={"title":email.subject,"content":email.body}
|
||||
from_email = "info@aimloperations.com"
|
||||
d = {"title": email.subject, "content": email.body}
|
||||
|
||||
html_content = get_template(f"emails/marketing_email.html").render(d)
|
||||
text_content = get_template(f"emails/marketing_email.txt").render(d)
|
||||
|
||||
msg = EmailMultiAlternatives(email.subject, text_content, from_email, [email.recipient])
|
||||
msg = EmailMultiAlternatives(
|
||||
email.subject, text_content, from_email, [email.recipient]
|
||||
)
|
||||
msg.attach_alternative(html_content, "text/html")
|
||||
|
||||
msg.send(fail_silently=False)
|
||||
@@ -36,29 +42,26 @@ def send_emails(modeladmin, request, queryset):
|
||||
raise UserWarning(e)
|
||||
modeladmin.message_user(request, f"{success_count} emails sent successfully.")
|
||||
|
||||
|
||||
@admin.register(EmailMessage, site=admin.site)
|
||||
class EmailMessageAdmin(admin.ModelAdmin):
|
||||
change_form_template = 'admin/public/emailmessage/change_form.html'
|
||||
list_display = ('subject', 'recipient', 'sent')
|
||||
change_form_template = "admin/public/emailmessage/change_form.html"
|
||||
list_display = ("subject", "recipient", "sent")
|
||||
actions = [send_emails]
|
||||
|
||||
def get_urls(self):
|
||||
urls = super().get_urls()
|
||||
custom_urls = [
|
||||
path('preview_email/<int:pk>/', self.admin_site.admin_view(self.preview_email), name="preview_email")
|
||||
|
||||
path(
|
||||
"preview_email/<int:pk>/",
|
||||
self.admin_site.admin_view(self.preview_email),
|
||||
name="preview_email",
|
||||
)
|
||||
]
|
||||
print(f'RETURNING: {custom_urls + urls}')
|
||||
print(f"RETURNING: {custom_urls + urls}")
|
||||
return custom_urls + urls
|
||||
|
||||
def preview_email(self, request, pk):
|
||||
email_instance = get_object_or_404(EmailMessage, pk=pk)
|
||||
context = {
|
||||
"title":email_instance.subject,
|
||||
"content":email_instance.body
|
||||
}
|
||||
return render(
|
||||
request, 'public/preview_email.html', context
|
||||
)
|
||||
|
||||
|
||||
context = {"title": email_instance.subject, "content": email_instance.body}
|
||||
return TemplateResponse(request, "public/preview_email.html", context)
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
{% extends 'admin/change_form.html' %}
|
||||
{% load static %}
|
||||
|
||||
{% block submit_buttons_bottom %}
|
||||
{% if original %}
|
||||
<div class="submit-row">
|
||||
<div class="preview-row">
|
||||
|
||||
<!-- <input type="submit" value="Preview Email" name="_preview_email" class="default" /> -->
|
||||
<form action="/public/emailmessage/preview_email/{{ original.pk }}/" method="post" target="_blank" style="display: inline;">
|
||||
<!-- <form action="/public/emailmessage/preview_email/{{ original.pk }}/" method="post" style="display: inline;"> -->
|
||||
<a href="{% url 'preview_email' original.pk %}">Preview Email</a>
|
||||
<!-- <form action="{% url 'preview_email' pk=2 %}" method="post" style="display: inline;"></form>
|
||||
{% csrf_token %}
|
||||
<input type="submit" value="Preview Email" name="_preview_email" class="default" />
|
||||
<input type="submit" value="Preview Email" name="preview_email" class="default" />
|
||||
|
||||
</form>
|
||||
</form> -->
|
||||
|
||||
|
||||
</div>
|
||||
{% endif %}
|
||||
{{ block.super }}
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
Reference in New Issue
Block a user