oacpl/newsletters/admin.py

35 lines
1.1 KiB
Python
Raw Normal View History

2017-08-18 21:25:17 -04:00
from django.contrib import admin
from django.utils import timezone
2017-09-30 12:47:47 -04:00
from OACPL import settings
2017-08-18 23:23:03 -04:00
from .models import Newsletter, Subscriber
2017-08-18 21:25:17 -04:00
2017-08-18 23:23:03 -04:00
admin.site.register(Subscriber)
2017-08-18 21:25:17 -04:00
@admin.register(Newsletter)
class NewsletterAdmin(admin.ModelAdmin):
list_display = ['subject', 'created', 'publish']
search_fields = ['subject', 'created', 'publish']
def get_form(self, request, obj=None, **kwargs):
if obj:
self.fields = ['creator', 'subject', 'body', 'sent', 'publish']
if obj.sent:
self.readonly_fields = ['creator', 'subject', 'body', 'sent', 'publish']
2017-08-18 21:25:17 -04:00
else:
self.readonly_fields = ['creator', 'sent']
2017-08-18 21:25:17 -04:00
else:
self.fields = ['subject', 'body', 'publish']
self.readonly_fields = []
return super(NewsletterAdmin, self).get_form(request, obj, **kwargs)
def save_model(self, request, instance, form, change):
user = request.user
instance = form.save(commit=False)
2017-09-30 12:47:47 -04:00
instance.body = instance.body.replace('src="', 'src="' + settings.BASE_URL)
2017-08-18 21:25:17 -04:00
instance.creator = user
instance.save()
return instance