mongodb.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # ============================================================================
  2. # This file is part of Pwman3.
  3. #
  4. # Pwman3 is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License, version 2
  6. # as published by the Free Software Foundation;
  7. #
  8. # Pwman3 is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with Pwman3; if not, write to the Free Software
  15. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  16. # ============================================================================
  17. # Copyright (C) 2015 Oz Nahum Tiram <nahumoz@gmail.com>
  18. # ============================================================================
  19. from pwman.data.database import Database, __DB_FORMAT__
  20. import pymongo
  21. class MongoDB(Database):
  22. @classmethod
  23. def check_db_version(cls, dburi):
  24. return __DB_FORMAT__
  25. def __init__(self, mongodb_uri, dbformat=__DB_FORMAT__):
  26. self.uri = mongodb_uri.geturl()
  27. def _open(self):
  28. self._con = pymongo.Connection(self.uri)
  29. self._db = self._con.get_default_database()
  30. counters = self._db.counters.find()
  31. if not counters.count():
  32. self._db.counters.insert({'_id': 'nodeid', 'seq': 0})
  33. def _get_next_node_id(self):
  34. # for newer pymongo versions ...
  35. # return_document=ReturnDocument.AFTER
  36. nodeid = self._db.counters.find_and_modify(
  37. {'_id': 'nodeid'}, {'$inc': {'seq': 1}}, new=True,
  38. fields={'seq': 1, '_id': 0})
  39. return nodeid['seq']
  40. def getnodes(self, ids):
  41. if ids:
  42. ids = list(map(int, ids))
  43. node_dicts = self._db.nodes.find({'_id': {'$in': ids}})
  44. else:
  45. node_dicts = self._db.nodes.find({})
  46. nodes = []
  47. for node in node_dicts:
  48. n = [node['_id'],
  49. node['user'],
  50. node['password'],
  51. node['url'],
  52. node['notes']]
  53. [n.append(t) for t in node['tags']]
  54. nodes.append(n)
  55. return nodes
  56. def listnodes(self, filter=None):
  57. if not filter:
  58. nodes = self._db.nodes.find({}, {'_id': 1})
  59. else:
  60. nodes = self._db.nodes.find({"tags": {'$in': [filter]}}, {'_id': 1})
  61. return [node['_id'] for node in list(nodes)]
  62. def add_node(self, node):
  63. nid = self._get_next_node_id()
  64. node = node.to_encdict()
  65. node['_id'] = nid
  66. self._db.nodes.insert(node)
  67. return nid
  68. def listtags(self):
  69. tags = self._db.nodes.distinct('tags')
  70. return tags
  71. def editnode(self, nid, **kwargs):
  72. self._db.nodes.find_and_modify({'_id': nid}, kwargs)
  73. def removenodes(self, nid):
  74. nid = list(map(int, nid))
  75. self._db.nodes.remove({'_id': {'$in': nid}})
  76. def fetch_crypto_info(self):
  77. pass
  78. def savekey(self, key):
  79. coll = self._db['crypto']
  80. salt, digest = key.split('$6$')
  81. coll.insert({'salt': salt, 'key': digest})
  82. def loadkey(self):
  83. coll = self._db['crypto']
  84. try:
  85. key = coll.find_one({}, {'_id': 0})
  86. key = key['salt'] + '$6$' + key['key']
  87. except TypeError:
  88. key = None
  89. return key
  90. def close(self):
  91. self._con.close()