From 972ad7a1d895018b77b13309f1941fbb0ad99289 Mon Sep 17 00:00:00 2001 From: jedi Date: Thu, 18 Jul 2024 20:32:31 +0200 Subject: [PATCH] stash --- core/tickets/api_v2.py | 30 +++++++++++++++++++++++++-- core/tickets/serializers.py | 11 ++++++++++ core/tickets/tests/v2/test_tickets.py | 20 ++++++++++++++++++ 3 files changed, 59 insertions(+), 2 deletions(-) diff --git a/core/tickets/api_v2.py b/core/tickets/api_v2.py index c1a3f8d..d32aca0 100644 --- a/core/tickets/api_v2.py +++ b/core/tickets/api_v2.py @@ -1,4 +1,5 @@ import logging +from base64 import b64decode from django.urls import re_path from django.contrib.auth.decorators import permission_required @@ -15,7 +16,7 @@ from mail.models import Email from mail.protocol import send_smtp, make_reply, collect_references from notify_sessions.models import SystemEvent from tickets.models import IssueThread, Comment, STATE_CHOICES, ShippingVoucher -from tickets.serializers import IssueSerializer, CommentSerializer, ShippingVoucherSerializer +from tickets.serializers import IssueSerializer, CommentSerializer, ShippingVoucherSerializer, SearchResultSerializer class IssueViewSet(viewsets.ModelViewSet): @@ -130,9 +131,33 @@ def add_comment(request, pk): return Response(CommentSerializer(comment).data, status=status.HTTP_201_CREATED) +def filter_issues(issues, query): + query_tokens = query.split(' ') + for issue in issues: + value = 0 + for token in query_tokens: + if token in issue.description: + value += 1 + if value > 0: + yield {'search_score': value, 'issue': issue} + + +@api_view(['GET']) +@permission_classes([]) +# @permission_classes([IsAuthenticated]) +# @permission_required('view_item', raise_exception=True) +def search_issues(request, event_slug, query): + try: + event = Event.objects.get(slug=event_slug) + items = filter_issues(IssueThread.objects.filter(event=event), b64decode(query).decode('utf-8')) + return Response(SearchResultSerializer(items, many=True).data) + except Event.DoesNotExist: + return Response(status=404) + + router = routers.SimpleRouter() router.register(r'tickets', IssueViewSet, basename='issues') -#router.register(r'comments', CommentViewSet, basename='comments') +# router.register(r'comments', CommentViewSet, basename='comments') router.register(r'shipping_vouchers', ShippingVoucherViewSet, basename='shipping_vouchers') urlpatterns = ([ @@ -140,4 +165,5 @@ urlpatterns = ([ re_path(r'^tickets/(?P\d+)/reply/$', reply, name='reply'), re_path(r'^tickets/(?P\d+)/comment/$', add_comment, name='add_comment'), re_path(r'^(?P[\w-]+)/tickets/manual/$', manual_ticket, name='manual_ticket'), + re_path(r'^(?P[\w-]+)/tickets/(?P.+)/$', search_issues, name='search_issues'), ] + router.urls) diff --git a/core/tickets/serializers.py b/core/tickets/serializers.py index 1bac83f..9779bc5 100644 --- a/core/tickets/serializers.py +++ b/core/tickets/serializers.py @@ -137,3 +137,14 @@ class IssueSerializer(serializers.ModelSerializer): def get_queryset(self): return IssueThread.objects.all().order_by('-last_activity') + + +class SearchResultSerializer(serializers.Serializer): + search_score = serializers.IntegerField() + item = IssueSerializer() + + def to_representation(self, instance): + return {**IssueSerializer(instance['item']).data, 'search_score': instance['search_score']} + + class Meta: + model = IssueThread diff --git a/core/tickets/tests/v2/test_tickets.py b/core/tickets/tests/v2/test_tickets.py index 43fd71e..e76e422 100644 --- a/core/tickets/tests/v2/test_tickets.py +++ b/core/tickets/tests/v2/test_tickets.py @@ -9,6 +9,8 @@ from tickets.models import IssueThread, StateChange, Comment from django.contrib.auth.models import Permission from knox.models import AuthToken +from base64 import b64encode + class IssueApiTest(TestCase): @@ -285,6 +287,7 @@ class IssueApiTest(TestCase): content_type='application/json') self.assertEqual(response.status_code, 400) + #def test_post_comment(self): # issue = IssueThread.objects.create( # name="test issue", @@ -359,3 +362,20 @@ class IssueApiTest(TestCase): self.assertEqual('pending_new', timeline[0]['state']) self.assertEqual('assignment', timeline[1]['type']) self.assertEqual(self.user.username, timeline[1]['assigned_to']) + + +class IssueSearchTest(TestCase): + + def setUp(self): + super().setUp() + self.user = ExtendedUser.objects.create_user('testuser', 'test', 'test') + self.user.user_permissions.add(*Permission.objects.all()) + self.user.save() + self.token = AuthToken.objects.create(user=self.user) + self.client = Client(headers={'Authorization': 'Token ' + self.token[1]}) + + def test_search(self): + search_query = b64encode(b'abc').decode('utf-8') + response = self.client.get(f'/api/2/evt/tickets/{search_query}/') + self.assertEqual(200, response.status_code) + self.assertEqual([], response.json())