mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2024-11-16 22:09:27 +08:00
Improve breakages
This commit is contained in:
parent
c1a7e21e95
commit
5d5ff22ee4
6 changed files with 100 additions and 152 deletions
|
@ -3,5 +3,6 @@ from django.urls import path, include
|
|||
urlpatterns = [
|
||||
path('accounts/', include('accounts.urls')),
|
||||
path('equipments/', include('equipments.urls')),
|
||||
path('transactions/', include('transactions.urls'))
|
||||
path('transactions/', include('transactions.urls')),
|
||||
path('breakages/', include('breakages.urls')),
|
||||
]
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -175,6 +175,11 @@ class EquipmentInstanceSerializer(serializers.HyperlinkedModelSerializer):
|
|||
"Non-technician users cannot delete equipment instances")
|
||||
instance.delete()
|
||||
|
||||
def to_representation(self, instance):
|
||||
representation = super().to_representation(instance)
|
||||
representation['equipment'] = instance.equipment.__str__()
|
||||
return representation
|
||||
|
||||
class Meta:
|
||||
model = EquipmentInstance
|
||||
fields = ('id', 'equipment', 'equipment_name', 'category', 'status', 'remarks',
|
||||
|
|
|
@ -513,6 +513,43 @@ paths:
|
|||
schema:
|
||||
$ref: '#/components/schemas/SetUsername'
|
||||
description: ''
|
||||
/api/v1/breakages/:
|
||||
get:
|
||||
operationId: api_v1_breakages_list
|
||||
tags:
|
||||
- api
|
||||
security:
|
||||
- jwtAuth: []
|
||||
responses:
|
||||
'200':
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: array
|
||||
items:
|
||||
$ref: '#/components/schemas/BreakageReport'
|
||||
description: ''
|
||||
/api/v1/breakages/{id}/:
|
||||
get:
|
||||
operationId: api_v1_breakages_retrieve
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
description: A unique integer value identifying this breakage report.
|
||||
required: true
|
||||
tags:
|
||||
- api
|
||||
security:
|
||||
- jwtAuth: []
|
||||
responses:
|
||||
'200':
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/BreakageReport'
|
||||
description: ''
|
||||
/api/v1/equipments/equipment_instances/:
|
||||
get:
|
||||
operationId: api_v1_equipments_equipment_instances_list
|
||||
|
@ -983,85 +1020,6 @@ paths:
|
|||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
description: ''
|
||||
put:
|
||||
operationId: api_v1_transactions_update
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
description: A unique integer value identifying this transaction.
|
||||
required: true
|
||||
tags:
|
||||
- api
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
application/x-www-form-urlencoded:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
multipart/form-data:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
required: true
|
||||
security:
|
||||
- jwtAuth: []
|
||||
responses:
|
||||
'200':
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
description: ''
|
||||
patch:
|
||||
operationId: api_v1_transactions_partial_update
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
description: A unique integer value identifying this transaction.
|
||||
required: true
|
||||
tags:
|
||||
- api
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PatchedTransaction'
|
||||
application/x-www-form-urlencoded:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PatchedTransaction'
|
||||
multipart/form-data:
|
||||
schema:
|
||||
$ref: '#/components/schemas/PatchedTransaction'
|
||||
security:
|
||||
- jwtAuth: []
|
||||
responses:
|
||||
'200':
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
$ref: '#/components/schemas/Transaction'
|
||||
description: ''
|
||||
delete:
|
||||
operationId: api_v1_transactions_destroy
|
||||
parameters:
|
||||
- in: path
|
||||
name: id
|
||||
schema:
|
||||
type: integer
|
||||
description: A unique integer value identifying this transaction.
|
||||
required: true
|
||||
tags:
|
||||
- api
|
||||
security:
|
||||
- jwtAuth: []
|
||||
responses:
|
||||
'204':
|
||||
description: No response body
|
||||
components:
|
||||
schemas:
|
||||
Activation:
|
||||
|
@ -1074,6 +1032,31 @@ components:
|
|||
required:
|
||||
- token
|
||||
- uid
|
||||
BreakageReport:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
readOnly: true
|
||||
transaction:
|
||||
type: integer
|
||||
equipments:
|
||||
type: array
|
||||
items:
|
||||
type: integer
|
||||
resolved:
|
||||
type: boolean
|
||||
readOnly: true
|
||||
timestamp:
|
||||
type: string
|
||||
format: date-time
|
||||
readOnly: true
|
||||
required:
|
||||
- equipments
|
||||
- id
|
||||
- resolved
|
||||
- timestamp
|
||||
- transaction
|
||||
CategoryEnum:
|
||||
enum:
|
||||
- Glassware
|
||||
|
@ -1426,29 +1409,6 @@ components:
|
|||
type: string
|
||||
format: date-time
|
||||
readOnly: true
|
||||
PatchedTransaction:
|
||||
type: object
|
||||
properties:
|
||||
id:
|
||||
type: integer
|
||||
readOnly: true
|
||||
borrower:
|
||||
type: integer
|
||||
nullable: true
|
||||
teacher:
|
||||
type: string
|
||||
format: uri
|
||||
nullable: true
|
||||
equipments:
|
||||
type: array
|
||||
items:
|
||||
type: integer
|
||||
transaction_status:
|
||||
$ref: '#/components/schemas/TransactionStatusEnum'
|
||||
timestamp:
|
||||
type: string
|
||||
format: date-time
|
||||
readOnly: true
|
||||
SendEmailReset:
|
||||
type: object
|
||||
properties:
|
||||
|
|
Loading…
Reference in a new issue