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
from db.mongo import Model
class Product(Model):
def __init__(self, name, price, available, tags):
self.__id = None
self.__name = name
self.__price = price
self.__available = available
self.__tags = tags
@staticmethod
def get_collection():
return Model.get_db().get_collection('products')
@staticmethod
def dict_to_object(dictionary):
pass
def to_dict(self):
dict = {}
if self.__id is not None:
dict["_id"] = self.__id
if self.__name is not None:
dict["name"] = self.__name
if self.__price is not None:
dict["price"] = self.__price
if self.__available is not None:
dict["available"] = self.__available
if self.__tags is not None:
dict["tags"] = self.__tags
return dict
def store(self):
add_document = Product.get_collection().insert_one(self.to_dict())
self.__id = str(add_document.inserted_id)
def search_by_tags(self):
pass
@staticmethod
def search(count, tags):
print(len(tags))
print(tags)
if len(tags) == 0:
objects = Product.get_collection().find().limit(count)
else:
objects = Product.get_collection().find({"tags": {"$all": tags}}).sort([("price", -1)]).limit(count)
result = []
for obj in objects:
obj["_id"] = str(obj["_id"])
result.append(obj)
return result
def get_id(self):
return self.__id