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

242 lines
8.3 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
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 ,send_file
  2. from bson import json_util
  3. import uuid
  4. import os
  5. import io
  6. from APP.view.database import db_session
  7. from APP.view.model import Block, CollectionBlock
  8. import requests
  9. from bs4 import BeautifulSoup
  10. bp_block = Blueprint("block", __name__, url_prefix="/block")
  11. basedir = os.path.abspath('.')
  12. @bp_block.route("/add", methods=["POST"])
  13. def add_block():
  14. type = request.form.get('type')
  15. collection_id = request.form.get("collection_id")
  16. id = str(uuid.uuid4())
  17. print(request.form)
  18. try:
  19. count = Block.query.count()
  20. print(count)
  21. if type == 'text' or type == 'url':
  22. content = request.form.get("content")
  23. b = Block(type=type, content_text=content, order=count, id=id)
  24. else:
  25. img = request.files.get("content")
  26. print(img)
  27. path = basedir + "/static/img/"
  28. file_path = path + id + '.png'
  29. img.save(file_path)
  30. b = Block(type=type, content_text='static/img/'+id+'.png', order=count, id=id)
  31. db_session.add(b)
  32. cb = CollectionBlock(id=collection_id, block_id=id)
  33. db_session.add(cb)
  34. db_session.commit()
  35. except BaseException as e:
  36. print(e)
  37. ret = {'msg': 'failed!', 'collection_id': collection_id}
  38. return json_util.dumps(ret)
  39. ret = {'msg': 'succuss', 'id': id}
  40. """
  41. ret['id'] =
  42. """
  43. return json_util.dumps(ret)
  44. @bp_block.route("/select", methods=["POST"])
  45. def get_block():
  46. # 查询这个id的collection的block
  47. id = request.form.get('id')
  48. blocks = []
  49. """
  50. blocks.append({'id': '1', 'content': 'xuanz','type':'text'})
  51. blocks.append({'id': '2', 'content': 'mingg','type':'url'})
  52. blocks.append({'id': '3', 'content': 'wnqian','type':'picture'})
  53. """
  54. try:
  55. row = db_session.query(Block).join(CollectionBlock, CollectionBlock.block_id == Block.id).filter(
  56. CollectionBlock.id == id).order_by(Block.order).all()
  57. for item in row:
  58. block_tmp = {}
  59. block_tmp['id'] = item.id
  60. block_tmp['type'] = item.type
  61. if item.type == 'url' or item.type == 'text':
  62. block_tmp['content'] = item.content_text
  63. else:
  64. block_tmp['content'] = item.content_text
  65. block_tmp['order'] = item.order
  66. blocks.append(block_tmp)
  67. except BaseException as e:
  68. print(str(e))
  69. ans = {'msg': 'failed'}
  70. return json_util.dumps(ans)
  71. ans = {'blocks': blocks, 'msg': 'succuss'}
  72. return json_util.dumps(ans)
  73. @bp_block.route("/delete", methods=["POST"])
  74. def delete():
  75. collection_id = request.form.get('collection_id')
  76. block_id = request.form.get('block_id')
  77. """
  78. id的collection的block
  79. """
  80. try:
  81. db_session.query(CollectionBlock).filter(CollectionBlock.id == collection_id,
  82. CollectionBlock.block_id == block_id).delete()
  83. item = db_session.query(Block).filter(Block.id == block_id).first()
  84. order_item = item.order
  85. db_session.delete(item)
  86. db_session.query(Block).filter(Block.order > order_item).update({Block.order: Block.order - 1})
  87. db_session.commit()
  88. except BaseException as e:
  89. print(str(e))
  90. ret = {'msg': 'failed!'}
  91. return json_util.dumps(ret)
  92. ret = {'msg': 'succuss'}
  93. return json_util.dumps(ret)
  94. @bp_block.route("/swap", methods=["POST"])
  95. def swap():
  96. id = request.form.get('id')
  97. collection_id = request.form.get('collection_id')
  98. order = request.form.get('new_order')
  99. """
  100. collection_id的collection的这个id的块和顺序是order的块交换
  101. """
  102. try:
  103. row = db_session.query(CollectionBlock).filter(CollectionBlock.id == collection_id,
  104. CollectionBlock.block_id == id).first()
  105. if row is None:
  106. ret = {'msg': 'collection_id and block_id error!'}
  107. return json_util.dumps(ret)
  108. item1 = db_session.query(Block).filter(Block.id == id).first()
  109. item2 = db_session.query(Block).filter(Block.order == order).first()
  110. order1 = item1.order
  111. # order2 = item2.order
  112. id2 = item2.id
  113. db_session.query(Block).filter(Block.id == id).update({Block.order: order})
  114. db_session.query(Block).filter(Block.id == id2).update({Block.order: order1})
  115. db_session.commit()
  116. except BaseException as e:
  117. print(str(e))
  118. ret = {'msg': 'failed!'}
  119. return json_util.dumps(ret)
  120. ret = {'msg': 'succuss'}
  121. return json_util.dumps(ret)
  122. @bp_block.route("/edit", methods=["POST"])
  123. def edit():
  124. collection_id = request.form.get('collection_id')
  125. block_id = request.form.get('block_id')
  126. content = request.form.get('content', None)
  127. """
  128. content = none的情况请直接返回
  129. """
  130. if content is None:
  131. ret = {'msg': 'content is None!'}
  132. return json_util.dumps(ret)
  133. try:
  134. row = db_session.query(CollectionBlock).filter(CollectionBlock.id == collection_id,
  135. CollectionBlock.block_id == block_id).first()
  136. if row is None:
  137. ret = {'msg': 'collection_id and block_id error!'}
  138. return json_util.dumps(ret)
  139. item = db_session.query(Block).filter(Block.id==block_id).first()
  140. type_item = item.type
  141. if type_item == 'text' or type_item == 'url':
  142. item.content_text = content
  143. else:
  144. item.content_pic = content
  145. db_session.commit()
  146. except BaseException as e:
  147. print(str(e))
  148. ret = {'msg': 'failed!'}
  149. return json_util.dumps(ret)
  150. ret = {'msg': 'succuss'}
  151. return json_util.dumps(ret)
  152. @bp_block.route("/get_web_name", methods=["POST"])
  153. def get_web_name():
  154. url = request.form.get('url')
  155. ret = {'msg': 'succuss'}
  156. ori_url = url
  157. title = None
  158. if ori_url[0:4] != "http":
  159. url = "https://" + ori_url
  160. try:
  161. html = requests.get(url)
  162. except:
  163. url = "http://" + ori_url
  164. try:
  165. html = requests.get(url)
  166. except:
  167. title = None
  168. else:
  169. soup = BeautifulSoup(html.text, "html.parser")
  170. coding = soup.find('meta', attrs={'http-equiv': 'Content-Type'})
  171. if coding != None:
  172. coding = coding['content']
  173. if 'charset' in coding:
  174. html.encoding = coding[coding.find('charset')+len('charset='):]
  175. else:
  176. html.encoding = 'utf-8'
  177. else:
  178. coding = soup.find('meta', attrs={'http-equiv': 'content-type'})
  179. if coding != None:
  180. coding = coding['content']
  181. if 'charset' in coding:
  182. html.encoding = coding[coding.find('charset')+len('charset='):]
  183. else:
  184. html.encoding = 'utf-8'
  185. else:
  186. html.encoding = 'utf-8'
  187. soup = BeautifulSoup(html.text, "html.parser")
  188. title = soup.find('title').text
  189. else:
  190. soup = BeautifulSoup(html.text, "html.parser")
  191. coding = soup.find('meta', attrs={'http-equiv': 'Content-Type'})
  192. if coding != None:
  193. coding = coding['content']
  194. if 'charset' in coding:
  195. html.encoding = coding[coding.find('charset')+len('charset='):]
  196. else:
  197. html.encoding = 'utf-8'
  198. else:
  199. coding = soup.find('meta', attrs={'http-equiv': 'content-type'})
  200. if coding != None:
  201. coding = coding['content']
  202. if 'charset' in coding:
  203. html.encoding = coding[coding.find('charset')+len('charset='):]
  204. else:
  205. html.encoding = 'utf-8'
  206. else:
  207. html.encoding = 'utf-8'
  208. soup = BeautifulSoup(html.text, "html.parser")
  209. title = soup.find('title').text
  210. """
  211. ret['name'] = url对应的网站的titleurl
  212. """
  213. if title != None:
  214. ret['name'] = title
  215. else:
  216. ret['name'] = url
  217. return json_util.dumps(ret)