def add_target_to_session(self, target_id, session_id=None): session_obj = self.db.session.query(models.Session).get(session_id) target_obj = self.db.session.query(models.Target).get(target_id) if session_obj is None: raise exceptions.InvalidSessionReference("No session with id: %s" % str(session_id)) if target_obj is None: raise exceptions.InvalidTargetReference("No target with id: %s" % str(target_id)) if session_obj not in target_obj.sessions: session_obj.targets.append(target_obj) self.db.session.commit()
def remove_target_from_session(self, target_id, session_id=None): session_obj = self.db.session.query(models.Session).get(session_id) target_obj = self.db.session.query(models.Target).get(target_id) if session_obj is None: raise exceptions.InvalidSessionReference("No session with id: %s" % str(session_id)) if target_obj is None: raise exceptions.InvalidTargetReference("No target with id: %s" % str(target_id)) session_obj.targets.remove(target_obj) # Delete target whole together if present in this session alone if len(target_obj.sessions) == 0: self.db.Target.DeleteTarget(ID=target_obj.id) self.db.session.commit()