Skip to content

Commit 102a35d

Browse files
committed
Tried making it as a proper api method but failed :)
1 parent 820cbc7 commit 102a35d

File tree

3 files changed

+10
-17
lines changed

3 files changed

+10
-17
lines changed

pretix_fzbackend_utils/templates/pretix_fzbackend_utils/settings.html

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ <h1>{% trans "Fz-backend-utils settings" %}</h1>
1313
{% bootstrap_form_errors form %}
1414

1515
{% bootstrap_field form.fzbackendutils_redirect_url layout="horizontal" %}
16-
{% bootstrap_field form.fzbackendutils_internal_endpoint_token layout="horizontal" %}
1716

1817
<div class="form-group submit-group">
1918
<button type="submit" class="btn btn-primary btn-save">

pretix_fzbackend_utils/urls.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from .views import FznackendutilsSettings
44

5-
from pretix.multidomain import event_url
5+
from pretix.api.urls import event_router
66

77
urlpatterns = [
88
re_path(r'^control/event/(?P<organizer>[^/]+)/(?P<event>[^/]+)/fzbackendutils/settings$',

pretix_fzbackend_utils/views.py

+9-15
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
from django.http import HttpResponse, JsonResponse
1212
from pretix.base.models import Event, OrderPosition
1313
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
1417
import json
1518

1619
logger = logging.getLogger(__name__)
@@ -27,12 +30,6 @@ class FznackendutilsSettingsForm(SettingsForm):
2730
widget=forms.TextInput,
2831
regex=re.compile(r'^(https://.*/.*|http://localhost[:/].*)*$')
2932
)
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-
)
3633

3734

3835
class FznackendutilsSettings(EventSettingsViewMixin, EventSettingsFormView):
@@ -49,11 +46,11 @@ def get_success_url(self) -> str:
4946

5047

5148
@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):
5754
data = json.loads(request.body)
5855
logger.info(f"Backend is trying to set is_bundle for position {data['position']} to {data['bundle']}")
5956

@@ -62,10 +59,7 @@ def post(self, request, *args, **kwargs):
6259
if data['bundle'] is not True and data['bundle'] is not False and not isinstance(data['bundle'], bool):
6360
return JsonResponse({'error': 'Invalid bundle value'}, status=400)
6461

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']))
6963

7064
position.is_bundled = data['bundle']
7165
position.save(update_fields=['is_bundled'])

0 commit comments

Comments
 (0)