Ejemplo n.º 1
0
    def _update_global_profile_attributes(
        cls,
        profile,
        email=None,
        legal_name=None,
        nick_name=None,
        date_of_birth=None,
        is_enrolled=None,
        final_grade=None,
        course_info=None,
    ):
        """Modifies various attributes of Student's Global Profile."""
        # TODO(psimakov): update of email does not work for student
        if email is not None:
            profile.email = email

        if legal_name is not None:
            profile.legal_name = legal_name

        if nick_name is not None:
            profile.nick_name = nick_name

        if date_of_birth is not None:
            profile.date_of_birth = date_of_birth

        if not (is_enrolled is None and final_grade is None and course_info is None):

            # Defer to avoid circular import.
            # pylint: disable-msg=g-import-not-at-top
            from controllers import sites

            course = sites.get_course_for_current_request()
            course_namespace = course.get_namespace_name()

            if is_enrolled is not None:
                enrollment_dict = transforms.loads(profile.enrollment_info)
                enrollment_dict[course_namespace] = is_enrolled
                profile.enrollment_info = transforms.dumps(enrollment_dict)

            if final_grade is not None or course_info is not None:
                course_info_dict = {}
                if profile.course_info:
                    course_info_dict = transforms.loads(profile.course_info)
                if course_namespace in course_info_dict.keys():
                    info = course_info_dict[course_namespace]
                else:
                    info = {}
                if final_grade:
                    info["final_grade"] = final_grade
                if course_info:
                    info["info"] = course_info
                course_info_dict[course_namespace] = info
                profile.course_info = transforms.dumps(course_info_dict)
Ejemplo n.º 2
0
    def _update_global_profile_attributes(cls,
                                          profile,
                                          email=None,
                                          legal_name=None,
                                          nick_name=None,
                                          date_of_birth=None,
                                          is_enrolled=None,
                                          final_grade=None,
                                          course_info=None):
        """Modifies various attributes of Student's Global Profile."""
        # TODO(psimakov): update of email does not work for student
        if email is not None:
            profile.email = email

        if legal_name is not None:
            profile.legal_name = legal_name

        if nick_name is not None:
            profile.nick_name = nick_name

        if date_of_birth is not None:
            profile.date_of_birth = date_of_birth

        if not (is_enrolled is None and final_grade is None
                and course_info is None):

            # Defer to avoid circular import.
            # pylint: disable-msg=g-import-not-at-top
            from controllers import sites
            course = sites.get_course_for_current_request()
            course_namespace = course.get_namespace_name()

            if is_enrolled is not None:
                enrollment_dict = transforms.loads(profile.enrollment_info)
                enrollment_dict[course_namespace] = is_enrolled
                profile.enrollment_info = transforms.dumps(enrollment_dict)

            if final_grade is not None or course_info is not None:
                course_info_dict = {}
                if profile.course_info:
                    course_info_dict = transforms.loads(profile.course_info)
                if course_namespace in course_info_dict.keys():
                    info = course_info_dict[course_namespace]
                else:
                    info = {}
                if final_grade:
                    info['final_grade'] = final_grade
                if course_info:
                    info['info'] = course_info
                course_info_dict[course_namespace] = info
                profile.course_info = transforms.dumps(course_info_dict)
Ejemplo n.º 3
0
 def get_results(job):
     if not job.output:
         return None
     content = transforms.loads(job.output)
     if MapReduceJob._OUTPUT_KEY_RESULTS in content:
         return content[MapReduceJob._OUTPUT_KEY_RESULTS]
     return None
Ejemplo n.º 4
0
    def get_all_scores(self, student):
        """Gets all score data for a student.

        Args:
            student: the student whose scores should be retrieved.

        Returns:
            an array of dicts, each representing an assessment. Each dict has
            the keys 'id', 'title', 'weight' and 'score' (if available),
            representing the unit id, the assessment title, the weight
            contributed by the assessment to the final score, and the
            assessment score.
        """
        assessment_list = self.get_assessment_list()
        scores = transforms.loads(student.scores) if student.scores else {}

        assessment_score_list = []
        for unit in assessment_list:
            # Compute the weight for this assessment.
            weight = 0
            if hasattr(unit, 'weight'):
                weight = unit.weight
            elif unit.unit_id in DEFAULT_LEGACY_ASSESSMENT_WEIGHTS:
                weight = DEFAULT_LEGACY_ASSESSMENT_WEIGHTS[unit.unit_id]

            assessment_score_list.append({
                'id': str(unit.unit_id),
                'title': unit.title,
                'weight': weight,
                'score': (scores[str(unit.unit_id)]
                          if str(unit.unit_id) in scores else 0),
            })

        return assessment_score_list
Ejemplo n.º 5
0
    def get_all_scores(self, student):
        """Gets all score data for a student.

        Args:
            student: the student whose scores should be retrieved.

        Returns:
            an array of dicts, each representing an assessment. Each dict has
            the keys 'id', 'title', 'weight' and 'score' (if available),
            representing the unit id, the assessment title, the weight
            contributed by the assessment to the final score, and the
            assessment score.
        """
        assessment_list = self.get_assessment_list()
        scores = transforms.loads(student.scores) if student.scores else {}

        assessment_score_list = []
        for unit in assessment_list:
            # Compute the weight for this assessment.
            weight = 0
            if hasattr(unit, 'weight'):
                weight = unit.weight
            elif unit.unit_id in DEFAULT_LEGACY_ASSESSMENT_WEIGHTS:
                weight = DEFAULT_LEGACY_ASSESSMENT_WEIGHTS[unit.unit_id]

            assessment_score_list.append({
                'id': str(unit.unit_id),
                'title': unit.title,
                'weight': weight,
                'score': (scores[str(unit.unit_id)]
                          if str(unit.unit_id) in scores else 0),
            })

        return assessment_score_list
Ejemplo n.º 6
0
 def deserialize(self, binary_data):
     """Loads instance from a JSON representation."""
     json_text = binary_data.decode('utf-8')
     adict = transforms.loads(json_text)
     if not self.version == adict.get('version'):
         raise Exception('Expected version %s, found %s.' % (
             self.version, adict.get('version')))
     self._from_dict(adict)
Ejemplo n.º 7
0
 def deserialize(self, binary_data):
     """Loads instance from a JSON representation."""
     json_text = binary_data.decode('utf-8')
     adict = transforms.loads(json_text)
     if not self.version == adict.get('version'):
         raise Exception('Expected version %s, found %s.' % (
             self.version, adict.get('version')))
     self._from_dict(adict)
Ejemplo n.º 8
0
 def get_status_url(job, namespace, xsrf_token):
     if not job.output:
         return None
     content = transforms.loads(job.output)
     pipeline_id = content[MapReduceJob._OUTPUT_KEY_ROOT_PIPELINE_ID]
     return ('/mapreduce/ui/pipeline/status?' +
             urllib.urlencode({'root': pipeline_id,
                               'namespace': namespace,
                               'xsrf_token': xsrf_token}))
Ejemplo n.º 9
0
 def get_status_url(job, namespace, xsrf_token):
     if not job.output:
         return None
     content = transforms.loads(job.output)
     pipeline_id = content[MapReduceJob._OUTPUT_KEY_ROOT_PIPELINE_ID]
     return ('/mapreduce/ui/pipeline/status?' +
             urllib.urlencode({'root': pipeline_id,
                               'namespace': namespace,
                               'xsrf_token': xsrf_token}))
Ejemplo n.º 10
0
def has_score(student, assessment_name):
    """Stores the score for the given student and assessment.

    Args:
        student: the student whose answer should be stored.
        assessment_name: the name of the assessment.
    """
    if not student.scores:
        return False

    score_dict = transforms.loads(student.scores)
    return score_dict.has_key(str(assessment_name))
Ejemplo n.º 11
0
 def filter(self, fun=None):
     if not fun:
         fun = lambda x: True
     try:
         with io.open(self.filename, "r") as f:
             for line in f:
                 meta, raw, comment = self.parse_line(line)
                 if not meta:
                     continue
                 if fun(meta):
                     yield (meta, transforms.loads(raw))
     except IOError:
         pass
Ejemplo n.º 12
0
    def get_info(cls, student, unit_id):
        info_ent = StudentPropertyEntity.get(student, cls.info_tmpl % unit_id)
        if not info_ent:
            return {
                'tries_left': TRIES_ALLOWED_ON_EXAMS.value,
                'start_time': None,
            }

        info = transforms.loads(info_ent.value)
        if info['start_time']:
            info['start_time'] = datetime.datetime.strptime(
                info['start_time'], cls.timestamp_format)
        return info
Ejemplo n.º 13
0
    def _add_new_student_for_current_user(
        cls, user_id, email, nick_name, additional_fields):
        """Create new or re-enroll old student."""

        # create profile if does not exist
        profile = cls._get_profile_by_user_id(user_id)
        if not profile:
            profile = cls._add_new_profile(user_id, email)

        # create new student or re-enroll existing
        student = Student.get_by_email(email)
        if not student:
            # TODO(psimakov): we must move to user_id as a key
            student = Student(key_name=email)

        # update profile
        cls._update_attributes(
            profile, student, nick_name=nick_name, is_enrolled=True)

        # update student
        student.user_id = user_id
        student.additional_fields = additional_fields

        # CGL-MOOC-Builder starts: additional data fields for a student
        additional_data = transforms.loads(additional_fields)
        for st in additional_data:
            if st[0] == 'age':
                student.age = st[1]
            elif st[0] == 'city':
                student.city = st[1]
            elif st[0] == 'state':
                student.state = st[1]
            elif st[0] == 'country':
                student.country = st[1]
            elif st[0] == 'education':
                student.education = st[1]
            elif st[0] == 'profession':
                student.profession = st[1]
            elif st[0] == 'organization':
                student.organization = st[1]
            elif st[0] == 'motivation':
                student.motivation = st[1]
            elif st[0] == 'referral':
                student.referral = st[1]
            elif st[0] == 'privacy':
                student.privacy = st[1]
        # CGL-MOOC-Builder ends

        # put both
        cls._put_profile(profile)
        student.put()
Ejemplo n.º 14
0
    def get_info(cls, student, unit_id):
        info_ent = StudentPropertyEntity.get(student,
                cls.info_tmpl % unit_id)
        if not info_ent:
            return {
                   'tries_left': TRIES_ALLOWED_ON_EXAMS.value,
                   'start_time': None,
                   }

        info = transforms.loads(info_ent.value)
        if info['start_time']:
            info['start_time'] = datetime.datetime.strptime(
                    info['start_time'],
                    cls.timestamp_format)
        return info
Ejemplo n.º 15
0
    def get_reviews_by_keys(self,
                            unit_id,
                            review_keys,
                            handle_empty_keys=False):
        """Gets a list of reviews, given their review keys.

        If handle_empty_keys is True, then no error is thrown on supplied keys
        that are None; the elements in the result list corresponding to those
        keys simply return None. This usually arises when this method is called
        immediately after get_review_steps_by_keys().

        Args:
            unit_id: string. Id of the unit to get the reviews for.
            review_keys: [db.Key of peer.ReviewStep]. May include None, if
                handle_empty_keys is True.
            handle_empty_keys: if True, the return value contains None for keys
                that are None. If False, the method throws if empty keys are
                supplied.

        Returns:
            List with the same number of elements as review_keys. It contains:
            - the JSON-decoded contents of the review corresponding to that
                review_key, or
            - None if either:
              - no review has been submitted for that review key, or
              - handle_empty_keys == True and the review_key is None.
        """
        impl = self._get_impl(unit_id)
        reviews = []
        if not handle_empty_keys:
            reviews = impl.get_reviews_by_keys(review_keys)
        else:
            nonempty_review_indices = []
            nonempty_review_keys = []
            for idx, review_key in enumerate(review_keys):
                if review_key is not None:
                    nonempty_review_indices.append(idx)
                    nonempty_review_keys.append(review_key)

            tmp_reviews = impl.get_reviews_by_keys(nonempty_review_keys)
            reviews = [None] * len(review_keys)
            for (i, idx) in enumerate(nonempty_review_indices):
                reviews[idx] = tmp_reviews[i]

        return [(transforms.loads(rev.contents) if rev else None)
                for rev in reviews]
Ejemplo n.º 16
0
    def _set_entity_value(self, student_property, key, value):
        """Sets the integer value of a student property.

        Note: this method does not commit the change. The calling method should
        call put() on the StudentPropertyEntity.

        Args:
          student_property: the StudentPropertyEntity
          key: the student property whose value should be incremented
          value: the value to increment this property by
        """
        try:
            progress_dict = transforms.loads(student_property.value)
        except (AttributeError, TypeError):
            progress_dict = {}

        progress_dict[key] = value
        student_property.value = transforms.dumps(progress_dict)
Ejemplo n.º 17
0
def set_answer(answers, assessment_name, answer):
    """Stores the answer array for the given student and assessment.

    The caller must call answers.put() to commit.
    This does not do any type-checking on 'answer'; it just stores whatever
    is passed in.

    Args:
        answers: the StudentAnswers entity in which the answer should be stored.
        assessment_name: the name of the assessment.
        answer: an array containing the student's answers.
    """
    if not answers.data:
        score_dict = {}
    else:
        score_dict = transforms.loads(answers.data)
    score_dict[assessment_name] = answer
    answers.data = transforms.dumps(score_dict)
Ejemplo n.º 18
0
def set_score(student, assessment_name, score):
    """Stores the score for the given student and assessment.

    The caller must call student.put() to commit.
    This does not do any type-checking on 'score'; it just stores whatever
    is passed in.

    Args:
        student: the student whose answer should be stored.
        assessment_name: the name of the assessment.
        score: the student's score.
    """
    if not student.scores:
        score_dict = {}
    else:
        score_dict = transforms.loads(student.scores)
    score_dict[assessment_name] = score
    student.scores = transforms.dumps(score_dict)
    def _set_entity_value(self, student_property, key, value):
        """Sets the integer value of a student property.

        Note: this method does not commit the change. The calling method should
        call put() on the StudentPropertyEntity.

        Args:
          student_property: the StudentPropertyEntity
          key: the student property whose value should be incremented
          value: the value to increment this property by
        """
        try:
            progress_dict = transforms.loads(student_property.value)
        except (AttributeError, TypeError):
            progress_dict = {}

        progress_dict[key] = value
        student_property.value = transforms.dumps(progress_dict)
Ejemplo n.º 20
0
def set_answer(answers, assessment_name, answer):
    """Stores the answer array for the given student and assessment.

    The caller must call answers.put() to commit.
    This does not do any type-checking on 'answer'; it just stores whatever
    is passed in.

    Args:
        answers: the StudentAnswers entity in which the answer should be stored.
        assessment_name: the name of the assessment.
        answer: an array containing the student's answers.
    """
    if not answers.data:
        score_dict = {}
    else:
        score_dict = transforms.loads(answers.data)
    score_dict[assessment_name] = answer
    answers.data = transforms.dumps(score_dict)
Ejemplo n.º 21
0
def set_score(student, assessment_name, score):
    """Stores the score for the given student and assessment.

    The caller must call student.put() to commit.
    This does not do any type-checking on 'score'; it just stores whatever
    is passed in.

    Args:
        student: the student whose answer should be stored.
        assessment_name: the name of the assessment.
        score: the student's score.
    """
    if not student.scores:
        score_dict = {}
    else:
        score_dict = transforms.loads(student.scores)
    score_dict[assessment_name] = score
    student.scores = transforms.dumps(score_dict)
Ejemplo n.º 22
0
    def get_reviews_by_keys(
        self, unit_id, review_keys, handle_empty_keys=False):
        """Gets a list of reviews, given their review keys.

        If handle_empty_keys is True, then no error is thrown on supplied keys
        that are None; the elements in the result list corresponding to those
        keys simply return None. This usually arises when this method is called
        immediately after get_review_steps_by_keys().

        Args:
            unit_id: string. Id of the unit to get the reviews for.
            review_keys: [db.Key of peer.ReviewStep]. May include None, if
                handle_empty_keys is True.
            handle_empty_keys: if True, the return value contains None for keys
                that are None. If False, the method throws if empty keys are
                supplied.

        Returns:
            List with the same number of elements as review_keys. It contains:
            - the JSON-decoded contents of the review corresponding to that
                review_key, or
            - None if either:
              - no review has been submitted for that review key, or
              - handle_empty_keys == True and the review_key is None.
        """
        impl = self._get_impl(unit_id)
        reviews = []
        if not handle_empty_keys:
            reviews = impl.get_reviews_by_keys(review_keys)
        else:
            nonempty_review_indices = []
            nonempty_review_keys = []
            for idx, review_key in enumerate(review_keys):
                if review_key is not None:
                    nonempty_review_indices.append(idx)
                    nonempty_review_keys.append(review_key)

            tmp_reviews = impl.get_reviews_by_keys(nonempty_review_keys)
            reviews = [None] * len(review_keys)
            for (i, idx) in enumerate(nonempty_review_indices):
                reviews[idx] = tmp_reviews[i]

        return [(transforms.loads(rev.contents) if rev else None)
                for rev in reviews]
Ejemplo n.º 23
0
 def get_results(job):
     if not job.output:
         return None
     content = transforms.loads(job.output)
     return content[MapReduceJob._OUTPUT_KEY_RESULTS]
Ejemplo n.º 24
0
 def get_root_pipeline_id(job):
     if not job or not job.output:
         return None
     content = transforms.loads(job.output)
     return content[MapReduceJob._OUTPUT_KEY_ROOT_PIPELINE_ID]
Ejemplo n.º 25
0
 def get_score(self, student, assessment_id):
     """Gets a student's score for a particular assessment."""
     assert self.is_valid_assessment_id(assessment_id)
     scores = transforms.loads(student.scores) if student.scores else {}
     return scores.get(assessment_id) if scores else None
Ejemplo n.º 26
0
 def load(cls, obj_id):
     entity = cls._load_entity(obj_id)
     if entity:
         return cls.DTO(obj_id, transforms.loads(entity.data))
     else:
         return None
Ejemplo n.º 27
0
 def get_all(cls):
     entities = cls.ENTITY.all().fetch(1000)
     return [
         cls.DTO(e.key().id(), transforms.loads(e.data)) for e in entities
     ]
Ejemplo n.º 28
0
 def _get_entity_value(self, progress, event_key):
     if not progress.value:
         return None
     return transforms.loads(progress.value).get(event_key)
Ejemplo n.º 29
0
 def get_all(cls):
     entities = cls.ENTITY.all().fetch(1000)
     return [
         cls.DTO(e.key().id(), transforms.loads(e.data)) for e in entities]
Ejemplo n.º 30
0
 def get_root_pipeline_id(job):
     if not job or not job.output:
         return None
     content = transforms.loads(job.output)
     return content[MapReduceJob._OUTPUT_KEY_ROOT_PIPELINE_ID]
Ejemplo n.º 31
0
 def make_value_from_datastore(self, db_value):
     if type(db_value) is list:
         return db_value
     elif not db_value:
         return []
     return transforms.loads(db_value)
Ejemplo n.º 32
0
 def get_contents_by_key(cls, submission_key):
     """Returns the contents of a submission, given a db.Key."""
     submission = entities.get(submission_key)
     return transforms.loads(submission.contents) if submission else None
Ejemplo n.º 33
0
 def get_error_message(job):
     if not job.output:
         return None
     content = transforms.loads(job.output)
     return content[MapReduceJob._OUTPUT_KEY_ERROR]
Ejemplo n.º 34
0
 def get_error_message(job):
     if not job.output:
         return None
     content = transforms.loads(job.output)
     return content[MapReduceJob._OUTPUT_KEY_ERROR]
Ejemplo n.º 35
0
 def get_contents_by_key(cls, submission_key):
     """Returns the contents of a submission, given a db.Key."""
     submission = entities.get(submission_key)
     return transforms.loads(submission.contents) if submission else None
Ejemplo n.º 36
0
 def make_value_from_datastore(self, db_value):
     if type(db_value) is list:
         return db_value
     elif not db_value:
         return []
     return transforms.loads(db_value)
Ejemplo n.º 37
0
 def get_score(self, student, assessment_id):
     """Gets a student's score for a particular assessment."""
     assert self.is_valid_assessment_id(assessment_id)
     scores = transforms.loads(student.scores) if student.scores else {}
     return scores.get(assessment_id) if scores else None
Ejemplo n.º 38
0
 def load(cls, obj_id):
     entity = cls._load_entity(obj_id)
     if entity:
         return cls.DTO(obj_id, transforms.loads(entity.data))
     else:
         return None
 def _get_entity_value(self, progress, event_key):
     if not progress.value:
         return None
     return transforms.loads(progress.value).get(event_key)