Example #1
0
    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 creating 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()
        return objective
Example #2
0
    def create(self, objective_data, by_user):
        """Create a new Objective from the given data

        :param objective_data: is a dictionary of data used to populate the
                               initial Objective.  It must match the schema
                               defined within.
        :param by_user: the `User` who is creating the `Objective`.
        """

        creation_schema = merge(self._base_schema, {
            s.Optional('id'): None,
            'subject_id': s.Or(None, s.Use(int)),
        })

        o = s.Schema(creation_schema).validate(objective_data)
        self._validate_topic(o['topic_id'], o['subject_id'])
        self._validate_name(o['name'])

        prerequisites = self._validate_prerequisites(o['prerequisites'], by_user)

        now = datetime.utcnow()
        o['prerequisites'] = prerequisites
        o['created_by_id'] = by_user.id
        o['last_updated'] = now
        o['time_created'] = now
        objective = Objective(**o)
        db.session.add(objective)
        db.session.commit()
        return objective
Example #3
0
 def __init__(self, errors=None, **kwargs):
     """
     :param errors: a dict of fieldname to strings describing accumulated
                    errors.
     :param kwargs: key, value pairs mapping fieldnames to error strings
     """
     super(ValidationError, self).__init__("Validation Error")
     errors = errors if errors else {}
     self.errors = merge(errors, kwargs)
Example #4
0
    def test_creates_objective_with_prerequisites(self):

        base = {
            'topic_id': self.topic.id,
            'subject_id': self.subject.id,
            'prerequisites': []
        }

        def create_prereq(name):
            d = merge(base, {'name': name})
            self.services.objectives.create(d, self.user)

        create_prereq('prereq-1')
        create_prereq('prereq-2')

        data = merge(base, {
            'prerequisites': ['prereq-1', 'prereq-2'],
            'name': 'test'
        })
        self.services.objectives.create(data, self.user)
    def test_creates_objective_with_prerequisites(self):

        base = {
            'topic_id': self.topic.id,
            'subject_id': self.subject.id,
            'prerequisites': []
        }

        def create_prereq(name):
            d = merge(base, { 'name': name })
            self.services.objectives.create(d, self.user)

        create_prereq('prereq-1')
        create_prereq('prereq-2')

        data = merge(base, {
            'prerequisites': ['prereq-1', 'prereq-2'],
            'name': 'test'
        })
        self.services.objectives.create(data, self.user)
Example #6
0
    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
Example #7
0
 def create_prereq(name):
     d = merge(base, {'name': name})
     self.services.objectives.create(d, self.user)
 def create_prereq(name):
     d = merge(base, { 'name': name })
     self.services.objectives.create(d, self.user)