def add(self, student): ''' Adds a student if it's valid in repo Input: student - Student obj ''' if not isinstance(student, Student): raise TypeError("Student must be an instace of Student class!") self._sVal.validate(student) undo = FunctionCall(self.remove, student.id) redo = FunctionCall(self.add, student) op = Operation(redo, undo) self._undo.addOperation(op) self._repo.add(student)
def remove(self, assignmnetID, cascadeOP=None): ''' Removes an assignmnet from repo Input: assignmnetID - positive integer ''' assignment = self.findByID(assignmnetID) self._repo.remove(assignmnetID) undo = FunctionCall(self.add, assignment) redo = FunctionCall(self.remove, assignmnetID) op = Operation(redo, undo) if cascadeOP: cascadeOP.addOp(op) else: self._undo.addOperation(op)
def add(self, grade, cascade=None): ''' Adds in repo a grade if it's valid Input: grade - Grade obj ''' if not isinstance(grade, Grade): raise TypeError("Grade must be an instace of Grade class!") self._gradeValid.validate(grade) if not cascade: undo = FunctionCall(self.remove, grade.studentID, grade.assignID) redo = FunctionCall(self.add, grade) op = Operation(redo, undo) self._undo.addOperation(op) self._repo.add(grade)
def update(self, newGrade): ''' Updates a grade's info by its id if newGrade is valid Input: newGrade - Grade obj ''' if not isinstance(newGrade, Grade): raise TypeError("Grade must be an instace of Grade class!") self._gradeValid.validate(newGrade) oldGrade = self.findByID(newGrade.studentID, newGrade.assignID) undo = FunctionCall(self.update, oldGrade) redo = FunctionCall(self.update, newGrade) op = Operation(redo, undo) self._undo.addOperation(op) self._repo.update(newGrade)
def remove(self, studentID, cascadeOpe=None): ''' Removes a student from repo Input: studentID - positive integer ''' st = self.findByID(studentID) self._repo.remove(studentID) undo = FunctionCall(self.add, st) redo = FunctionCall(self.remove, studentID) op = Operation(redo, undo) if cascadeOpe is None: self._undo.addOperation(op) else: op = Operation(redo, undo) cascadeOpe.addOp(op)
def add(self, assignmnet): ''' Adds in repo an assignmnets if it's valid Input: assignmnet - Assignmnet obj ''' if not isinstance(assignmnet, Assignment): raise TypeError( "Assignment must be an instace of Assignment class!") self._aVal.validate(assignmnet) undo = FunctionCall(self.remove, assignmnet.id) redo = FunctionCall(self.add, assignmnet) op = Operation(redo, undo) self._undo.addOperation(op) self._repo.add(assignmnet)
def update(self, studentNew): ''' Updates a student's info by its id if studentNew is valid Input: studentNew - Student obj ''' if not isinstance(studentNew, Student): raise TypeError("Student must be an instace of Student class!") self._sVal.validate(studentNew) st = self.findByID(studentNew.id) undo = FunctionCall(self.update, studentNew) redo = FunctionCall(self.update, st) op = Operation(redo, undo) self._undo.addOperation(op) self._repo.update(studentNew)
def update(self, assignmnetNew): ''' Updates an assignmnet info by its id if assignmnetNew is valid Input: assignmnetNew - Assignmnet obj ''' if not isinstance(assignmnetNew, Assignment): raise TypeError( "Assignment must be an instace of Assignment class!") self._aVal.validate(assignmnetNew) assignmentOld = self.findByID(assignmnetNew.id) undo = FunctionCall(self.update, assignmentOld) redo = FunctionCall(self.update, assignmnetNew) op = Operation(redo, undo) self._undo.addOperation(op) self._repo.update(assignmnetNew)
def remove(self, studID, assigID, cascade=None): ''' Removes a grade Input: studID - positive int assigID - positive int Output: True - if removing succeded False - otherwise ''' fd = self.findByID(studID, assigID) if type(fd) != Grade: raise ValueError("This grade does not exist!") self._repo.removeGrade(fd) undo = FunctionCall(self.add, fd) redo = FunctionCall(self.remove, fd.studentID, fd.assignID) op = Operation(redo, undo) if not cascade: self._undo.addOperation(op)
def setAssigStud(self, assignmentID, studentID, cascade=None): ''' Assign an assignment to a student by adding in grades list a grade assignmenID, studentID and a mark of 0 Input: - assignmentID - int >=0 - studentID - int >=0 Output: - True - if setting succeded - Fale - otherwise ''' if self.filterGradesBy(studentID, assignmentID) == []: redo = FunctionCall(self.setAssigStud, assignmentID, studentID, 0.0) undo = FunctionCall(self.remove, studentID, assignmentID, True) if not isinstance(cascade, OperationsCascade): self._undo.addOperation(Operation(redo, undo)) else: cascade.addOp(Operation(redo, undo)) self.add(Grade(assignmentID, studentID, 0.0), True) return True return False
def removeStud(self, studID): ''' Removes a student from repo and its grades Input: studentID - positive integer Output: True - if removing succeded False - otherwise ''' cmds = OperationsCascade() x = self._stud.findByID(studID) if type(x) == Student: gr = self.getAll() for i in range(len(gr) - 1, -1, -1): if gr[i].studentID == studID: redo = FunctionCall(self.remove, gr[i].studentID, gr[i].assignID) undo = FunctionCall(self.add, gr[i]) op = Operation(redo, undo) cmds.addOp(op) self._repo.removeGrade(gr[i]) self._stud.remove(studID, cmds) cmds.reverseOper() self._undo.addOperation(cmds) return True return False
def removeAssign(self, assignID): ''' Removes an assignmnet from repo and also its grades Input: assignmnetID - positive integer Output: True - if removing succeded False - otherwise ''' cmds = OperationsCascade() x = self._assig.findByID(assignID) if type(x) == Assignment: gr = self.getAll() for i in range(len(gr) - 1, -1, -1): if gr[i].assignID == assignID: redo = FunctionCall(self.remove, gr[i].studentID, gr[i].assignID) undo = FunctionCall(self.add, gr[i]) op = Operation(redo, undo) cmds.addOp(op) del gr[i] self._assig.remove(assignID, cmds) cmds.reverseOper() self._undo.addOperation(cmds) return True return False