11
11
from django .http import HttpResponse , JsonResponse
12
12
from pretix .base .models import Event , OrderPosition
13
13
from pretix .control .views .event import EventSettingsFormView , EventSettingsViewMixin
14
+ from django .shortcuts import get_object_or_404
15
+ from rest_framework import viewsets
16
+ from rest_framework .response import Response
14
17
import json
15
18
16
19
logger = logging .getLogger (__name__ )
@@ -27,12 +30,6 @@ class FznackendutilsSettingsForm(SettingsForm):
27
30
widget = forms .TextInput ,
28
31
regex = re .compile (r'^(https://.*/.*|http://localhost[:/].*)*$' )
29
32
)
30
- fzbackendutils_internal_endpoint_token = forms .CharField (
31
- label = _ ("Internal endpoint token" ),
32
- help_text = _ ("This plugin exposes some api for extra access to the fz-backend. This token needs to be specified in the "
33
- "<code>fz-backend-api</code> header to access these endpoints." ),
34
- required = False ,
35
- )
36
33
37
34
38
35
class FznackendutilsSettings (EventSettingsViewMixin , EventSettingsFormView ):
@@ -49,11 +46,11 @@ def get_success_url(self) -> str:
49
46
50
47
51
48
@method_decorator (xframe_options_exempt , "dispatch" )
52
- class ApiSetItemBundle ( View ):
53
- def post ( self , request , * args , ** kwargs ):
54
- token = request . headers . get ( 'fz-backend-api' )
55
- if request . event . settings . fzbackendutils_internal_endpoint_token and ( not token or token != request . event . settings . fzbackendutils_internal_endpoint_token ):
56
- return JsonResponse ({ 'error' : 'Invalid token' }, status = 403 )
49
+ class ApiViewSet ( viewsets . ViewSet ):
50
+ permission = 'can_view_orders'
51
+ write_permission = 'can_change_orders'
52
+
53
+ def set_is_bundle ( self , request ):
57
54
data = json .loads (request .body )
58
55
logger .info (f"Backend is trying to set is_bundle for position { data ['position' ]} to { data ['bundle' ]} " )
59
56
@@ -62,10 +59,7 @@ def post(self, request, *args, **kwargs):
62
59
if data ['bundle' ] is not True and data ['bundle' ] is not False and not isinstance (data ['bundle' ], bool ):
63
60
return JsonResponse ({'error' : 'Invalid bundle value' }, status = 400 )
64
61
65
- positionQuery = OrderPosition .objects .filter (id = data ['position' ])
66
- if not positionQuery :
67
- return JsonResponse ({'error' : 'Position not found' }, status = 404 )
68
- position : OrderPosition = positionQuery .first ()
62
+ position = get_object_or_404 (OrderPosition .objects .filter (id = data ['position' ]))
69
63
70
64
position .is_bundled = data ['bundle' ]
71
65
position .save (update_fields = ['is_bundled' ])
0 commit comments