@@ -19,17 +19,23 @@ def __init__(self, dbname=None, store=None, **kwargs):
1919 self .collection = self .dbclient [self .dbname ][self .store ]
2020
2121 def insert (self , report ):
22- # create a json object from an NmapReport instance
22+ """
23+ create a json object from an NmapReport instance
24+ :param NmapReport: obj to insert
25+ :return: str id
26+ """
2327 j = json .dumps (report , cls = ReportEncoder )
2428 try :
2529 id = self .collection .insert (json .loads (j ))
2630 except :
2731 print "MONGODB cannot insert"
2832 raise
29- return id
33+ return str ( id )
3034
3135 def get (self , str_report_id = None ):
32- """get return a NmapReport object
36+ """ select a NmapReport by Id
37+ :param str: id
38+ :return: NmapReport object
3339 """
3440 rid = str_report_id
3541 nmapreport = None
@@ -38,11 +44,11 @@ def get(self, str_report_id=None):
3844
3945 if isinstance (rid , ObjectId ):
4046 #get a specific report by mongo's id
41- r = self .collection .find ({'_id' : rid })
42- if r is not None :
47+ resultSet = self .collection .find ({'_id' : rid })
48+ if resultSet . count () == 1 :
4349 #search by id means only one in the iterator
44- record = r [0 ]
45- #remove mongo's id
50+ record = resultSet [0 ]
51+ #remove mongo's id to recreate the NmapReport Obj
4652 del record ['_id' ]
4753 nmapreport = NmapParser .parse_fromdict (record )
4854 return nmapreport
@@ -60,7 +66,12 @@ def getall(self, dict_filter=None):
6066 return nmapreportList
6167
6268 def delete (self , report_id = None ):
69+ """
70+ delete an obj from the backend
71+ :param str: id
72+ :return: dict document with result or None
73+ """
6374 if report_id is not None and isinstance (report_id , str ):
64- self .collection .remove ({'_id' : ObjectId (report_id )})
75+ return self .collection .remove ({'_id' : ObjectId (report_id )})
6576 else :
66- self .collection .remove ({'_id' : report_id })
77+ return self .collection .remove ({'_id' : report_id })
0 commit comments