-
Notifications
You must be signed in to change notification settings - Fork 0
/
DataAccess.py
executable file
·202 lines (161 loc) · 6.72 KB
/
DataAccess.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
from bson import Code
import bson
from pymongo import Connection
from pymongo.database import Database
from im.core.config import conf
import logging
__author__ = 'manuel'
class ConfigDB:
def __init__(self):
db_server_ip = conf("config.db_server_ip")
db_server_port = conf("config.db_server_port")
self.__connection = Connection(host=db_server_ip, port=db_server_port)
db_name = conf("config.db_name")
self.__db = Database(connection= self.__connection, name=db_name)
# Returns a tuple with paired values to be used in drop down lists.
def get_tuple(self, collection, text, value="_id", where={}):
l = []
for i in list(self.__db[collection].find(spec=where)):
l += [(i[value], i[text])]
return tuple(l)
# Moves unique records from source collection to destination, order parameter still pending.
def move_unique(self, source, destination):
map_func = Code(
"function () {"
" emit(this._id, 1);"
"}")
reduce_func = Code(
"function (key, values) {"
" return key;"
"}")
self.__db[destination].drop()
self.__db[source].map_reduce(map=map_func, reduce=reduce_func, out=destination)
# Return collection with the specified name and filter.
def get_collection(self, collection, where={}):
return self.__db[collection].find(spec=where)
# Returns the first document that matches the where clause within the specified collection.
def get_document(self, collection, where={}):
return self.__db[collection].find_one(spec_or_id=where)
# Removes the specified document.
def dispose_document(self, collection, where):
return self.__db[collection].remove(spec_or_id=where)
# Updates the document with the specified id, the document is inserted if the _id is not found.
# Returns the updated document.
def set_document(self, collection, _id, values):
if _id is None:
# If no _id to update, insert it.
_id = self.__db[collection].insert(values)
else:
# If _id to update, update it.
self.__db[collection].update(
spec={"_id": _id},
document={"$set": values},
upsert=True,
new=True)
# Return modified document.
return self.__db[collection].find_one({"_id": _id})
# Removes the specified collection from the database.
def dispose_collection(self, collection):
self.__db[collection].drop()
# Insert a bulk of documents into a collection.
def bulk_import(self, collection, doc_or_docs):
self.__db[collection].insert(doc_or_docs)
# Return the count of documents for a specific collection.
def count_documents(self, collection, where={}):
cur = self.__db[collection].find(where)
return cur.count()
def exists_collection(self, name):
return name in self.__db.collection_names()
def get_categories(self):
return self.get_tuple(
collection="categories",
text="name"
)
def get_products(self):
return self.get_tuple(
collection="products",
text="name"
)
def get_priorities(self):
return self.get_tuple(
collection="priorities",
text="label"
)
def get_users(self, role):
return self.get_tuple(
collection="users",
text="user_name",
where={"role": role}
)
def get_destinations(self):
return self.get_tuple(
collection="destinations",
text="dial"
)
def get_whitelists(self):
return self.get_tuple(
collection="lists",
text="name",
where={"type": "white"}
)
def get_blacklists(self):
return self.get_tuple(
collection="lists",
text="name",
where={"type": "black"}
)
class DataDB:
def __init__(self, name):
db_server_ip = conf("config.db_server_ip")
db_server_port = conf("config.db_server_port")
self.__db_name = "camp{0}".format(name)
self.__connection = Connection(host=db_server_ip, port=db_server_port)
self.__db = Database(connection=self.__connection, name=self.__db_name)
# Return collection with the specified name and filter.
def get_collection(self, collection, where={}):
return self.__db[collection].find(spec=where)
# Returns the first document that matches the where clause within the specified collection.
def get_document(self, collection, where={}):
return self.__db[collection].find_one(spec_or_id=where)
# Removes the specified document.
def dispose_document(self, collection, where):
return self.__db[collection].remove(spec_or_id=where)
# Updates the document with the specified id, the document is inserted if the _id is not found.
# Returns the updated document.
def set_document(self, collection, _id, values):
if _id is None:
# If no _id to update, insert it.
_id = self.__db[collection].insert(values)
else:
# If _id to update, update it.
self.__db[collection].update(
spec={"_id": _id},
document={"$set": values},
upsert=True,
new=True)
# Return modified document.
return self.__db[collection].find_one({"_id": _id})
# Removes the specified collection from the database.
def dispose_collection(self, collection):
self.__db[collection].drop()
# Insert a bulk of documents into a collection.
def bulk_import(self, collection, doc_or_docs):
self.__db[collection].insert(doc_or_docs)
# Return the count of documents for a specific collection.
def count_documents(self, collection, where={}):
cur = self.__db[collection].find(where)
return cur.count()
def exists_collection(self, name):
return name in self.__db.collection_names()
# Moves unique records from source collection to destination, order parameter still pending.
def perform_cleanup(self, source, destination):
func = (
"function(doc){" +
("if (db.whitelist.find(doc._id).count() == 1)" if self.exists_collection("whitelists") else "") +
("if (db.blacklist.find(doc._id).count() == 0)" if self.exists_collection("blacklists") else "") +
" db.data.insert(doc._id)" +
"}")
logging.debug("Function foreach: " + func)
self.__db[destination].drop()
self.__db[source].find().forEach(bson.Code(func))
logging.debug("forEach execution completed")