Added attachments
This commit is contained in:
parent
fca60d4376
commit
9a48e3f724
@ -1,8 +1,9 @@
|
|||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.utils import timezone
|
|
||||||
|
|
||||||
from OACPL import settings
|
from .models import Attachment, Newsletter, Subscriber
|
||||||
from .models import Newsletter, Subscriber
|
|
||||||
|
|
||||||
|
admin.site.register(Attachment)
|
||||||
|
|
||||||
|
|
||||||
@admin.register(Subscriber)
|
@admin.register(Subscriber)
|
||||||
@ -16,16 +17,17 @@ class SubscriberAdmin(admin.ModelAdmin):
|
|||||||
class NewsletterAdmin(admin.ModelAdmin):
|
class NewsletterAdmin(admin.ModelAdmin):
|
||||||
list_display = ['subject', 'created', 'publish']
|
list_display = ['subject', 'created', 'publish']
|
||||||
search_fields = ['subject', 'created', 'publish']
|
search_fields = ['subject', 'created', 'publish']
|
||||||
|
filter_horizontal = ['attachments']
|
||||||
|
|
||||||
def get_form(self, request, obj=None, **kwargs):
|
def get_form(self, request, obj=None, **kwargs):
|
||||||
if obj:
|
if obj:
|
||||||
self.fields = ['creator', 'subject', 'body', 'sent', 'publish']
|
self.fields = ['creator', 'subject', 'body', 'sent', 'publish', 'attachments']
|
||||||
if obj.sent:
|
if obj.sent:
|
||||||
self.readonly_fields = ['creator', 'subject', 'body', 'sent', 'publish']
|
self.readonly_fields = ['creator', 'subject', 'body', 'sent', 'publish', 'attachments']
|
||||||
else:
|
else:
|
||||||
self.readonly_fields = ['creator', 'sent']
|
self.readonly_fields = ['creator', 'sent']
|
||||||
else:
|
else:
|
||||||
self.fields = ['subject', 'body', 'publish']
|
self.fields = ['subject', 'body', 'publish', 'attachments']
|
||||||
self.readonly_fields = []
|
self.readonly_fields = []
|
||||||
return super(NewsletterAdmin, self).get_form(request, obj, **kwargs)
|
return super(NewsletterAdmin, self).get_form(request, obj, **kwargs)
|
||||||
|
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
|
import mimetypes
|
||||||
|
|
||||||
from django.db.models import Q
|
from django.db.models import Q
|
||||||
from django.core.mail import EmailMessage
|
from django.core.mail import EmailMessage
|
||||||
from django.core.management.base import BaseCommand
|
from django.core.management.base import BaseCommand
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.html import strip_tags
|
|
||||||
|
|
||||||
from OACPL import settings
|
from OACPL import settings
|
||||||
from OACPL.utils import url_fix_render_to_string
|
from OACPL.utils import url_fix_render_to_string
|
||||||
@ -21,6 +22,8 @@ class Command(BaseCommand):
|
|||||||
print('Sending newsletter: "%s"' % newsletter.subject)
|
print('Sending newsletter: "%s"' % newsletter.subject)
|
||||||
msg = EmailMessage(subject=newsletter.subject, body=url_fix_render_to_string('email.html', {'content': newsletter.body, 'unsubscribe': True}), from_email=settings.EMAIL_HOST_USER, bcc=subscribers)
|
msg = EmailMessage(subject=newsletter.subject, body=url_fix_render_to_string('email.html', {'content': newsletter.body, 'unsubscribe': True}), from_email=settings.EMAIL_HOST_USER, bcc=subscribers)
|
||||||
msg.content_subtype = 'html'
|
msg.content_subtype = 'html'
|
||||||
|
for attachment in newsletter.attachments.all():
|
||||||
|
msg.attach(attachment.name(), attachment.file.read(), mimetypes.guess_type(attachment.name())[0])
|
||||||
msg.send()
|
msg.send()
|
||||||
newsletter.sent = True
|
newsletter.sent = True
|
||||||
newsletter.save()
|
newsletter.save()
|
||||||
|
27
newsletters/migrations/0005_auto_20180226_1740.py
Normal file
27
newsletters/migrations/0005_auto_20180226_1740.py
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.11.5 on 2018-02-26 22:40
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('newsletters', '0004_auto_20180114_2108'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.CreateModel(
|
||||||
|
name='Attachment',
|
||||||
|
fields=[
|
||||||
|
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||||
|
('file', models.FileField(upload_to='Newsletter')),
|
||||||
|
],
|
||||||
|
),
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='newsletter',
|
||||||
|
name='attachments',
|
||||||
|
field=models.ManyToManyField(to='newsletters.Attachment'),
|
||||||
|
),
|
||||||
|
]
|
@ -5,6 +5,16 @@ from django.utils import timezone
|
|||||||
from tinymce.models import HTMLField
|
from tinymce.models import HTMLField
|
||||||
|
|
||||||
|
|
||||||
|
class Attachment(models.Model):
|
||||||
|
file = models.FileField(upload_to='Newsletter')
|
||||||
|
|
||||||
|
def name(self):
|
||||||
|
return self.file.name.replace('Newsletter/', '')
|
||||||
|
|
||||||
|
def __str__(self):
|
||||||
|
return self.file.name
|
||||||
|
|
||||||
|
|
||||||
class Newsletter(models.Model):
|
class Newsletter(models.Model):
|
||||||
body = HTMLField()
|
body = HTMLField()
|
||||||
created = models.DateTimeField(auto_now_add=True)
|
created = models.DateTimeField(auto_now_add=True)
|
||||||
@ -12,6 +22,7 @@ class Newsletter(models.Model):
|
|||||||
publish = models.DateTimeField(default=timezone.now)
|
publish = models.DateTimeField(default=timezone.now)
|
||||||
sent = models.BooleanField(default=False)
|
sent = models.BooleanField(default=False)
|
||||||
subject = models.CharField(max_length=255)
|
subject = models.CharField(max_length=255)
|
||||||
|
attachments = models.ManyToManyField(Attachment)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.subject
|
return self.subject
|
||||||
|
@ -31,6 +31,20 @@
|
|||||||
<div class="p-3" style="overflow:hidden;">
|
<div class="p-3" style="overflow:hidden;">
|
||||||
{{ newsletter.body | safe }}
|
{{ newsletter.body | safe }}
|
||||||
</div>
|
</div>
|
||||||
|
{% if newsletter.attachments.all %}
|
||||||
|
<div class="px-3 pb-3">
|
||||||
|
<u>
|
||||||
|
<i class="fa fa-paperclip"></i> Attachments
|
||||||
|
</u>
|
||||||
|
<br>
|
||||||
|
{% for attachment in newsletter.attachments.all %}
|
||||||
|
{{ attachment.name }}
|
||||||
|
<a class="inline" href="/media/{{ attachment }}" target="_blank">view</a> |
|
||||||
|
<a class="inline" href="/media/{{ attachment }}" target="_blank" download>download</a>
|
||||||
|
<br>
|
||||||
|
{% endfor %}
|
||||||
|
</div>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
|
20
newsroom/migrations/0002_auto_20180226_1740.py
Normal file
20
newsroom/migrations/0002_auto_20180226_1740.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
# Generated by Django 1.11.5 on 2018-02-26 22:40
|
||||||
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('newsroom', '0001_initial'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='attachment',
|
||||||
|
name='file',
|
||||||
|
field=models.FileField(upload_to='PressRelease'),
|
||||||
|
),
|
||||||
|
]
|
Loading…
Reference in New Issue
Block a user