Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
S
Shoppe
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
mongo101
Shoppe
Commits
124c18d3
Commit
124c18d3
authored
Sep 12, 2019
by
Ahmad Anvari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add order model
parent
c65b1ffd
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
1 deletion
+37
-1
order.py
db/order.py
+37
-1
No files found.
db/order.py
View file @
124c18d3
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
():
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment