def print_student_by_school(argv): '''Takes the system arguments as parameter and then prints the students 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.school.id == argv[2]): for student in Student.select(): if str(student.batch.school.id) == str(argv[2]): print student for item in School.select(): if str(item.id) == str(argv[2]): break i += 1 if i == len(School.select()): 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)
except ValueError, e: print "Please set a JSON string" return try: int(argv[2]) 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,
User.create_table() except peewee.OperationError: pass 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':