Commit 8234ef48 authored by Alan Marchiori's avatar Alan Marchiori
Browse files

Added db query and insert, added metadata to history, changed dates to date types

parent 8796cc79
......@@ -6,6 +6,8 @@ from pymongo import MongoClient
import pymongo
from utils.db import make_connect_str, encrypt_pass, make_password
import utils.db
from bson import json_util
import sys
@click.group()
def db():
"Debug interface for db"
......@@ -21,6 +23,55 @@ def show():
users = db.command({'usersInfo':1})
pprint(users)
@db.command()
@click.argument('username')
@click.option('--course', type=str, default=None,
help='A course string like CSCI206-S20-60 (default is all courses).')
def query(username, course):
"shows all gradedb entries for a given username. Useful for backups or moving sections (see insert)."
with config.UserConfig() as uc:
col = MongoClient(make_connect_str()).get_default_database()[config.labtool_mongodb_collection]
qry = {'path': {'$regex': username}}
if course:
qry['path']['$regex'] = course + '/' + username
docs = list(col.find(qry,{'_id': False}))
print(json_util.dumps(docs, indent=2))
@db.command()
@click.option('--username', type=str, default=None,
help='New username to use in path (default is no change).')
@click.option('--course', type=str, default=None,
help='New course string like CSCI206-S20-60 to use in path (default is no change).')
def insert(username, course):
"Inserts json documents from stdin as grades (export with query), use username/course to move students"
docs = json_util.loads(sys.stdin.read())
# remap paths if needed
for doc in docs:
old_path = doc['path'].split("/")
if course == None:
course = old_path[0]
if username == None:
username = old_path[1]
new_path = "/".join([course, username, old_path[2]])
echo(f"Changed path {doc['path']} -> {new_path}")
doc['path'] = new_path
with config.UserConfig() as uc:
col = MongoClient(make_connect_str()).get_default_database()[config.labtool_mongodb_collection]
res = col.insert_many(docs, ordered=False)
echo ("Result", res.inserted_ids)
@db.command()
@click.argument('username')
@click.option('--role', type=str, default='readWrite',
......
......@@ -203,7 +203,7 @@ def grade(lab, part, clone, dograde, regrade, user, skip, push, **kwargs):
'result': presults [-1],
'output': end_capture_output(),
'who': who,
'at': utils.timestamp().isoformat()}
'at': utils.timestamp()}
for partstr, info in rubric['parts'].items():
# part can be zero.
......@@ -275,7 +275,7 @@ def grade(lab, part, clone, dograde, regrade, user, skip, push, **kwargs):
'comment': grade_msg if grade_msg else "",
'output': end_capture_output(),
'who': who,
'at': utils.timestamp().isoformat()}
'at': utils.timestamp()}
except click.exceptions.Abort:
end_capture_output()
warn("Ctrl-c -- abort.")
......@@ -291,7 +291,7 @@ def grade(lab, part, clone, dograde, regrade, user, skip, push, **kwargs):
# make total from rubric points not the graded items only (hist)
'total': sum([x['points'] for x in rubric['parts'].values()]),
'who': who,
'at': utils.timestamp().isoformat()
'at': utils.timestamp()
}
echo("----TOTAL----{} of {}".format(
hist['grade']['TOTAL']['grade'],
......
......@@ -22,7 +22,7 @@ import os
import platform
import sys
__version__ = '2.1.2'
__version__ = '2.2.0'
__date__ = '2021-01-05T14:53:15.549387'
__user__ = 'cs206'
__host__ = 'linuxremote3.bucknell.edu'
......@@ -73,7 +73,9 @@ if __name__ =="__main__":
# open primary userconfig context
try:
with config.UserConfig() as uc:
# expose labtool version through config module
config.labtool_version = labtool_version
with config.UserConfig() as uc:
main()
except Exception as x:
error("Oops--Something went wrong in labtool ({}).".format(str(x)))
......
......@@ -4,6 +4,8 @@ import platform
import json
import sys
import config
import utils
from utils.db import make_connect_str
from config.echo import debug, error, echo, warn
from pymongo import MongoClient
......@@ -88,6 +90,10 @@ class History(dict):
# prompt to take lock or abort
self['path'] = self.dbpath
self['created'] = utils.timestamp()
# this will keep the lt_version what it was at creation.
self['lt_ver'] = config.labtool_version
if d != None:
self.update(d)
......@@ -135,7 +141,9 @@ class History(dict):
debug("HISTORY EXIT")
if not self.save:
return
# this will always update the modified to latest
self['modified'] = utils.timestamp()
if self.gradebackend == History.backend_mongodb:
# unlock so when saved, the document is not locked
if 'lock' in self:
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment