Harry Moreno
Harry Moreno

Reputation: 11663

Define Django Rest Framework view with flaggit

I'm trying to use this django app https://github.com/TheBimHub/django-flaggit

I have installed the flaggit via pip. Then in views.py I have

import flaggit

def flag_thread(request, **kwargs):
    thread_id = request.GET.get('thread_id')
    thread = Thread.objects.find(id=thread_id)
    flaggit.utils.flag(thread, user=None, ip=None, comment=None)

is this correct? what does urls.py look like?

Upvotes: 2

Views: 89

Answers (1)

Harry Moreno
Harry Moreno

Reputation: 11663

First make a serializer

from flaggit.models import FlagInstance                                                                                
...
class FlagInstanceSerializer(serializers.ModelSerializer):                                                             
    class Meta:                                                                                                        
        model = FlagInstance                                                                                           
        fields = '__all__'    

then use the serializer in a new endpoint

class ThreadViewSet(viewsets.ModelViewSet):                                                                            
    permission_classes = (IsAuthenticated,)                                                                            
    queryset = Thread.objects.all().order_by('-created_at')                                                            
    pagination_class = ThreadViewSetPaginationClass                                                                                                                                                                                                                                                       

    @action(detail=True, methods=['post'])                                                                             
    def flag(self, request, pk=None):                                                                                  
        thread = self.get_object()                                                                                     
        flag_instance = flaggit.utils.flag(thread, user=request.user, ip=None, comment=None)                           
        serializer = FlagInstanceSerializer(data=flag_instance)                                                        
        serializer.is_valid()                                                                                          
        return Response(serializer.data)  

I went ahead and fixed the migrations and admin panel in a fork https://github.com/morenoh149/django-flaggit

Upvotes: 2

Related Questions