Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to expose model methods to django-rest-framework

I have a model WeddingInvite which has a method for declining and accepting the invite like this:

class WeddingInvite(models.Model):
     from_user=models.ForeignKey(User,related_name="person_inviting")
     to_user=models.ForeignKey(User,related_name="person_invited")
     wedding=models.ForeignKey(Wedding,related_name="wedding_invite")
     status=models.IntegerField(choices=CHOICES)

     def accept(self):

         attending=Attending(attendant=to_user,wedding=Wedding)
         attendant.save()

         self.status=1
         self.save()

         notification.send([self.to_user],"invite_accepted",{"invitation":self})


     def decline(self):
         self.status=2
         self.save()

How do I expose the methods to the view or serializer so that people can accept or decline through django-rest-framework so that it can be passed as json? Here are the serializer and view:

class WeddingInviteList(generics.ListCreateAPIView):
     serializer_class=WeddingInviteSerializer


     def list(self,request,*args,**kwargs):
         wedding_invites=[]

         invited=request.user


         qs=WeddingInvite.objects.filter(Q(to_user=invited))
         for invite in qs:
             wedding_invites.append({invite.from_user,"invited you to a wedding ",invite.wedding})

         return Response(wedding_invites)    

class WeddingInviteSerializer(serializers.ModelSerializer):
      class Meta:
           model=WeddingInvite

How do I enable accept and decline methods in the view or serializer?

EDIT: I realize that there is a serializer method field but it returns a value. I don't know if this can be used since my methods are different form this example serializer method field

A point in the right direction regarding this or any other way would be really helpful.

like image 674
zacmwa Avatar asked Sep 03 '14 06:09

zacmwa


1 Answers

Untested code below, which is an approach that I may take.

For your serializers:

class WeddingInviteResponseSerializer(serializers.Serializer):
    response = serializers.BooleanField(required=True)

For your view, feel free to add in more validation methods and user checking.

from django.http import Http404
from rest_framework.views import APIView
from .models import WeddingInvite
from .serializers import WeddingInviteResponseSerializer


class WeddingInviteResponseView(APIView):
    serializer_class = WeddingInviteResponseSerializer

    def get_object(self, pk):
        try:
            return WeddingInvite.objects.get(pk=pk)
        except WeddingInvite.DoesNotExist():
            raise Http404

    def post(self, request, pk):
        serializer = self.serializer_class(data=request.DATA)

        if serializer.is_valid():
            response = serializer.data['response']
            invite = self.get_object(pk)

            if response.accept:
                invite.accept()
                return Response({'success': "Wedding Invited accepted"}) status=status.HTTP_200_OK)
            else:
                invite.decline()
                return Response({'success': "Wedding Invited declined"}) status=status.HTTP_200_OK)
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

Finally, just expose the following end point in your urls:

url(r'^invite/(?P<pk>\d+)/response$', WeddingInviteResponseView.as_view(), name='wedding-invite-response')

Hope this helps to point you in the right direction.

like image 106
mrkre Avatar answered Sep 29 '22 14:09

mrkre