Skip to content

Commit

Permalink
build: upgrade pymongo to 4.7.2 (#220)
Browse files Browse the repository at this point in the history
Co-authored-by: Athitheya Gobinathan <athith_g@AthitheasLaptop.attlocal.net>
  • Loading branch information
uniqueg and Athitheya Gobinathan committed May 20, 2024
1 parent 19c127b commit 63456a5
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 19 deletions.
2 changes: 1 addition & 1 deletion examples/petstore-access-control/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def addPet(pet):
)
counter = 0
ctr = db_collection.find({}).sort([('$natural', -1)])
if not ctr.count() == 0:
if not db_collection.count_documents({}) == 0:
counter = ctr[0].get('id') + 1
record = {
"id": counter,
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore-access-control/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
- "80:8080"

mongodb:
image: mongo:3.6
image: mongo:7.0
restart: unless-stopped
volumes:
- ./data/petstore-access-control/db:/data/db
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore/controllers.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def addPet(pet):
)
counter = 0
ctr = db_collection.find({}).sort([('$natural', -1)])
if not ctr.count() == 0:
if not db_collection.count_documents({}) == 0:
counter = ctr[0].get('id') + 1
record = {
"id": counter,
Expand Down
2 changes: 1 addition & 1 deletion examples/petstore/docker-compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ services:
- "80:8080"

mongodb:
image: mongo:3.6
image: mongo:7.0
restart: unless-stopped
volumes:
- ./data/petstore/db:/data/db
Expand Down
13 changes: 8 additions & 5 deletions foca/database/register_mongodb.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,19 @@ def register_mongodb(
db_conf.client = mongo.db

# Add collections
if db_conf.collections is not None:
if db_conf.collections is not None and db_conf.client is not None:
for coll_name, coll_conf in db_conf.collections.items():

coll_conf.client = mongo.db[coll_name]
coll_conf.client = db_conf.client[coll_name]
logger.info(
f"Added database collection '{coll_name}'."
)

# Add indexes
if coll_conf.indexes is not None:
if (
coll_conf.indexes is not None
and coll_conf.client is not None
):
# Remove already created indexes if any
coll_conf.client.drop_indexes()
for index in coll_conf.indexes:
Expand Down Expand Up @@ -94,10 +97,10 @@ def add_new_database(
db_conf.client = mongo.db

# Add collections
if db_conf.collections is not None:
if db_conf.collections is not None and db_conf.client is not None:
for coll_name, coll_conf in db_conf.collections.items():

coll_conf.client = mongo.db[coll_name]
coll_conf.client = db_conf.client[coll_name]
logger.info(
f"Added database collection '{coll_name}'."
)
Expand Down
11 changes: 6 additions & 5 deletions foca/security/access_control/access_control_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,12 @@ def getAllPermissions(limit=None) -> List[Dict]:

if not limit:
limit = 0
permissions = db_coll_permission.find(
filter={},
projection={'_id': False}
).sort([('$natural', -1)]).limit(limit)
permissions = list(permissions)
permissions = list(
db_coll_permission.find(
filter={},
projection={'_id': False}
).sort([('$natural', -1)]).limit(limit)
)
user_permission_list = []
for _permission in permissions:
policy_type = _permission.get("ptype", None)
Expand Down
8 changes: 5 additions & 3 deletions foca/security/access_control/foca_casbin_adapter/adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ def __init__(
self, uri: str, dbname: str, collection: Optional[str] = "casbin_rule"
):
"""Create an adapter for Mongodb."""
client = MongoClient(uri)
client: MongoClient = MongoClient(uri)
db = client[dbname]
assert collection is not None
self._collection = db[collection]

def load_policy(self, model: CasbinRule):
Expand Down Expand Up @@ -100,8 +101,9 @@ def _delete_policy_lines(self, ptype: str, rule: List[str]) -> int:
for result in results
if line_dict_keys_len == len(result.keys()) - 1
]
results = self._collection.delete_many({"_id": {"$in": to_delete}})
return results.deleted_count
return self._collection.delete_many(
{"_id": {"$in": to_delete}}
).deleted_count

def save_policy(self, model: Model) -> bool:
"""Method to save a casbin model.
Expand Down
2 changes: 1 addition & 1 deletion foca/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from typing import (Any, Mapping, Optional)

from bson.objectid import ObjectId
from pymongo import collection as Collection
from pymongo.collection import Collection


def find_one_latest(collection: Collection) -> Optional[Mapping[Any, Any]]:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Flask-Cors==4.0.1
Flask-PyMongo==2.3.0
pydantic==1.10.13
PyJWT==2.4.0
pymongo==3.10.1
pymongo==4.7.2
PyYAML==6.0.1
requests==2.31.0
swagger-ui-bundle==0.0.6
Expand Down

0 comments on commit 63456a5

Please sign in to comment.