Skip to content

Commit d2c7059

Browse files
committed
Continue updating for PyMongo API changes
1 parent 8321491 commit d2c7059

6 files changed

Lines changed: 160 additions & 74 deletions

File tree

3-binary-data/bookshelf/model_mongodb.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# limitations under the License.
1414

1515
from bson.objectid import ObjectId
16-
from flask.ext.pymongo import PyMongo
16+
from flask_pymongo import PyMongo
1717

1818

1919
builtin_list = list
2020

2121

22-
mongo = PyMongo()
22+
mongo = None
2323

2424

2525
def _id(id):
@@ -28,6 +28,7 @@ def _id(id):
2828
return id
2929

3030

31+
# [START from_mongo]
3132
def from_mongo(data):
3233
"""
3334
Translates the MongoDB dictionary format into the format that's expected
@@ -38,12 +39,17 @@ def from_mongo(data):
3839

3940
data['id'] = str(data['_id'])
4041
return data
42+
# [END from_mongo]
4143

4244

4345
def init_app(app):
46+
global mongo
47+
48+
mongo = PyMongo(app)
4449
mongo.init_app(app)
4550

4651

52+
# [START list]
4753
def list(limit=10, cursor=None):
4854
cursor = int(cursor) if cursor else 0
4955

@@ -52,22 +58,29 @@ def list(limit=10, cursor=None):
5258

5359
next_page = cursor + limit if len(books) == limit else None
5460
return (books, next_page)
61+
# [END list]
5562

5663

64+
# [START read]
5765
def read(id):
58-
result = mongo.db.books.find_one(_id(id))
66+
result = mongo.db.books.find_one({'_id': _id(id)})
5967
return from_mongo(result)
68+
# [END read]
6069

6170

71+
# [START create]
6272
def create(data):
63-
new_id = mongo.db.books.insert(data)
64-
return read(new_id)
73+
result = mongo.db.books.insert_one(data)
74+
return read(result.inserted_id)
75+
# [END create]
6576

6677

78+
# [START update]
6779
def update(data, id):
68-
mongo.db.books.update({'_id': _id(id)}, data)
80+
mongo.db.books.replace_one({'_id': _id(id)}, data)
6981
return read(id)
82+
# [END update]
7083

7184

7285
def delete(id):
73-
mongo.db.books.remove(_id(id))
86+
mongo.db.books.delete_one({'_id': _id(id)})

4-auth/bookshelf/model_mongodb.py

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# limitations under the License.
1414

1515
from bson.objectid import ObjectId
16-
from flask.ext.pymongo import PyMongo
16+
from flask_pymongo import PyMongo
1717

1818

1919
builtin_list = list
2020

2121

22-
mongo = PyMongo()
22+
mongo = None
2323

2424

2525
def _id(id):
@@ -28,6 +28,7 @@ def _id(id):
2828
return id
2929

3030

31+
# [START from_mongo]
3132
def from_mongo(data):
3233
"""
3334
Translates the MongoDB dictionary format into the format that's expected
@@ -38,50 +39,62 @@ def from_mongo(data):
3839

3940
data['id'] = str(data['_id'])
4041
return data
42+
# [END from_mongo]
4143

4244

4345
def init_app(app):
46+
global mongo
47+
48+
mongo = PyMongo(app)
4449
mongo.init_app(app)
4550

4651

47-
def list(limit=10, cursor=None):
52+
# [START list_by_user]
53+
def list_by_user(user_id, limit=10, cursor=None):
4854
cursor = int(cursor) if cursor else 0
4955

50-
results = mongo.db.books.find(skip=cursor, limit=10).sort('title')
56+
results = mongo.db.books\
57+
.find({'createdById': user_id}, skip=cursor, limit=10)\
58+
.sort('title')
5159
books = builtin_list(map(from_mongo, results))
5260

5361
next_page = cursor + limit if len(books) == limit else None
5462
return (books, next_page)
63+
# [END list_by_user]
5564

5665

57-
# [START list_by_user]
58-
def list_by_user(user_id, limit=10, cursor=None):
66+
# [START list]
67+
def list(limit=10, cursor=None):
5968
cursor = int(cursor) if cursor else 0
6069

61-
results = mongo.db.books\
62-
.find({'createdById': user_id}, skip=cursor, limit=10)\
63-
.sort('title')
70+
results = mongo.db.books.find(skip=cursor, limit=10).sort('title')
6471
books = builtin_list(map(from_mongo, results))
6572

6673
next_page = cursor + limit if len(books) == limit else None
6774
return (books, next_page)
68-
# [END list_by_user]
75+
# [END list]
6976

7077

78+
# [START read]
7179
def read(id):
72-
result = mongo.db.books.find_one(_id(id))
80+
result = mongo.db.books.find_one({'_id': _id(id)})
7381
return from_mongo(result)
82+
# [END read]
7483

7584

85+
# [START create]
7686
def create(data):
77-
new_id = mongo.db.books.insert(data)
78-
return read(new_id)
87+
result = mongo.db.books.insert_one(data)
88+
return read(result.inserted_id)
89+
# [END create]
7990

8091

92+
# [START update]
8193
def update(data, id):
82-
mongo.db.books.update({'_id': _id(id)}, data)
94+
mongo.db.books.replace_one({'_id': _id(id)}, data)
8395
return read(id)
96+
# [END update]
8497

8598

8699
def delete(id):
87-
mongo.db.books.remove(_id(id))
100+
mongo.db.books.delete_one({'_id': _id(id)})

5-logging/bookshelf/model_mongodb.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# limitations under the License.
1414

1515
from bson.objectid import ObjectId
16-
from flask.ext.pymongo import PyMongo
16+
from flask_pymongo import PyMongo
1717

1818

1919
builtin_list = list
2020

2121

22-
mongo = PyMongo()
22+
mongo = None
2323

2424

2525
def _id(id):
@@ -28,6 +28,7 @@ def _id(id):
2828
return id
2929

3030

31+
# [START from_mongo]
3132
def from_mongo(data):
3233
"""
3334
Translates the MongoDB dictionary format into the format that's expected
@@ -38,48 +39,62 @@ def from_mongo(data):
3839

3940
data['id'] = str(data['_id'])
4041
return data
42+
# [END from_mongo]
4143

4244

4345
def init_app(app):
46+
global mongo
47+
48+
mongo = PyMongo(app)
4449
mongo.init_app(app)
4550

4651

47-
def list(limit=10, cursor=None):
52+
# [START list_by_user]
53+
def list_by_user(user_id, limit=10, cursor=None):
4854
cursor = int(cursor) if cursor else 0
4955

50-
results = mongo.db.books.find(skip=cursor, limit=10).sort('title')
56+
results = mongo.db.books\
57+
.find({'createdById': user_id}, skip=cursor, limit=10)\
58+
.sort('title')
5159
books = builtin_list(map(from_mongo, results))
5260

5361
next_page = cursor + limit if len(books) == limit else None
5462
return (books, next_page)
63+
# [END list_by_user]
5564

5665

57-
def list_by_user(user_id, limit=10, cursor=None):
66+
# [START list]
67+
def list(limit=10, cursor=None):
5868
cursor = int(cursor) if cursor else 0
5969

60-
results = mongo.db.books\
61-
.find({'createdById': user_id}, skip=cursor, limit=10)\
62-
.sort('title')
70+
results = mongo.db.books.find(skip=cursor, limit=10).sort('title')
6371
books = builtin_list(map(from_mongo, results))
6472

6573
next_page = cursor + limit if len(books) == limit else None
6674
return (books, next_page)
75+
# [END list]
6776

6877

78+
# [START read]
6979
def read(id):
70-
result = mongo.db.books.find_one(_id(id))
80+
result = mongo.db.books.find_one({'_id': _id(id)})
7181
return from_mongo(result)
82+
# [END read]
7283

7384

85+
# [START create]
7486
def create(data):
75-
new_id = mongo.db.books.insert(data)
76-
return read(new_id)
87+
result = mongo.db.books.insert_one(data)
88+
return read(result.inserted_id)
89+
# [END create]
7790

7891

92+
# [START update]
7993
def update(data, id):
80-
mongo.db.books.update({'_id': _id(id)}, data)
94+
mongo.db.books.replace_one({'_id': _id(id)}, data)
8195
return read(id)
96+
# [END update]
8297

8398

8499
def delete(id):
85-
mongo.db.books.remove(_id(id))
100+
mongo.db.books.delete_one({'_id': _id(id)})

6-pubsub/bookshelf/model_mongodb.py

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@
1313
# limitations under the License.
1414

1515
from bson.objectid import ObjectId
16-
from flask.ext.pymongo import PyMongo
16+
from flask_pymongo import PyMongo
1717

1818

1919
builtin_list = list
2020

2121

22-
mongo = PyMongo()
22+
mongo = None
2323

2424

2525
def _id(id):
@@ -28,6 +28,7 @@ def _id(id):
2828
return id
2929

3030

31+
# [START from_mongo]
3132
def from_mongo(data):
3233
"""
3334
Translates the MongoDB dictionary format into the format that's expected
@@ -38,48 +39,62 @@ def from_mongo(data):
3839

3940
data['id'] = str(data['_id'])
4041
return data
42+
# [END from_mongo]
4143

4244

4345
def init_app(app):
46+
global mongo
47+
48+
mongo = PyMongo(app)
4449
mongo.init_app(app)
4550

4651

47-
def list(limit=10, cursor=None):
52+
# [START list_by_user]
53+
def list_by_user(user_id, limit=10, cursor=None):
4854
cursor = int(cursor) if cursor else 0
4955

50-
results = mongo.db.books.find(skip=cursor, limit=10).sort('title')
56+
results = mongo.db.books\
57+
.find({'createdById': user_id}, skip=cursor, limit=10)\
58+
.sort('title')
5159
books = builtin_list(map(from_mongo, results))
5260

5361
next_page = cursor + limit if len(books) == limit else None
5462
return (books, next_page)
63+
# [END list_by_user]
5564

5665

57-
def list_by_user(user_id, limit=10, cursor=None):
66+
# [START list]
67+
def list(limit=10, cursor=None):
5868
cursor = int(cursor) if cursor else 0
5969

60-
results = mongo.db.books\
61-
.find({'createdById': user_id}, skip=cursor, limit=10)\
62-
.sort('title')
70+
results = mongo.db.books.find(skip=cursor, limit=10).sort('title')
6371
books = builtin_list(map(from_mongo, results))
6472

6573
next_page = cursor + limit if len(books) == limit else None
6674
return (books, next_page)
75+
# [END list]
6776

6877

78+
# [START read]
6979
def read(id):
70-
result = mongo.db.books.find_one(_id(id))
80+
result = mongo.db.books.find_one({'_id': _id(id)})
7181
return from_mongo(result)
82+
# [END read]
7283

7384

85+
# [START create]
7486
def create(data):
75-
new_id = mongo.db.books.insert(data)
76-
return read(new_id)
87+
result = mongo.db.books.insert_one(data)
88+
return read(result.inserted_id)
89+
# [END create]
7790

7891

92+
# [START update]
7993
def update(data, id):
80-
mongo.db.books.update({'_id': _id(id)}, data)
94+
mongo.db.books.replace_one({'_id': _id(id)}, data)
8195
return read(id)
96+
# [END update]
8297

8398

8499
def delete(id):
85-
mongo.db.books.remove(_id(id))
100+
mongo.db.books.delete_one({'_id': _id(id)})

0 commit comments

Comments
 (0)