Added attachments

This commit is contained in:
Zakary Timson 2018-02-26 18:13:02 -05:00
parent fca60d4376
commit 9a48e3f724
6 changed files with 84 additions and 7 deletions

View File

@ -1,8 +1,9 @@
from django.contrib import admin
from django.utils import timezone
from OACPL import settings
from .models import Newsletter, Subscriber
from .models import Attachment, Newsletter, Subscriber
admin.site.register(Attachment)
@admin.register(Subscriber)
@ -16,16 +17,17 @@ class SubscriberAdmin(admin.ModelAdmin):
class NewsletterAdmin(admin.ModelAdmin):
list_display = ['subject', 'created', 'publish']
search_fields = ['subject', 'created', 'publish']
filter_horizontal = ['attachments']
def get_form(self, request, obj=None, **kwargs):
if obj:
self.fields = ['creator', 'subject', 'body', 'sent', 'publish']
self.fields = ['creator', 'subject', 'body', 'sent', 'publish', 'attachments']
if obj.sent:
self.readonly_fields = ['creator', 'subject', 'body', 'sent', 'publish']
self.readonly_fields = ['creator', 'subject', 'body', 'sent', 'publish', 'attachments']
else:
self.readonly_fields = ['creator', 'sent']
else:
self.fields = ['subject', 'body', 'publish']
self.fields = ['subject', 'body', 'publish', 'attachments']
self.readonly_fields = []
return super(NewsletterAdmin, self).get_form(request, obj, **kwargs)

View File

@ -1,8 +1,9 @@
import mimetypes
from django.db.models import Q
from django.core.mail import EmailMessage
from django.core.management.base import BaseCommand
from django.utils import timezone
from django.utils.html import strip_tags
from OACPL import settings
from OACPL.utils import url_fix_render_to_string
@ -21,6 +22,8 @@ class Command(BaseCommand):
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.content_subtype = 'html'
for attachment in newsletter.attachments.all():
msg.attach(attachment.name(), attachment.file.read(), mimetypes.guess_type(attachment.name())[0])
msg.send()
newsletter.sent = True
newsletter.save()

View 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'),
),
]

View File

@ -5,6 +5,16 @@ from django.utils import timezone
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):
body = HTMLField()
created = models.DateTimeField(auto_now_add=True)
@ -12,6 +22,7 @@ class Newsletter(models.Model):
publish = models.DateTimeField(default=timezone.now)
sent = models.BooleanField(default=False)
subject = models.CharField(max_length=255)
attachments = models.ManyToManyField(Attachment)
def __str__(self):
return self.subject

View File

@ -31,6 +31,20 @@
<div class="p-3" style="overflow:hidden;">
{{ newsletter.body | safe }}
</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>
{% endfor %}

View 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'),
),
]