def serialize_homework(hw): """ Unpacks a Homework object into a dictionary for easy JSON parsing. :param hw: A Homework item to be unpacked. :return: Dictionary containing the Homework along with nested Items and Tests. """ items = [] for item in hw.items: tests = [] for test in item.tests: tests.append( { 'id': testserializer.dumps(test.id), 'description': test.description, 'input': test.stdin, 'timeout': test.timeout } ) items.append( { 'id': itemserializer.dumps(item.id), 'name': item.name, 'description': item.description, 'tests': tests } ) return { 'id': hwserializer.dumps(hw.id), 'name': hw.name, 'description': hw.description, 'items': items }
def activate_hw(): id = click.prompt('ID of the homework to activate', type=str) realid = hwserializer.loads(id) hw = Homework.query.get(realid) if hw: hw.activate() db.session.commit() click.echo('Activated homework: ' + hwserializer.dumps(hw.id)) else: click.echo('No such homework: ' + id)
def create_hw(): name = click.prompt('Name of the assignment', type=str) click.echo('Description: (Ctrl-D to finish):') description = sys.stdin.read() hw = Homework(name, description) db.session.add(hw) db.session.commit() click.echo('Homework created with id: ' + hwserializer.dumps(hw.id)) additem = click.confirm('Do you wish to add an item to this homework?') while additem: add_item_to_homework(hw) additem = click.confirm('Do you wish to add another item?')
def main(): with open(sys.argv[1], 'r') as infile: hw = json.loads(infile.read()) dbhw = Homework(hw['name'], hw['description']) db.session.add(dbhw) db.session.commit() for item in hw['items']: dbitem = dbhw.add_item(item['name'], item['description']) db.session.add(dbitem) db.session.commit() for test in item['tests']: dbtest = dbitem.add_test(test['description'], test['input'], test['output']) db.session.add(dbtest) db.session.commit() return hwserializer.dumps(dbhw.id)
def delete_hw(): id = click.prompt('ID of the homework to delete', type=str) realid = hwserializer.loads(id) hw = Homework.query.get(realid) if hw: if not click.confirm('Please confirm!', default=False): return for item in hw.items: for test in item.tests: subs = RequestLog.query.filter( RequestLog.test_id == test.id).all() for sub in subs: db.session.delete(sub) db.session.delete(test) db.session.delete(item) db.session.delete(hw) db.session.commit() click.echo('Deleted homework: ' + hwserializer.dumps(hw.id)) else: click.echo('No such homework: ' + id)
def delete_hw(): id = click.prompt('ID of the homework to delete', type=str) realid = hwserializer.loads(id) hw = Homework.query.get(realid) if hw: if not click.confirm('Please confirm!', default=False): return for item in hw.items: for test in item.tests: subs = RequestLog.query.filter(RequestLog.test_id == test.id).all() for sub in subs: db.session.delete(sub) db.session.delete(test) db.session.delete(item) db.session.delete(hw) db.session.commit() click.echo('Deleted homework: ' + hwserializer.dumps(hw.id)) else: click.echo('No such homework: ' + id)
def list_all(): active = Homework.query.all() click.echo('Assigments: (id - name)') for hw in active: click.echo(hwserializer.dumps(hw.id) + ' - ' + hw.name) click.echo('\n')
from moulinette import hwserializer from moulinette.homework.models import * from moulinette.stats_and_logs.models import * def get_submissions_hw(id): count = 0 items = Item.query.filter(Item.homework_id == id).all() for item in items: tests = Test.query.filter(Test.item_id == item.id).all() for test in tests: submissions = RequestLog.query.filter(RequestLog.test_id == test.id).all() count += len(submissions) return count if __name__ == '__main__': homeworks = Homework.query.all() for homework in homeworks: serialid = hwserializer.dumps(homework.id) print('ID: ' + serialid) print('Name: ' + homework.name) print('Count: ' + str(get_submissions_hw(homework.id))) print()