def update_project(request): """ 更新项目 :param request: :return: """ if request.method == "POST": req = common.json_to_dict(request.body) pid = common.get_request_key(req, "id") name = common.get_request_key(req, "name") describe = common.get_request_key(req, "describe") status = common.get_request_key(req, "status") print("状态", status) if pid is None or name is None or describe is None: return common.response_failed("必传参数为空") if status is None: status = True elif isinstance(status, bool) is False: return common.response_failed("状态类型错误") req = ProjectDao.update(pid, name, describe, status) if req is None: return common.response_failed("更新项目失败") return common.response_succeed("更新成功") else: common.response_failed("请求方法错误")
def add_project(request): """ 添加项目 :param request: :return: """ if request.method == "POST": req = common.json_to_dict(request.body) name = common.get_request_key(req, "name") describe = common.get_request_key(req, "describe") status = common.get_request_key(req, "status") if name is None or describe is None: return common.response_failed("必传参数为空") if status is None: status = True elif isinstance(status, bool) is False: return common.response_failed("状态类型错误") req = ProjectDao.get_object_by_name(name) if req is None: return common.response_failed("项目查询错误") elif len(req) != 0: return common.response_failed("项目已经存在") req = ProjectDao.create(name, describe, status) if req is None: return common.response_failed("创建项目失败") return common.response_succeed("创建成功") else: return common.response_failed("请求方法错误")
def user_logout(request): """ 退出登录 """ token = Token.objects.get(user=request.user) token.delete() return common.response_succeed()
def user_login(request): """ 用户登录 """ if request.method == "POST": login_username = request.POST.get("username") login_password = request.POST.get("password") if login_username == '' or login_password == '': return common.response_failed("用户名密码为空") else: user = auth.authenticate(username=login_username, password=login_password) if user is not None and user.is_active: auth.login(request, user) # 验证登录 # update the token try: token = Token.objects.get(user=user) except Token.DoesNotExist: pass else: token.delete() token = Token.objects.create(user=user) return common.response_succeed("登录成功", data={"Token": str(token)}) else: return common.response_failed("用户名或密码错误") else: return common.response_failed("请求方法错误")
def add_module(request): """ 添加模块 :param request: :return: """ if request.method == "POST": req = common.json_to_dict(request.body) project = common.get_request_key(req, "porject") name = common.get_request_key(req, "name") describe = common.get_request_key(req, "describe") if project is None or name is None or describe is None: return common.response_failed("必传参数为空") req = ProjectDao.get_object_by_name(name) if req is None: return common.response_failed("项目查询错误") if len(req) != 0: req = ModuleDao.create(req[0], name, describe) if req is None: return common.response_failed("创建模块失败") return common.response_succeed("创建成功") else: return common.response_failed("请求方法错误")
def get_projects(request): """ 获得对象列表 :param request: :return: """ if request.method == "POST": req = ProjectDao.get_project_list() if req is None: return common.response_failed("查询失败") return common.response_succeed("查询成功", data=req) else: return common.response_failed("请求方法错误")
def get_modules(request): """ 获得模块列表 :param request: :return: """ if request.method == "GET": req = ModuleDao.get_list() if req is None: return common.response_failed("查询失败") return common.response_succeed("查询成功", data=req) else: return common.response_failed("请求方法错误")
def get_project_info(request): """ 获取项目信息 :param request: :return: """ if request.method == "POST": req = common.json_to_dict(request.body) pid = common.get_request_key(req, "id") if pid is None: return common.response_failed("id不能为空") req = ProjectDao.get_project_info_by_id(pid) if req is None: return common.response_failed("查询失败") return common.response_succeed("查询成功", data=req) else: return common.response_failed("请求方法错误")
def delete_project(request): """ 删除project :param request: :return: """ if request.method == "POST": req = common.json_to_dict(request.body) id_ = common.get_request_key(req, "id") if id_ is None: return common.response_failed("id不能为空") ret = ProjectDao.delete_by_id(id_) if ret is None: return common.response_failed("删除失败") return common.response_succeed("删除成功") else: return common.response_failed("请求方法错误")
def update_module(request): """ 更新模块 :param request: :return: """ if request.method == "POST": req = common.json_to_dict(request.body) id_ = common.get_request_key(req, "id") project = common.get_request_key(req, "project") name = common.get_request_key(req, "name") describe = common.get_request_key(req, "describe") if id_ is None or project is None or name is None or describe is None: return common.response_failed("必传参数为空") req = ModuleDao.update(id_, project, name, describe) if req is None: return common.response_failed("更新项目失败") return common.response_succeed("更新成功") else: common.response_failed("请求方法错误")
def get_username(request): """ 获取登录用户名 """ username = str(request.user) return common.response_succeed(data={"username": username})