示例#1
0
文件: views.py 项目: WarmerHu/subject
def publish_title(req):
    if req.method == "POST" and req.COOKIES.has_key("userid"):
        method = str(req.GET.get("p"))
        userid = req.COOKIES["userid"]
        if method == "input":
            jsonReq = simplejson.loads(req.body)
            ED = exerciseDao({"userid": userid})
            for v in jsonReq:
                ED.insert_a_title(v)
            content = ("发布了题目").decode("utf-8")
            ADao = activityDao({"userid": userid})
            ADao.add_a_activity(content)
            return HttpResponse(json.dumps({"tips": "添加成功"}), content_type="application/json")
        elif method == "upload":
            if not exerciseDao({"userid": userid}).if_excel():
                file = req.FILES["uploadedfile"]  # @ReservedAssignment
                filename = req.POST["filename"].encode("utf-8")
                if file:
                    tips = fileCon({"filename": filename, "file": file, "userid": userid})
            else:
                tips = "今天已上传了excel"
            return HttpResponse(json.dumps({"tips": tips}), content_type="application/json")
    return HttpResponse(json.dumps({"tips": "添加失败"}), content_type="application/json")
示例#2
0
import xlrd
from exercise.dao import exerciseDao
def fileCon(req):
    f_path = settings.MEDIA_ROOT + req['filename']
    with open(f_path,'wb+') as info:
        for chunk in req['file'].chunks():
            info.write(chunk)
    data=''
    tips = ''
    try:
        data = xlrd.open_workbook(f_path)
    except Exception,e:
        tips = str(e)
    table = data.sheets()[0]
    nrows = table.nrows #行数
    rs = []
    for i in range(1,nrows):
        cell_A1 = table.cell(i,0).value
        cell_A2 = table.cell(i,1).value
        cell_A3 = table.cell(i,2).value
        if cell_A1 and cell_A2 and cell_A3:    
            rs.append({'title':cell_A1,
                       'answer':cell_A2,
                       'tips':cell_A3})
        else:
            tips = "execl格式不正确"
    dao = exerciseDao({'userid':req['userid']})
    dao.insert_titles(rs)
    os.remove(f_path)
    tips = "添加成功"
    return tips