def edit(): collection_id = request.form.get('collection_id') block_id = request.form.get('block_id') content = request.form.get('content', None) """ content = none的情况请直接返回 """ if content is None: ret = {'msg': 'content is None!'} return json_util.dumps(ret) try: row = db_session.query(CollectionBlock).filter( CollectionBlock.id == collection_id, CollectionBlock.block_id == block_id).first() if row is None: ret = {'msg': 'collection_id and block_id error!'} return json_util.dumps(ret) item = db_session.query(Block).filter(Block.id == block_id).first() type_item = item.type if type_item == 'text' or type_item == 'url': item.content_text = content else: item.content_pic = content db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'msg': 'succuss'} return json_util.dumps(ret)
def login(): if request.method == 'POST': phonenum = request.form['phonenum'] password = request.form['password'] name = '' msg = '' userlist = db_session.query(Users) flag = 0 for user in userlist: if user.phonenum == phonenum: if user.password == password: flag = 1 name = user.name else: flag = 2 break if flag == 1: return jsonify({'code': 1, 'name': name, 'phonenum': phonenum}) else: if phonenum != '' and password != '': if flag == 0: msg = "手机号还未注册!" return jsonify({'code': 0, 'msg': msg}) else: msg = "密码错误!" return jsonify({'code': 2, 'msg': msg}) else: msg = "请输入完整登录信息!" return jsonify({'code': 3, 'msg': msg}) else: msg = "提交失败,请重新登录!" return jsonify({'code': 4, 'msg': msg})
def register(): if request.method == 'POST': phonenum = request.form['phonenum'] username = request.form['username'] password = request.form['password'] password1 = request.form['password1'] msg = '' userlist = db_session.query(Users) flag = 1 for user in userlist: if user.phonenum == phonenum: flag = 0 break if flag == 0: msg = "手机号已被注册!" return jsonify({'code': 0, 'msg': msg}) else: if phonenum != '' and password != '': if password == password1: user = Users(phonenum=phonenum, name=username, password=password) db_session.add(user) db_session.commit() return jsonify({'code': 1, 'name': username, 'phonenum': phonenum}) else: msg = "两次输入密码不一致!" return jsonify({'code': 2, 'msg': msg}) else: msg = "请输入完整登录信息!" return jsonify({'code': 3, 'msg': msg}) else: msg = "提交失败,请重新注册!" return jsonify({'code': 4, 'msg': msg})
def get_block(): # 查询这个id的collection的block id = request.form.get('id') blocks = [] """ blocks.append({'id': '1', 'content': 'xuanz','type':'text'}) blocks.append({'id': '2', 'content': 'mingg','type':'url'}) blocks.append({'id': '3', 'content': 'wnqian','type':'picture'}) """ try: row = db_session.query(Block).join( CollectionBlock, CollectionBlock.block_id == Block.id).filter( CollectionBlock.id == id).order_by(Block.order).all() for item in row: block_tmp = {} block_tmp['id'] = item.id block_tmp['type'] = item.type if item.type == 'url' or item.type == 'text': block_tmp['content'] = item.content_text else: block_tmp['content'] = item.content_text block_tmp['order'] = item.order blocks.append(block_tmp) except BaseException as e: print(str(e)) ans = {'msg': 'failed'} return json_util.dumps(ans) ans = {'blocks': blocks, 'msg': 'succuss'} return json_util.dumps(ans)
def get_collection(): # 搜索的关键词 name = request.form.get('name', "") # 搜索对象 phonenum = request.form.get('phonenum', None) collections = [] """ collections.append({'id': '1', 'name': 'xuanz','like':1}) collections.append({'id': '2', 'name': 'mingg','like':2}) collections.append({'id': '3', 'name': 'wnqian','like':3}) """ try: row = db_session.query(Collection).filter(Collection.name.like('%' + name + '%'), Collection.phonenum == phonenum).order_by(Collection.order).all() for item in row: collection_tmp = {} collection_tmp['id'] = item.id collection_tmp['name'] = item.name collection_tmp['like'] = item.like collection_tmp['tag'] = item.tag collection_tmp['order'] = item.order collections.append(collection_tmp) except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'collections': collections, 'msg': 'succuss'} return json_util.dumps(ret)
def swap(): id = request.form.get('id') collection_id = request.form.get('collection_id') order = request.form.get('new_order') """ 把这个collection_id的collection的这个id的块和顺序是order的块交换 编号从零开始,有可能是自己 """ try: row = db_session.query(CollectionBlock).filter( CollectionBlock.id == collection_id, CollectionBlock.block_id == id).first() if row is None: ret = {'msg': 'collection_id and block_id error!'} return json_util.dumps(ret) item1 = db_session.query(Block).filter(Block.id == id).first() item2 = db_session.query(Block).filter(Block.order == order).first() order1 = item1.order # order2 = item2.order id2 = item2.id db_session.query(Block).filter(Block.id == id).update( {Block.order: order}) db_session.query(Block).filter(Block.id == id2).update( {Block.order: order1}) db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'msg': 'succuss'} return json_util.dumps(ret)
def get_like(): id = request.form.get('collection_id') phonenum = request.form.get('phonenum') """ 增加点赞数 更新用户的点赞状态 """ ret = {'msg': 'succuss'} try: s = UserLike(phonenum=phonenum, collection_id=id, state=1) row = db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum).first() if row is None: db_session.add(s) db_session.query(Collection).filter(Collection.id == id).update({Collection.like: Collection.like + 1}) db_session.commit() else: db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum).update( {UserLike.state: 1}) db_session.query(Collection).filter(Collection.id == id).update({Collection.like: Collection.like + 1}) db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) return json_util.dumps(ret)
def get_unlike(): id = request.form.get('collection_id') phonenum = request.form.get('phonenum') """ 减少点赞数 更新用户的点赞状态 """ ret = {'msg': 'succuss'} try: db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum).update( {UserLike.state: 0}) db_session.query(Collection).filter(Collection.id == id).update({Collection.like: Collection.like - 1}) db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) return json_util.dumps(ret)
def edit(): id = request.form.get('collection_id') name = request.form.get('name', None) tag = request.form.get('tag', None) """ 全部是none的情况请直接返回 """ if id is None and name is None and tag is None: ret = {'msg': 'id,name,tag are all none!'} return json_util.dumps(ret) try: db_session.query(Collection).filter(Collection.id == id).update({Collection.name: name, Collection.tag: tag}) db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'msg': 'succuss'} return json_util.dumps(ret)
def recommand_collection(): phonenum = request.form.get('phonenum') collections = [] """ 返回推荐的内容 collections.append({'id': '1', 'name': 'xuanz','like':1}) collections.append({'id': '2', 'name': 'mingg','like':2}) collections.append({'id': '3', 'name': 'wnqian','like':3}) """ try: row0 = db_session.query(Collection).join(UserLike, UserLike.collection_id == Collection.id).filter( UserLike.phonenum == phonenum).all() # 构建用户tag字典 user_like = {} for collection in row0: tag_tmp = collection.tag if tag_tmp in user_like.keys(): user_like[tag_tmp] += 1 else: user_like[tag_tmp] = 1 print(user_like) row = db_session.query(Collection).filter().all() for item in row: collection_tmp = {} collection_tmp['id'] = item.id collection_tmp['name'] = item.name collection_tmp['like'] = item.like collection_tmp['tag'] = item.tag collection_tmp['priority'] = user_like[item.tag] if item.tag in user_like.keys() else 0 collections.append(collection_tmp) # 对tag相关性进行排序 collections_new = sorted(collections, key=lambda x: x.__getitem__('priority'), reverse=True) except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'collections': collections_new, 'msg': 'succuss'} return json_util.dumps(ret)
def delete(): id = request.form.get('collection_id') """ 删除这个id的collection,记得刷新顺序 """ try: # 删除collection item = db_session.query(Collection).filter(Collection.id == id).first() order_item = item.order db_session.delete(item) # 刷新order顺序 db_session.query(Collection).filter(Collection.order > order_item).update( {Collection.order: Collection.order - 1}) db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'msg': 'succuss'} return json_util.dumps(ret)
def delete(): collection_id = request.form.get('collection_id') block_id = request.form.get('block_id') """ 删除这个id的collection的block,记得刷新顺序 """ try: db_session.query(CollectionBlock).filter( CollectionBlock.id == collection_id, CollectionBlock.block_id == block_id).delete() item = db_session.query(Block).filter(Block.id == block_id).first() order_item = item.order db_session.delete(item) db_session.query(Block).filter(Block.order > order_item).update( {Block.order: Block.order - 1}) db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'msg': 'succuss'} return json_util.dumps(ret)
def islike(): id = request.form.get('collection_id') phonenum = request.form.get('phonenum') """ 返回当前用户(用session确定)对这个id的collection是否有点赞 ans['isLike'] = True """ row = db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum, UserLike.state == 1).first() print(row, id, phonenum) if row is not None: islike = True else: islike = False ret = {'msg': 'succuss', 'isLike': islike} return json_util.dumps(ret)
def forget(): if request.method == 'POST': phonenum = request.form['phonenum'] username = request.form['username'] password = request.form['password'] password1 = request.form['password1'] msg = '' userlist = db_session.query(Users) flag = 0 for user in userlist: if user.phonenum == phonenum: if user.name == username: user.password = password db_session.commit() flag = 1 else: flag = 2 break if flag == 0: msg = "手机号还未注册!" return jsonify({'code': 0, 'msg': msg}) else: if phonenum != '' and password != '': if password == password1: if flag == 1: msg = "修改密码成功!" return jsonify({'code': 1, 'name': username, 'phonenum': phonenum}) else: msg = "用户名输入错误!" return jsonify({'code': 2, 'msg': msg}) else: msg = "两次输入密码不一致!" return jsonify({'code': 3, 'msg': msg}) else: msg = "请输入完整修改密码信息!" return jsonify({'code': 4, 'msg': msg}) else: msg = "提交失败,请重新修改密码!" return jsonify({'code': 5, 'msg': msg})
def swap(): id = request.form.get('id') order = request.form.get('new_order') """ 把这个id的collection和顺序是order的collection交换 编号从零开始,有可能是自己 """ try: item1 = db_session.query(Collection).filter(Collection.id == id).first() item2 = db_session.query(Collection).filter(Collection.order == order).first() order1 = item1.order # order2 = item2.order id2 = item2.id db_session.query(Collection).filter(Collection.id == id).update({Collection.order: order}) db_session.query(Collection).filter(Collection.id == id2).update({Collection.order: order1}) db_session.commit() except BaseException as e: print(str(e)) ret = {'msg': 'failed!'} return json_util.dumps(ret) ret = {'msg': 'succuss'} return json_util.dumps(ret)