Added breakage report model

This commit is contained in:
Keannu Christian Bernasol 2023-12-09 00:38:29 +08:00
parent aa078a78c5
commit c1a7e21e95
10 changed files with 222 additions and 31 deletions

View file

@ -0,0 +1,18 @@
# Generated by Django 4.2.7 on 2023-12-08 15:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('transactions', '0001_initial'),
]
operations = [
migrations.AddField(
model_name='transaction',
name='remarks',
field=models.TextField(max_length=512, null=True),
),
]

View file

@ -27,6 +27,7 @@ class Transaction(models.Model):
borrower = models.ForeignKey(
CustomUser, on_delete=models.SET_NULL, null=True, related_name='borrowed_transactions')
remarks = models.TextField(max_length=512, null=True)
teacher = models.ForeignKey(
CustomUser, on_delete=models.SET_NULL, null=True, related_name='teacher_transactions')
equipments = models.ManyToManyField(EquipmentInstance)

View file

@ -1,7 +1,8 @@
from rest_framework import serializers
from rest_framework import serializers, exceptions
from accounts.models import CustomUser
from equipments.models import EquipmentInstance
from .models import Transaction
from breakages.models import BreakageReport
from accounts.models import CustomUser
from config.settings import DEBUG
@ -22,6 +23,11 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
'equipments', 'transaction_status', 'timestamp']
read_only_fields = ['id', 'timestamp']
# Do not allow deletion of transactions
def delete(self):
raise exceptions.ValidationError(
"Deletion of transactions is not allowed. Please opt to cancel a transaction or finalize it")
def create(self, validated_data):
# Any transactions created will be associated with the one sending the POST/CREATE request
user = self.context['request'].user
@ -40,23 +46,23 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
raise serializers.ValidationError(
"No borrower assigned for this transaction!")
# If the user in the teacher field != actually a teacher, raise an error
# If the user in the teacher field is not actually a teacher, raise an error
borrower = validated_data.get('borrower')
if borrower and borrower.is_teacher or borrower.is_technician:
raise serializers.ValidationError(
"The borrower must be a student. Not a teacher or techician")
# If the user in the teacher field != actually a teacher, raise an error
# If the user in the teacher field is not actually a teacher, raise an error
teacher = validated_data.get('teacher')
if teacher and not teacher.is_teacher:
raise serializers.ValidationError(
"The assigned teacher != a valid teacher")
"The assigned teacher is not a valid teacher")
# If the user in the teacher field != actually a teacher, raise an error
# If the user in the teacher field is not actually a teacher, raise an error
teacher = validated_data.get('teacher')
if teacher and not teacher.is_teacher:
raise serializers.ValidationError(
"The specified user != a teacher.")
"The specified user is not a teacher.")
# If there are no equipments specified, raise an error
if 'equipments' in validated_data and validated_data['equipments'] == []:
@ -64,27 +70,27 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
"You cannot create a transaction without any equipments selected"
)
return super().create(validated_data)
def update(self, instance, validated_data):
user = self.context['request'].user
equipments = validated_data['equipments']
# Check if any of the equipment instances are already in a non-finalized transaction
equipments = validated_data['equipments']
for equipment in equipments:
existing__pending_transactions = Transaction.objects.filter(
equipments=equipment, status__in=['Pending', 'Approved', 'Borrowed', 'With Breakages: Pending Resolution'])
if existing__pending_transactions.exists():
raise serializers.ValidationError(
f"Equipment {equipment.id} is still part of a non-finalized transaction")
f"Cannot add Equipment #{equipment.id}. It is still part of a non-finalized transaction")
# If user != a teacher or a technician, forbid them from changing the status of a transaction
return super().create(validated_data)
def update(self, instance, validated_data):
user = self.context['request'].user
# If user is not a teacher or a technician, forbid them from changing the status of a transaction
if not user.is_teacher and not user.technician and 'transaction_status' in validated_data and validated_data['transaction_status'] != instance.status:
raise serializers.ValidationError(
"You are not a teacher or technician. You do not have permission to change the status of transactions"
)
# If user != a teacher, forbid them from changing the status of a transaction
# If user is not a teacher, forbid them from changing the status of a transaction
if not user.is_teacher and 'transaction_status' in validated_data and validated_data['transaction_status'] != instance.status:
raise serializers.ValidationError(
"You do not have permission to change the status of a transaction"
@ -151,6 +157,20 @@ class TransactionSerializer(serializers.HyperlinkedModelSerializer):
equipment.status = 'Borrowed'
equipment.save()
return super().update(validated_data)
# Changing equipment status of broken items when returned is handled in breakage reports
# If the transaction changes from Borrowed to Finalized and there are no breakages, label the selected equipment's statuses as Working again from Borrowed
if instance.status == 'Borrowed' and validated_data['transaction_status'] == 'Finalized':
equipments = validated_data.get('equipments', [])
for equipment in equipments:
equipment.status = 'Working'
equipment.save()
return super().update(validated_data)
# If the transaction changes from Borrowed to With Breakages, we create a Breakage Report instance
if instance.status == 'Borrowed' and validated_data['transaction_status'] == 'Finalized':
BreakageReport.objects.create(
transaction=instance,
equipments=instance.equipments.all(),
resolved=False
)
# Changing equipment status of broken items when there are breakages is handled in breakage reports
return super().update(instance, validated_data)