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-14 19:32:00 -05:00
|
|
|
list_display = ['title', 'resolved', 'thread', 'creator', 'created']
|
|
|
|
list_filter = ['thread', 'resolved', 'created']
|
|
|
|
search_fields = ['title', 'resolved', 'thread', 'creator', 'created']
|
2017-08-26 11:40:35 -04:00
|
|
|
|
|
|
|
def get_form(self, request, obj=None, **kwargs):
|
|
|
|
if obj:
|
2018-01-14 19:32:00 -05:00
|
|
|
self.fields = ['creator', 'created', 'resolved', 'title', 'thread', 'question']
|
2017-08-26 11:40:35 -04:00
|
|
|
self.readonly_fields = ['creator', 'created']
|
|
|
|
else:
|
2018-01-14 19:32:00 -05:00
|
|
|
self.fields = ['title', 'thread', '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
|