Skip to content

Commit

Permalink
fixes to deprecated pymongo apis - remove/count
Browse files Browse the repository at this point in the history
  • Loading branch information
sud335 committed Jan 24, 2024
1 parent 7ac852a commit b92daa8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def delete(self, name):
if sensor_group is None:
return jsonify(responses.invalid_usergroup)
if email == sensor_group.owner and defs.invalidate_permission(name):
SensorGroup._get_collection().remove({"name": sensor_group["name"]})
SensorGroup._get_collection().delete_one({"name": sensor_group["name"]})
response = dict(responses.success_true)
else:
response = dict(responses.sensorgroup_delete_authorization)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def delete(self, name):
if user_group is None:
return jsonify(responses.invalid_usergroup)
if authorize_addition(name, get_email()):
UserGroup._get_collection().remove({"name": user_group["name"]})
UserGroup._get_collection().delete_one({"name": user_group["name"]})
response = dict(responses.success_true)
else:
response = dict(responses.usergroup_delete_authorization)
Expand Down
2 changes: 1 addition & 1 deletion buildingdepot/CentralService/app/rest_api/users/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def delete(self, email):
return jsonify(responses.invalid_user)

# Remove client object
Client._get_collection().remove({"user": email})
Client._get_collection().delete_many({"user": email})

# Find token objects
tokens = Token._get_collection().find({"email": email})
Expand Down
30 changes: 17 additions & 13 deletions buildingdepot/DataService/app/rest_api/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ def get(self):
email = get_email()
if email is None:
return jsonify(responses.missing_parameters)
apps = Application.objects(user=email)
if apps.count() == 0:
app_len = Application._get_collection().count_documents({"user": email})
if app_len == 0:
app_list = []
else:
apps = Application._get_collection().find({"user": email})
app_list = apps[0]["apps"]
return jsonify({"success": "True", "app_list": app_list})

Expand Down Expand Up @@ -186,36 +187,37 @@ def delete(self):
"""
# get current user's list of applications
email = get_email()
apps = Application._get_collection().find({"user": email})
app_len = Application._get_collection().count_documents({"user": email})

app_to_be_deleted = []
json_result = {}
error_flag = False
channel = None
name = ""

json_data = request.get_json()
if "data" not in list(json_data.keys()):
return jsonify(responses.missing_parameters)
elif "name" not in list(json_data["data"].keys()):
return jsonify(responses.missing_parameters)
else:
name = json_data["data"]["name"]
app_name = json_data["data"]["name"]

# check whether there is an application with the given name
# case 1 - there is already an application instance for the given user
if apps.count() > 0:
if not isinstance(name, list):
if app_len > 0:
apps = Application._get_collection().find({"user": email})
if not isinstance(app_name, list):
app_to_be_deleted = None
app_filter = [x for x in apps[0]["apps"] if x["name"] == name]
app_filter = [x for x in apps[0]["apps"] if x["name"] == app_name]

if len(app_filter) > 0:
app_to_be_deleted = app_filter[0]
else:
# app_name is a list
app_to_be_deleted = []
json_result = {}
error_flag = False
for nm in name:
for nm in app_name:
app_filter = [x for x in apps[0]["apps"] if x["name"] == nm]
if len(app_filter) > 0:
app_to_be_deleted.append(app_filter[0])
Expand All @@ -234,14 +236,16 @@ def delete(self):
if pubsub is None:
return jsonify(responses.broker_connection_failure)

if not isinstance(name, list):
apps = Application._get_collection().find({"user": email})

if not isinstance(app_name, list):
try:
channel = pubsub.channel()

if "value" in list(app_to_be_deleted.keys()):
result = channel.queue_delete(queue=app_to_be_deleted["value"])

new_app_list = list([x for x in apps[0]["apps"] if x["name"] != name])
new_app_list = list([x for x in apps[0]["apps"] if x["name"] != app_name])
Application.objects(user=email).update(set__apps=new_app_list)

except Exception as e:
Expand All @@ -260,7 +264,7 @@ def delete(self):

return jsonify(responses.success_true)

elif isinstance(name, list):
elif isinstance(app_name, list):
for app_to_delete in app_to_be_deleted:
try:
channel = pubsub.channel()
Expand All @@ -281,7 +285,7 @@ def delete(self):
"error": "Failed to create queue",
}

new_app_list = list([x for x in apps[0]["apps"] if x["name"] not in name])
new_app_list = list([x for x in apps[0]["apps"] if x["name"] not in app_name])
Application.objects(user=email).update(set__apps=new_app_list)

if pubsub:
Expand Down

0 comments on commit b92daa8

Please sign in to comment.