邓淳远、崔鹏宇、翁思扬组云计算期末项目
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.

160 lines
4.6 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
  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
  6. bp_collection = Blueprint("collection", __name__, url_prefix="/collection")
  7. @bp_collection.route("/add", methods=["POST"])
  8. def add_collection():
  9. name = request.form.get('name')
  10. tag = request.form.get('tag', None)
  11. phonenum = request.form.get('phonenum')
  12. try:
  13. count = Collection.query.count()
  14. print(count)
  15. c = Collection(id=count + 1, name=name, tag=tag, phonenum=phonenum)
  16. db_session.add(c)
  17. db_session.commit()
  18. except BaseException as e:
  19. print(e)
  20. ret = {'msg': 'failed!', 'name': name}
  21. return json_util.dumps(ret)
  22. ret = {'msg': 'succuss', 'name': name, 'id': str(count + 1)}
  23. """
  24. ret['id'] =
  25. """
  26. return json_util.dumps(ret)
  27. @bp_collection.route("/select", methods=["POST"])
  28. def get_collection():
  29. # 搜索的关键词
  30. name = request.form.get('name', None)
  31. # 搜索对象
  32. phonenum = request.form.get('phonenum', None)
  33. collections = []
  34. """
  35. collections.append({'id': '1', 'name': 'xuanz','like':1})
  36. collections.append({'id': '2', 'name': 'mingg','like':2})
  37. collections.append({'id': '3', 'name': 'wnqian','like':3})
  38. """
  39. try:
  40. row = db_session.query(Collection).filter(Collection.name.like('%' + name + '%'),
  41. Collection.phonenum == phonenum).all()
  42. for item in row:
  43. collection_tmp = {}
  44. collection_tmp['id'] = item.id
  45. collection_tmp['name'] = item.name
  46. collection_tmp['like'] = item.like
  47. collection_tmp['tag'] = item.tag
  48. collections.append(collection_tmp)
  49. except BaseException as e:
  50. print(str(e))
  51. ret = {'msg': 'failed!'}
  52. return json_util.dumps(ret)
  53. ret = {'collections': collections, 'msg': 'succuss'}
  54. return json_util.dumps(ret)
  55. @bp_collection.route("/recommand", methods=["POST"])
  56. def recommand_collection():
  57. collections = []
  58. """
  59. collections.append({'id': '1', 'name': 'xuanz','like':1})
  60. collections.append({'id': '2', 'name': 'mingg','like':2})
  61. collections.append({'id': '3', 'name': 'wnqian','like':3})
  62. """
  63. try:
  64. row = db_session.query(Collection).filter().all()
  65. for item in row:
  66. collection_tmp = {}
  67. collection_tmp['id'] = item.id
  68. collection_tmp['name'] = item.name
  69. collection_tmp['like'] = item.like
  70. collection_tmp['tag'] = item.tag
  71. random_int = random.randint(0, 9)
  72. if random_int <= 4:
  73. continue
  74. else:
  75. collections.append(collection_tmp)
  76. except BaseException as e:
  77. print(str(e))
  78. ret = {'msg': 'failed!'}
  79. return json_util.dumps(ret)
  80. ret = {'collections': collections, 'msg': 'succuss'}
  81. return json_util.dumps(ret)
  82. @bp_collection.route("/isLike", methods=["POST"])
  83. def islike():
  84. ret = {'msg': 'succuss'}
  85. id = request.form.get('collection_id')
  86. """
  87. session确定id的collection是否有点赞
  88. ans['isLike'] = True
  89. """
  90. return json_util.dumps(ret)
  91. @bp_collection.route("/like", methods=["POST"])
  92. def get_like():
  93. id = request.form.get('colllection_id')
  94. """
  95. """
  96. ret = {'msg': 'succuss'}
  97. return json_util.dumps(ret)
  98. @bp_collection.route("/unlike", methods=["POST"])
  99. def get_unlike():
  100. id = request.form.get('colllection_id')
  101. """
  102. """
  103. ret = {'msg': 'succuss'}
  104. return json_util.dumps(ret)
  105. @bp_collection.route("/swap", methods=["POST"])
  106. def swap():
  107. id = request.form.get('id')
  108. order = request.form.get('new_order')
  109. """
  110. id的collection和顺序是order的collection交换
  111. """
  112. ret = {'msg': 'succuss'}
  113. return json_util.dumps(ret)
  114. @bp_collection.route("/delete", methods=["POST"])
  115. def delete():
  116. id = request.form.get('collection_id')
  117. """
  118. id的collection
  119. """
  120. ret = {'msg': 'succuss'}
  121. return json_util.dumps(ret)
  122. @bp_collection.route("/edit", methods=["POST"])
  123. def edit():
  124. id = request.form.get('collection_id')
  125. name = request.form.get('name', None)
  126. tag = request.form.get('tag', None)
  127. """
  128. none的情况请直接返回
  129. """
  130. ret = {'msg': 'succuss'}
  131. return json_util.dumps(ret)