webui.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #!/usr/bin/env python
  2. #============================================================================
  3. # This file is part of Pwman3.
  4. #
  5. # Pwman3 is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License, version 2
  7. # as published by the Free Software Foundation;
  8. #
  9. # Pwman3 is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with Pwman3; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. #============================================================================
  18. # Copyright (C) 2012-2014 Oz Nahum <nahumoz@gmail.com>
  19. #============================================================================
  20. from __future__ import print_function
  21. from bottle import route, run, debug, template, request, redirect
  22. from pwman.util.crypto import CryptoEngine
  23. import pwman.data.factory
  24. from pwman.data.tags import TagNew
  25. from pwman import parser_options, get_conf_options
  26. from pkg_resources import resource_filename
  27. templates_path = [resource_filename('pwman', 'ui/templates')]
  28. AUTHENTICATED = False
  29. TAGS = None
  30. DB = None
  31. def require_auth(fn):
  32. def check_auth(**kwargs):
  33. if AUTHENTICATED:
  34. return fn(**kwargs)
  35. else:
  36. redirect("/auth")
  37. return check_auth
  38. @route('/node/:no')
  39. @require_auth
  40. def view_node(no):
  41. global DB
  42. node = DB.getnodes([no])
  43. output = template("view.tpl", node=node[0], template_lookup=templates_path)
  44. return output
  45. def submit_node(id, request):
  46. # create new\update node based on request.params.items()
  47. redirect('/')
  48. @route('/new/', method=['GET', 'POST'])
  49. @route('/edit/:no', method=['GET', 'POST'])
  50. @require_auth
  51. def edit_node(no=None):
  52. global DB
  53. if 'POST' in request.method:
  54. submit_node(no, request)
  55. if no:
  56. node = DB.getnodes([no])[0]
  57. else:
  58. class Node(object):
  59. def __init__(self):
  60. self._id = None
  61. self.username = ''
  62. self.password = ''
  63. self.url = ''
  64. self.notes = ''
  65. self.tags = ''
  66. node = Node()
  67. output = template('edit.tpl', node=node,
  68. template_lookup=templates_path)
  69. return output
  70. @route('/auth', method=['GET', 'POST'])
  71. def is_authenticated():
  72. global AUTHENTICATED
  73. crypto = CryptoEngine.get()
  74. if request.method == 'POST':
  75. key = request.POST.get('pwd', '')
  76. crypto.auth(key)
  77. AUTHENTICATED = True
  78. redirect('/')
  79. else:
  80. return template("login.tpl", template_lookup=templates_path)
  81. @route('/', method=['GET', 'POST'])
  82. @require_auth
  83. def listnodes(apply=['require_login']):
  84. global AUTHENTICATED, TAGS, DB
  85. _filter = None
  86. if 'POST' in request.method:
  87. _filter = request.POST.get('tag')
  88. if _filter:
  89. DB._filtertags = [TagNew(_filter.strip())]
  90. if _filter == 'None':
  91. DB._filtertags = []
  92. nodeids = DB.listnodes()
  93. nodes = DB.getnodes(nodeids)
  94. nodesd = [''] * len(nodes)
  95. for idx, node in enumerate(nodes):
  96. ntags = [t.strip() for t in filter(None, node.tags)]
  97. nodesd[idx] = ('@'.join((node.username, node.url)),
  98. ', '.join(ntags))
  99. if not TAGS:
  100. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  101. TAGS.sort()
  102. TAGS.insert(0, 'None')
  103. html_nodes = template("main.tpl", nodes=nodes, tags=TAGS,
  104. template_lookup=[resource_filename('pwman',
  105. 'ui/templates')])
  106. return html_nodes
  107. if __name__ == '__main__':
  108. OSX = False
  109. args = parser_options().parse_args()
  110. xselpath, dbtype = get_conf_options(args, OSX)
  111. dbver = 0.4
  112. DB = pwman.data.factory.create(dbtype, dbver)
  113. DB.open()
  114. crypto = CryptoEngine.get()
  115. debug(True)
  116. run(reloader=True)