def add_assignment(self, assign): ''' Add an assignment to assignments repo :param assign: Assignment object :return: True if succeded :exception: - Repository: ID already in ussage ''' if self.__assign_repo.get(assign.getID()) is None: self.__assign_repo.append(assign) undo = FunctionCall(self.remove_assignment, assign.getID()) redo = FunctionCall(self.add_assignment, deepcopy(assign)) op = Operation(undo, redo) self.__timeline.append(op) try: self.__assign_repo.backup() except: pass return True else: raise RepositoryError("ID already in use!")
def allocate_assign_student(self, ID, target): ''' Allocate an assignment for a student by ID :param ID: int ID of assignment :param target: int ID of student :return: None ''' link = self.create_grade(target, ID) if self.__grade_repo.get(link.getID()) is None: self.__grade_repo.append(link) #print(link.getID()) undo = FunctionCall(self.remove_grade_obj, link.getID()) redo = FunctionCall(self.allocate_assign_student, ID, target) op = Operation(undo, redo) self.__timeline.append(op) try: self.__grade_repo.backup() except: pass return True else: raise RepositoryError("ID already in use!")
def add_student(self, student): ''' Add a student to Students repo :param student: student object :return: True if succeded :exception: - Repository : ID not unique ''' if self.__stud_repo.get(student.getID()) is None: self.__stud_repo.append(student) undo = FunctionCall(self.remove_student, student.getID()) redo = FunctionCall(self.add_student, deepcopy(student)) op = Operation(undo, redo) self.__timeline.append(op) try: self.__stud_repo.backup() except: pass return True else: raise RepositoryError("ID " + str(student.getID()) + " already in use!")
def update_student_group(self, ID, group): ''' Update a student's GROUP id :param ID: int ID of student :param group: new GROUP ID :return: None :exception: - Repository: No records for given ID ''' if self.__stud_repo.get(ID) is not None: undo = FunctionCall(self.update_student_group, ID, self.__stud_repo.get(ID).getGroup()) redo = FunctionCall(self.update_student_group, ID, group) op = Operation(undo, redo) self.__timeline.append(op) self.__stud_repo.get(ID).setGroup(group) try: self.__stud_repo.backup() except: pass else: raise RepositoryError("No student for given ID, " + str(ID))
def set_grade(self, aid, sid, grd): ''' Set a grade by Stud ID and Assginment ID :param aid: int :param sid: int :param grd: float :return: ''' grade = self.__grade_repo.get(str(sid) + "::" + str(aid)) if self.__assign_repo.get(aid) is None: raise RepositoryError("This assignment doesn't exist") if grade is None: undo = FunctionCall(self.set_grade, aid, sid, -1) redo = FunctionCall(self.set_grade, aid, sid, grd) op = Operation(undo, redo) self.__timeline.append(op) self.__grade_repo.append(self.create_grade(sid, aid, grd)) else: undo = FunctionCall(self.set_grade, aid, sid, grade.getGrade) redo = FunctionCall(self.set_grade, aid, sid, grd) op = Operation(undo, redo) self.__timeline.append(op) grade.getGrade = grd
def remove_student(self, ID): ''' Remove a student from Repo by ID :param ID: int ID :return: True if succeded :exception: - Repository: No records for given ID ''' if self.__stud_repo.get(ID) is None: raise RepositoryError("No student for given ID, " + str(ID)) else: CSC = Cascade() undo = FunctionCall(self.add_student, deepcopy(self.__stud_repo.get(ID))) redo = FunctionCall(self.remove_student, ID) self.__stud_repo.remove(ID) self.remove_grade_student(ID, CSC) # REMOVE FROM GRADE op = Operation(undo, redo) CSC.append(op) self.__timeline.append(CSC) try: self.__stud_repo.backup() except: pass return True
def update_student_name(self, ID, name): ''' Update a student's NAME by given ID :param ID: int ID of student :param name: string NAME :return: None :exception: - Repository: No records for given ID ''' if self.__stud_repo.get(ID) is not None: undo = FunctionCall(self.update_student_name, ID, self.__stud_repo.get(ID).getName()) redo = FunctionCall(self.update_student_name, ID, name) op = Operation(undo, redo) self.__timeline.append(op) self.__stud_repo.get(ID).setName(name) try: self.__stud_repo.backup() except Exception as E: print(E) else: raise RepositoryError("No student for given ID, " + str(ID))
def update_assignment_deadline(self, ID, date): ''' Update assignment's deadline by ID :param ID: int ID of assignment :param date: date type Deadline :return: None :exception: - Repository: No records for given ID ''' if self.__assign_repo.get(ID) is not None: undo = FunctionCall(self.update_assignment_deadline, ID, self.__assign_repo.get(ID).getDeadline()) redo = FunctionCall(self.update_assignment_deadline, ID, date) op = Operation(undo, redo) self.__timeline.append(op) self.__assign_repo.get(ID).setDeadline(date) try: self.__assign_repo.backup() except: pass else: raise RepositoryError("No assignment for given ID")
def update_assignment_descr(self, ID, desc): ''' Update assignment description by ID :param ID: int ID of assignment :param desc: new string Description :return: None :exception: - Repository: No records for given ID ''' if self.__assign_repo.get(ID) is not None: undo = FunctionCall(self.update_assignment_descr, ID, self.__assign_repo.get(ID).getDescription()) redo = FunctionCall(self.update_assignment_descr, ID, desc) op = Operation(undo, redo) self.__timeline.append(op) self.__assign_repo.get(ID).setDescription(desc) try: self.__assign_repo.backup() except: pass else: raise RepositoryError("No assignment for given ID")
def __save_file(self): try: f = open(self.__filename, "wb") pickle.dump(self.get_all(), f) f.close() except Exception as E: raise RepositoryError(E)
def __save_file(self): try: f = open(self.__filename, "w") for obj in self.get_all(): f.write(self.obj_to_string(obj) + "\n") f.close() except Exception as E: raise RepositoryError(str(E))
def json_to_obj(self, json): try: if len(json) is not 0: if self.__method == "student": return Student(json["id"], json["name"].strip(), int(json["group"])) elif self.__method == "assignment": date = json["date"].strip().split("/") return Assignment( json["id"], json["descr"], datetime(int(date[2]), int(date[1]), int(date[0]))) elif self.__method == "grade": return Grade(json["stud_id"], json["assign_id"], float(json["grade"])) else: raise RepositoryError("Data file is corrupted") else: raise RepositoryError("Data file is corrupted") except IndexError as E: raise RepositoryError( "Data file corrupted or wrong conversion method")
def insert_grade(self, gradeOBJ): ''' Insert a grade object in grades repository :param gradeOBJ: grade :return: boolean ''' if self.__assign_repo.get(gradeOBJ.getID()) is None: self.__grade_repo.append(gradeOBJ) else: raise RepositoryError("This assignment already exists")
def obj_to_string(self, obj): if self.__method == "student": return str(obj.getID()) + "," + str(obj.getName()) + "," + str( obj.getGroup()) elif self.__method == "assignment": return str(obj.getID()) + "," + obj.getDescription() + "," + str( obj.getDeadline().strftime("%d/%m/%Y")) elif self.__method == "grade": return str(obj.getStudent()) + "," + str( obj.getAssignment()) + "," + str(obj.getGrade) else: raise RepositoryError('Invalid conversion method')
def string_to_obj(self, line): try: if len(line) is not 0: sline = line.split(",") if len(sline) == 3: if self.__method == "student": return Student(int(sline[0].strip()), sline[1].strip(), int(sline[2].strip())) elif self.__method == "assignment": date = sline[2].strip().split("/") return Assignment( int(sline[0].strip()), sline[1], datetime(int(date[2]), int(date[1]), int(date[0]))) elif self.__method == "grade": return Grade(int(sline[0].strip()), int(sline[1].strip()), float(sline[2].strip())) else: raise RepositoryError("Data file is corrupted") else: raise RepositoryError("Data file is corrupted") except IndexError as E: raise RepositoryError( "Data file corrupted or wrong conversion method")
def __save_file(self): try: FINAL = [] f = open(self.__filename, 'w', encoding='utf-8') for obj in self.get_all(): FINAL.append(self.obj_to_dict(obj)) strs = json.dumps(FINAL, indent=4, sort_keys=True, separators=(',', ': '), ensure_ascii=False) f.write(strs) f.close() except Exception as E: raise RepositoryError(str(E))
def obj_to_dict(self, obj): if self.__method == "student": return { "id": obj.getID(), "name": str(obj.getName()), "group": obj.getGroup() } elif self.__method == "assignment": return { "id": obj.getID(), "descr": obj.getDescription(), "date": str(obj.getDeadline().strftime("%d/%m/%Y")) } elif self.__method == "grade": return { "stud_id": obj.getStudent(), "assign_id": obj.getAssignment(), "grade": obj.getGrade } else: raise RepositoryError('Invalid conversion method')