Commit 124c18d3 authored by Ahmad Anvari's avatar Ahmad Anvari

add order model

parent c65b1ffd
from db.mongo import Model from db.mongo import Model
from pymongo import ReadPreference
from bson.objectid import ObjectId
from pymongo.read_concern import ReadConcern
from pymongo.write_concern import WriteConcern
class Order(Model): class Order(Model):
...@@ -8,8 +12,40 @@ class Order(Model): ...@@ -8,8 +12,40 @@ class Order(Model):
self.__product_id = product_id self.__product_id = product_id
def store(self): def store(self):
client = Model.get_client()
my_write_concern_majority = WriteConcern('majority', wtimeout=1000)
def callback(my_session):
product_collection = my_session.client.shoppe.products
user_collection = my_session.client.shoppe.users
# Important:: You must pass the session to the operations.
p = product_collection.find_one({'_id': ObjectId(self.__product_id)}, session=my_session)
u = user_collection.update_one(
{'_id': ObjectId(self.__user_id)},
{"$inc": {"balance": -1 * p["price"]}},
session=my_session)
print(u)
print(p)
pu = product_collection.update_one(
{'_id': ObjectId(self.__product_id)},
{"$inc": {"available": -1}},
session=my_session)
print(pu)
# . Step 2: Start a client session.
with client.start_session() as session:
# Step 3: Use with_transaction to start a transaction, execute the callback, and commit (or abort on error).
session.with_transaction(callback,
read_concern=ReadConcern('local'),
write_concern=my_write_concern_majority,
read_preference=ReadPreference.PRIMARY)
res = Order.get_collection().insert_one(self.to_dict()) res = Order.get_collection().insert_one(self.to_dict())
return res.inserted_id return str(res.inserted_id)
@staticmethod @staticmethod
def get_collection(): def get_collection():
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment