Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a small script #70

Merged
merged 2 commits into from
Nov 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions app/scripts/find_and_replace_some_text_in_all_quizzes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from pymongo import MongoClient
import os

if __name__ == "__main__":

if "MONGO_AUTH_CREDENTIALS" not in os.environ:
from dotenv import load_dotenv

load_dotenv("../../.env")
client = MongoClient(os.getenv("MONGO_AUTH_CREDENTIALS"))
quiz_collection = client.quiz.quizzes
question_collection = client.quiz.questions

changed_questions = []
changed_quizzes = set()

# search in the quiz collection, and in each quiz and each question set, find a the question
# which are the respective text you're looking to replace
# then update the question inside the quiz and separately in the questions collection as well
for quiz_index, quiz in enumerate(quiz_collection.find()):
quiz_id = quiz["_id"]
does_quiz_need_to_change = False
for question_set_index, question_set in enumerate(quiz["question_sets"]):
question_set_id = question_set["_id"]
for question_index, question in enumerate(question_set["questions"]):
if "text" in question and "\u20d7" in question["text"]:
does_quiz_need_to_change = True
changed_questions.append(question["_id"])
changed_quizzes.add(quiz_id)
question["text"] = question["text"].replace("\u20d7", "\\vec.")

# update the question also inside question collection
question_collection.update_one(
{"_id": question["_id"]}, {"$set": {"text": question["text"]}}
)

if does_quiz_need_to_change:
print("quiz needs to be updated")
quiz_collection.update_one({"_id": quiz_id}, {"$set": quiz})

# search in the questions collection
# we need to because the new subset pattern will cause not all questions ka text to exist in the QUIZZES collection
# so we need to check the QUESTIONS collection as well
for question_index, question in enumerate(question_collection.find()):
if "text" in question and "\u20d7" in question["text"]:
question["text"] = question["text"].replace("\u20d7", "\\vec.")
# update the question also inside question collection
question_collection.update_one(
{"_id": question["_id"]}, {"$set": {"text": question["text"]}}
)

print("DONE!")
print(len(changed_questions))
print(changed_questions)

print(len(changed_quizzes))
print(changed_quizzes)
74 changes: 37 additions & 37 deletions app/scripts/update_specific_user_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,55 +11,55 @@
client = MongoClient(os.getenv("MONGO_AUTH_CREDENTIALS"))
session_collection = client.quiz.sessions
# create a key value pair array
# each element of the array will be a dict.
# The dict will have two keys. one is the old user id that we're looking for in the sessions collection
# and another key will be the new user_id that we want to replace with
# each element of the array will be a dict.
# The dict will have two keys. one is the old user id that we're looking for in the sessions collection
# and another key will be the new user_id that we want to replace with
key_value_pair_arr = []

# read the mapping json file
mapping_data = {}
with open('mapping.json', 'r') as f:
mapping_data = json.load(f)
with open("mapping.json", "r") as f:
mapping_data = json.load(f)

for key, value in mapping_data.items():
key_value_pair_arr.append({
"old_user_id": key,
"new_user_id": value
})
key_value_pair_arr.append({"old_user_id": key, "new_user_id": value})

list_of_old_user_ids = list(map((lambda x: x["old_user_id"]), key_value_pair_arr))
response = session_collection.update_many(
{"user_id": {"$in": list_of_old_user_ids}},
[
{
"$set": {
"user_id": {
"$let": {
"vars": {
"obj": {
"$arrayElemAt": [
{
"$filter": {
"input": key_value_pair_arr,
"as": "kvpa",
"cond": {
"$eq": ["$$kvpa.old_user_id", "$user_id"]
}
}
},
0
]
}
},
"in": "$$obj.new_user_id"
}
{"user_id": {"$in": list_of_old_user_ids}},
[
{
"$set": {
"user_id": {
"$let": {
"vars": {
"obj": {
"$arrayElemAt": [
{
"$filter": {
"input": key_value_pair_arr,
"as": "kvpa",
"cond": {
"$eq": [
"$$kvpa.old_user_id",
"$user_id",
]
},
}
},
0,
]
}
},
"in": "$$obj.new_user_id",
}
}
}
}
}
]
}
],
)
print(response)
print(response.acknowledged)
print(response.matched_count)
print(response.modified_count)
print(response.upserted_id)
print(response.upserted_id)