mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2025-04-27 10:11:24 +08:00
Added breakage report model
This commit is contained in:
parent
aa078a78c5
commit
c1a7e21e95
10 changed files with 222 additions and 31 deletions
|
@ -1,3 +1,9 @@
|
|||
from django.contrib import admin
|
||||
from .models import BreakageReport
|
||||
|
||||
# Register your models here.
|
||||
|
||||
class BreakageReportAdmin(admin.ModelAdmin):
|
||||
list_display = ('id', 'transaction', 'resolved', 'timestamp')
|
||||
|
||||
|
||||
admin.site.register(BreakageReport, BreakageReportAdmin)
|
||||
|
|
28
equipment_tracker/breakages/migrations/0001_initial.py
Normal file
28
equipment_tracker/breakages/migrations/0001_initial.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Generated by Django 4.2.7 on 2023-12-08 15:33
|
||||
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
import django.utils.timezone
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('transactions', '0001_initial'),
|
||||
('equipments', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='BreakageReport',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('resolved', models.BooleanField(default=False)),
|
||||
('timestamp', models.DateTimeField(default=django.utils.timezone.now, editable=False)),
|
||||
('equipments', models.ManyToManyField(to='equipments.equipmentinstance')),
|
||||
('transaction', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='transactions.transaction')),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -1,3 +1,46 @@
|
|||
from django.db import models
|
||||
from accounts.models import CustomUser
|
||||
from transactions.models import Transaction
|
||||
from equipments.models import EquipmentInstance
|
||||
from django.utils.timezone import now
|
||||
|
||||
# Create your models here.
|
||||
|
||||
class BreakageReport(models.Model):
|
||||
transaction = models.ForeignKey(
|
||||
Transaction, on_delete=models.CASCADE)
|
||||
equipments = models.ManyToManyField(EquipmentInstance)
|
||||
resolved = models.BooleanField(default=False)
|
||||
timestamp = models.DateTimeField(default=now, editable=False)
|
||||
|
||||
def __str__(self):
|
||||
return f"Breakage report for transaction #{self.transaction.id} by {self.transaction.borrower} under {self.transaction.teacher}"
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
# Check if the instance is being updated
|
||||
if not self._state.adding:
|
||||
# Check if all associated equipment instances have status "Working"
|
||||
all_working = all(
|
||||
eq.status == 'Working' for eq in self.equipments.all())
|
||||
|
||||
# If all equipment instances are working
|
||||
if all_working:
|
||||
# set resolved field to True
|
||||
self.resolved = True
|
||||
# set the status of the associated transaction to "Finalized"
|
||||
self.transaction.status = 'Finalized'
|
||||
self.transaction.save()
|
||||
|
||||
# Then save the instance again to reflect the changes
|
||||
super().save(*args, **kwargs)
|
||||
# If not, set the resolved field to False
|
||||
else:
|
||||
if (self.resolved != False or self.transaction.status != 'With Breakages: Pending Resolution'):
|
||||
# set resolved field to False
|
||||
self.resolved = False
|
||||
# set the status of the associated transaction to still be pending
|
||||
self.transaction.status = 'With Breakages: Pending Resolution'
|
||||
self.transaction.save()
|
||||
# Then save the instance again to reflect the changes
|
||||
super().save(*args, **kwargs)
|
||||
else:
|
||||
super().save(*args, **kwargs)
|
||||
|
|
67
equipment_tracker/breakages/serializers.py
Normal file
67
equipment_tracker/breakages/serializers.py
Normal file
|
@ -0,0 +1,67 @@
|
|||
from rest_framework import serializers
|
||||
from accounts.models import CustomUser
|
||||
from equipments.models import EquipmentInstance
|
||||
from equipments.serializers import EquipmentInstanceSerializer
|
||||
from .models import Transaction
|
||||
from accounts.models import CustomUser
|
||||
from config.settings import DEBUG
|
||||
from .models import BreakageReport
|
||||
|
||||
|
||||
class BreakageReportSerializer(serializers.HyperlinkedModelSerializer):
|
||||
transaction = serializers.SlugRelatedField(
|
||||
many=False, slug_field='id', queryset=Transaction.objects.all(), required=True)
|
||||
|
||||
equipments = serializers.SlugRelatedField(
|
||||
many=True, slug_field='id', queryset=EquipmentInstance.objects.all())
|
||||
|
||||
class Meta:
|
||||
model = BreakageReport
|
||||
fields = ['id', 'transaction', 'equipments', 'status', 'timestamp']
|
||||
read_only_fields = ['id', '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)
|
Loading…
Add table
Add a link
Reference in a new issue