Free and open source ticket system written in python
1from ninja import ModelSchema
2from ninja.orm import create_schema
3from .models import Ticket, Category, Comment, FileAttachment
4from core.models import PawUser
5
6class UserSchema(ModelSchema):
7 class Meta:
8 model = PawUser
9 fields = ['id', 'username', 'profile_picture']
10
11class CategorySchema(ModelSchema):
12 class Meta:
13 model = Category
14 fields = ['id', 'name']
15
16class FileAttachmentSchema(ModelSchema):
17 class Meta:
18 model = FileAttachment
19 fields = ['id', 'file', 'uploaded_at']
20
21class TicketSchema(ModelSchema):
22 """Schema for ticket with nested user information."""
23 user: UserSchema
24 assigned_to: UserSchema | None = None
25 category: CategorySchema | None = None
26
27 class Meta:
28 model = Ticket
29 fields = ['id', 'title', 'user', 'assigned_to', 'category', 'status', 'priority', 'created_at', 'updated_at']
30
31class TicketDetailSchema(ModelSchema):
32 user: UserSchema
33 assigned_to: UserSchema | None = None
34 category: CategorySchema | None = None
35 follow_up_to: TicketSchema | None = None
36 followed_up_by: list[TicketSchema] = []
37 attachments: list[FileAttachmentSchema] = []
38
39 class Meta:
40 model = Ticket
41 fields = ['id', 'title', 'user', 'assigned_to', 'category', 'status', 'priority', 'created_at', 'updated_at', 'description', 'follow_up_to']
42
43 @staticmethod
44 def resolve_attachments(obj: Ticket):
45 """Resolve attachments from reverse ForeignKey relationship."""
46 return obj.fileattachment_set.all()
47
48class CommentSchema(ModelSchema):
49 user: UserSchema
50 class Meta:
51 model = Comment
52 fields = ['id', 'user', 'text', 'created_at']