oacpl/newsletter/admin.py

33 lines
1.0 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-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', 'publish']
if obj.publish < timezone.now():
self.readonly_fields = ['creator', 'subject', 'body', 'publish']
else:
self.readonly_fields = ['creator']
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)
instance.creator = user
instance.save()
return instance