Пример #1
0
    def put(self):
        """Handles REST PUT verb with JSON payload."""
        request = transforms.loads(self.request.get('request'))
        key = request.get('key')
        edit_mode = request.get('mode')

        if GLOBAL_DEBUG:
            logging.warning('***RAM*** put request = ' + str(request))

        if not self.assert_xsrf_token_or_fail(
                request, 'teacher-put', {'key': key}):
            return

        if not TeacherRights.can_edit(self):
            transforms.send_json_response(
                self, 401, 'MobileCSP: Admin access denied.', {'key': key})
            return

        entity = TeacherEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'MobileCSP: Teacher Entity not found.', {'key': key})
            return

        schema = TeacherItemRESTHandler.SCHEMA()

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(
            transforms.loads(payload), schema.get_json_schema_dict())

        #  Get the teacher's user_id
        user = Student.get_first_by_email(update_dict['email'])[0]  # returns a tuple
        if not user:
            transforms.send_json_response(
                self, 404, 'MobileCSP: No registered user found for ' + update_dict['email'], {'key': key})
            return

        # Check that the teacher isn't already registered
        if update_dict['mode'] == 'Add':
            teachers = TeacherEntity.get_teachers()
            for teacher in teachers:
                if teacher.email == update_dict['email']:
                    transforms.send_json_response(
                        self, 404, 'MobileCSP: User is already registered as a teacher ' + update_dict['email'], {'key': key})
                    return

        if GLOBAL_DEBUG:
            logging.debug('****RAM**** teacher id ' + str(user.user_id))

        # Store the user_id
        update_dict['user_id'] = user.user_id

        transforms.dict_to_entity(entity, update_dict)

        entity.put()

        transforms.send_json_response(self, 200, 'Saved.')
Пример #2
0
    def retrieve_student_scores_and_attempts(self, student_email, course):
        scores = {}

        student = Student.get_first_by_email(student_email)[0]  # returns a tuple

        scores = ActivityScoreParser.get_activity_scores([student.user_id], course, True)
        if GLOBAL_DEBUG:
            logging.debug('***RAM*** get activity scores ' + str(scores))
      
        return scores
Пример #3
0
    def retrieve_student_scores_and_attempts(self, student_email, course):
        scores = {}

        student = Student.get_first_by_email(student_email)[
            0]  # returns a tuple

        scores = ActivityScoreParser.get_activity_scores([student.user_id],
                                                         course, True)
        if GLOBAL_DEBUG:
            logging.debug('***RAM*** get activity scores ' + str(scores))

        return scores
Пример #4
0
    def create_student_table(self, email, course, tracker, units, get_scores=False):
        student_dict = {}
        student = Student.get_first_by_email(email)[0]  # returns a tuple
        if student:
            progress_dict = self.calculate_student_progress_data(student,course,tracker,units)
            if get_scores:
                scores = self.retrieve_student_scores_and_attempts(email, course)
                student_dict['attempts'] = scores['attempts']
#                    student_dict['scores'] = scores['scores']
                student_dict['scores'] = self.calculate_performance_ratio(scores['scores'], email)
            student_dict['name'] = student.name
            student_dict['email'] = student.email
            student_dict['progress_dict'] = progress_dict
            student_dict['has_scores'] = get_scores
        return student_dict
    def add_new_teacher_for_user(
            cls, email,  school, additional_fields, alerts):

        student_by_first_email = Student.get_first_by_email(email)
        student_by_email = student_by_first_email[0]

        logging.info(dump(student_by_email))

        if not student_by_email:
            alerts.append('This email is not registered as a student for this course')
            return None

        # assume a new teacher is active by default
        teacher = cls._add_new_teacher_for_user(
            student_by_email.user_id, email, student_by_email.name, school, True, additional_fields)

        if teacher:
            alerts.append('Teacher was successfully registered')

        return teacher
Пример #6
0
 def create_student_table(self,
                          email,
                          course,
                          tracker,
                          units,
                          get_scores=False):
     student_dict = {}
     student = Student.get_first_by_email(email)[0]  # returns a tuple
     if student:
         progress_dict = self.calculate_student_progress_data(
             student, course, tracker, units)
         if get_scores:
             scores = self.retrieve_student_scores_and_attempts(
                 email, course)
             student_dict['attempts'] = scores['attempts']
             #                    student_dict['scores'] = scores['scores']
             student_dict['scores'] = self.calculate_performance_ratio(
                 scores['scores'], email)
         student_dict['name'] = student.name
         student_dict['email'] = student.email
         student_dict['progress_dict'] = progress_dict
         student_dict['has_scores'] = get_scores
     return student_dict
Пример #7
0
    def put(self):
        """Handles REST PUT verb with JSON payload."""
        request = transforms.loads(self.request.get('request'))
        key = request.get('key')
        edit_mode = request.get('mode')

        if GLOBAL_DEBUG:
            logging.warning('***RAM*** put request = ' + str(request))

        if not self.assert_xsrf_token_or_fail(request, 'teacher-put',
                                              {'key': key}):
            return

        if not TeacherRights.can_edit(self):
            transforms.send_json_response(self, 401,
                                          'MobileCSP: Admin access denied.',
                                          {'key': key})
            return

        entity = TeacherEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'MobileCSP: Teacher Entity not found.',
                {'key': key})
            return

        schema = TeacherItemRESTHandler.SCHEMA()

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(transforms.loads(payload),
                                              schema.get_json_schema_dict())

        #  Get the teacher's user_id
        user = Student.get_first_by_email(
            update_dict['email'])[0]  # returns a tuple
        if not user:
            transforms.send_json_response(
                self, 404, 'MobileCSP: No registered user found for ' +
                update_dict['email'], {'key': key})
            return

        # Check that the teacher isn't already registered
        if update_dict['mode'] == 'Add':
            teachers = TeacherEntity.get_teachers()
            for teacher in teachers:
                if teacher.email == update_dict['email']:
                    transforms.send_json_response(
                        self, 404,
                        'MobileCSP: User is already registered as a teacher ' +
                        update_dict['email'], {'key': key})
                    return

        if GLOBAL_DEBUG:
            logging.debug('****RAM**** teacher id ' + str(user.user_id))

        # Store the user_id
        update_dict['user_id'] = user.user_id

        transforms.dict_to_entity(entity, update_dict)

        entity.put()

        transforms.send_json_response(self, 200, 'Saved.')
Пример #8
0
    def put(self):
        """Handles REST PUT verb with JSON payload."""

        request = transforms.loads(self.request.get('request'))
        key = request.get('key')

        if not self.assert_xsrf_token_or_fail(request, 'section-put',
                                              {'key': key}):
            #            logging.warning('***RAM*** put FAIL (saving) ' + str(request))
            return

#        logging.warning('***RAM*** put (saving) ' + str(request))

        if not TeacherRights.can_edit_section(self):
            transforms.send_json_response(self, 401,
                                          'MobileCSP: Access denied.',
                                          {'key': key})
            return

        entity = CourseSectionEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'MobileCSP: Course Section not found.',
                {'key': key})
            return

        schema = SectionItemRESTHandler.SCHEMA()

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(transforms.loads(payload),
                                              schema.get_json_schema_dict())

        # Check for invalid emails -- email must be a registered student
        # Found the regular expression on Stackoverflow
        emails_raw = update_dict['students']
        emails = re.findall(r'[\w\.-]+@[\w\.-]+', emails_raw)

        return_code = 200
        bad_emails = []
        good_emails = []
        for email in emails:
            #            email = email.strip(' \t\n\r')
            if email:
                logging.debug('***RAM*** email = |' + email + '|')
                student = Student.get_first_by_email(email)[
                    0]  # returns a tuple
                if not student:
                    bad_emails.append(email)
                else:
                    good_emails.append(email)

        confirm_message = 'Confirmation\n'
        confirm_message += '------------\n\n'
        if bad_emails:
            logging.info('***RAM*** bad_emails found = ' + str(bad_emails))
            return_code = 401
            confirm_message = 'The following were invalid emails:\n'
            for email in bad_emails:
                confirm_message += email + '\n'
            confirm_message += 'Either there is no student with that email\n'
            confirm_message += 'registered for the course.  Or there is a \n'
            confirm_message += 'typo in the email address provided.\n\n'
        if good_emails:
            logging.info('***RAM*** good_emails found = ' + str(good_emails))
            confirm_message += 'Students with the following emails\n'
            confirm_message += 'are currently registered in your section:\n'
            for email in good_emails:
                confirm_message += email + '\n'

            update_dict['students'] = ','.join(
                good_emails)  # New-line delimited

        transforms.dict_to_entity(entity, update_dict)

        entity.put()
        if return_code == 200:
            confirm_message += 'Your section was successfully updated and saved.\n\n\n\n\n'
        else:
            confirm_message += 'Other information for your section was successfully updated and saved.\n\n\n\n\n'
        confirm_message += 'Confirmation\n'
        confirm_message += '------------\n'

        transforms.send_json_response(self, return_code, confirm_message,
                                      {'key': key})
Пример #9
0
    def put(self):
        """Handles REST PUT verb with JSON payload."""
 
        request = transforms.loads(self.request.get('request'))
        key = request.get('key')

        if not self.assert_xsrf_token_or_fail(
                request, 'section-put', {'key': key}):
#            logging.warning('***RAM*** put FAIL (saving) ' + str(request))
            return

#        logging.warning('***RAM*** put (saving) ' + str(request))

        if not TeacherRights.can_edit_section(self):
            transforms.send_json_response(
                self, 401, 'MobileCSP: Access denied.', {'key': key})
            return

        entity = CourseSectionEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'MobileCSP: Course Section not found.', {'key': key})
            return

        schema = SectionItemRESTHandler.SCHEMA()

        payload = request.get('payload')
        update_dict = transforms.json_to_dict(
            transforms.loads(payload), schema.get_json_schema_dict())

        # Check for invalid emails -- email must be a registered student
        # Found the regular expression on Stackoverflow
        emails_raw = update_dict['students']
        emails = re.findall(r'[\w\.-]+@[\w\.-]+', emails_raw)
        
        return_code = 200
        bad_emails = []
        good_emails = []
        for email in emails:
#            email = email.strip(' \t\n\r')
            if email:
                logging.debug('***RAM*** email = |' + email + '|')
                student = Student.get_first_by_email(email)[0]  # returns a tuple
                if not student:
                    bad_emails.append(email)
                else:
                    good_emails.append(email)
     
        confirm_message  = 'Confirmation\n'
        confirm_message += '------------\n\n' 
        if bad_emails:
            logging.info('***RAM*** bad_emails found = ' + str(bad_emails))
            return_code = 401
            confirm_message = 'The following were invalid emails:\n'
            for email in bad_emails: 
                confirm_message += email + '\n'
            confirm_message += 'Either there is no student with that email\n'
            confirm_message += 'registered for the course.  Or there is a \n'
            confirm_message += 'typo in the email address provided.\n\n'
        if good_emails:
            logging.info('***RAM*** good_emails found = ' + str(good_emails))
            confirm_message += 'Students with the following emails\n'
            confirm_message += 'are currently registered in your section:\n'
            for email in good_emails:
                confirm_message += email + '\n'
          
            update_dict['students'] = ','.join(good_emails)  # New-line delimited

        transforms.dict_to_entity(entity, update_dict)

        entity.put()
        if return_code == 200:
            confirm_message += 'Your section was successfully updated and saved.\n\n\n\n\n'
        else:
            confirm_message += 'Other information for your section was successfully updated and saved.\n\n\n\n\n'
        confirm_message += 'Confirmation\n'
        confirm_message += '------------\n'

        transforms.send_json_response(
            self, return_code, confirm_message, {'key': key})