def print_batch_by_school(argv): '''Takes the system arguments as parameter and then prints the batch based on the school id. Prints 'School not found' if the id does not exist. Keyword arguments: argv -- An array of command line arguments passed to the program. ''' i = 0 for item in Batch.select(): if str(item.school.id) == str(argv[2]): print item break i += 1 if i == len(Batch.select()): print "School not found"
def print_student_by_batch(argv): '''Takes the system arguments as parameter and then prints the students in that batch based on the student id. Prints 'School not found' if the id does not exist. Keyword arguments: argv -- An array of command line arguments passed to the program. ''' i = 0 for student in Student.select().where(Student.batch == argv[2]): print student for item in Batch.select(): if str(item.school.id) == str(argv[2]): break i += 1 for batch in Batch.select(): if str(batch.id) == str(argv[2]): return print "School not found"
def export_json(argv): '''Export all data in JSON format. Keyword arguments: argv -- An array of command line arguments. ''' data = [] for school in School.select(): dict = {"name": school.name} data.append(dict) '''Add the batches.''' for dict in data: batches = [] for batch in Batch.select(): if batch.school.name == dict["name"]: batch_dict = {} dict["batches"] = batches batch_dict["name"] = batch.name batches.append(batch_dict) '''Add the students in their batch.''' for dict in data: if dict.get("batches") is not None: for batch in dict.get("batches"): students = [] for student in Student.select(): if str(student.batch.name) == str(batch["name"]): student_dict = {} batch["students"] = students student_dict["first_name"] = student.first_name student_dict["last_name"] = student.last_name student_dict["age"] = student.age students.append(student_dict) '''Add the exercises to the corresponding student.''' for dict in data: if dict.get("batches") is not None: for batch in dict.get("batches"): for student in batch["students"]: exercises = [] for e in Exercise.select(): if e.student.first_name == student.get("first_name"): exercsie_dict = {} student["exercises"] = exercises exercsie_dict["note"] = e.note exercsie_dict["subject"] = e.subject exercises.append(exercsie_dict) print json.dumps(data)
def print_all(argv): '''Print all data in database, ordered with tab heirarchy. Keyword arguments: argv -- An array of command line arguments passed to the program. ''' for school in School.select(): print school for batch in Batch.select(): if batch.school.id == school.id: print "\t" + str(batch) for student in Student.select(): if student.batch.id == batch.id: print "\t\t" + str(student) for exercise in Exercise.select(): if exercise.student.id == student.id: print "\t\t\t" + str(exercise)
def change_batch(argv): '''Takes the system arguments as parameter. argv[2] is id of the student to change, argv[3] is the batch to change the student to. Keyword arguments: argv -- An array of command line arguments passed to the program. ''' student_found = False batch_found = False for student in Student.select(): if str(student.id) == str(argv[2]): student_found = True for batch in Batch.select(): if str(batch.id) == str(argv[3]): batch_found = True if student_found is False: print "Student not found" return if batch_found is False: print "Batch not found" return for student in Student.select(): if str(student.id) == str(argv[2]): if str(student.batch.id) == str(argv[3]): student_found = True print student, print "already in " + str(student.batch) else: '''If the student is not already in the batch, then reassign.''' tmp_id = student.id tmp_age = student.age tmp_first_name = student.first_name tmp_last_name = student.last_name d = Student.delete().where(Student.id == tmp_id) d.execute() new = Student.create(id=tmp_id, batch=argv[3], age=tmp_age, last_name=tmp_last_name, first_name=tmp_first_name) print student, print "has been move to", print new.batch
print "Please set a JSON string" return except ValueError, e: batch_id = 0 student_id = 0 for k in loaded_json: for batch in k["batches"]: for student in batch["students"]: for exercise in student["exercises"]: insert_item(["", "insert", "school", k["name"]]) for school in School.select(): if school.name == k["name"]: school_id = school.id insert_item(["", "insert", "batch", school_id, batch.get("name")]) for orig_batch in Batch.select(): if orig_batch.name == batch.get("name"): batch_id = orig_batch.id insert_item(["", "insert", "student", batch_id, student.get("age"), student.get("last_name"), student.get("first_name")]) for s in Student.select(): if s.first_name == student.get("first_name"): student_id = s.id insert_item(["", "insert", "exercise", student_id, exercise.get("subject"), exercise.get("note")]) def export_json(argv):
try: Student.create_table() except peewee.OperationError: pass try: Exercise.create_table() except peewee.OperationError: pass elif sys.argv[1] == 'print': if len(sys.argv) >= 3: if sys.argv[2] == 'school': for school in School.select(): print school elif sys.argv[2] == 'batch': for batch in Batch.select(): print batch elif sys.argv[2] == 'user': for user in User.select(): print user elif sys.argv[2] == 'student': for student in Student.select(): print student elif sys.argv[2] == 'exercise': for exercise in Exercise.select(): print exercise elif sys.argv[1] == 'insert': if sys.argv[2] == 'school': school = School.insert(name=sys.argv[3]).execute() print "New school: " + str(School.select().where(School.id==school).get()) elif sys.argv[2] == 'batch':