<commit_before>from rest_framework.response import Response
from rest_framework.decorators import link, action
from wq.db.rest.views import ModelViewSet
from wq.db.contrib.dbio import tasks
from wq.io.exceptions import IoException
from celery.result import AsyncResult


class IoViewSet(ModelViewSet):
    @link()
    def status(self, request, *args, **kwargs):
        taskid = request.GET.get('task', None)
        if not taskid:
            return Response({})

        result = AsyncResult(taskid)
        response = {
            'status': result.state
        }
        if result.state in ('PROGRESS', 'SUCCESS'):
            response.update(result.result)
        elif result.state == 'FAILURE':
            response['error'] = repr(result.result)
        return Response(response)

    def run_task(self, name, async=False):
        response = self.retrieve(self.request, **self.kwargs)
        task = getattr(tasks, name).delay(
            self.get_instance(), self.request.user
        )
        if async:
            response.data['task_id'] = task.task_id
        else:
            try:
                response.data['result'] = task.get()
            except IoException as e:
                response.data['error'] = repr(e)

        return response

    @link()
    def start(self, request, *args, **kwargs):
        self.action = 'columns'
        return self.run_task('read_columns')

    @action()
    def columns(self, request, *args, **kwargs):
        response = self.run_task('read_columns')
        result = tasks.update_columns.delay(
            self.get_instance(), request.user, request.POST
        )
        response.data['result'] = result.get()
        return response

    @link()
    def ids(self, request, *args, **kwargs):
        response = self.run_task('read_row_identifiers')
        return response

    @action()
    def updateids(self, request, *args, **kwargs):
        response = self.run_task('read_row_identifiers')
        self.action = 'ids'
        result = tasks.update_row_identifiers.delay(<commit_msg>use str to serialize exceptions<commit_after>from rest_framework.response import Response
from rest_framework.decorators import link, action
from wq.db.rest.views import ModelViewSet
from wq.db.contrib.dbio import tasks
from wq.io.exceptions import *
from celery.result import AsyncResult


class IoViewSet(ModelViewSet):
    @link()
    def status(self, request, *args, **kwargs):
        taskid = request.GET.get('task', None)
        if not taskid:
            return Response({})

        result = AsyncResult(taskid)
        response = {
            'status': result.state
        }
        if result.state in ('PROGRESS', 'SUCCESS'):
            response.update(result.result)
        elif result.state == 'FAILURE':
            response['error'] = str(result.result)
        return Response(response)

    def run_task(self, name, async=False):
        response = self.retrieve(self.request, **self.kwargs)
        task = getattr(tasks, name).delay(
            self.get_instance(), self.request.user
        )
        if async:
            response.data['task_id'] = task.task_id
        else:
            try:
                response.data['result'] = task.get()
            except IoException as e:
                response.data['error'] = str(e)

        return response

    @link()
    def start(self, request, *args, **kwargs):
        self.action = 'columns'
        return self.run_task('read_columns')

    @action()
    def columns(self, request, *args, **kwargs):
        response = self.run_task('read_columns')
        result = tasks.update_columns.delay(
            self.get_instance(), request.user, request.POST
        )
        response.data['result'] = result.get()
        return response

    @link()
    def ids(self, request, *args, **kwargs):
        response = self.run_task('read_row_identifiers')
        return response

    @action()
    def updateids(self, request, *args, **kwargs):
        response = self.run_task('read_row_identifiers')
        self.action = 'ids'
        result = tasks.update_row_identifiers.delay(
{'fraction_non_alphanumeric': 0.11789013125911521, 'fraction_numerical': 0.0, 'mean_word_length': 2.3924154987633965, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3458563', 'n_tokens_mistral': 1169, 'n_tokens_neox': 1147, 'n_words': 274}