mirror of
https://github.com/lemeow125/Borrowing-TrackerBackend.git
synced 2025-04-27 10:11:24 +08:00
Improved serializer for transaction and added available equipment viewset
This commit is contained in:
parent
94ba018c9e
commit
d0ca68149a
6 changed files with 167 additions and 47 deletions
|
@ -6,11 +6,11 @@ from .models import Equipment, EquipmentInstance
|
|||
@admin.register(Equipment)
|
||||
class EquipmentAdmin(SimpleHistoryAdmin):
|
||||
readonly_fields = ('date_added', 'last_updated')
|
||||
list_display = ('name', 'date_added', 'last_updated')
|
||||
list_display = ('id', 'name', 'date_added', 'last_updated')
|
||||
|
||||
|
||||
@admin.register(EquipmentInstance)
|
||||
class EquipmentInstanceAdmin(SimpleHistoryAdmin):
|
||||
readonly_fields = ('date_added', 'last_updated')
|
||||
list_display = ('equipment', 'status', 'remarks',
|
||||
list_display = ('id', 'equipment', 'status', 'remarks',
|
||||
'date_added', 'last_updated')
|
||||
|
|
|
@ -19,12 +19,13 @@ class Equipment(models.Model):
|
|||
history = HistoricalRecords()
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.name} ID:{self.id}'
|
||||
return f'{self.name}'
|
||||
|
||||
|
||||
class EquipmentInstance(models.Model):
|
||||
EQUIPMENT_INSTANCE_STATUS_CHOICES = (
|
||||
('Available', 'Available'),
|
||||
('Pending', 'Pending'),
|
||||
('Broken', 'Broken'),
|
||||
('Borrowed', 'Borrowed'),
|
||||
)
|
||||
|
@ -37,7 +38,7 @@ class EquipmentInstance(models.Model):
|
|||
history = HistoricalRecords()
|
||||
|
||||
def __str__(self):
|
||||
return f'{self.equipment.name} ID:{self.id}'
|
||||
return f'{self.equipment.name}'
|
||||
|
||||
|
||||
@receiver(post_migrate)
|
||||
|
@ -48,19 +49,25 @@ def create_superuser(sender, **kwargs):
|
|||
'name': 'Pyrex Beaker',
|
||||
'description': '',
|
||||
'category': 'Glassware',
|
||||
'remarks': 'First beaker of equipment tracker!'
|
||||
'remarks': 'A beaker for storing fluids'
|
||||
},
|
||||
{
|
||||
'name': 'Bunsen Burner',
|
||||
'description': '',
|
||||
'category': 'Miscellaneous',
|
||||
'remarks': 'First bunsen burner of equipment tracker!'
|
||||
'remarks': 'A burner for heating things'
|
||||
},
|
||||
{
|
||||
'name': 'Microscope',
|
||||
'description': '',
|
||||
'category': 'Miscellaneous',
|
||||
'remarks': 'First microscope of equipment tracker!'
|
||||
'remarks': 'A microscope for zooming into tiny objects'
|
||||
},
|
||||
{
|
||||
'name': 'Petri Dish',
|
||||
'description': '',
|
||||
'category': 'Glassware',
|
||||
'remarks': 'A petri dish'
|
||||
}
|
||||
]
|
||||
|
||||
|
@ -72,6 +79,8 @@ def create_superuser(sender, **kwargs):
|
|||
)
|
||||
if (CREATED):
|
||||
print('Created Equipment: ' + data['name'])
|
||||
print(
|
||||
'Generating 3 Equipment Instances for Equipment: ' + data['name'])
|
||||
# Generate 3 equipment instances per SKU
|
||||
for x in range(3):
|
||||
EQUIPMENT_INSTANCE = EquipmentInstance.objects.create(
|
||||
|
|
|
@ -18,6 +18,10 @@ urlpatterns = [
|
|||
views.EquipmentLogViewSet.as_view({'get': 'list'})),
|
||||
# Last changed equipment
|
||||
path('equipments/latest', views.LastUpdatedEquipmentViewSet.as_view()),
|
||||
# List of equipment instances that are available for borrowing (those not belonging to a non-finalized transaction)
|
||||
path('equipment_instances/available',
|
||||
views.AvailableEquipmentInstanceViewSet.as_view()),
|
||||
# Logs for each equipment instance
|
||||
# Logs for all equipment instances
|
||||
path('equipment_instances/logs', views.EquipmentInstancesLogsViewSet.as_view()),
|
||||
# Logs for each equipment instance
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
from rest_framework.permissions import IsAuthenticated
|
||||
from rest_framework import viewsets, generics
|
||||
from .models import Equipment, EquipmentInstance
|
||||
from django.db.models import Q
|
||||
from . import serializers
|
||||
from config.settings import DEBUG
|
||||
from accounts.permissions import IsTechnician
|
||||
from transactions.models import Transaction
|
||||
|
||||
# -- Equipment Viewsets
|
||||
|
||||
|
@ -56,6 +58,31 @@ class EquipmentInstanceViewSet(viewsets.ModelViewSet):
|
|||
serializer_class = serializers.EquipmentInstanceSerializer
|
||||
queryset = EquipmentInstance.objects.all().order_by('id')
|
||||
|
||||
|
||||
class AvailableEquipmentInstanceViewSet(generics.ListAPIView):
|
||||
if (not DEBUG):
|
||||
permission_classes = [IsAuthenticated]
|
||||
serializer_class = serializers.EquipmentInstanceSerializer
|
||||
|
||||
def get_queryset(self):
|
||||
"""
|
||||
This view should return a list of all the equipment instances
|
||||
that are not associated with a non-finalized transaction.
|
||||
"""
|
||||
# Get all non-finalized transactions
|
||||
non_finalized_transactions = Transaction.objects.filter(
|
||||
~Q(transaction_status__in=['Finalized', 'Cancelled']))
|
||||
|
||||
# Get all equipment instances associated with non-finalized transactions
|
||||
non_finalized_equipments = EquipmentInstance.objects.filter(
|
||||
transaction__in=non_finalized_transactions)
|
||||
|
||||
# Get all equipment instances which are not associated with non-finalized transactions
|
||||
queryset = EquipmentInstance.objects.exclude(
|
||||
id__in=non_finalized_equipments.values_list('id', flat=True)).order_by('id')
|
||||
|
||||
return queryset
|
||||
|
||||
# For viewing all equipment instance logs
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue