def api_project_save_result(project_type):
    data = request.get_json()
    if project_type.lower() == ProjectType.ANNOTATION.value.lower():
        result = AnnotationResult.update_result(**data)
    elif project_type.lower() == ProjectType.EVALUATION.value.lower():
        result = EvaluationResult.update_result(**data)
    if result:
        return '', http.HTTPStatus.CREATED
    else:
        return '', http.HTTPStatus.CONFLICT
def api_project_single_doc(project_type, project_category, project_id):
    random.seed(datetime.now())
    if project_type.lower() == ProjectType.ANNOTATION.value.lower():
        project = AnnotationProject.query.get(project_id)
        if not project:
            return '', http.HTTPStatus.NOT_FOUND
        else:
            # Clean open project, very dirty
            for doc_status in project.doc_statuses:
                results = AnnotationResult.query.filter_by(status_id=doc_status.id, is_filled=False).all()
                for result in results:
                    if result.opened_at:
                        delta = datetime.utcnow() - result.opened_at
                        if delta >= timedelta(minutes=15):
                            AnnotationResult.del_result(result)
            # Retrieve result
            random_doc_statuses = list(project.doc_statuses)
            random.shuffle(random_doc_statuses)
            min_result = 999
            n_results_list = []
            for doc_status in random_doc_statuses:
                n_results = AnnotationResult.query.filter_by(status_id=doc_status.id).count()
                if n_results < min_result:
                    min_result = n_results
                n_results_list.append(n_results)
            for idx, doc_status in enumerate(random_doc_statuses):
                if doc_status.total_exp_results > n_results_list[idx] == min_result:
                    document = Document.query.filter_by(id=doc_status.doc_id).first()
                    turk_code = '%s_%s_%s' % (doc_status.doc_id, randomword(5), project.id)
                    doc_json = json.dumps(json.loads(document.doc_json))
                    doc_id = doc_status.doc_id
                    return jsonify(dict(doc_json=doc_json,
                                        doc_id=doc_id,
                                        doc_status_id=doc_status.id,
                                        turk_code=turk_code,
                                        sanity_statement=document.sanity_statement,
                                        sanity_answer=document.sanity_answer,
                                        summary=document.summary,
                                        ))
            return '', http.HTTPStatus.NOT_FOUND
    elif project_type.lower() == ProjectType.EVALUATION.value.lower():
        project = EvaluationProject.query.get(project_id)
        if not project:
            return '', http.HTTPStatus.NOT_FOUND
        else:
            for summ_status in project.summ_statuses:
                results = EvaluationResult.query.filter_by(
                    status_id=summ_status.id, is_filled=False).all()
                for result in results:
                    if result.opened_at:
                        delta = datetime.utcnow() - result.opened_at
                        if delta >= timedelta(minutes=3):
                            EvaluationResult.del_result(result)
            random_summ_statuses = list(project.summ_statuses)
            random.shuffle(random_summ_statuses)
            min_result = 999
            n_results_list = []
            for summ_status in random_summ_statuses:
                n_results = EvaluationResult.query.filter_by(status_id=summ_status.id).count()
                if n_results < min_result:
                    min_result = n_results
                n_results_list.append(n_results)
            for idx, summ_status in enumerate(random_summ_statuses):
                if summ_status.total_exp_results > n_results_list[idx] == min_result:
                    system_summary = Summary.query.get(summ_status.summary_id)
                    document = Document.query.filter_by(id=system_summary.doc_id).first()
                    system_text = system_summary.text
                    doc_json = json.loads(document.doc_json)
                    doc_id = system_summary.doc_id
                    turk_code = '%s_%s_%s' % (system_summary.doc_id, randomword(5), project.id)
                    if project_category.lower() == ProjectCategory.INFORMATIVENESS_REF.value.lower():
                        ref_text = Summary.query.filter_by(id=summ_status.ref_summary_id).first().text
                        return jsonify(dict(system_text=system_text,
                                            ref_text=ref_text,
                                            summ_status_id=summ_status.id,
                                            sanity_statement=document.sanity_statement_2,
                                            sanity_answer=document.sanity_answer_2,
                                            turk_code=turk_code,
                                            summary=document.summary,
                                            ))
                    elif project_category.lower() == ProjectCategory.INFORMATIVENESS_DOC.value.lower():
                        return jsonify(dict(system_text=system_text,
                                            doc_json=doc_json,
                                            doc_id=doc_id,
                                            summ_status_id=summ_status.id,
                                            sanity_statement=document.sanity_statement,
                                            sanity_answer=document.sanity_answer,
                                            turk_code=turk_code,
                                            summary=document.summary
                                            ))
                    elif project_category.lower() == ProjectCategory.FLUENCY.value.lower():
                        return jsonify(dict(system_text=system_text,
                                            summ_status_id=summ_status.id,
                                            turk_code=turk_code,))
            return '', http.HTTPStatus.NOT_FOUND
    else:
        return '', http.HTTPStatus.BAD_REQUEST
def api_get_result_id(project_type, status_id):
    if project_type.lower() == ProjectType.ANNOTATION.value.lower():
        result_id = AnnotationResult.create_empty_result(status_id)
    elif project_type.lower() == ProjectType.EVALUATION.value.lower():
        result_id = EvaluationResult.create_empty_result(status_id)
    return jsonify(dict(result_id=result_id))