Ejemplo n.º 1
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, 'announcement-put',
                                              {'key': key}):
            return

        if not AnnouncementsRights.can_edit(self):
            transforms.send_json_response(self, 401, 'Access denied.',
                                          {'key': key})
            return

        entity = AnnouncementEntity.get(key)
        if not entity:
            transforms.send_json_response(self, 404, 'Object not found.',
                                          {'key': key})
            return

        schema = AnnouncementsItemRESTHandler.SCHEMA(
            'Announcement',
            self.get_course().get_course_announcement_list_email())

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

        entity.labels = utils.list_to_text(
            LabelGroupsHelper.decode_labels_group(
                update_dict.get('label_groups')))

        transforms.dict_to_entity(entity, update_dict)

        entity.put()
        AnnouncementUtil.update_course_with_last_announcement(
            self.app_context.get_namespace_name())

        email_sent = False
        if entity.send_email:
            email_manager = notify.EmailManager(self.get_course())
            email_sent = email_manager.send_announcement(
                entity.title, entity.html, intent=ANNOUNCEMENTS_INTENT)

        if entity.send_email and not email_sent:
            if not self.get_course().get_course_announcement_list_email():
                message = 'Saved. Announcement list not configured.'
            else:
                message = 'Saved, but there was an error sending email.'
        else:
            message = 'Saved.'
        transforms.send_json_response(self, 200, message)
Ejemplo n.º 2
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, 'announcement-put', {'key': key}):
            return

        if not AnnouncementsRights.can_edit(self):
            transforms.send_json_response(
                self, 401, 'Access denied.', {'key': key})
            return

        entity = AnnouncementEntity.get(key)
        if not entity:
            transforms.send_json_response(
                self, 404, 'Object not found.', {'key': key})
            return

        payload = request.get('payload')
        transforms.dict_to_entity(entity, transforms.json_to_dict(
            transforms.loads(payload),
            AnnouncementsItemRESTHandler.SCHEMA_DICT))
        entity.put()

        if not (entity.is_draft or entity.already_notified):
            Notification.notify_all_students(url='/announcements',
                    text=entity.title)
            entity.already_notified = True
            entity.put()

        email_sent = False
        if entity.send_email:
            email_manager = notify.EmailManager(self.get_course())
            email_sent = email_manager.send_announcement(
                entity.title, entity.html)

        if entity.send_email and not email_sent:
            if not self.get_course().get_course_announcement_list_email():
                message = 'Saved. Announcement list not configured.'
            else:
                message = 'Saved, but there was an error sending email.'
        else:
            message = 'Saved.'
        transforms.send_json_response(self, 200, message)
Ejemplo n.º 3
0
    def email_to_forum(self, question):
        """Post the student question to the Group."""
        question_data = json.loads(question.question_data)
        receiver = self.get_course().get_course_forum_email()
        if receiver is None:
            return

        timestamp = (
            int(question_data['timestamp']) if question_data['timestamp'] else 0
            )
        timestamp_string = time.strftime('%H:%M:%S', time.gmtime(timestamp))
        subject = '%s' % (question_data['summary'])
        if not subject:
          subject = '%s' % (question_data['question'])
        if len(subject) > 50:
            subject = '%s ...' % subject[:46]

        body = """
        Hi,<br/><br/>
        I have a question about what is happening in this video at %s -
        <br/><br/>

            http://www.youtube.com/watch?v=%s#t=%ss. <br/><br/>

        %s.<br/><br/>

        Thanks!<br/><br/>
        <hr/><br/>

        Reference Key - %s""" % (timestamp_string,
                                 question.resource_id,
                                 question_data['timestamp'],
                                 question_data['question'],
                                 question.key())

        email_manager = notify.EmailManager(self.get_course())
        email_manager.send_mail_sync(subject, body, receiver)