tildetown-admin/ttadmin/help/admin.py

45 lines
1.5 KiB
Python
Raw Permalink Normal View History

2016-12-19 07:20:48 +00:00
from django.contrib import admin
2019-07-16 02:44:03 +00:00
from django.forms import ModelForm
from .models import Ticket, Note
2019-07-18 22:04:59 +00:00
class ImmutableNoteInline(admin.TabularInline):
2019-07-16 02:44:03 +00:00
model = Note
extra = 1
2019-07-18 22:04:59 +00:00
max_num = 0
fields = ('author', 'created', 'body')
readonly_fields = ('author', 'created', 'body')
can_delete = False
ordering = ('created',)
class NewNoteInline(admin.StackedInline):
model = Note
extra = 0
fields = ('body',)
def get_queryset(self, request):
queryset = super().get_queryset(request)
return queryset.none()
2016-12-19 07:20:48 +00:00
@admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin):
2019-07-18 22:04:59 +00:00
inlines = [ImmutableNoteInline, NewNoteInline]
2019-07-18 22:25:28 +00:00
readonly_fields = ('submitted', 'issue_type')
list_display = ('submitted', 'issue_status', 'assigned', 'issue_type', 'name', 'email',)
list_filter = ('issue_status', 'issue_type', 'assigned')
fields = ('submitted', 'name', 'email', 'assigned', 'issue_status', 'issue_type', 'issue_text')
2019-07-16 02:44:03 +00:00
2019-07-18 22:04:59 +00:00
def save_related(self, request, form, formsets, change):
# THIS IS EXTREMELY BOOTLEG AND MAY BREAK IF MORE INLINES ARE ADDED TO THIS ADMIN.
2019-07-16 02:44:03 +00:00
for formset in formsets:
2019-07-18 22:04:59 +00:00
if len(formset.forms) == 1:
# It's probably the add new note form (i hope).
note_form = formset.forms[0]
note_form.instance.author = request.user
note_form.instance.save()
note_form.save(commit=False)
note_form.save_m2m()
return super().save_related(request, form, formsets, change)