diff --git a/equipment_tracker/api/urls.py b/equipment_tracker/api/urls.py index c5b9e1f..fcf1cef 100644 --- a/equipment_tracker/api/urls.py +++ b/equipment_tracker/api/urls.py @@ -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')), ] diff --git a/equipment_tracker/breakages/serializers.py b/equipment_tracker/breakages/serializers.py index 4a2c9c4..eae1418 100644 --- a/equipment_tracker/breakages/serializers.py +++ b/equipment_tracker/breakages/serializers.py @@ -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 diff --git a/equipment_tracker/breakages/urls.py b/equipment_tracker/breakages/urls.py new file mode 100644 index 0000000..a784a6b --- /dev/null +++ b/equipment_tracker/breakages/urls.py @@ -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)), +] diff --git a/equipment_tracker/breakages/views.py b/equipment_tracker/breakages/views.py index 91ea44a..7a0666a 100644 --- a/equipment_tracker/breakages/views.py +++ b/equipment_tracker/breakages/views.py @@ -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() diff --git a/equipment_tracker/equipments/serializers.py b/equipment_tracker/equipments/serializers.py index e878d27..1cbccb9 100644 --- a/equipment_tracker/equipments/serializers.py +++ b/equipment_tracker/equipments/serializers.py @@ -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', diff --git a/equipment_tracker/schema.yml b/equipment_tracker/schema.yml index 4d3387a..db6b1a0 100644 --- a/equipment_tracker/schema.yml +++ b/equipment_tracker/schema.yml @@ -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: