From b7a582a86f5dd55b395135a23a81cf869e624a8a Mon Sep 17 00:00:00 2001 From: jedi Date: Wed, 20 Dec 2023 17:13:07 +0100 Subject: [PATCH] stash --- core/core/settings.py | 4 ++-- core/tickets/api_v2.py | 22 ++++++++++++++----- ...002_alter_issuethread_options_and_more.py} | 6 ++++- core/tickets/models.py | 1 + core/tickets/tests/v2/test_tickets.py | 19 +++++++++++++++- 5 files changed, 42 insertions(+), 10 deletions(-) rename core/tickets/migrations/{0002_issuethread_manually_created.py => 0002_alter_issuethread_options_and_more.py} (56%) diff --git a/core/core/settings.py b/core/core/settings.py index f8d7ebb..6fe4b9c 100644 --- a/core/core/settings.py +++ b/core/core/settings.py @@ -29,7 +29,7 @@ SECRET_KEY = 'django-insecure-tm*$w_14iqbiy-!7(8#ba7j+_@(7@rf2&a^!=shs&$03b%2*rv # SECURITY WARNING: don't run with debug turned on in production! DEBUG = True -ALLOWED_HOSTS = [os.getenv('HTTP_HOST', 'localhost')] +ALLOWED_HOSTS = [os.getenv('HTTP_HOST', 'localhost'), 'c3lf.de'] MAIL_DOMAIN = os.getenv('MAIL_DOMAIN', 'localhost') @@ -66,7 +66,7 @@ REST_FRAMEWORK = { 'TEST_REQUEST_DEFAULT_FORMAT': 'json', 'DEFAULT_AUTHENTICATION_CLASSES': ('knox.auth.TokenAuthentication',), 'DEFAULT_PERMISSION_CLASSES': ( - 'rest_framework.permissions.IsAuthenticated', 'rest_framework.permissions.DjangoModelPermissions'), + 'rest_framework.permissions.IsAuthenticated', 'rest_framework.permissions.DjangoModelPermissions'), } AUTH_USER_MODEL = 'authentication.ExtendedUser' diff --git a/core/tickets/api_v2.py b/core/tickets/api_v2.py index 02d4a10..695aa7f 100644 --- a/core/tickets/api_v2.py +++ b/core/tickets/api_v2.py @@ -1,6 +1,6 @@ import logging -from django.urls import path +from django.urls import re_path from django.contrib.auth.decorators import permission_required from rest_framework import routers, viewsets, serializers, status from rest_framework.decorators import api_view, permission_classes @@ -86,6 +86,15 @@ def reply(request, pk): @permission_classes([IsAuthenticated]) @permission_required('tickets.add_issuethread_manual', raise_exception=True) def manual_ticket(request): + if 'name' not in request.data: + return Response({'status': 'error', 'message': 'missing name'}, status=status.HTTP_400_BAD_REQUEST) + if 'sender' not in request.data: + return Response({'status': 'error', 'message': 'missing sender'}, status=status.HTTP_400_BAD_REQUEST) + if 'recipient' not in request.data: + return Response({'status': 'error', 'message': 'missing recipient'}, status=status.HTTP_400_BAD_REQUEST) + if 'body' not in request.data: + return Response({'status': 'error', 'message': 'missing body'}, status=status.HTTP_400_BAD_REQUEST) + issue = IssueThread.objects.create( name=request.data['name'], manually_created=True, @@ -103,13 +112,14 @@ def manual_ticket(request): 'general', {"type": "generic.event", "name": "send_message_to_frontend", "event_id": systemevent.id, "message": "email received"} ) - return Response({'status': 'ok'}, status=status.HTTP_201_CREATED) + + return Response(IssueSerializer(issue).data, status=status.HTTP_201_CREATED) router = routers.SimpleRouter() router.register(r'tickets', IssueViewSet, basename='issues') -urlpatterns = router.urls + [ - path('tickets//reply/', reply, name='reply'), - path('tickets/manual', manual_ticket, name='manual_ticket'), -] +urlpatterns = ([ + re_path(r'^tickets/(?P\d+)/reply/$', reply, name='reply'), + re_path(r'^tickets/manual/$', manual_ticket, name='manual_ticket'), + ] + router.urls) diff --git a/core/tickets/migrations/0002_issuethread_manually_created.py b/core/tickets/migrations/0002_alter_issuethread_options_and_more.py similarity index 56% rename from core/tickets/migrations/0002_issuethread_manually_created.py rename to core/tickets/migrations/0002_alter_issuethread_options_and_more.py index f55865a..3c1e4a7 100644 --- a/core/tickets/migrations/0002_issuethread_manually_created.py +++ b/core/tickets/migrations/0002_alter_issuethread_options_and_more.py @@ -1,4 +1,4 @@ -# Generated by Django 4.2.7 on 2023-12-17 23:50 +# Generated by Django 4.2.7 on 2023-12-22 20:57 from django.db import migrations, models @@ -10,6 +10,10 @@ class Migration(migrations.Migration): ] operations = [ + migrations.AlterModelOptions( + name='issuethread', + options={'permissions': [('send_mail', 'Can send mail'), ('add_issuethread_manual', 'Can add issue thread manually')]}, + ), migrations.AddField( model_name='issuethread', name='manually_created', diff --git a/core/tickets/models.py b/core/tickets/models.py index 807fe62..7130fe5 100644 --- a/core/tickets/models.py +++ b/core/tickets/models.py @@ -15,6 +15,7 @@ class IssueThread(SoftDeleteModel): class Meta: permissions = [ ('send_mail', 'Can send mail'), + ('add_issuethread_manual', 'Can add issue thread manually'), ] diff --git a/core/tickets/tests/v2/test_tickets.py b/core/tickets/tests/v2/test_tickets.py index 753c0b6..1f29963 100644 --- a/core/tickets/tests/v2/test_tickets.py +++ b/core/tickets/tests/v2/test_tickets.py @@ -5,7 +5,7 @@ from django.test import TestCase, Client from authentication.models import ExtendedUser from mail.models import Email from tickets.models import IssueThread, StateChange, Comment -from django.contrib.auth.models import User +from django.contrib.auth.models import Permission from knox.models import AuthToken @@ -14,6 +14,7 @@ class IssueApiTest(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]}) @@ -91,3 +92,19 @@ class IssueApiTest(TestCase): self.assertEqual(response.json()[0]['timeline'][3]['comment'], 'test') self.assertEqual(response.json()[0]['timeline'][3]['timestamp'], comment.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%fZ')) + + def test_manual_creation(self): + response = self.client.post('/api/2/tickets/manual/', {'name': 'test issue', 'sender': 'test', + 'recipient': 'test', 'body': 'test'}) + self.assertEqual(response.status_code, 201) + self.assertEqual(response.json()['state'], 'new') + self.assertEqual(response.json()['name'], 'test issue') + self.assertEqual(response.json()['assigned_to'], None) + timeline = response.json()['timeline'] + self.assertEqual(len(timeline), 1) + self.assertEqual(timeline[0]['type'], 'mail') + self.assertEqual(timeline[0]['sender'], 'test') + self.assertEqual(timeline[0]['recipient'], 'test') + self.assertEqual(timeline[0]['subject'], 'test issue') + self.assertEqual(timeline[0]['body'], 'test') +