def addArticleModel(request): reqJson=requestBody(request) a_title=reqJson['a_title'] a_content=reqJson['a_content'] a_tag=reqJson['a_tag'] a_desc=reqJson['a_desc'] a_userid=reqJson['a_userid'] a_classid=reqJson['a_classid'] resData={ "flag":"error", "msg":"" } if a_userid == None or a_userid == "": resData['msg']="用户ID不存在" return resData try: articlem=ArticleM(a_title,a_content,a_classid,a_tag,a_desc,a_userid) session.add(articlem) session.commit() resData['flag']="success" resData['msg']="文章新增成功" except InvalidRequestError: session.rollback() resData['msg']="文章新增失败" except Exception as e: print(str(type(e))) resData['msg']=repr(e) session.rollback() return resData
def isLogin(loginData): uname = loginData.get("uname") pwd = loginData.get("pwd") resData = { #返回的JSON字符串 "flag": "error", "msg": "" } try: ures = session.query(User).filter(User.uname == uname).all() if len(ures) != 0: dictUser = ures[0].to_json() if dictUser['pwd'] == pwd: if dictUser['active'] == 0: resData['msg'] = "对不起,该账号还未激活!" else: resData['flag'] = "success" resData['msg'] = "登录成功!" data = {"userid": dictUser["userid"]} resData['data'] = data else: resData['msg'] = "密码错误!" else: resData['msg'] = "用户名错误!" except InvalidRequestError: session.rollback() resData['msg'] = "查询失败" except Exception as e: print(str(type(e))) resData['msg'] = repr(e) session.rollback() return resData
def deleteArticleModel(request): reqJson=requestBody(request) a_id=reqJson['a_id'] a_userid=reqJson['a_userid'] resData={ "flag":"error", "msg":"" } if a_id == None or a_id == "": resData['msg']="文章ID不存在" return resData if a_userid == None or a_userid == "": resData['msg']="用户ID不存在" return resData try: session.query(ArticleM).filter_by(id=a_id).delete(synchronize_session=False) resData['flag']="success" resData['msg']="删除成功" except InvalidRequestError: session.rollback() resData['msg']="删除失败" except Exception as e: print(str(type(e))) resData['msg']=repr(e) session.rollback() return resData
def isRegin(reginData): uname = reginData.get("uname") pwd = reginData.get("pwd") email = reginData.get("email") resData = {"flag": "error", "msg": ""} #返回的JSON字符串 try: qData1 = session.query(User).filter_by(uname=uname).all() qData2 = session.query(User).filter_by(email=email).all() if len(qData1) != 0: resData['msg'] = "用户已存在" elif len(qData2) != 0: resData['msg'] = "该邮箱已注册" else: user = User(uname, pwd, email) session.add(user) session.commit() resData['flag'] = "success" resData['msg'] = "账号注册成功" token = generate_confirmation_token(email) htmlStr = '<h4>欢迎您注册,请单击<a href="' + LINK_URI + '/regin/email?qt=' + token + '" target="_blank">立即激活</a>进行激活</h4>' send_email("测试邮件", htmlStr, email) except InvalidRequestError: session.rollback() resData['msg'] = "账号注册失败" except Exception as e: print(str(type(e))) resData['msg'] = repr(e) session.rollback() return resData
def rEmail(): data = requestBody(request) emailStr = confirm_token(data['qt']) try: query = session.query(User).filter_by(email=emailStr).all() if len(query) != 0: query[0].active = 1 session.commit() except InvalidRequestError: session.rollback() except Exception as e: print(e) return redirect(LINK_URI + "/#/email")
def updateArticleModel(request): reqJson=requestBody(request) a_id=reqJson['a_id'] a_title=reqJson['a_title'] a_content=reqJson['a_content'] a_tag=reqJson['a_tag'] a_desc=reqJson['a_desc'] a_userid=reqJson['a_userid'] a_classid=reqJson['a_classid'] resData={ "flag":"error", "msg":"" } if a_id == None or a_id == "": resData['msg']="文章ID不存在" return resData if a_userid == None or a_userid == "": resData['msg']="用户ID不存在" return resData try: queryData=session.query(ArticleM).filter_by(id=a_id).all() if len(queryData) !=0: queryData[0].a_title=a_title queryData[0].a_content=a_content queryData[0].a_classid=a_classid queryData[0].a_tag=a_tag queryData[0].a_desc=a_desc queryData[0].a_userid=a_userid session.commit() resData['flag']="success" resData['msg']="修改成功" except InvalidRequestError: session.rollback() resData['msg']="文章修改失败" except Exception as e: print(str(type(e))) resData['msg']=repr(e) session.rollback() return resData
def queryArticleModel(request): reqJson=requestBody(request) ca_userid=reqJson['ca_userid'] ca_keystr=reqJson['ca_keystr'] #关键字 ca_classid=reqJson['ca_classid'] #分类ID pageno=reqJson['pageno'] #当前页 pagesize=reqJson['pagesize'] #单页总记录数 resData={ "flag":"error", "msg":"" } if ca_userid == None or ca_userid == "": resData['msg']="用户ID不存在" return resData try: queryData=session.query(ArticleM,ClassArticleM).outerjoin(ClassArticleM,ClassArticleM.id==ArticleM.a_classid).filter(or_(or_(ArticleM.a_title.like('%'+ca_keystr+'%'),ArticleM.a_tag.like('%'+ca_keystr+'%')),ArticleM.a_classid==ca_classid)).limit(pagesize).offset((pageno-1)*pagesize).all() totalNum=session.query(ArticleM).filter(or_(or_(ArticleM.a_title.like('%'+ca_keystr+'%'),ArticleM.a_tag.like('%'+ca_keystr+'%')),ArticleM.a_classid==ca_classid)).count() resData['flag']="success" resData['msg']="数据查询成功" data=[] for item in queryData: obj={} if len(item) != 0: for cItem in item: obj.update(cItem.to_json()) data.append(obj) resData['data']=data resData['total']=totalNum except InvalidRequestError: session.rollback() resData['msg']="数据查询失败" except Exception as e: print(str(type(e))) resData['msg']=repr(e) session.rollback() return resData