oacpl/forum/admin.py

51 lines
1.6 KiB
Python
Raw Normal View History

2017-08-26 11:40:35 -04:00
from django.contrib import admin
from .models import Thread, Post, Comment
admin.site.register(Thread)
@admin.register(Post)
class PostAdmin(admin.ModelAdmin):
2018-01-11 00:58:07 -05:00
list_display = ['title', 'resolved', 'topic', 'creator', 'created']
search_fields = ['title', 'resolved', 'topic', 'creator', 'created']
2017-08-26 11:40:35 -04:00
def get_form(self, request, obj=None, **kwargs):
if obj:
2018-01-11 00:58:07 -05:00
self.fields = ['creator', 'created', 'resolved', 'title', 'topic', 'question']
2017-08-26 11:40:35 -04:00
self.readonly_fields = ['creator', 'created']
else:
2018-01-11 00:58:07 -05:00
self.fields = ['title', 'topic', 'question']
2017-08-26 11:40:35 -04:00
self.readonly_fields = []
return super(PostAdmin, 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
@admin.register(Comment)
class CommentAdmin(admin.ModelAdmin):
list_display = ['post', 'creator', 'created']
search_fields = ['post', 'creator', 'created']
def get_form(self, request, obj=None, **kwargs):
if obj:
self.fields = ['creator', 'created', 'post', 'reply']
self.readonly_fields = ['post', 'creator', 'created']
else:
self.fields = ['post', 'reply']
self.readonly_fields = []
return super(CommentAdmin, 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