def SCHEMA(cls, title, announcement_email): schema = FieldRegistry(title) schema.add_property(SchemaField( 'key', 'ID', 'string', editable=False, extra_schema_dict_values={'className': 'inputEx-Field keyHolder'})) schema.add_property(SchemaField( 'title', 'Title', 'string', optional=True)) schema.add_property(SchemaField( 'html', 'Body', 'html', optional=True, extra_schema_dict_values={ 'supportCustomTags': tags.CAN_USE_DYNAMIC_TAGS.value, 'excludedCustomTags': tags.EditorBlacklists.COURSE_SCOPE})) schema.add_property(SchemaField( 'date', 'Date', 'date', optional=True, extra_schema_dict_values={ '_type': 'date', 'dateFormat': 'Y-m-d', 'valueFormat': 'Y-m-d'})) schema.add_property(FieldArray( 'label_groups', 'Labels', item_type=LabelGroupsHelper.make_labels_group_schema_field(), extra_schema_dict_values={ 'className': 'inputEx-Field label-group-list'})) schema.add_property(SchemaField( 'send_email', 'Send Email', 'boolean', optional=True, extra_schema_dict_values={ 'description': AnnouncementsItemRESTHandler.get_send_email_description( announcement_email)})) schema.add_property(SchemaField( 'is_draft', 'Status', 'boolean', select_data=[ (True, resources_display.DRAFT_TEXT), (False, resources_display.PUBLISHED_TEXT)], extra_schema_dict_values={'className': 'split-from-main-group'})) return schema
def SCHEMA(cls): schema = FieldRegistry('Announcement', extra_schema_dict_values={ 'className': 'inputEx-Group new-form-layout'}) schema.add_property(SchemaField( 'key', 'ID', 'string', editable=False, hidden=True)) schema.add_property(SchemaField( 'title', 'Title', 'string', optional=True)) schema.add_property(SchemaField( 'html', 'Body', 'html', optional=True, extra_schema_dict_values={ 'supportCustomTags': tags.CAN_USE_DYNAMIC_TAGS.value, 'excludedCustomTags': tags.EditorBlacklists.COURSE_SCOPE})) schema.add_property(SchemaField( 'date', 'Date', 'string', optional=True, extra_schema_dict_values={ '_type': 'datetime', 'className': 'inputEx-CombineField gcb-datetime ' 'inputEx-fieldWrapper date-only'})) schema.add_property(FieldArray( 'label_groups', 'Labels', item_type=LabelGroupsHelper.make_labels_group_schema_field(), extra_schema_dict_values={ 'className': 'inputEx-Field label-group-list'})) schema.add_property(SchemaField( 'is_draft', 'Status', 'boolean', select_data=[ (True, resources_display.DRAFT_TEXT), (False, resources_display.PUBLISHED_TEXT)], extra_schema_dict_values={'className': 'split-from-main-group'})) return schema
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)
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() 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)
def get(self): """Handles REST GET verb and returns an object as JSON payload.""" key = self.request.get('key') try: entity = AnnouncementEntity.get(key) except db.BadKeyError: entity = None if not entity: transforms.send_json_response( self, 404, 'Object not found.', {'key': key}) return viewable = AnnouncementsRights.apply_rights(self, [entity]) if not viewable: transforms.send_json_response( self, 401, 'Access denied.', {'key': key}) return entity = viewable[0] schema = AnnouncementsItemRESTHandler.SCHEMA() entity_dict = transforms.entity_to_dict(entity) # Format the internal date object as ISO 8601 datetime, with time # defaulting to 00:00:00 date = entity_dict['date'] date = datetime.datetime(date.year, date.month, date.day) entity_dict['date'] = date.strftime(courses.ISO_8601_DATE_FORMAT) entity_dict['label_groups'] = ( LabelGroupsHelper.announcement_labels_to_dict(entity)) json_payload = transforms.dict_to_json(entity_dict) transforms.send_json_response( self, 200, 'Success.', payload_dict=json_payload, xsrf_token=XsrfTokenManager.create_xsrf_token( 'announcement-put'))
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() payload = request.get('payload') update_dict = transforms.json_to_dict( transforms.loads(payload), schema.get_json_schema_dict()) # The datetime widget returns a datetime object and we need a UTC date. update_dict['date'] = datetime.datetime.strptime( update_dict['date'], courses.ISO_8601_DATE_FORMAT).date() entity.labels = utils.list_to_text( LabelGroupsHelper.decode_labels_group( update_dict.get('label_groups'))) transforms.dict_to_entity(entity, update_dict) entity.put() transforms.send_json_response(self, 200, 'Saved.')
def get(self): """Handles REST GET verb and returns an object as JSON payload.""" key = self.request.get('key') try: entity = AnnouncementEntity.get(key) except db.BadKeyError: entity = None if not entity: transforms.send_json_response(self, 404, 'Object not found.', {'key': key}) return viewable = AnnouncementsRights.apply_rights(self, [entity]) if not viewable: transforms.send_json_response(self, 401, 'Access denied.', {'key': key}) return entity = viewable[0] schema = AnnouncementsItemRESTHandler.SCHEMA( 'Announcement', self.get_course().get_course_announcement_list_email()) entity_dict = transforms.entity_to_dict(entity) entity_dict['label_groups'] = ( LabelGroupsHelper.announcement_labels_to_dict(entity)) json_payload = transforms.dict_to_json(entity_dict, schema.get_json_schema_dict()) transforms.send_json_response( self, 200, 'Success.', payload_dict=json_payload, xsrf_token=XsrfTokenManager.create_xsrf_token('announcement-put'))
def get(self): """Handles REST GET verb and returns an object as JSON payload.""" key = self.request.get('key') try: entity = AnnouncementEntity.get(key) except db.BadKeyError: entity = None if not entity: transforms.send_json_response( self, 404, 'Object not found.', {'key': key}) return viewable = AnnouncementsRights.apply_rights(self, [entity]) if not viewable: transforms.send_json_response( self, 401, 'Access denied.', {'key': key}) return entity = viewable[0] schema = AnnouncementsItemRESTHandler.SCHEMA( 'Announcement', self.get_course().get_course_announcement_list_email()) entity_dict = transforms.entity_to_dict(entity) entity_dict['label_groups'] = ( LabelGroupsHelper.announcement_labels_to_dict(entity)) json_payload = transforms.dict_to_json( entity_dict, schema.get_json_schema_dict()) transforms.send_json_response( self, 200, 'Success.', payload_dict=json_payload, xsrf_token=XsrfTokenManager.create_xsrf_token( 'announcement-put'))
def SCHEMA(cls, title, announcement_email): schema = FieldRegistry(title) schema.add_property( SchemaField('key', 'ID', 'string', editable=False, extra_schema_dict_values={ 'className': 'inputEx-Field keyHolder' })) schema.add_property( SchemaField('title', 'Title', 'string', optional=True)) schema.add_property( SchemaField('html', 'Body', 'html', optional=True, extra_schema_dict_values={ 'supportCustomTags': tags.CAN_USE_DYNAMIC_TAGS.value, 'excludedCustomTags': tags.EditorBlacklists.COURSE_SCOPE })) schema.add_property( SchemaField('date', 'Date', 'date', optional=True, extra_schema_dict_values={ '_type': 'date', 'dateFormat': 'Y-m-d', 'valueFormat': 'Y-m-d' })) schema.add_property( FieldArray( 'label_groups', 'Labels', item_type=LabelGroupsHelper.make_labels_group_schema_field(), extra_schema_dict_values={ 'className': 'inputEx-Field label-group-list' })) schema.add_property( SchemaField( 'send_email', 'Send Email', 'boolean', optional=True, extra_schema_dict_values={ 'description': AnnouncementsItemRESTHandler.get_send_email_description( announcement_email) })) schema.add_property( SchemaField('is_draft', 'Status', 'boolean', select_data=[(True, resources_display.DRAFT_TEXT), (False, resources_display.PUBLISHED_TEXT) ], extra_schema_dict_values={ 'className': 'split-from-main-group' })) return schema