Skip to content

Commit

Permalink
Merge pull request #49 from akirachix/dev
Browse files Browse the repository at this point in the history
staging
  • Loading branch information
Ndichu-shee authored Nov 21, 2024
2 parents cff3bdf + e343066 commit f51a8ee
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 27 deletions.
2 changes: 2 additions & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@
path('notify-seller/<int:land_details_id>/', NotifySellerView.as_view(), name='notify-seller'),
path('accept-interest/', AcceptInterestView.as_view(), name='accept-interest'),
path('notifications/<phone_number>/', GetNotificationsView.as_view(), name='get_notifications'),
path('agreements/<int:agreement_id>/', update_agreement, name='update-agreement'),
path('agreements/<int:agreement_id>/update/', update_agreement, name='update-agreement'),

]

Expand Down
90 changes: 63 additions & 27 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,15 @@
from django.utils.timezone import now
from datetime import timedelta
import requests
from rest_framework import status, generics
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.permissions import AllowAny
from rest_framework.exceptions import NotFound
from rest_framework.decorators import api_view
from agreements.models import Agreements
from .serializers import AgreementsSerializer, AgreementSerializer


def get_google_vision_client():
google_credentials_json = settings.GOOGLE_VISION_CREDENTIALS
Expand Down Expand Up @@ -515,35 +524,48 @@ def get(self, request, pk=None, format=None):
serializer = LandMapSerializer(land_detail, context={'request': request})
return Response(serializer.data)



class AgreementsView(APIView):
def get(self, request):
agreements = Agreements.objects.all()
serializer = AgreementsSerializer(agreements, many=True)
return Response(serializer.data)

def post(self, request):
serializer = AgreementsSerializer(data=request.data)
if serializer.is_valid():
agreement = serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

def count(self, request):
payment_count = Agreements.objects.count()
return Response({"count": payment_count}, status=status.HTTP_200_OK)
class AgreementDetailView(APIView):
def get_object(self, id):

class AgreementDetailView(generics.RetrieveUpdateAPIView):
queryset = Agreements.objects.all()
serializer_class = AgreementsSerializer
lookup_field = 'id'
permission_classes = [AllowAny] # Ensure this import is added

def get_object(self, id=None):
try:
return Agreements.objects.get(agreement_id=id)
return Agreements.objects.get(agreement_id=id or self.kwargs.get('id'))
except Agreements.DoesNotExist:
return None
raise NotFound(detail="Agreement not found")

def get(self, request, id):
agreement = self.get_object(id)
if agreement is not None:
try:
agreement = self.get_object(id)
serializer = AgreementsSerializer(agreement)
return Response(serializer.data)
return Response({"detail": "Not found."}, status=status.HTTP_404_NOT_FOUND)
except NotFound:
return Response({"detail": "Agreement not found"}, status=status.HTTP_404_NOT_FOUND)

def put(self, request, id):
agreement = self.get_object(id)
if agreement is not None:
try:
agreement = self.get_object(id)
if hasattr(request.user, 'lawyer'):
serializer = AgreementsSerializer(agreement, data=request.data)
if serializer.is_valid():
Expand All @@ -552,31 +574,45 @@ def put(self, request, id):
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
else:
return Response({"error": "Only lawyers can update agreements"}, status=status.HTTP_403_FORBIDDEN)
else:
return Response({"error": "Agreement not found"}, status=status.HTTP_404_NOT_FOUND)

def get(self, request, id):
try:
agreement = self.get_object(id)
serializer = AgreementSerializer(agreement)
return Response(serializer.data)
except Agreements.DoesNotExist:
raise NotFound(detail="Agreement not found")
def get_object(self, id):
return Agreements.objects.get(agreement_id=id)
return Response({"error": "Agreement not found"}, status=status.HTTP_404_NOT_FOUND)

def partial_update(self, request, *args, **kwargs):
kwargs['partial'] = True
return self.update(request, *args, **kwargs)

def update(self, request, *args, **kwargs):
partial = kwargs.pop('partial', False)
instance = self.get_object(kwargs.get('id'))

# Explicitly handle buyer_agreed and seller_agreed
if 'buyer_agreed' in request.data:
instance.buyer_agreed = request.data['buyer_agreed']
if 'seller_agreed' in request.data:
instance.seller_agreed = request.data['seller_agreed']

serializer = self.get_serializer(instance, data=request.data, partial=partial)
serializer.is_valid(raise_exception=True)
self.perform_update(serializer)

return Response(serializer.data)

# Optional standalone PATCH view if needed
@api_view(['PATCH'])
def update_agreement(request, agreement_id):
try:
agreement = Agreements.objects.get(agreement_id=agreement_id)

if 'buyer_agreed' in request.data:
agreement.buyer_agreed = request.data['buyer_agreed']
if 'seller_agreed' in request.data:
agreement.seller_agreed = request.data['seller_agreed']

agreement.save()
serializer = AgreementsSerializer(agreement)
return Response(serializer.data, status=status.HTTP_200_OK)
except Agreements.DoesNotExist:
return Response({"error": "Agreement not found"}, status=status.HTTP_404_NOT_FOUND)
if 'buyer_agreed' in request.data:
agreement.buyer_agreed = request.data['buyer_agreed']
if 'seller_agreed' in request.data:
agreement.seller_agreed = request.data['seller_agreed']
agreement.save()
serializer = AgreementsSerializer(agreement)
return Response(serializer.data, status=status.HTTP_200_OK)

class TransactionsListView(APIView):
def get(self, request):
Expand Down

0 comments on commit f51a8ee

Please sign in to comment.