Improve transaction serializer and add user id to django admin list view in admin panel

This commit is contained in:
Keannu Bernasol 2023-12-26 17:02:18 +08:00
parent 9d2f777ffc
commit 9443631102
2 changed files with 6 additions and 5 deletions

View file

@ -6,7 +6,8 @@ from .models import CustomUser
class CustomUserAdmin(UserAdmin): class CustomUserAdmin(UserAdmin):
model = CustomUser model = CustomUser
list_display = UserAdmin.list_display + ('is_technician', 'is_teacher') list_display = UserAdmin.list_display + \
('is_technician', 'is_teacher', 'id')
fieldsets = UserAdmin.fieldsets + ( fieldsets = UserAdmin.fieldsets + (
(None, {'fields': ('is_technician', 'is_teacher')}), (None, {'fields': ('is_technician', 'is_teacher')}),
) )

View file

@ -58,10 +58,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
def create(self, validated_data): def create(self, validated_data):
# Any transactions created will be associated with the one sending the POST/CREATE request # Any transactions created will be associated with the one sending the POST/CREATE request
user = self.context['request'].user user = self.context['request'].user
validated_data.data['borrower'] = user validated_data['borrower'] = user
# All created transactions will be labelled as Pending # 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 no teacher responsible for the borrow transaction is selected, raise an error
if 'teacher' not in validated_data: if 'teacher' not in validated_data:
@ -101,10 +101,10 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
equipments = validated_data['equipments'] equipments = validated_data['equipments']
for equipment in equipments: for equipment in equipments:
existing__pending_transactions = Transaction.objects.filter( 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(): if existing__pending_transactions.exists():
raise serializers.ValidationError( 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) return super().create(validated_data)