class CourseMetadataSerailizer(serializers.Serializer): """ Serializer for course metadata. """ id = CourseKeyField(help_text="The identifier of the course") blackouts = serializers.ListField( child=BlackoutDateSerializer(), help_text="A list of objects representing blackout periods " "(during which discussions are read-only except for privileged users)." ) thread_list_url = serializers.URLField( help_text="The URL of the list of all threads in the course.", ) following_thread_list_url = serializers.URLField( help_text="thread_list_url with parameter following=True", ) topics_url = serializers.URLField(help_text="The URL of the topic listing for the course.") allow_anonymous = serializers.BooleanField( help_text="A boolean which indicating whether anonymous posts are allowed or not.", ) allow_anonymous_to_peers = serializers.BooleanField( help_text="A boolean which indicating whether posts anonymous to peers are allowed or not.", ) user_roles = serializers.ListField( child=serializers.CharField(), help_text="A list of all the roles the requesting user has for this course.", ) user_is_privileged = serializers.BooleanField( help_text="A boolean indicating if the current user has a privileged role", )
class CreditCourseSerializer(serializers.ModelSerializer): """ CreditCourse Serializer """ course_key = CourseKeyField() class Meta(object): model = CreditCourse exclude = ('id', )
class ContentLibraryBlockImportTaskCreateSerializer(serializers.Serializer): """ Serializer to create a new block import task. The serializer accepts the following parameter: - The courseware course key to import blocks from. """ course_key = CourseKeyField()
class BookmarkSerializer(serializers.ModelSerializer): """ Serializer for the Bookmark model. """ id = serializers.SerializerMethodField() # pylint: disable=invalid-name course_id = CourseKeyField(source='course_key') usage_id = UsageKeyField(source='usage_key') block_type = serializers.ReadOnlyField(source='usage_key.block_type') display_name = serializers.ReadOnlyField() path = serializers.SerializerMethodField() def __init__(self, *args, **kwargs): # Don't pass the 'fields' arg up to the superclass try: fields = kwargs['context'].pop('fields', DEFAULT_FIELDS) or DEFAULT_FIELDS except KeyError: fields = DEFAULT_FIELDS # Instantiate the superclass normally super(BookmarkSerializer, self).__init__(*args, **kwargs) # Drop any fields that are not specified in the `fields` argument. required_fields = set(fields) all_fields = set(self.fields.keys()) for field_name in all_fields - required_fields: self.fields.pop(field_name) class Meta(object): """ Serializer metadata. """ model = Bookmark fields = ( 'id', 'course_id', 'usage_id', 'block_type', 'display_name', 'path', 'created', ) def get_id(self, bookmark): """ Return the REST resource id: {username,usage_id}. """ return u"{0},{1}".format(bookmark.user.username, bookmark.usage_key) def get_path(self, bookmark): """ Serialize and return the path data of the bookmark. """ path_items = [path_item._asdict() for path_item in bookmark.path] for path_item in path_items: path_item['usage_key'] = six.text_type(path_item['usage_key']) return path_items
class IntegritySignatureSerializer(serializers.ModelSerializer): """ Serializer for the IntegritySignature model """ username = serializers.CharField(source='user.username') course_id = CourseKeyField(source='course_key') created_at = serializers.DateTimeField(source='created') class Meta: model = IntegritySignature() fields = ('username', 'course_id', 'created_at')
class CourseEntitlementSupportDetailSerializer(serializers.ModelSerializer): """ Serialize the details of a support team interaction with a learner's course entitlement. """ support_user = serializers.SlugRelatedField( read_only=True, slug_field='username', default=serializers.CurrentUserDefault()) unenrolled_run = CourseKeyField('unenrolled_run.id') class Meta: model = CourseEntitlementSupportDetail fields = ('support_user', 'reason', 'comments', 'unenrolled_run')
class CourseMetadataSerailizer(serializers.Serializer): """ Serializer for course metadata. """ id = CourseKeyField(help_text="The identifier of the course") blackouts = serializers.ListField( child=BlackoutDateSerializer(), help_text="A list of objects representing blackout periods " "(during which discussions are read-only except for privileged users)." ) thread_list_url = serializers.URLField( help_text="The URL of the list of all threads in the course.", ) following_thread_list_url = serializers.URLField( help_text="thread_list_url with parameter following=True", ) topics_url = serializers.URLField( help_text="The URL of the topic listing for the course.") allow_anonymous = serializers.BooleanField( help_text= "A boolean indicating whether anonymous posts are allowed or not.", ) allow_anonymous_to_peers = serializers.BooleanField( help_text= "A boolean indicating whether posts anonymous to peers are allowed or not.", ) user_roles = serializers.ListField( child=serializers.CharField(), help_text= "A list of all the roles the requesting user has for this course.", ) user_is_privileged = serializers.BooleanField( help_text= "A boolean indicating if the current user has a privileged role", ) provider = serializers.CharField( help_text="The discussion provider used by this course", ) enable_in_context = serializers.BooleanField( help_text= "A boolean indicating whether in-context discussion is enabled for the course", ) group_at_subsection = serializers.BooleanField( help_text= "A boolean indicating whether discussions should be grouped at subsection", ) post_close_reasons = serializers.ListField( child=ReasonCodeSeralizer(), help_text= "A list of reasons that can be specified by moderators for closing a post", ) edit_reasons = serializers.ListField( child=ReasonCodeSeralizer(), help_text= "A list of reasons that can be specified by moderators for editing a post, response, or comment", )
class BookmarkSerializer(serializers.ModelSerializer): """ Serializer for the Bookmark model. """ id = serializers.SerializerMethodField( # pylint: disable=invalid-name help_text= "The identifier string for the bookmark: {user_id},{usage_id}.", ) course_id = CourseKeyField( source='course_key', help_text="The identifier string of the bookmark's course.", ) usage_id = UsageKeyField( source='usage_key', help_text="The identifier string of the bookmark's XBlock.", ) block_type = serializers.ReadOnlyField(source='usage_key.block_type') display_name = serializers.ReadOnlyField( help_text="Display name of the XBlock.", ) path = serializers.SerializerMethodField(help_text=""" List of dicts containing {"usage_id": <usage-id>, display_name:<display-name>} for the XBlocks from the top of the course tree till the parent of the bookmarked XBlock. """, ) def __init__(self, *args, **kwargs): # Don't pass the 'fields' arg up to the superclass try: fields = kwargs['context'].pop('fields', DEFAULT_FIELDS) or DEFAULT_FIELDS except KeyError: fields = DEFAULT_FIELDS # Instantiate the superclass normally super().__init__(*args, **kwargs) # Drop any fields that are not specified in the `fields` argument. required_fields = set(fields) if 'request' in kwargs['context'] and is_schema_request( kwargs['context']['request']): # We are serving the schema: include everything required_fields.update(OPTIONAL_FIELDS) all_fields = set(self.fields.keys()) for field_name in all_fields - required_fields: self.fields.pop(field_name) class Meta: """ Serializer metadata. """ model = Bookmark fields = ( 'id', 'course_id', 'usage_id', 'block_type', 'display_name', 'path', 'created', ) def get_id(self, bookmark): """ Return the REST resource id: {username,usage_id}. """ return f"{bookmark.user.username},{bookmark.usage_key}" def get_path(self, bookmark): """ Serialize and return the path data of the bookmark. """ path_items = [path_item._asdict() for path_item in bookmark.path] for path_item in path_items: path_item['usage_key'] = str(path_item['usage_key']) return path_items