コード例 #1
0
def view_modules(request):
    if request.user.is_authenticated:
        if request.method == 'POST' and checkRole(request, 'admin'):
            moduleObj = Module(moduleCode=request.POST['moduleCode'],
                               moduleName=request.POST['moduleName'])
            moduleObj.save()

        modules = Module.objects.all()

        # getting all modules enrolled by "request.user"
        # from module model
        enrolledModules = request.user.student.all()

        context_variable = {
            'modules': modules,
            'enrolledModules': enrolledModules,
            'staff_roles': STAFF_ROLES
        }
        return render(request, 'modules.html', context_variable)
    messages.error(request, "Login to view modules")
    return redirect('/')
コード例 #2
0
def create(request):
    if request.method == "GET":
        p_id = request.GET.get('p_id', None)
        is_extends = False
        module = None

        if p_id:
            is_extends = True
            module = Module.objects.get(pk=p_id)
        groups = Group.objects()
        return render_to_response("module/module_create.html",
                                  locals(),
                                  context_instance=RequestContext(request))

    elif request.method == 'POST':
        response = {"success": False, "error": "", "id": None}
        try:
            # 获取参数
            request_json = json.loads(request.POST.get("json"))
            now = datetime.datetime.now()
            # 校验参数
            module_id = request_json["moduleId"]
            module_name = request_json["moduleName"]
            module_remark = request_json["module_remark"]
            module_group = request_json["module_group"]
            module = None
            if module_id is not None:
                module = Module.objects.get(pk=module_id)
                module_by_name = Module.objects(name=module_name)
                if len(module_by_name) > 0:
                    if len(module_by_name) > 1:
                        response["error"] = "模块名称重复!"
                        logger.error(response["error"])
                        return HttpResponse(json.dumps(response),
                                            mimetype="application/json")

                    module_by_name = module_by_name[0]
                    if module_by_name.id != module.id:
                        response["error"] = "模块名称重复!"
                        logger.error(response["error"])
                        return HttpResponse(json.dumps(response),
                                            mimetype="application/json")
            else:
                module_by_name = Module.objects(name=module_name)
                if len(module_by_name) > 0:
                    response["error"] = "模块名称重复!"
                    logger.error(response["error"])
                    return HttpResponse(json.dumps(response),
                                        mimetype="application/json")
                module = Module()

            # 保存
            # 先保存文件信息
            runInfoList = request_json["runInfoList"]
            run_info_list = []
            for item in runInfoList:
                run_info = None
                run_info_id = item["runInfoId"]
                if run_info_id is not None:
                    run_info = RunInfo.objects(pk=run_info_id)[0]
                else:
                    run_info = RunInfo()
                run_info.run_info_name = item["run_info_name"]
                run_info.workingDir = item["workingDir"]
                run_info.runParam = item["runParam"]
                run_info.runType = item["runType"]
                run_info.timerParam = item["timerParam"]
                run_info.save()
                run_info_list.append(run_info)

            # 保存文件信息
            fileInfoList = request_json["fileInfoList"]
            file_info_list = []
            for item in fileInfoList:
                file_info_id = item["fileInfoId"]
                if file_info_id is not None:
                    file_info = FileInfo.objects(pk=file_info_id)[0]
                else:
                    file_info = FileInfo()
                file_info.filePath = item["filePath"]
                file_info.mod = item["mod"]
                file_info.descript = item["descript"]
                file_info.fileType = item["file_type"]
                file_info.rawPath = item["rawPath"]
                file_info.remark = item["remark"]
                if file_info_id is None:
                    file_info.createTime = now
                file_info.updateTime = now
                file_info.save()
                file_info_list.append(file_info)

            # 保存模块信息
            module.name = module_name
            module.head = request_json["moduleHead"]
            module.remark = module_remark
            module.version = VERSION_PREFIX_MODULE + str(
                int(time.time() * 1000))
            module.group = module_group
            # 删除已删除的file和run信息
            for item in set(module.files) - set(file_info_list):
                if type(item) is not DBRef:
                    item.delete()
            for item in set(module.runInfos) - set(run_info_list):
                if type(item) is not DBRef:
                    item.delete()

            module.files = file_info_list
            module.runInfos = run_info_list
            if module_id is None:
                module.createTime = now
            module.save()

            response["success"] = True
            response["id"] = str(module.id)
            response["error"] = "执行成功!"
            return HttpResponse(json.dumps(response),
                                mimetype="application/json")
        except Exception, e:
            response["error"] = "系统异常![%s]" % str(e)
            logger.error(response["error"] + getTraceBack())
            return HttpResponse(json.dumps(response),
                                mimetype="application/json")