Skip to content

Commit

Permalink
fix mypy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueg committed May 20, 2024
1 parent ea672ac commit 5a6883b
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
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.12.3
pymongo==4.7.2
PyYAML==6.0.1
requests==2.31.0
swagger-ui-bundle==0.0.6
Expand Down

0 comments on commit 5a6883b

Please sign in to comment.