scripts: Add add_level.py
This commit is contained in:
parent
8e71aa85dc
commit
1a07fd2ddb
@ -17,10 +17,23 @@ writes the data into the MongoDB database.
|
|||||||
## add_user.js
|
## add_user.js
|
||||||
Asks the user about his data (username, password, class** and adds the user to the database.
|
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
|
### Usage
|
||||||
`node add_user.js <URI> <Database>`
|
`node add_user.js <URI> <Database>`
|
||||||
|
|
||||||
- `URI`: The URI of the MongoDB instance
|
- `URI`: The URI of the MongoDB instance
|
||||||
- `Database`: The name of the database in 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
|
||||||
|
50
scripts/add_level.py
Normal file
50
scripts/add_level.py
Normal file
@ -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)
|
Reference in New Issue
Block a user