tildetown-admin/ttadmin/help/admin.py

73 lines
2.3 KiB
Python
Raw Normal View History

2019-08-07 06:05:58 +00:00
from django.conf.urls import url
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
2019-08-07 06:05:58 +00:00
from .models import Ticket, Note, EmailTemplate
2019-07-16 02:44:03 +00:00
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
2019-08-07 06:05:58 +00:00
@admin.register(EmailTemplate)
class EmailTemplateAdmin(admin.ModelAdmin):
fields = ('name', 'body')
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-08-07 06:05:58 +00:00
change_form_template = "admin/ticket_with_email.html"
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)
2019-08-07 06:05:58 +00:00
def change_view(self, request, object_id, form_url='', extra_context=None):
if extra_context is None:
extra_context = {}
extra_context['email_templates'] = EmailTemplate.objects.all()
return super().change_view(request, object_id, form_url, extra_context=extra_context)
def get_urls(self):
urls = super().get_urls()
custom_urls = [
url(r'emails/send$', self.admin_site.admin_view(self.send_email_view)),
]
return urls + custom_urls
def send_email_view(self, request):
pass