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 add_item_to_homework(hw): name = click.prompt('Name of the homework item', type=str) click.echo('Description: (Ctrl-D to finish):') description = sys.stdin.read() item = hw.add_item(name, description) click.echo('Created item with id: ' + itemserializer.dumps(item.id)) addtest = click.confirm('Do you wish to add a test to this item?') while addtest: add_test_to_item(item) addtest = click.confirm('Do you wish to add another test?')
def edit_item(): active = Item.query.all() click.echo('Items: (id - name)') for item in active: click.echo(itemserializer.dumps(item.id) + ' - ' + item.name) click.echo('\n') id = click.prompt('ID of item to edit: ', type=str) item = Item.query.get(itemserializer.loads(id)) click.echo("Item name: " + item.name) click.echo("Item description: " + item.description) if click.confirm('Change name?', default=True): name = click.prompt('New name: ', type=str) item.name = name if click.confirm('Change description?', default=True): click.echo('New description: (Ctrl-D to finish):') description = sys.stdin.read() item.description = description db.session.add(item) db.session.commit()