webui.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. login = """
  32. <p>Please enter your database password: <b>
  33. <form action="/auth" method="POST">
  34. Password: <input type="password" name="pwd">
  35. </form>"""
  36. @route('/node/:no')
  37. def view_node(no):
  38. global DB
  39. node = DB.getnodes([no])
  40. tmplt = """
  41. <table border="1">
  42. <tr><td>Username:</td> <td>{{ node.username }}</td></tr>
  43. <tr><td>Password:</td> <td>{{ node.password }}</td></tr>
  44. <tr><td>Url:</td> <td>{{node.url}} </td></tr>
  45. <tr><td>Notes:</td> <td>{{node.notes}}</td></tr>
  46. <tr><td>Tags:</td> <td>{{node.tags}}</td></tr>
  47. </table>
  48. """
  49. output = template(tmplt, node=node[0])
  50. return output
  51. def submit_node(id, request):
  52. # create new\update node based on request.params.items()
  53. redirect('/')
  54. @route('/new/', method=['GET', 'POST'])
  55. @route('/edit/:no', method=['GET', 'POST'])
  56. def edit_node(no=None):
  57. global DB
  58. if 'POST' in request.method:
  59. submit_node(no, request)
  60. if no:
  61. node = DB.getnodes([no])[0]
  62. else:
  63. class node(object):
  64. def __init__(self):
  65. self._id = None
  66. self.username = ''
  67. self.password = ''
  68. self.url = ''
  69. self.notes = ''
  70. self.tags = ''
  71. node = node()
  72. output = template('edit.tpl', node=node,
  73. template_lookup=templates_path)
  74. return output
  75. @route('/auth', method=['GET', 'POST'])
  76. def is_authenticated():
  77. global AUTHENTICATED
  78. crypto = CryptoEngine.get()
  79. if request.method == 'POST':
  80. key = request.POST.get('pwd', '')
  81. crypto.auth(key)
  82. AUTHENTICATED = True
  83. redirect('/')
  84. else:
  85. return login
  86. @route('/', method=['GET', 'POST'])
  87. def listnodes():
  88. global AUTHENTICATED, TAGS, DB
  89. _filter = None
  90. OSX = False
  91. args = parser_options().parse_args()
  92. xselpath, dbtype = get_conf_options(args, OSX)
  93. dbver = 0.4
  94. DB = pwman.data.factory.create(dbtype, dbver)
  95. DB.open()
  96. crypto = CryptoEngine.get()
  97. if not AUTHENTICATED:
  98. redirect('/auth')
  99. if 'POST' in request.method:
  100. _filter = request.POST.get('tag')
  101. if _filter:
  102. DB._filtertags = [TagNew(_filter.strip())]
  103. if _filter == 'None':
  104. DB._filtertags = []
  105. nodeids = DB.listnodes()
  106. nodes = DB.getnodes(nodeids)
  107. nodesd = [''] * len(nodes)
  108. for idx, node in enumerate(nodes):
  109. ntags = [t.strip() for t in filter(None, node.tags)]
  110. nodesd[idx] = ('@'.join((node.username, node.url)),
  111. ', '.join(ntags))
  112. if not TAGS:
  113. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  114. TAGS.sort()
  115. TAGS.insert(0, 'None')
  116. html_nodes = template("main.tpl", nodes=nodes, tags=TAGS,
  117. template_lookup=[resource_filename('pwman',
  118. 'ui/templates')])
  119. return html_nodes
  120. debug(True)
  121. run(reloader=True)