views.py 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from django.shortcuts import render
  2. from django.forms.models import model_to_dict
  3. from rest_framework.views import APIView
  4. from rest_framework.generics import ListAPIView
  5. from rest_framework.response import Response
  6. from .serializers import RunsURLSerializer
  7. from .models import Run
  8. class RunsStatusView(ListAPIView):
  9. serializer_class = RunsURLSerializer
  10. def get_queryset(self):
  11. runs = Run.objects.all()
  12. return runs
  13. class RunsUploadView(APIView):
  14. serializer_class = RunsURLSerializer
  15. """
  16. payload data needed....
  17. crawler_name, run_started_at to constitute the name attr
  18. run_id which will be the run attr
  19. url attribute which will be the run url passed for upload, will be the url attr
  20. """
  21. def post(self, request, *args, **kwargs):
  22. crawler_name = self.request.data.get('crawler')
  23. run_started_at = self.request.data.get('run_started_at')
  24. run_id = self.kwargs.get("run_id")
  25. run_url = self.request.data.get("run_url")
  26. run = Run()
  27. run.name = f"{crawler_name}_{run_id}_{run_started_at}"
  28. run.run = run_id
  29. run.url = run_url
  30. run.save()
  31. return Response(model_to_dict(run))