def validate_threads(self, data): if len(data) > THREADS_LIMIT: message = ungettext( "No more than %(limit)s thread can be deleted at single time.", "No more than %(limit)s threads can be deleted at single time.", THREADS_LIMIT, ) raise ValidationError(message % {'limit': THREADS_LIMIT}) request = self.context['request'] viewmodel = self.context['viewmodel'] threads = [] errors = [] sorted_ids = sorted(data, reverse=True) for thread_id in sorted_ids: try: thread = viewmodel(request, thread_id).unwrap() allow_delete_thread(request.user, thread) threads.append(thread) except PermissionDenied as permission_error: errors.append({ 'thread': { 'id': thread.id, 'title': thread.title }, 'error': permission_error, }) except Http404 as e: pass # skip invisible threads if errors: raise ValidationError({'details': errors}) if len(threads) != len(data): raise ValidationError( _("One or more threads to delete could not be found.")) return threads
def delete_thread(request, thread): allow_delete_thread(request.user, thread) moderation.delete_thread(request.user, thread) return Response({})