邓淳远、崔鹏宇、翁思扬组云计算期末项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

239 lines
7.8 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. from flask import Flask, render_template, request, jsonify, session, Blueprint
  2. from bson import json_util
  3. import random
  4. from APP.view.database import db_session
  5. from APP.view.model import Collection, UserLike
  6. import uuid
  7. bp_collection = Blueprint("collection", __name__, url_prefix="/collection")
  8. @bp_collection.route("/add", methods=["POST"])
  9. def add_collection():
  10. name = request.form.get('name')
  11. tag = request.form.get('tag', None)
  12. phonenum = request.form.get('phonenum')
  13. id = str(uuid.uuid1())
  14. try:
  15. count = Collection.query.count()
  16. print(count)
  17. c = Collection(id=id, name=name, tag=tag, phonenum=phonenum, order=count)
  18. db_session.add(c)
  19. db_session.commit()
  20. except BaseException as e:
  21. print(e)
  22. ret = {'msg': 'failed!', 'name': name}
  23. return json_util.dumps(ret)
  24. ret = {'msg': 'succuss', 'name': name, 'id': id}
  25. """
  26. ret['id'] =
  27. """
  28. return json_util.dumps(ret)
  29. @bp_collection.route("/select", methods=["POST"])
  30. def get_collection():
  31. # 搜索的关键词
  32. name = request.form.get('name', None)
  33. # 搜索对象
  34. phonenum = request.form.get('phonenum', None)
  35. collections = []
  36. """
  37. collections.append({'id': '1', 'name': 'xuanz','like':1})
  38. collections.append({'id': '2', 'name': 'mingg','like':2})
  39. collections.append({'id': '3', 'name': 'wnqian','like':3})
  40. """
  41. try:
  42. row = db_session.query(Collection).filter(Collection.name.like('%' + name + '%'),
  43. Collection.phonenum == phonenum).all()
  44. for item in row:
  45. collection_tmp = {}
  46. collection_tmp['id'] = item.id
  47. collection_tmp['name'] = item.name
  48. collection_tmp['like'] = item.like
  49. collection_tmp['tag'] = item.tag
  50. collection_tmp['order'] = item.order
  51. collections.append(collection_tmp)
  52. except BaseException as e:
  53. print(str(e))
  54. ret = {'msg': 'failed!'}
  55. return json_util.dumps(ret)
  56. ret = {'collections': collections, 'msg': 'succuss'}
  57. return json_util.dumps(ret)
  58. @bp_collection.route("/recommand", methods=["POST"])
  59. def recommand_collection():
  60. collections = []
  61. """
  62. collections.append({'id': '1', 'name': 'xuanz','like':1})
  63. collections.append({'id': '2', 'name': 'mingg','like':2})
  64. collections.append({'id': '3', 'name': 'wnqian','like':3})
  65. """
  66. try:
  67. row = db_session.query(Collection).filter().all()
  68. for item in row:
  69. collection_tmp = {}
  70. collection_tmp['id'] = item.id
  71. collection_tmp['name'] = item.name
  72. collection_tmp['like'] = item.like
  73. collection_tmp['tag'] = item.tag
  74. random_int = random.randint(0, 9)
  75. if random_int <= 4:
  76. continue
  77. else:
  78. collections.append(collection_tmp)
  79. except BaseException as e:
  80. print(str(e))
  81. ret = {'msg': 'failed!'}
  82. return json_util.dumps(ret)
  83. ret = {'collections': collections, 'msg': 'succuss'}
  84. return json_util.dumps(ret)
  85. @bp_collection.route("/isLike", methods=["POST"])
  86. def islike():
  87. id = request.form.get('collection_id')
  88. phonenum = request.form.get('phonenum')
  89. """
  90. session确定id的collection是否有点赞
  91. ans['isLike'] = True
  92. """
  93. row = db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum,
  94. UserLike.state == 1).first()
  95. print(row, id, phonenum)
  96. if row is not None:
  97. islike = True
  98. else:
  99. islike = False
  100. ret = {'msg': 'succuss', 'isLike': islike}
  101. return json_util.dumps(ret)
  102. @bp_collection.route("/like", methods=["POST"])
  103. def get_like():
  104. id = request.form.get('collection_id')
  105. phonenum = request.form.get('phonenum')
  106. """
  107. """
  108. ret = {'msg': 'succuss'}
  109. try:
  110. s = UserLike(phonenum=phonenum, collection_id=id, state=1)
  111. row = db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum).first()
  112. if row is None:
  113. db_session.add(s)
  114. db_session.query(Collection).filter(Collection.id == id).update({Collection.like: Collection.like + 1})
  115. db_session.commit()
  116. else:
  117. db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum).update(
  118. {UserLike.state: 1})
  119. db_session.query(Collection).filter(Collection.id == id).update({Collection.like: Collection.like + 1})
  120. db_session.commit()
  121. except BaseException as e:
  122. print(str(e))
  123. ret = {'msg': 'failed!'}
  124. return json_util.dumps(ret)
  125. return json_util.dumps(ret)
  126. @bp_collection.route("/unlike", methods=["POST"])
  127. def get_unlike():
  128. id = request.form.get('collection_id')
  129. phonenum = request.form.get('phonenum')
  130. """
  131. """
  132. ret = {'msg': 'succuss'}
  133. try:
  134. db_session.query(UserLike).filter(UserLike.collection_id == id, UserLike.phonenum == phonenum).update(
  135. {UserLike.state: 0})
  136. db_session.query(Collection).filter(Collection.id == id).update({Collection.like: Collection.like - 1})
  137. db_session.commit()
  138. except BaseException as e:
  139. print(str(e))
  140. ret = {'msg': 'failed!'}
  141. return json_util.dumps(ret)
  142. return json_util.dumps(ret)
  143. @bp_collection.route("/swap", methods=["POST"])
  144. def swap():
  145. id = request.form.get('id')
  146. order = request.form.get('new_order')
  147. """
  148. id的collection和顺序是order的collection交换
  149. """
  150. try:
  151. item1 = db_session.query(Collection).filter(Collection.id == id).first()
  152. item2 = db_session.query(Collection).filter(Collection.order == order).first()
  153. order1 = item1.order
  154. # order2 = item2.order
  155. id2 = item2.id
  156. db_session.query(Collection).filter(Collection.id == id).update({Collection.order: order})
  157. db_session.query(Collection).filter(Collection.id == id2).update({Collection.order: order1})
  158. db_session.commit()
  159. except BaseException as e:
  160. print(str(e))
  161. ret = {'msg': 'failed!'}
  162. return json_util.dumps(ret)
  163. ret = {'msg': 'succuss'}
  164. return json_util.dumps(ret)
  165. @bp_collection.route("/delete", methods=["POST"])
  166. def delete():
  167. id = request.form.get('collection_id')
  168. """
  169. id的collection
  170. """
  171. try:
  172. # 删除collection
  173. item = db_session.query(Collection).filter(Collection.id == id).first()
  174. order_item = item.order
  175. db_session.delete(item)
  176. # 刷新order顺序
  177. db_session.query(Collection).filter(Collection.order > order_item).update(
  178. {Collection.order: Collection.order - 1})
  179. db_session.commit()
  180. except BaseException as e:
  181. print(str(e))
  182. ret = {'msg': 'failed!'}
  183. return json_util.dumps(ret)
  184. ret = {'msg': 'succuss'}
  185. return json_util.dumps(ret)
  186. @bp_collection.route("/edit", methods=["POST"])
  187. def edit():
  188. id = request.form.get('collection_id')
  189. name = request.form.get('name', None)
  190. tag = request.form.get('tag', None)
  191. """
  192. none的情况请直接返回
  193. """
  194. if id is None and name is None and tag is None:
  195. ret = {'msg': 'id,name,tag are all none!'}
  196. return json_util.dumps(ret)
  197. try:
  198. db_session.query(Collection).filter(Collection.id == id).update({Collection.name: name, Collection.tag: tag})
  199. db_session.commit()
  200. except BaseException as e:
  201. print(str(e))
  202. ret = {'msg': 'failed!'}
  203. return json_util.dumps(ret)
  204. ret = {'msg': 'succuss'}
  205. return json_util.dumps(ret)