webui.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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, get, redirect
  22. import os
  23. import sys
  24. import re
  25. import shutil
  26. from pwman import default_config, which
  27. from pwman import parser_options
  28. from pwman.ui import get_ui_platform
  29. from pwman.ui.tools import CLICallback
  30. from pwman.util.crypto import CryptoEngine
  31. import pwman.util.config as config
  32. import pwman.data.factory
  33. from pwman.data.tags import TagNew
  34. from pwman import parser_options, get_conf_options
  35. AUTHENTICATED = False
  36. TAGS = None
  37. DB = None
  38. tmplt = """
  39. %#template to generate a HTML table from a list of tuples (or list of lists, or tuple of tuples or ...)
  40. <form action="/" method="POST">
  41. <select multiple name="tag" onchange="this.form.submit()">
  42. %for tag in tags:
  43. <option value="{{tag}}">{{tag}}</option>
  44. %end
  45. </select>
  46. </form>
  47. <p>Click on username to view the details:</p>
  48. <table border="1">
  49. %for node in nodes:
  50. <tr>
  51. %#for item in node:
  52. %# <td><a href={{node._id}}><{{item}}</a></td>
  53. <td><a href=/node/{{node._id}}>{{node.username}}@{{node.url}}</a></td>
  54. <td>{{ ', '.join([t.strip() for t in filter(None, node.tags)]) }}</td>
  55. <td>edit</td>
  56. %end
  57. </tr>
  58. %end
  59. </table>
  60. """
  61. edit_node_tmplt = """
  62. <form action="/edit/" method="POST">
  63. Username: <input type="text" name="username"><br>
  64. Password: <input type="password" name="lastname"><br>
  65. Repeat Password: <input type="password" name="lastname"><br>
  66. Notes: <input type="text" name="notes"><br>
  67. Tags: <input type="text" name="tags"><br>
  68. <input type="submit" value="Save edits">
  69. </form>
  70. """
  71. login = """
  72. <p>Please enter your database password: <b>
  73. <form action="/auth" method="POST">
  74. Password: <input type="password" name="pwd">
  75. </form>"""
  76. @route('/node/:no')
  77. def view_node(no):
  78. global DB
  79. node = DB.getnodes([no])
  80. tmplt = """
  81. <table border="1">
  82. <tr><td>Username:</td> <td>{{ node.username }}</td></tr>
  83. <tr><td>Password:</td> <td>{{ node.password }}</td></tr>
  84. <tr><td>Url:</td> <td>{{node.url}} </td></tr>
  85. <tr><td>Notes:</td> <td>{{node.notes}}</td></tr>
  86. <tr><td>Tags:</td> <td>{{node.tags}}</td></tr>
  87. </table>
  88. """
  89. output = template(tmplt, node=node[0])
  90. return output
  91. @route('/new', method=['GET', 'POST'])
  92. def new():
  93. pass
  94. @route('/edit/:no', method=['GET', 'POST'])
  95. def edit_node(no):
  96. output = template(edit_node_tmplt,)
  97. return output
  98. @route('/auth', method=['GET', 'POST'])
  99. def is_authenticated():
  100. global AUTHENTICATED
  101. crypto = CryptoEngine.get()
  102. if request.method == 'POST':
  103. key = request.POST.get('pwd', '')
  104. crypto.auth(key)
  105. AUTHENTICATED = True
  106. redirect('/')
  107. else:
  108. return login
  109. @route('/', method=['GET', 'POST'])
  110. def listnodes():
  111. global AUTHENTICATED, TAGS, DB
  112. _filter = None
  113. OSX = False
  114. args = parser_options().parse_args()
  115. xselpath, dbtype = get_conf_options(args, OSX)
  116. dbver = 0.4
  117. DB = pwman.data.factory.create(dbtype, dbver)
  118. DB.open()
  119. crypto = CryptoEngine.get()
  120. if not AUTHENTICATED:
  121. redirect('/auth')
  122. if 'POST' in request.method:
  123. _filter = request.POST.get('tag')
  124. if _filter:
  125. DB._filtertags = [TagNew(_filter.strip())]
  126. if _filter == 'None':
  127. DB._filtertags = []
  128. nodeids = DB.listnodes()
  129. nodes = DB.getnodes(nodeids)
  130. nodesd = [''] * len(nodes)
  131. for idx, node in enumerate(nodes):
  132. ntags = [t.strip() for t in filter(None, node.tags)]
  133. nodesd[idx] = ('@'.join((node.username, node.url)),
  134. ', '.join(ntags))
  135. if not TAGS:
  136. TAGS = list(set([''.join(node.tags).strip() for node in nodes]))
  137. TAGS.sort()
  138. TAGS.insert(0, 'None')
  139. print(len(TAGS))
  140. output = template(tmplt, nodes=nodes, tags=TAGS)
  141. return output
  142. debug(True)
  143. run(reloader=True)