def assess(self, objective_id, student_id, tutor_id, by_user): """Remove an objective from a users set of adopted objectives. :param objective_id: is the id of the `Objective` to be removed. :param student_id: is the id of the `User` for whom the `Objective` is being removed. :param tutor_id: is the id of the `User` who is removing the `Objective`. :param by_user: is the `User` who is removing the `Objective`. """ assess_schema = {'student_id': s.Use(int), 'tutor_id': s.Use(int), 'objective_id': s.Use(int) } u = s.Schema(assess_schema).validate({'objective_id': objective_id, 'student_id': student_id, 'tutor_id': tutor_id}) self._check_user_id(tutor_id, by_user) userobjective = self.find_or_include(by_user=by_user, common_assessors=True, **u) states = UserObjective.assessment_states().keys() completed = states[(states.index(userobjective.completed) + 1) % len(states)] # Cycles through the list of states userobjective.completed = completed db.session.add(userobjective) db.session.commit() self._set_common_assessors(userobjective) self._set_student_objective(userobjective) return UserObjective.assessment_states()[completed]
def find_or_include(self, objective_id, student_id, tutor_id, by_user, common_assessors=True): """Include an objective among a users set of adopted objectives. :param objective_id: is the id of the `Objective` to be included. :param student_id: is the id of the `User` for whom the `Objective` is being included. :param tutor_id: is the id of the `User` who is including the `Objective`. :param by_user: is the `User` who is calling the action. :param common_assessors: determines whether common assessors should have their assessment updated for the 'Student'. """ include_schema = {'objective_id': s.Use(int), 'user_id': s.Use(int), 'assessor_id': s.Use(int) } u = s.Schema(include_schema).validate({'objective_id': objective_id, 'user_id': student_id, 'assessor_id': tutor_id}) self._check_user_id_or_admin(u['assessor_id'], by_user) userobjective = UserObjective.query.filter_by(**u).first() if userobjective is None: userobjective = UserObjective.create(**u) # DJG - this includes a repetition of the logic to first search for a userobjective before creating it - is it better to have a fat model which listens for the new userobjective to be created and then acts on that if common_assessors: self._set_common_assessors(userobjective) self._set_student_objective(userobjective) return userobjective
def remove(self, objective_id, student_id, tutor_id, by_user): """Remove an objective from a users set of adopted objectives. :param objective_id: is the id of the `Objective` to be removed. :param student_id: is the id of the `User` for whom the `Objective` is being removed. :param tutor_id: is the id of the `User` who is removing the `Objective`. :param by_user: is the `User` who is calling the action. """ remove_schema = {'id': s.Use(int), 'student_id': s.Use(int), 'tutor_id': s.Use(int) } s.Schema(remove_schema).validate({'id': objective_id, 'student_id': student_id, 'tutor_id': tutor_id}) self._check_user_id_or_admin(tutor_id, by_user) UserObjective.ignore_or_delete(student_id, tutor_id, objective_id)
def update(self, objective_data, by_user): """Update an existing Objective from the given data. :param objective_data: is a dictionary of data with the updated state of the Objective. It must match the schema defined within. :param by_user: the `User` who is updating the `Objective`. """ update_schema = merge(self._base_schema, { 'id': s.Use(int), }) o = s.Schema(update_schema).validate(objective_data) objective = self.require_by_id(o['id']) self._validate_topic(o['topic_id'], objective.subject_id) self._validate_name(o['name'], old_name=objective.name) prerequisites = self._validate_prerequisites( o['prerequisites'], by_user, check_cyclic_against=objective) self._check_update_auth(objective, by_user) now = datetime.utcnow() objective.last_updated = now objective.name = o['name'] objective.prerequisites = prerequisites objective.topic_id = o['topic_id'] db.session.add(objective) db.session.commit() UserObjective.FindOrCreate(by_user.id, by_user.id, objective.id) # Adds a record to the UserObjective table if not already there. This is the official record of what objectives # should be visible to the user return objective