working support for tickete notes

admin-improvements
Nate Smith 2019-07-18 17:04:59 -05:00
parent dfd843064d
commit 02a50e1d11
3 changed files with 56 additions and 9 deletions

View File

@ -2,22 +2,43 @@ from django.contrib import admin
from django.forms import ModelForm from django.forms import ModelForm
from .models import Ticket, Note from .models import Ticket, Note
class NoteInline(admin.StackedInline):
class ImmutableNoteInline(admin.TabularInline):
model = Note model = Note
fields = ('created_at', 'body', 'author')
readonly_fields = ('created_at', 'body', 'author')
ordering = ('created_at',)
extra = 1 extra = 1
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()
@admin.register(Ticket) @admin.register(Ticket)
class TicketAdmin(admin.ModelAdmin): class TicketAdmin(admin.ModelAdmin):
inlines = [ImmutableNoteInline, NewNoteInline]
readonly_fields = ('submitted',) readonly_fields = ('submitted',)
list_display = ('submitted', 'issue_status', 'issue_type', 'name', 'email') list_display = ('submitted', 'issue_status', 'issue_type', 'name', 'email')
list_filter = ('issue_status', 'issue_type') list_filter = ('issue_status', 'issue_type')
fields = ('submitted', 'name', 'email', 'issue_status', 'issue_type', 'issue_text') fields = ('submitted', 'name', 'email', 'issue_status', 'issue_type', 'issue_text')
def save_related(request, form, formsets, change): def save_related(self, request, form, formsets, change):
# THIS IS EXTREMELY BOOTLEG AND MAY BREAK IF MORE INLINES ARE ADDED TO THIS ADMIN.
for formset in formsets: for formset in formsets:
import ipdb; ipdb.set_trace() if len(formset.forms) == 1:
pass # TODO set author based on request # It's probably the add new note form (i hope).
super() 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)

View File

@ -0,0 +1,22 @@
# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2019-07-16 02:58
from __future__ import unicode_literals
from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('help', '0005_note'),
]
operations = [
migrations.AlterField(
model_name='note',
name='author',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]

View File

@ -1,3 +1,4 @@
from django.contrib.auth.models import User
from django.db.models import Model, TextField, EmailField, CharField, DateTimeField, ForeignKey from django.db.models import Model, TextField, EmailField, CharField, DateTimeField, ForeignKey
ISSUE_TYPE_CHOICES = ( ISSUE_TYPE_CHOICES = (
@ -39,5 +40,8 @@ class Ticket(Model):
class Note(Model): class Note(Model):
created = DateTimeField(auto_now_add=True) created = DateTimeField(auto_now_add=True)
body = TextField(blank=False, null=False) body = TextField(blank=False, null=False)
author = ForeignKey('users.Townie') author = ForeignKey(User)
ticket = ForeignKey(Ticket) ticket = ForeignKey(Ticket)
def __str__(self):
return "admin note"