HIS pharmacy views

mail@pastecode.io avatar
unknown
python
2 years ago
1.3 kB
19
Indexable
from rest_framework.response import Response
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.filters import OrderingFilter, SearchFilter
from .pagination import DrugPagination
from .models import Drug, DrugRequestDetail
from .serializers import DrugSerializer, DrugRequestSerializer

class DrugViewSet(viewsets.ModelViewSet):
    queryset = Drug.objects.all()
    serializer_class = DrugSerializer
    pagination_class = DrugPagination
    filter_backends = [OrderingFilter, SearchFilter]
    ordering_fields = ['name', 'stock_level', 'expiry_date']
    search_fields = ['name', 'stock_level']

class DrugRequestViewSet(viewsets.ModelViewSet):
    queryset = DrugRequestDetail.objects.all()
    serializer_class = DrugRequestSerializer

    @action(detail=True, methods=['get', 'put'])
    def UpdateDispense(self, request, pk=id):
        drug_request = self.get_object()

        if request.method == 'GET':
            serializer = self.get_serializer(drug_request)
            return Response(serializer.data)
        elif request.method == 'PUT':
            serializer = self.get_serializer(drug_request, data=request.data, partial=True)
            serializer.is_valid(raise_exception=True)
            serializer.save()
            return Response(serializer.data)