From 9443631102def00e8ebd20676c1648475814bde9 Mon Sep 17 00:00:00 2001 From: Keannu Bernasol Date: Tue, 26 Dec 2023 17:02:18 +0800 Subject: [PATCH] Improve transaction serializer and add user id to django admin list view in admin panel --- equipment_tracker/accounts/admin.py | 3 ++- equipment_tracker/transactions/serializers.py | 8 ++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/equipment_tracker/accounts/admin.py b/equipment_tracker/accounts/admin.py index 33db357..c7b0b42 100644 --- a/equipment_tracker/accounts/admin.py +++ b/equipment_tracker/accounts/admin.py @@ -6,7 +6,8 @@ from .models import CustomUser class CustomUserAdmin(UserAdmin): model = CustomUser - list_display = UserAdmin.list_display + ('is_technician', 'is_teacher') + list_display = UserAdmin.list_display + \ + ('is_technician', 'is_teacher', 'id') fieldsets = UserAdmin.fieldsets + ( (None, {'fields': ('is_technician', 'is_teacher')}), ) diff --git a/equipment_tracker/transactions/serializers.py b/equipment_tracker/transactions/serializers.py index 3aca4d3..0416051 100644 --- a/equipment_tracker/transactions/serializers.py +++ b/equipment_tracker/transactions/serializers.py @@ -58,10 +58,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): def create(self, validated_data): # Any transactions created will be associated with the one sending the POST/CREATE request user = self.context['request'].user - validated_data.data['borrower'] = user + validated_data['borrower'] = user # All created transactions will be labelled as Pending - validated_data['transaction_status'] = 'Pending' + validated_data['transaction_status'] = 'Pending Approval' # If no teacher responsible for the borrow transaction is selected, raise an error if 'teacher' not in validated_data: @@ -101,10 +101,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer): equipments = validated_data['equipments'] for equipment in equipments: existing__pending_transactions = Transaction.objects.filter( - equipments=equipment, transaction_status__in=['Pending', 'Approved', 'Borrowed', 'With Breakages: Pending Resolution']) + equipments=equipment, transaction_status__in=['Pending Approval', 'Approved', 'Borrowed', 'With Breakages: Pending Resolution', 'Returned: Pending Checking']) if existing__pending_transactions.exists(): raise serializers.ValidationError( - f"Cannot add Equipment #{equipment.id}. It is still part of a non-finalized transaction") + f"Cannot add Equipment ID:{equipment.id}. It is still part of a non-finalized transaction") return super().create(validated_data)