add /item/comment endpoint and prefetch related models
This commit is contained in:
parent
a59423780e
commit
b109e5995e
4 changed files with 66 additions and 11 deletions
|
@ -21,7 +21,13 @@ from tickets.shared_serializers import RelationSerializer
|
|||
|
||||
class IssueViewSet(viewsets.ModelViewSet):
|
||||
serializer_class = IssueSerializer
|
||||
queryset = IssueThread.objects.all().prefetch_related('state_changes', 'comments', 'emails', 'emails__attachments', 'assignments', 'item_relation_changes', 'shipping_vouchers')
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = IssueThread.objects.all()
|
||||
serializer = self.get_serializer_class()
|
||||
if hasattr(serializer, 'Meta') and hasattr(serializer.Meta, 'prefetch_related_fields'):
|
||||
queryset = queryset.prefetch_related(*serializer.Meta.prefetch_related_fields)
|
||||
return queryset
|
||||
|
||||
|
||||
class RelationViewSet(viewsets.ModelViewSet):
|
||||
|
@ -171,5 +177,5 @@ urlpatterns = ([
|
|||
re_path(r'^tickets/(?P<pk>\d+)/comment/$', add_comment, name='add_comment'),
|
||||
re_path(r'^(?P<event_slug>[\w-]+)/tickets/manual/$', manual_ticket, name='manual_ticket'),
|
||||
re_path(r'^(?P<event_slug>[\w-]+)/tickets/(?P<query>[-A-Za-z0-9+/]*={0,3})/$', search_issues,
|
||||
name='search_issues'),
|
||||
name='search_issues'),
|
||||
] + router.urls)
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue