2023-11-23 22:17:20 +00:00
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from django.urls import path, re_path
|
2023-12-13 07:09:29 +00:00
|
|
|
from django.contrib.auth.decorators import permission_required
|
2023-11-23 22:17:20 +00:00
|
|
|
from rest_framework import routers, viewsets, serializers
|
2023-12-13 07:09:29 +00:00
|
|
|
from rest_framework.decorators import api_view, permission_classes
|
2023-11-23 22:17:20 +00:00
|
|
|
from rest_framework.response import Response
|
2023-12-13 07:09:29 +00:00
|
|
|
from rest_framework.permissions import IsAuthenticated
|
2023-11-23 22:17:20 +00:00
|
|
|
|
|
|
|
from files.models import File
|
|
|
|
from inventory.models import Event, Container, Item
|
|
|
|
|
|
|
|
|
|
|
|
class EventSerializer(serializers.ModelSerializer):
|
|
|
|
class Meta:
|
|
|
|
model = Event
|
|
|
|
fields = ['eid', 'slug', 'name', 'start', 'end', 'pre_start', 'post_end']
|
|
|
|
read_only_fields = ['eid']
|
|
|
|
|
|
|
|
|
|
|
|
class EventViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = EventSerializer
|
|
|
|
queryset = Event.objects.all()
|
|
|
|
permission_classes = []
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerSerializer(serializers.ModelSerializer):
|
|
|
|
itemCount = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Container
|
|
|
|
fields = ['cid', 'name', 'itemCount']
|
|
|
|
read_only_fields = ['cid', 'itemCount']
|
|
|
|
|
|
|
|
def get_itemCount(self, instance):
|
|
|
|
return Item.objects.filter(container=instance.cid).count()
|
|
|
|
|
|
|
|
|
|
|
|
class ContainerViewSet(viewsets.ModelViewSet):
|
|
|
|
serializer_class = ContainerSerializer
|
|
|
|
queryset = Container.objects.all()
|
|
|
|
|
|
|
|
|
|
|
|
class ItemSerializer(serializers.ModelSerializer):
|
2023-12-11 21:51:05 +00:00
|
|
|
dataImage = serializers.CharField(write_only=True, required=False)
|
2023-11-23 22:17:20 +00:00
|
|
|
cid = serializers.SerializerMethodField()
|
|
|
|
box = serializers.SerializerMethodField()
|
|
|
|
file = serializers.SerializerMethodField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Item
|
2023-12-11 21:51:05 +00:00
|
|
|
fields = ['cid', 'box', 'uid', 'description', 'file', 'dataImage']
|
2023-11-23 22:17:20 +00:00
|
|
|
read_only_fields = ['uid']
|
|
|
|
|
|
|
|
def get_cid(self, instance):
|
|
|
|
return instance.container.cid
|
|
|
|
|
|
|
|
def get_box(self, instance):
|
|
|
|
return instance.container.name
|
|
|
|
|
|
|
|
def get_file(self, instance):
|
|
|
|
if len(instance.files.all()) > 0:
|
|
|
|
return instance.files.all().order_by('-created_at')[0].hash
|
|
|
|
return None
|
|
|
|
|
|
|
|
def to_internal_value(self, data):
|
|
|
|
if 'cid' in data:
|
|
|
|
container = Container.objects.get(cid=data['cid'])
|
|
|
|
internal = super().to_internal_value(data)
|
|
|
|
internal['container'] = container
|
|
|
|
return internal
|
|
|
|
return super().to_internal_value(data)
|
|
|
|
|
|
|
|
def validate(self, attrs):
|
|
|
|
return super().validate(attrs)
|
|
|
|
|
|
|
|
def create(self, validated_data):
|
|
|
|
if 'dataImage' in validated_data:
|
2023-12-11 21:51:05 +00:00
|
|
|
file = File.objects.create(data=validated_data['dataImage'])
|
2023-11-23 22:17:20 +00:00
|
|
|
validated_data.pop('dataImage')
|
2023-12-11 21:51:05 +00:00
|
|
|
item = Item.objects.create(**validated_data)
|
|
|
|
item.files.set([file])
|
|
|
|
return item
|
2023-11-23 22:17:20 +00:00
|
|
|
return Item.objects.create(**validated_data)
|
|
|
|
|
|
|
|
def update(self, instance, validated_data):
|
|
|
|
if 'returned' in validated_data:
|
|
|
|
if validated_data['returned']:
|
|
|
|
validated_data['returned_at'] = datetime.now()
|
|
|
|
validated_data.pop('returned')
|
|
|
|
if 'dataImage' in validated_data:
|
2023-12-11 21:51:05 +00:00
|
|
|
file = File.objects.create(data=validated_data['dataImage'])
|
2023-11-23 22:17:20 +00:00
|
|
|
validated_data.pop('dataImage')
|
2023-12-11 21:51:05 +00:00
|
|
|
instance.files.add(file)
|
2023-11-23 22:17:20 +00:00
|
|
|
return super().update(instance, validated_data)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET'])
|
2023-12-13 07:09:29 +00:00
|
|
|
@permission_classes([IsAuthenticated])
|
2023-12-13 16:12:15 +00:00
|
|
|
@permission_required('view_item', raise_exception=True)
|
2023-11-23 22:17:20 +00:00
|
|
|
def search_items(request, event_slug, query):
|
|
|
|
try:
|
|
|
|
event = Event.objects.get(slug=event_slug)
|
|
|
|
query_tokens = query.split(' ')
|
|
|
|
q = Item.objects.filter(event=event)
|
|
|
|
for token in query_tokens:
|
|
|
|
if token:
|
|
|
|
q = q.filter(description__icontains=token)
|
|
|
|
return Response(ItemSerializer(q, many=True).data)
|
|
|
|
except Event.DoesNotExist:
|
|
|
|
return Response(status=404)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET', 'POST'])
|
2023-12-13 07:09:29 +00:00
|
|
|
@permission_classes([IsAuthenticated])
|
2023-11-23 22:17:20 +00:00
|
|
|
def item(request, event_slug):
|
|
|
|
try:
|
|
|
|
event = Event.objects.get(slug=event_slug)
|
|
|
|
if request.method == 'GET':
|
2023-12-13 16:12:15 +00:00
|
|
|
if not request.user.has_event_perm(event, 'view_item'):
|
2023-12-13 07:09:29 +00:00
|
|
|
return Response(status=403)
|
2023-11-23 22:17:20 +00:00
|
|
|
return Response(ItemSerializer(Item.objects.filter(event=event), many=True).data)
|
|
|
|
elif request.method == 'POST':
|
2023-12-13 16:12:15 +00:00
|
|
|
if not request.user.has_event_perm(event, 'add_item'):
|
2023-12-13 07:09:29 +00:00
|
|
|
return Response(status=403)
|
2023-11-23 22:17:20 +00:00
|
|
|
validated_data = ItemSerializer(data=request.data)
|
|
|
|
if validated_data.is_valid():
|
|
|
|
validated_data.save(event=event)
|
|
|
|
return Response(validated_data.data, status=201)
|
|
|
|
except Event.DoesNotExist:
|
|
|
|
return Response(status=404)
|
|
|
|
|
|
|
|
|
|
|
|
@api_view(['GET', 'PUT', 'DELETE'])
|
2023-12-13 07:09:29 +00:00
|
|
|
@permission_classes([IsAuthenticated])
|
2023-11-23 22:17:20 +00:00
|
|
|
def item_by_id(request, event_slug, id):
|
|
|
|
try:
|
|
|
|
event = Event.objects.get(slug=event_slug)
|
|
|
|
item = Item.objects.get(event=event, uid=id)
|
|
|
|
if request.method == 'GET':
|
2023-12-13 16:12:15 +00:00
|
|
|
if not request.user.has_event_perm(event, 'view_item'):
|
2023-12-13 07:09:29 +00:00
|
|
|
return Response(status=403)
|
2023-11-23 22:17:20 +00:00
|
|
|
return Response(ItemSerializer(item).data)
|
|
|
|
elif request.method == 'PUT':
|
2023-12-13 16:12:15 +00:00
|
|
|
if not request.user.has_event_perm(event, 'change_item'):
|
2023-12-13 07:09:29 +00:00
|
|
|
return Response(status=403)
|
2023-11-23 22:17:20 +00:00
|
|
|
validated_data = ItemSerializer(item, data=request.data)
|
|
|
|
if validated_data.is_valid():
|
|
|
|
validated_data.save()
|
|
|
|
return Response(validated_data.data)
|
2023-12-13 16:12:15 +00:00
|
|
|
return Response(validated_data.errors, status=400)
|
2023-11-23 22:17:20 +00:00
|
|
|
elif request.method == 'DELETE':
|
2023-12-13 16:12:15 +00:00
|
|
|
if not request.user.has_event_perm(event, 'delete_item'):
|
2023-12-13 07:09:29 +00:00
|
|
|
return Response(status=403)
|
2023-11-23 22:17:20 +00:00
|
|
|
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 + [
|
|
|
|
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),
|
|
|
|
]
|