add /item/comment endpoint and prefetch related models

This commit is contained in:
j3d1 2024-11-28 21:58:26 +01:00
parent 52cf00ef9c
commit c03d298a58
4 changed files with 66 additions and 11 deletions

View file

@ -47,6 +47,8 @@ class IssueSerializer(BasicIssueSerializer):
model = IssueThread
fields = ('id', 'timeline', 'name', 'state', 'assigned_to', 'last_activity', 'uuid', 'related_items', 'event')
read_only_fields = ('id', 'timeline', 'last_activity', 'uuid', 'related_items')
prefetch_related_fields = ['state_changes', 'comments', 'emails', 'emails__attachments', 'assignments',
'item_relation_changes', 'shipping_vouchers']
def to_internal_value(self, data):
ret = super().to_internal_value(data)
@ -63,12 +65,14 @@ class IssueSerializer(BasicIssueSerializer):
@staticmethod
def get_last_activity(self):
try:
last_state_change = max([t.timestamp for t in self.state_changes.all()]) if self.state_changes.exists() else None
last_state_change = max(
[t.timestamp for t in self.state_changes.all()]) if self.state_changes.exists() else None
last_comment = max([t.timestamp for t in self.comments.all()]) if self.comments.exists() else None
last_mail = max([t.timestamp for t in self.emails.all()]) if self.emails.exists() else None
last_assignment = max([t.timestamp for t in self.assignments.all()]) if self.assignments.exists() else None
last_relation = max([t.timestamp for t in self.item_relation_changes.all()]) if self.item_relation_changes.exists() else None
last_relation = max([t.timestamp for t in
self.item_relation_changes.all()]) if self.item_relation_changes.exists() else None
args = [x for x in [last_state_change, last_comment, last_mail, last_assignment, last_relation] if
x is not None]
return max(args)
@ -129,7 +133,6 @@ class IssueSerializer(BasicIssueSerializer):
return sorted(timeline, key=lambda x: x['timestamp'])
class SearchResultSerializer(serializers.Serializer):
search_score = serializers.IntegerField()
item = IssueSerializer()