2017-08-18 21:25:17 -04:00
|
|
|
from django.contrib import admin
|
|
|
|
|
2018-02-26 18:13:02 -05:00
|
|
|
from .models import Attachment, Newsletter, Subscriber
|
|
|
|
|
|
|
|
|
|
|
|
admin.site.register(Attachment)
|
2017-08-18 21:25:17 -04:00
|
|
|
|
|
|
|
|
2018-01-14 19:32:00 -05:00
|
|
|
@admin.register(Subscriber)
|
|
|
|
class SubscriberAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['email', 'date']
|
|
|
|
list_filter = ['date']
|
|
|
|
search_fields = ['email']
|
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']
|
2018-02-26 18:13:02 -05:00
|
|
|
filter_horizontal = ['attachments']
|
2017-08-18 21:25:17 -04:00
|
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
if obj:
|
2018-02-26 18:13:02 -05:00
|
|
|
self.fields = ['creator', 'subject', 'body', 'sent', 'publish', 'attachments']
|
2017-09-03 18:06:59 -04:00
|
|
|
if obj.sent:
|
2018-02-26 18:13:02 -05:00
|
|
|
self.readonly_fields = ['creator', 'subject', 'body', 'sent', 'publish', 'attachments']
|
2017-08-18 21:25:17 -04:00
|
|
|
else:
|
2017-09-03 18:06:59 -04:00
|
|
|
self.readonly_fields = ['creator', 'sent']
|
2017-08-18 21:25:17 -04:00
|
|
|
else:
|
2018-02-26 18:13:02 -05:00
|
|
|
self.fields = ['subject', 'body', 'publish', 'attachments']
|
2017-08-18 21:25:17 -04:00
|
|
|
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
|