delete.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from rest_framework.response import Response
  2. from django.db import transaction
  3. from misago.threads.moderation import threads as moderation
  4. from misago.threads.permissions import allow_delete_thread
  5. from misago.threads.serializers import DeleteThreadsSerializer
  6. @transaction.atomic
  7. def delete_thread(request, thread):
  8. allow_delete_thread(request.user, thread)
  9. moderation.delete_thread(request.user, thread)
  10. return Response({})
  11. def delete_bulk(request, viewmodel):
  12. serializer = DeleteThreadsSerializer(
  13. data={
  14. 'threads': request.data,
  15. },
  16. context={
  17. 'request': request,
  18. 'viewmodel': viewmodel,
  19. },
  20. )
  21. if not serializer.is_valid():
  22. if 'threads' in serializer.errors:
  23. errors = serializer.errors['threads']
  24. if 'details' in errors:
  25. return Response(
  26. hydrate_error_details(errors['details']), status=400)
  27. return Response({'detail': errors[0]}, status=403)
  28. else:
  29. errors = list(serializer.errors)[0][0]
  30. return Response({'detail': errors}, status=400)
  31. for thread in serializer.validated_data['threads']:
  32. with transaction.atomic():
  33. delete_thread(request, thread)
  34. return Response([])
  35. def hydrate_error_details(errors):
  36. for error in errors:
  37. error['thread']['id'] = int(error['thread']['id'])
  38. return errors