def create(request): data = request.data workspace = WorkspaceUtil.get_workspace(request) if not workspace: return Response({"status": 401, "msg": "token过期"}) if not data.get("catalog_id"): return Response({"status": 400, "msg": "参数错误"}) kwargs = {} kwargs["id"] = data["catalog_id"] catalog = catalogRepository.load(**kwargs) if not catalog: return Response({"status": 404, "msg": "类目不存在"}) article = Article() article.passport_id = workspace.passport_id article.passport_name = workspace.name article.catalog_id = data.get("catalog_id") article.catalog_name = catalog.name article.title = data.get("title") article.content = data.get("content") article.created = TimeUtil.get_utc_time() article.pv = 0 article.version = 1 article.save() return Response({"status": 200, "msg": "新增成功"})
def register(request): # 判断参数是否正确 data = request.data code = data.get("code") if not code: return Response({"status": 400, "msg": "验证码不能为空"}) validate_code = None kwargs = {} if data.get("mobile"): kwargs["mobile"] = data["mobile"] passport = passportRepository.load(**kwargs) if passport: return Response({"status": 400, "msg": "用户已存在"}) validate_code = cache.get(data.get("mobile")) elif data.get("email"): kwargs["email"] = data["email"] passport = passportRepository.load(**kwargs) if passport: return Response({"status": 400, "msg": "用户已存在"}) validate_code = cache.get(data.get("email")) else: return Response({"status": 400, "msg": "参数错误"}) kwargs = {} kwargs["name"] = data["name"] passport_list = passportRepository.search(**kwargs) if passport_list: return Response({"status": 400, "msg": "用户名已存在"}) if validate_code != str(code): return Response({"status": 400, "msg": "验证码错误"}) password = data.get("password") if not password: return Response({"status": 400, "msg": "密码不能为空"}) if not Validate.password_validate(password): return Response({"status": 400, "msg": "密码格式不正确"}) # 随机生成盐值加密 salt = CryptogramGenerate.get_salt().decode() password = CryptogramGenerate.md5(password + salt) password = password + '|' + salt created = TimeUtil.get_utc_time() avert = user_settings.defaultAvert passport = Passport(password=password, created=created, email=data.get("email"), mobile=data.get("mobile"), name=data.get("name"), avert=avert) passport.save() return Response({"status": 200, "msg": ""})
def collect(request): data = request.data if not data.get("article_id"): return Response({"status": 400, "msg": "参数错误"}) workspace = WorkspaceUtil.get_workspace(request) if not workspace: return Response({"status": 401, "msg": "token过期"}) article_id = data["article_id"] passport_id = workspace.passport_id kwargs = {"article_id": article_id, "passport_id": passport_id} collect = collectRepository.load(**kwargs) if collect: return Response({"status": 400, "msg": "已收藏"}) utc_time = TimeUtil.get_utc_time() collect = Collect(article_id=article_id, passport_id=passport_id, created=utc_time) collect.save() return Response({"status": 200, "msg": "收藏文章成功"})
def create_catalog(request): data = request.data parent_id = data.get('parent_id', 0) catalog_type = data.get("catalog_type") name = data.get("name") if not (catalog_type and name): return Response({"status": 400, "msg": "参数错误"}) utc_time = TimeUtil.get_utc_time() if parent_id == 0: # 判断是否有根节点存在 kwargs = {} kwargs["layer"] = 0 kwargs["catalog_type"] = catalog_type catalog_res = catalogRepository.load(**kwargs) # 根节点不存在 先创建一个根节点 if not catalog_res: catalog = Catalog() catalog.parent_id = 0 catalog.layer = 0 catalog.left_id = 1 catalog.right_id = 4 catalog.name = "" catalog.catalog_type = catalog_type catalog.version = 1 catalog.created = utc_time catalog.save() catalog_id = catalog.id catalog1 = Catalog() catalog1.parent_id = catalog_id catalog1.layer = 1 catalog1.left_id = 2 catalog1.right_id = 3 catalog1.name = name catalog1.icon = data.get("icon") catalog1.code = data.get("code") catalog1.catalog_type = catalog_type catalog1.version = 1 catalog1.created = utc_time catalog1.save() else: # 更新需要更新的左节点 kwargs = {} kwargs["catalog_type"] = catalog_type kwargs["right_id"] = catalog_res.right_id catalogRepository.update_left_id(**kwargs) # 更新需要更新的右节点 catalogRepository.update_right_id(**kwargs) parent_id = catalog_res.id catalog = Catalog() catalog.parent_id = parent_id catalog.layer = catalog_res.layer + 1 catalog.left_id = catalog_res.right_id catalog.right_id = catalog_res.right_id + 1 catalog.name = name catalog.icon = data.get("icon") catalog.code = data.get("code") catalog.catalog_type = catalog_type catalog.version = 1 catalog.created = utc_time catalog.save() else: # 新增节点 kwargs = {} kwargs["id"] = parent_id catalog_res = catalogRepository.load(**kwargs) # 更新需要更新的左节点 kwargs = {} kwargs["catalog_type"] = catalog_res.catalog_type kwargs["right_id"] = catalog_res.right_id catalogRepository.update_left_id(**kwargs) # 更新需要更新的右节点 catalogRepository.update_right_id(**kwargs) catalog = Catalog() catalog.parent_id = parent_id catalog.layer = catalog_res.layer + 1 catalog.left_id = catalog_res.right_id catalog.right_id = catalog_res.right_id + 1 catalog.name = name catalog.icon = data.get("icon") catalog.code = data.get("code") catalog.catalog_type = catalog_res.catalog_type catalog.version = 1 catalog.created = utc_time catalog.save() return Response({"status": 200, "msg": "新增分类成功"})
def __deco(request): token = request.META.get("HTTP_AUTHORIZATION") if sign: if not token: return Response({"status": 401, "msg": "请先登录"}) # 先从缓存获取token是否存在 token = token.split(" ")[1] token_info = cache.get(token) if not token_info: # 从数据库查询 kwargs = {} kwargs["token_id"] = token token_info = tokenRepository.load(**kwargs) if not token_info or token_info.expire_time < TimeUtil.get_utc_time( ): return Response({"status": 401, "msg": "请先登录"}) expire_time = settings.webExpireTime if token_info.login_type == Token.LoginType.app.value: expire_time = settings.appExpireTime token_info.start_time = TimeUtil.get_utc_time() token_info.expire_time = TimeUtil.get_utc_time( ) + expire_time token_info.save() workspace = WorkspaceUtil.get_workspace_by_token( token_info) cache.set(token_info.token_id, workspace, timeout=expire_time) else: # 更新缓存过期时间 expire_time = settings.webExpireTime if token_info.login_type == Token.LoginType.app.value: expire_time = settings.appExpireTime token_info.expire_time = TimeUtil.get_utc_time( ) + expire_time token_info.start_time = TimeUtil.get_utc_time() cache.set(token, token_info, timeout=expire_time) result = signature(request) if not result: return Response({"status": 401, "msg": "签名错误"}) else: # 不需要登录的接口 更新token过期时间 if token: token = token.split(" ")[1] token_info = cache.get(token) if not token_info: # 从数据库查询, 如果存在更新过期时间 kwargs = {} kwargs["token_id"] = token token_info = tokenRepository.load(**kwargs) if token_info: # 判断token 是否过期 if token_info.expire_time > TimeUtil.get_utc_time( ): print(11111) expire_time = settings.webExpireTime if token_info.login_type == Token.LoginType.app.value: expire_time = settings.appExpireTime token_info.start_time = TimeUtil.get_utc_time() token_info.expire_time = TimeUtil.get_utc_time( ) + expire_time token_info.save() workspace = WorkspaceUtil.get_workspace_by_token( token_info) cache.set(token_info.token_id, workspace, timeout=expire_time) else: print(22222) return func(request)
def add_comment(request): data = request.data parent_id = data.get('parent_id', 0) content = data.get("content") article_id = data.get("article_id") if not (content and article_id): return Response({"status": 400, "msg": "参数错误"}) kwargs = {"id": article_id} article = articleRepository.load(**kwargs) if not article: return Response({"status": 400, "msg": "文章不存在"}) utc_time = TimeUtil.get_utc_time() catalog_type = data.get("catalog_type") workspace = WorkspaceUtil.get_workspace(request) passport_id = workspace.passport_id message = Message() message.reply_id = passport_id message.reply_name = workspace.name message.passport_id = article.passport_id message.article_id = article.id message.article_title = article.title message.created = utc_time message.status = 0 message.reply_type = Message.Type.reply.value flag = True if parent_id == 0: message.reply_type = Message.Type.answer.value if article.passport_id == passport_id: flag = False # 判断是否有根节点存在 kwargs = {} kwargs["layer"] = 0 kwargs["article_id"] = article_id catalog_res = commentRepository.load(**kwargs) # 根节点不存在 先创建一个根节点 if not catalog_res: catalog = Comment() catalog.passport_id = passport_id catalog.parent_id = 0 catalog.layer = 0 catalog.left_id = 1 catalog.right_id = 4 catalog.content = "" catalog.catalog_type = catalog_type catalog.version = 1 catalog.created = utc_time catalog.article_id = article_id catalog.save() catalog_id = catalog.id catalog = Comment() catalog.parent_id = catalog_id catalog.layer = 1 catalog.left_id = 2 catalog.right_id = 3 catalog.content = data["content"] catalog.passport_id = passport_id catalog.catalog_type = catalog_type catalog.article_id = article_id catalog.version = 1 catalog.created = utc_time catalog.save() else: # 更新需要更新的左节点 kwargs = {} kwargs["catalog_type"] = catalog_type kwargs["right_id"] = catalog_res.right_id commentRepository.update_left_id(**kwargs) # 更新需要更新的右节点 commentRepository.update_right_id(**kwargs) parent_id = catalog_res.id catalog = Comment() catalog.parent_id = parent_id catalog.layer = catalog_res.layer + 1 catalog.left_id = catalog_res.right_id catalog.right_id = catalog_res.right_id + 1 catalog.catalog_type = catalog_type catalog.passport_id = passport_id catalog.article_id = article_id catalog.content = data["content"] catalog.version = 1 catalog.created = utc_time catalog.save() else: # 新增节点 kwargs = {} kwargs["id"] = parent_id catalog_res = commentRepository.load(**kwargs) message.passport_id = catalog_res.passport_id if catalog_res.passport_id == passport_id: flag = False # 更新需要更新的左节点 kwargs = {} kwargs["catalog_type"] = catalog_res.catalog_type kwargs["right_id"] = catalog_res.right_id commentRepository.update_left_id(**kwargs) # 更新需要更新的右节点 commentRepository.update_right_id(**kwargs) catalog = Comment() catalog.parent_id = parent_id catalog.layer = catalog_res.layer + 1 catalog.left_id = catalog_res.right_id catalog.right_id = catalog_res.right_id + 1 catalog.passport_id = passport_id catalog.article_id = article_id catalog.catalog_type = catalog_res.catalog_type catalog.content = data["content"] catalog.version = 1 catalog.created = utc_time catalog.save() catalog = CommentSerializers(catalog).data if flag: message.save() return Response({"status": 200, "msg": "新增评论成功", "data": catalog})