mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2025-04-27 10:11:24 +08:00
Improve breakages
This commit is contained in:
parent
c1a7e21e95
commit
5d5ff22ee4
6 changed files with 100 additions and 152 deletions
|
@ -17,51 +17,12 @@ class BreakageReportSerializer(serializers.HyperlinkedModelSerializer):
|
|||
|
||||
class Meta:
|
||||
model = BreakageReport
|
||||
fields = ['id', 'transaction', 'equipments', 'status', 'timestamp']
|
||||
read_only_fields = ['id', 'timestamp']
|
||||
fields = ['id', 'transaction', 'equipments', 'resolved', 'timestamp']
|
||||
read_only_fields = ['id', 'transaction',
|
||||
'equipments', 'resolved', 'timestamp']
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
transaction = validated_data.get('transaction')
|
||||
equipments = validated_data.get('equipments')
|
||||
user = self.context['request'].user
|
||||
|
||||
if 'transaction' in validated_data:
|
||||
raise serializers.ValidationError({
|
||||
'equipments': 'You cannot change the associated transaction for a breakage report after it has been created'
|
||||
})
|
||||
|
||||
if 'equipments' in validated_data:
|
||||
raise serializers.ValidationError({
|
||||
'equipments': 'You cannot change the equipments in a breakage report after it has been created'
|
||||
})
|
||||
|
||||
if not DEBUG:
|
||||
if not user.is_teacher and 'status' in validated_data and validated_data['status'] != instance.status:
|
||||
raise serializers.ValidationError(
|
||||
"You do not have permission to change the status of a breakage report"
|
||||
)
|
||||
return super().update(instance, validated_data)
|
||||
|
||||
def create(self, instance, validated_data):
|
||||
transaction = validated_data.get('transaction')
|
||||
equipments = validated_data.get('equipments')
|
||||
user = self.context['request'].user
|
||||
if transaction is None:
|
||||
raise serializers.ValidationError({
|
||||
'equipments': 'Please selected a transaction'
|
||||
})
|
||||
if equipments is None:
|
||||
raise serializers.ValidationError({
|
||||
'equipments': 'Please select equipments covered by the breakage report'
|
||||
})
|
||||
for equipment in equipments:
|
||||
if equipment not in transaction.equipments.all():
|
||||
raise serializers.ValidationError({
|
||||
'equipments': 'All equipments must be associated with the specified transaction'
|
||||
})
|
||||
if not DEBUG:
|
||||
if not user.is_teacher and 'status' in validated_data and validated_data['status'] != instance.status:
|
||||
raise serializers.ValidationError(
|
||||
"You do not have permission to create a breakage report"
|
||||
)
|
||||
return super().create(validated_data)
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
representation['equipments'] = [
|
||||
eq.__str__() for eq in instance.equipments.all()]
|
||||
return representation
|
||||
|
|
10
equipment_tracker/breakages/urls.py
Normal file
10
equipment_tracker/breakages/urls.py
Normal file
|
@ -0,0 +1,10 @@
|
|||
from django.urls import include, path
|
||||
from rest_framework import routers
|
||||
from . import views
|
||||
|
||||
router = routers.DefaultRouter()
|
||||
router.register(r'', views.BreakageReportViewSet)
|
||||
|
||||
urlpatterns = [
|
||||
path('', include(router.urls)),
|
||||
]
|
|
@ -1,3 +1,14 @@
|
|||
from django.shortcuts import render
|
||||
from .serializers import BreakageReportSerializer
|
||||
from .models import BreakageReport
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework import viewsets
|
||||
|
||||
# Create your views here.
|
||||
|
||||
class BreakageReportViewSet(viewsets.ModelViewSet):
|
||||
# Only allow GET
|
||||
# Breakage Reports cannot be updated directly
|
||||
# Changes to the associated Equipment Instances will resolve the Breakage Report itself
|
||||
http_method_names = ['get']
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = BreakageReportSerializer
|
||||
queryset = BreakageReport.objects.all()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue