Example #1
0
    def post(self, request, *args, **kwargs):
        result = []
        error = []
        flag = True

        if 'query' in request.POST:
            query = request.POST['query']
        else:
            error.append('query is not given')
            flag = False
            status = 400
        if 'entity_list' in request.POST:
            entity_list = request.POST.getlist('entity_list')
        else:
            error.append('entity is not given')
            flag = False
            status = 400

        if flag:
            query_data = {'query': query}
            query_form = QueryDBForm(query_data)
            # pdb.set_trace()
            if query_form.is_valid():
                query_form.save(commit=True)

            if query_form.errors:
                error.append(query_form.errors)
                status = 400
            # save entity

            for i in range(0, len(entity_list), 3):
                entity_data = {
                    'query': query,
                    'entity_name': entity_list[i + 2],
                    'start_pos': int(entity_list[i + 0]),
                    'end_pos': int(entity_list[i + 1])
                }
                entity_form = EntityDBForm(entity_data)
                if entity_form.is_valid():
                    entity_form.save(commit=True)
                    result.append('%s(%s,%s) added' %
                                  (entity_list[i + 2], entity_list[i + 0],
                                   entity_list[i + 1]))
                    status = 200
                if entity_form.errors:
                    error.append(entity_form.errors)
                    status = 400
        f_result = {'result': result, 'error': error}
        json_data = json.dumps(f_result)
        return self.render_to_http_response(json_data, status=status)
Example #2
0
    def post(self, request, *args, **kwargs):
        flag = False
        error = []
        result = []
        status = 200
        if 'id' in request.POST:
            id = int(request.POST['id'])
            flag = True
        else:
            flag = False
            error.append("id is not given")
        if flag:
            entity = self.get_object_by_id(id)
            if entity is None:
                error.append("No matched resources are found for deletion")
                status = 404
                flag = False

            else:
                if 'query' in request.POST:
                    query = request.POST['query']
                    query_data = {'query': query}
                    query_form = QueryDBForm(query_data)
                    # pdb.set_trace()
                    if query_form.is_valid():
                        query_form.save(commit=True)
                        flag = True
                    if query_form.errors:
                        error.append(query_form.errors)
                        status = 400
                        flag = False
                else:
                    query = entity.query
                if 'start_pos' in request.POST:
                    start_pos = int(request.POST['start_pos'])
                else:
                    start_pos = entity.start_pos
                if 'end_pos' in request.POST:
                    end_pos = int(request.POST['end_pos'])
                else:
                    end_pos = entity.end_pos
                if flag:
                    entity_data = {
                        'id': id,
                        'query': query,
                        'entity_name': 'ORG',  #entity_list[i+2],
                        'start_pos': start_pos,
                        'end_pos': end_pos,
                        'is_trained': False
                    }
                    entity_form = EntityDBForm(entity_data, instance=entity)
                    if entity_form.is_valid():
                        entity_form.save(commit=True)
                        result.append('%d updated successfully' % (id))
                        status = 200
                    if entity_form.errors:
                        error.append(entity_form.errors)
                        status = 400
        json_data = json.dumps({'result': result, 'error': error})
        return HttpResponse(json_data,
                            content_type='application/json',
                            status=status)


#______________________________________________________________________________________________________
Example #3
0
    def post(self, request, *args, **kwargs):
        result = []
        error = []
        flag = True

        data = request.body
        valid_jason = is_json(data)
        if valid_jason:
            data = json.loads(data)
            if 'query' in data:
                query = data['query']
            else:
                error.append('query is not given')
                flag = False
                status = 400

            if 'entity_list' in data:
                entity_list = data['entity_list']
            else:
                error.append('entity is not given')
                flag = False
                status = 400
        else:
            # if 'query' in request.POST:
            #     query = request.POST['query']
            # else:
            #     error.append('query is not given')
            #     flag = False
            #     status = 400

            # if 'entity_list' in request.POST:
            #     entity_list = request.POST.getlist('entity_list')
            # else:
            #     error.append('entity is not given')
            #     flag = False
            #     status = 400
            flag = False
            status = 400
            error.append("Please sent valid json data")
        if flag:
            # pdb.set_trace()
            query_data = {'query': query}
            query_form = QueryDBForm(query_data)

            if query_form.is_valid():
                query_form.save(commit=True)

            if query_form.errors:
                error.append(query_form.errors)
                status = 400
            # save entity

            # for i in range(0,len(entity_list)):
            for entity in entity_list:
                # pdb.set_trace()

                entity_data = {
                    'query': query,
                    'entity_name': entity[2],
                    'start_pos': int(entity[0]),
                    'end_pos': int(entity[1]),
                    'is_trained': False
                }
                entity_form = EntityDBForm(entity_data)
                if entity_form.is_valid():
                    entity_form.save(commit=True)

                    # finding id of saved data
                    obj_cur_data = EntityDB.objects.filter(
                        query=query,
                        start_pos=int(entity[0]),
                        end_pos=int(entity[1]),
                        is_trained=False).order_by('-id')
                    for obj in obj_cur_data:
                        tmp_entity_data = {}
                        tmp_entity_data['id'] = obj.id
                        tmp_entity_data['query'] = query
                        tmp_entity_data['start_pos'] = int(entity[0])
                        tmp_entity_data['end_pos'] = int(entity[1])
                        tmp_entity_data['entity_name'] = entity[2]
                        result.append(tmp_entity_data)
                    status = 200
                if entity_form.errors:
                    # for er in entity_form.errors:
                    error.append(entity_form.errors)
                    status = 400
        f_result = {'result': result, 'error': error}
        json_data = json.dumps(f_result)
        return self.render_to_http_response(json_data, status=status)