diff --git a/scripts/README.md b/scripts/README.md index 85ed35e..1c21ad9 100644 --- a/scripts/README.md +++ b/scripts/README.md @@ -17,10 +17,23 @@ writes the data into the MongoDB database. ## add_user.js Asks the user about his data (username, password, class** and adds the user to the database. -**NOTE**: Requires `mongodb` to be installed via npm +**NOTE**: Requires `mongodb** to be installed via npm + +**NOTE**: This script is written in JavaScript, so we can ensure that the same pbkdf2 function is used +to create the user and to hash the password later on. ### Usage `node add_user.js ` - `URI`: The URI of the MongoDB instance - `Database`: The name of the database in the MongoDB instance + +## add_level.py +Asks about a new level to add + +**NOTE**: Requires `mongodb** to be installed via npm + +### Usage + +- `URI`: The URI of the MongoDB instance +- `Database`: The name of the database in the MongoDB instance diff --git a/scripts/add_level.py b/scripts/add_level.py new file mode 100644 index 0000000..dcae353 --- /dev/null +++ b/scripts/add_level.py @@ -0,0 +1,50 @@ +import pymongo +import sys + +def log(msg, err=False, tabs=0): + if (not err): + print("[*] " + "\t" * tabs + msg) + else: + print("[X] " + "\t" * tabs + msg) + +def dbg(msg, tabs=0): + print("[D] " + "\t" * tabs + msg) + +if (len(sys.argv) < 3): + log("Not enough arguments!", err=True) + sys.exit(1) + +log("Level") +level = int(input("> ")) + +log("Level name") +name = input("> ") + +log("Level description") +desc = input("> ") + +log("Vocabulary (Comma separated)") +vocab_raw = input("> ") + +log("Preprocess vocabulary list") +vocab = [] +for id_raw in vocab_raw.split(","): + try: + vocab.append(int(id_raw)) + except: + log("Invalid id found: '{0}'".format(id), err=True) + sys.exit(1) + +log("Connecting to database...") +client = pymongo.MongoClient(sys.argv[1]) +log("Getting DB...") +db = client[sys.argv[2]] +log("Inserting...") +res = db["levels"].insert_one({ + "level": level, + "name": name, + "description": desc, + "vocab": vocab +}) + +log("Success", tabs=1)