2024-11-13 22:51:54 +00:00
|
|
|
from django.urls import re_path
|
2024-01-07 20:13:44 +00:00
|
|
|
from django.contrib.auth.decorators import permission_required
|
2024-06-22 23:28:21 +00:00
|
|
|
from rest_framework import routers, viewsets
|
2024-01-07 20:13:44 +00:00
|
|
|
from rest_framework.decorators import api_view, permission_classes
|
|
|
|
from rest_framework.response import Response
|
|
|
|
from rest_framework.permissions import IsAuthenticated
|
|
|
|
|
|
|
|
from inventory.models import Event, Container, Item
|
2024-07-17 18:22:40 +00:00
|
|
|
from inventory.serializers import EventSerializer, ContainerSerializer, ItemSerializer, SearchResultSerializer
|
|
|
|
|
|
|
|
from base64 import b64decode
|
2024-01-07 20:13:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
class EventViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = EventSerializer
|
|
|
|
queryset = Event.objects.all()
|
|
|
|
permission_classes = []
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = ContainerSerializer
|
|
|
|
queryset = Container.objects.all()
|
|
|
|
|
|
|
|
|
2024-07-17 16:59:54 +00:00
|
|
|
def filter_items(items, query):
|
|
|
|
query_tokens = query.split(' ')
|
|
|
|
for item in items:
|
2024-07-17 18:22:40 +00:00
|
|
|
value = 0
|
|
|
|
for token in query_tokens:
|
|
|
|
if token in item.description:
|
|
|
|
value += 1
|
|
|
|
if value > 0:
|
|
|
|
yield {'search_score': value, 'item': item}
|
|
|
|
|
2024-07-17 16:59:54 +00:00
|
|
|
|
2024-01-07 20:13:44 +00:00
|
|
|
@api_view(['GET'])
|
2024-11-19 22:03:44 +00:00
|
|
|
@permission_classes([IsAuthenticated])
|
2024-01-07 20:13:44 +00:00
|
|
|
def search_items(request, event_slug, query):
|
|
|
|
try:
|
|
|
|
event = Event.objects.get(slug=event_slug)
|
2024-11-19 22:03:44 +00:00
|
|
|
if not request.user.has_event_perm(event, 'view_item'):
|
|
|
|
return Response(status=403)
|
2024-07-17 18:22:40 +00:00
|
|
|
items = filter_items(Item.objects.filter(event=event), b64decode(query).decode('utf-8'))
|
|
|
|
return Response(SearchResultSerializer(items, many=True).data)
|
2024-01-07 20:13:44 +00:00
|
|
|
except Event.DoesNotExist:
|
|
|
|
return Response(status=404)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET', 'POST'])
|
|
|
|
@permission_classes([IsAuthenticated])
|
|
|
|
def item(request, event_slug):
|
|
|
|
try:
|
2024-11-13 22:51:54 +00:00
|
|
|
event = None
|
|
|
|
if event_slug != 'none':
|
|
|
|
event = Event.objects.get(slug=event_slug)
|
2024-01-07 20:13:44 +00:00
|
|
|
if request.method == 'GET':
|
|
|
|
if not request.user.has_event_perm(event, 'view_item'):
|
|
|
|
return Response(status=403)
|
|
|
|
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data)
|
|
|
|
elif request.method == 'POST':
|
|
|
|
if not request.user.has_event_perm(event, 'add_item'):
|
|
|
|
return Response(status=403)
|
|
|
|
validated_data = ItemSerializer(data=request.data)
|
|
|
|
if validated_data.is_valid():
|
|
|
|
validated_data.save(event=event)
|
|
|
|
return Response(validated_data.data, status=201)
|
2024-11-19 22:03:44 +00:00
|
|
|
return Response(status=400)
|
2024-01-07 20:13:44 +00:00
|
|
|
except Event.DoesNotExist:
|
|
|
|
return Response(status=404)
|
2024-11-19 22:03:44 +00:00
|
|
|
except KeyError:
|
|
|
|
return Response(status=400)
|
2024-01-07 20:13:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET', 'PUT', 'DELETE', 'PATCH'])
|
|
|
|
@permission_classes([IsAuthenticated])
|
|
|
|
def item_by_id(request, event_slug, id):
|
|
|
|
try:
|
|
|
|
event = Event.objects.get(slug=event_slug)
|
2024-11-19 22:03:44 +00:00
|
|
|
item = Item.objects.get(event=event, id=id)
|
2024-01-07 20:13:44 +00:00
|
|
|
if request.method == 'GET':
|
|
|
|
if not request.user.has_event_perm(event, 'view_item'):
|
|
|
|
return Response(status=403)
|
|
|
|
return Response(ItemSerializer(item).data)
|
|
|
|
elif request.method == 'PUT':
|
|
|
|
if not request.user.has_event_perm(event, 'change_item'):
|
|
|
|
return Response(status=403)
|
|
|
|
validated_data = ItemSerializer(item, data=request.data)
|
|
|
|
if validated_data.is_valid():
|
|
|
|
validated_data.save()
|
|
|
|
return Response(validated_data.data)
|
|
|
|
return Response(validated_data.errors, status=400)
|
|
|
|
elif request.method == 'PATCH':
|
|
|
|
if not request.user.has_event_perm(event, 'change_item'):
|
|
|
|
return Response(status=403)
|
|
|
|
validated_data = ItemSerializer(item, data=request.data, partial=True)
|
|
|
|
if validated_data.is_valid():
|
|
|
|
validated_data.save()
|
|
|
|
return Response(validated_data.data)
|
|
|
|
return Response(validated_data.errors, status=400)
|
|
|
|
elif request.method == 'DELETE':
|
|
|
|
if not request.user.has_event_perm(event, 'delete_item'):
|
|
|
|
return Response(status=403)
|
|
|
|
item.delete()
|
|
|
|
return Response(status=204)
|
|
|
|
except Item.DoesNotExist:
|
|
|
|
return Response(status=404)
|
|
|
|
except Event.DoesNotExist:
|
|
|
|
return Response(status=404)
|
|
|
|
|
|
|
|
|
|
|
|
router = routers.SimpleRouter()
|
|
|
|
router.register(r'events', EventViewSet, basename='events')
|
|
|
|
router.register(r'boxes', ContainerViewSet, basename='boxes')
|
|
|
|
router.register(r'box', ContainerViewSet, basename='boxes')
|
|
|
|
|
|
|
|
urlpatterns = router.urls + [
|
2024-07-18 19:11:53 +00:00
|
|
|
# path('<event_slug>/items/', item),
|
|
|
|
# path('<event_slug>/items/<query>/', search_items),
|
|
|
|
# path('<event_slug>/item/', item),
|
|
|
|
# path('<event_slug>/item/<id>/', item_by_id),
|
2024-11-13 22:51:54 +00:00
|
|
|
re_path(r'^(?P<event_slug>[\w-]+)/items/$', item, name='item'),
|
|
|
|
re_path(r'^(?P<event_slug>[\w-]+)/items/(?P<query>[-A-Za-z0-9+/]*={0,3})/$', search_items, name='search_items'),
|
|
|
|
re_path(r'^(?P<event_slug>[\w-]+)/item/$', item, name='item'),
|
|
|
|
re_path(r'^(?P<event_slug>[\w-]+)/item/(?P<id>\d+)/$', item_by_id, name='item_by_id'),
|
2024-01-07 20:13:44 +00:00
|
|
|
]
|