ssh_key.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import json
  4. from flask import Blueprint, render_template, url_for, request, redirect
  5. import requests
  6. from math import ceil
  7. __author__ = 'James Iter'
  8. __date__ = '2018/2/27'
  9. __contact__ = 'james.iter.cn@gmail.com'
  10. __copyright__ = '(c) 2018 by James Iter.'
  11. blueprint = Blueprint(
  12. 'v_ssh_key',
  13. __name__,
  14. url_prefix='/ssh_key'
  15. )
  16. blueprints = Blueprint(
  17. 'v_ssh_keys',
  18. __name__,
  19. url_prefix='/ssh_keys'
  20. )
  21. def show():
  22. args = list()
  23. page = int(request.args.get('page', 1))
  24. page_size = int(request.args.get('page_size', 10))
  25. keyword = request.args.get('keyword', None)
  26. order_by = request.args.get('order_by', None)
  27. order = request.args.get('order', None)
  28. filters = list()
  29. if page is not None:
  30. args.append('page=' + page.__str__())
  31. if page_size is not None:
  32. args.append('page_size=' + page_size.__str__())
  33. if keyword is not None:
  34. args.append('keyword=' + keyword.__str__())
  35. if order_by is not None:
  36. args.append('order_by=' + order_by)
  37. if order is not None:
  38. args.append('order=' + order)
  39. if filters.__len__() > 0:
  40. args.append('filter=' + ','.join(filters))
  41. host_url = request.host_url.rstrip('/')
  42. ssh_keys_url = host_url + url_for('api_ssh_keys.r_get_by_filter')
  43. if keyword is not None:
  44. ssh_keys_url = host_url + url_for('api_ssh_keys.r_content_search')
  45. if args.__len__() > 0:
  46. ssh_keys_url = ssh_keys_url + '?' + '&'.join(args)
  47. ssh_keys_ret = requests.get(url=ssh_keys_url, cookies=request.cookies)
  48. ssh_keys_ret = json.loads(ssh_keys_ret.content)
  49. last_page = int(ceil(ssh_keys_ret['paging']['total'] / float(page_size)))
  50. page_length = 5
  51. pages = list()
  52. if page < int(ceil(page_length / 2.0)):
  53. for i in range(1, page_length + 1):
  54. pages.append(i)
  55. if i == last_page or last_page == 0:
  56. break
  57. elif last_page - page < page_length / 2:
  58. for i in range(last_page - page_length + 1, last_page + 1):
  59. if i < 1:
  60. continue
  61. pages.append(i)
  62. else:
  63. for i in range(page - page_length / 2, page + int(ceil(page_length / 2.0))):
  64. pages.append(i)
  65. if i == last_page or last_page == 0:
  66. break
  67. return render_template('ssh_keys_show.html', ssh_keys_ret=ssh_keys_ret,
  68. page=page, page_size=page_size, keyword=keyword, pages=pages, order_by=order_by, order=order,
  69. last_page=last_page)
  70. def create():
  71. host_url = request.host_url.rstrip('/')
  72. if request.method == 'POST':
  73. label = request.form.get('label')
  74. public_key = request.form.get('public_key', '')
  75. payload = {
  76. "label": label,
  77. "public_key": public_key
  78. }
  79. url = host_url + '/api/ssh_key'
  80. headers = {'content-type': 'application/json'}
  81. r = requests.post(url, data=json.dumps(payload), headers=headers, cookies=request.cookies)
  82. j_r = json.loads(r.content)
  83. if j_r['state']['code'] != '200':
  84. return render_template('failure.html',
  85. go_back_url='/ssh_keys',
  86. timeout=10000, title='添加失败',
  87. message_title='添加 SSH Key 失败',
  88. message=j_r['state']['sub']['zh-cn'])
  89. return render_template('success.html', go_back_url='/ssh_keys', timeout=10000, title='提交成功',
  90. message_title='添加 SSH Key 的请求已被接受',
  91. message='您所提交的 SSH Key 已创建。页面将在10秒钟后自动跳转到模板列表页面!')
  92. else:
  93. return redirect(url_for('v_ssh_keys.show'))