webui.py 3.9 KB

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