API文档

基本概念

├── task_1
│   ├── unit_1
│   │   ├── question_1
│   │   │   ├── answer_1
│   │   │   ├── answer_2
│   │   │   ├── answer_3
│   │   │   └── answer_4
│   │   └── question_2
│   ├── unit_2
│   │   ├── question_1
│   │   └── question_2
│   └── unit_3
│       ├── question_1
│       └── question_2
├── task_2
└── task_3

api调用文档

目前的api部署在

token验证的方式:

除了login/register/stat/resetpwd-request/check-vtoken/reset-password不需要发送token外,
    其余所有的api都需要url里附加token=XXX(无论get/post/put...);
    token可以在login的时候获取, login会返回id和token, 以后api可以通过这个token获取用户信息;
    token的过期时间是7天

1. 基本CRUD

read: GET请求

例如
    $.ajax(api_url+"/task/", {
    "type": "GET",
    "data": {
    "id":1,
    "publishedv":'True',
    "finished":'False',
    "ended":'False',
    "requester":1,
    "ordering":'-id',
    "page_size":10,
    "page":1,
    "token":'U:17838894685435883767',
    "tags":'',
    "search":''
    }
    })
    返回:
    {
    "result":[{
    "id":372,
    "title":"",
    "description":"",
    "timestamp":"2015-04-01 08:12:59",
    "template_type":1,
    "judgements_per_unit":3,
    "units_for_qualification_test":10,
    "golden_units_in_first_pages":5,
    "golden_units_per_page":1,
    "units_per_page":10,
    "price":0.1,
    "min_accuracy_hidden":0.7,
    "min_accuracy":0.7,
    "total_units":0,
    "finished_units":0,
    "submitted_units":0,
    "published":false,
    "finished":false,
    "ended":false,
    "status":"init",
    "requester":26,
    "tags":[1,2,4]
    }]
    }
    id 为非必须,取值范围为task id的值;
    published 为非必须,取值范围为”True”,”False”;
    finished为非必须,取值范围为”True”,”False”;
    ended为非必须,取值范围为”True”,”False”;
    requester为非必须,取值范围为requester id的值;
    ordering为非必须,取值范围为(+-)” id”, ” title”, ” price”, ” published”等;
    page_size为非必须,分页时每页的大小;
    page为非必须,分页时的页数;
    token为必须;
    tags为非必须,为数组,数组元素取值范围为整数类型1-6;
    search为非必须,为搜索内容。

过滤:

目前在Task表上可以根据id/published/finished/requster过滤, 注意published/finished需要写True/False
    过滤示例: /task?requester=15&finished=False

排序:

目前在所有表上都可以进行排序, 排序的参数是ordering
    排序示例: /task?ordering=-id,finished

分页:

目前所有表上都可以进行分页, 分页的参数是page_size 如果没有page_size参数, 那么不分页; 分页和不分页的结果会很不一样, 分页了的结果是在json的result字段里面
    分页示例:
    /task?ordering=-id&page_size=2:
    {
    "count": 5,
    "next":"http://localhost:8000/task/?ordering=-id&page=2&page_size=2",
    "previous": null,
    "results": [{
    "id": 6,
    "timestamp": "2015-01-13T16:11:30Z",
    "description": "test",
    "judgements": 1,
    "units": 1,
    "published": false,
    "finished": false,
    "requester": 16
    },
    {
    "id": 5,
    "timestamp": "2015-01-13T15:37:25Z",
    "description": "test1",
    "judgements": 3,
    "units": 1,
    "published": false,
    "finished": false,
    "requester": 13
    }]
    }

获取Tag:

$.ajax({
    type:'get',
    url:api_url+'/tag/?token=',
    dataType: 'json',
    });
    返回
    {
    [
    {"id":1,"description":"Technology"},
    {"id":2,"description":"Life"},
    {"id":3,"description":"Entertainment"},
    {"id":4,"description":"Sports"},
    {"id":5,"description":"Education"},
    {"id":6,"description":"Health"}
    ]
    }

update: PUT/PATCH 请求

例如
    $.ajax(api_url+'/task/1/?token=',{
    "type":"PATCH",
    traditional: true, // 支持传输组
    "data":{
    "tag":[1,2,4],
    "title":"title",
    "description":"This is a new task"
    }
    });
    返回:
    {
    "code": 0,
    "result": null
    }

    例如
    $.ajax(api_url+'/task/1/?token=',{
    "type":"PATCH",
    "data":{
    "units_for_qualification_test":"3",
    "min_accuracy":"0.7"
    }
    });
    返回:
    {
    "code": 0,
    "result": null
    }

delete: DELETE请求到id对应的url

$.ajax("http://166.111.71.88:5678/task/1/?token=", {"type": "DELETE"})
    无返回

2. workflow相关

这里的api包括核心的工作流程:

requester任务发布流程: create -> upload_option -> upload_normal_data -> upload_golden_data(optional) -> publish

worker做任务流程: unit(获取qualification test page或者normal page)->submit

task的status状态变化:

create: (创建一个新任务)

$.ajax(api_url+"/workflow/create?token=", {
    "type": "POST",
    "data": {
    "requester_id":1,
    }
    })
    返回:
    {
    "code":0
    "result":{
    "id":1
    }
    }

upload_option: (上传option)

$.ajax(api_url+"/workflow/upload-option?token=", {
    "type": "POST",
    "data": {
    "task_id":1,
    "option":{
    "template":"please answer the following questions about {fruit}",
    "questions":[{
    "options":["red","orange","yellow"],
    "type-radio":1,
    "name":"question-id",
    "description":"choose the color"
    },
    {
    "type-checkbox":1,
    "options":["fruit","vegetable"],
    "name":"question-id",
    "description":"choose the category(multiple choices)"
    },
    {
    "type-input":1,
    "name":"question-id",
    "description":"input the chinese name"
    }]
    }
    }
    })
    返回:
    {
    "code":0
    "result":{
    "golden-sample": "/media/task-373/golden-sample.txt",
    "normal-sample": "/media/task-373/normal-sample.txt"
    }
    }

get_option: (获取上传的option信息)

$.ajax(api_url+"/workflow/get-option?token=", {
    "type": "POST",
    "data": {
    "task_id":1,
    }
    })
    返回:
    {
    "code":0
    "result":{
    "option":{
    "template":"please answer the following questions about {fruit}",
    "questions":[{
    "options":["red","orange","yellow"],
    "type-radio":1,
    "name":"question-id",
    "description":"choose the color"
    },
    {
    "type-checkbox":1,
    "options":["fruit","vegetable"],
    "name":"question-id",
    "description":"choose the category(multiple choices)"
    },
    {
    "type-input":1,
    "name":"question-id",
    "description":"input the chinese name"
    }]
    },
    golden-sample: "/media/task-373/golden-sample.txt",
    normal-sample: "/media/task-373/normal-sample.txt"
    }
    }

unit: (获取可以渲染template的json数据)

例如
    $.ajax(api_url+"/workflow/unit?token=", {
    "type": "POST",
    "data": {
    "num_units":3
    }
    })
    返回:
    {
    "code":0,
    "result":{
    "template":"please answer the following questions about {fruit}",
    "format":[{
    "description":"please answer the following questions about apple",
    "questions":[{
    "options":["red","orange","yellow"],
    "type-radio":1,
    "name":"question-id",
    "description":"choose the color"
    },
    {
    "type-checkbox":1,
    "options":["fruit","vegetable"],
    "name":"question-id",
    "description":"choose the category(multiple choices)"
    },
    {
    "type-input":1,
    "name":"question-id",
    "description":"input the chinese name"
    }]
    },
    {
    "description":"please answer the following questions about banana"
    },
    {
    "description":"please answer the following questions about orange"
    }]
    }
    }

    例如:
    $.ajax(api_url+"/workflow/unit?token=", {
    "type": "POST",
    "data": {
    "task_id":1,
    "num_units":3
    }
    })
    返回:
    { "code":0,
    "result":{
    "template":"please answer the following questions about {fruit}",
    "format":[{
    "description":"please answer the following questions about apple",
    "questions":[{
    "options":["red","orange","yellow"],
    "type-radio":1,
    "name":"question-id",
    "description":"choose the color"
    },
    {
    "type-checkbox":1,
    "options":["fruit","vegetable"],
    "name":"question-id",
    "description":"choose the category(multiple choices)"
    },
    {
    "type-input":1,
    "name":"question-id",
    "description":"input the chinese name"
    }]
    },
    {
    "description":"please answer the following questions about banana"
    },
    {
    "description":"please answer the following questions about orange"
    }]
    }
    }

upload_golden_data/upload_normal_data: (上传data)

$.ajax(api_url+"/workflow/upload-normal-data?token=", {
    "type": "POST",
    "data": {
    "task_id":1,
    "data":file
    }
    })
    返回:
    {
    "code":0
    }

publish: (最终发布任务)

$.ajax({
    type: 'post',
    url: api_url+'/workflow/publish/?token=',
    data: {
    "task_id":1
    }
    });
    返回:
    {
    "code":0
    }

download_answer: (下载答题结果)

$.ajax(api_url+"/workflow/download_answer?token=", {
    "type": "POST",
    "data": {
    "task_id":1,
    }
    })
    返回:
    {
    csv文件内容(string)
    }

append_data: (追加data)

$.ajax(api_url+"/workflow/append-data?token=", {
    "type": "POST",
    "data": {
    "task_id":1,
    "data":file
    }
    })
    返回:
    {
    "code":0
    }

end: (终止一个任务, 返钱给requester, 不能再次发布)

$.ajax({
    type: 'post',
    url: api_url+'/workflow/end/?token=',
    data: {"task_id":1},
    });
    返回
    {
    "code":0
    }

3. 算法自分配

task分为普通模式的和开发者模式, 默认为普通模式, 如果将task的developer字段设置为True, 则变为开发者模式. 成为开发者模式之后就可以自分配算法.

* 设置developer=True
    * 设置developer_site="http://your_server_site.com"
    * 搭建web server, 监听请求
    1. units
    2. submit

分配单元

url: developer_site/units/
    参数: {"data": u'{"task_id":421, "worker_id":10}'}
    返回: json, 注意unit的数量不要超过之前设置的每页unit数量上限, 例如
    {"unit_ids": [0, 2], "allowed": True}

提交答案

url: developer_site/submit/
    参数: {"data": {"worker_id": 10, "answers": [{"answer": "something...", "inner_unit_id": 0, "question_description":
    "something..."}, {"answer": "something...", "inner_unit_id": 2, "question_description": "something..."}], "task_id":
    421}
    返回: 空

4. 其他

upload photo

参数: task_id
    示例: /workflow/upload-zip?task_id=9
    说明: POST请求,记得将content-type改为'multipart/form-data'. 请求的文件必须是一个.zip的文件,否则我们会返回错误代码.
    结果示例:
    {
    "result" : [
    "tasks/8/task-got/creey.dany.jorah.jpg",
    "tasks/8/task-got/kings_landing_croata.jpg",
    "tasks/8/task-got/winterfell_linodrieghe_by_lyno3ghe-d6ru8mz.jpg"
    ],
    "code" : 0
    }

get photo/thumbnail

[] means optional.

    参数: photo_url, [width, ratio, quality]
    示例: /media/?photo_url=tasks/9/task-got/winterfell_linodrieghe_by_lyno3ghe-d6ru8mz.jpg&width=200
    说明: GET请求, photo_url 就是上述API返回的地址. width指定图片的宽度,ratio指定图片的长宽比,目前只支持"original"和"square",quality指定图片质量,1-100.

    把这个地址拼接好填入 <img src=""> 即可.