class SubmitterStatsSerializer(serializers.Serializer): url = NestedHyperlinkedIdentityField( view_name='api:exervise-submitter_stats-detail', lookup_map={ 'exercise_id': 'exercise_id', 'user_id': 'user.user_id', }, ) exercise = NestedHyperlinkedIdentityField( view_name='api:exercise-detail', lookup_map={'exercise_id': 'exercise_id'}, ) user = UserBriefSerializer() submission_count = serializers.IntegerField() best_submission = SubmissionBriefSerializer() grade = serializers.IntegerField() class Meta(AplusSerializerMeta): fields = ( 'url', 'exercise', 'user', 'submission_count', 'best_submission', 'grade', )
class ExerciseSerializer(ExerciseBriefSerializer): course = CourseBriefSerializer(source='course_instance') post_url = serializers.SerializerMethodField() exercise_info = serializers.JSONField() submissions = NestedHyperlinkedIdentityField( view_name='api:exercise-submissions-list', lookup_map='exercise.api.views.ExerciseViewSet', ) my_submissions = NestedHyperlinkedIdentityField( view_name='api:exercise-submissions-detail', lookup_map={ 'exercise_id': 'id', 'user_id': lambda o=None: 'me', }, ) my_stats = NestedHyperlinkedIdentityField( view_name='api:exercise-submitter_stats-detail', lookup_map={ 'exercise_id': 'id', 'user_id': lambda o=None: 'me', }, ) def get_post_url(self, obj): # FIXME: obj should implement .get_post_url() and that should be used here if obj.is_submittable: request = self.context['request'] url = obj.get_url("exercise") return request.build_absolute_uri(url) return None class Meta(ExerciseBriefSerializer.Meta): fields = ( 'name', 'course', 'is_submittable', 'post_url', 'max_points', 'max_submissions', 'exercise_info', 'templates', 'submissions', 'my_submissions', 'my_stats', )
class ProjectSerializer(HyperlinkedModelSerializer): files = NestedHyperlinkedIdentityField( read_only=True, view_name='projectfile-list', ) operations = NestedHyperlinkedIdentityField( read_only=True, view_name='operation-list', ) class Meta: model = models.Project depth = 0 fields = ('url', 'owner', 'files', 'operations') extra_kwargs = { 'url': { 'lookup_url_kwarg': 'project_id' } }
class CourseSerializer(CourseBriefSerializer): """ ... """ exercises = NestedHyperlinkedIdentityField( view_name='api:course-exercises-list', format='html') students = NestedHyperlinkedIdentityField( view_name='api:course-students-list', format='html') usertags = NestedHyperlinkedIdentityField( view_name='api:course-usertags-list', format='html') taggings = NestedHyperlinkedIdentityField( view_name='api:course-taggings-list', format='html') points = NestedHyperlinkedIdentityField(view_name='api:course-points-list', format='html') submissiondata = NestedHyperlinkedIdentityField( view_name='api:course-submissiondata-list', format='html') class Meta(CourseBriefSerializer.Meta): fields = ( 'language', 'starting_time', 'ending_time', 'visible_to_students', 'exercises', 'students', 'usertags', 'taggings', 'points', 'submissiondata', )
class ExerciseGraderSerializer(AplusModelSerializerBase): url = NestedHyperlinkedIdentityField( view_name='api:exercise-grader', lookup_map='exercise.api.views.ExerciseViewSet', ) exercise = ExerciseBriefSerializer(source='*') class Meta(AplusSerializerMeta): model = Submission fields = ( 'url', 'exercise', )
class SubmittedFileBriefSerializer(AplusModelSerializer): #url = HtmlViewField() url = NestedHyperlinkedIdentityField( view_name='api:submission-files-detail', lookup_map='exercise.api.views.SubmissionFileViewSet', ) class Meta(AplusModelSerializer.Meta): model = SubmittedFile fields = ( 'url', 'filename', 'param_name', )
class ExerciseBriefSerializer(AplusModelSerializer): url = NestedHyperlinkedIdentityField( view_name='api:exercise-detail', lookup_map='exercise.api.views.ExerciseViewSet', ) display_name = serializers.CharField(source='__str__') class Meta(AplusModelSerializer.Meta): model = BaseExercise fields = ( 'url', 'html_url', 'display_name', )
class CourseSerializer(CourseBriefSerializer): """ ... """ exercises = NestedHyperlinkedIdentityField( view_name='api:course-exercises-list', format='html') students = NestedHyperlinkedIdentityField( view_name='api:course-students-list', format='html') usertags = NestedHyperlinkedIdentityField( view_name='api:course-usertags-list', format='html') taggings = NestedHyperlinkedIdentityField( view_name='api:course-taggings-list', format='html') my_points = NestedHyperlinkedIdentityField( view_name='api:course-points-detail', lookup_map={ 'course_id': 'id', 'user_id': lambda o=None: 'me', }, ) my_data = NestedHyperlinkedIdentityField( view_name='api:course-submissiondata-detail', lookup_map={ 'course_id': 'id', 'user_id': lambda o=None: 'me', }, ) data = NestedHyperlinkedIdentityField( view_name='api:course-submissiondata-list') aggregate_data = NestedHyperlinkedIdentityField( view_name='api:course-aggregatedata-list') class Meta(CourseBriefSerializer.Meta): fields = ( 'language', 'starting_time', 'ending_time', 'visible_to_students', 'exercises', 'students', 'usertags', 'taggings', 'my_points', 'my_data', 'data', 'aggregate_data', )
class UserListFieldWithStatsLink(AlwaysListSerializer, UserBriefSerializer): exercise_stats = NestedHyperlinkedIdentityField( view_name='api:exervise-submitter_stats-detail', lookup_map={ 'exercise_id': 'exercise_id', 'user_id': 'user_id', }, ) class Meta(UserBriefSerializer.Meta): list_serializer_class = CompositeListSerializer.with_extra({ 'exercise_id': 'exercise_id', }) fields = ('exercise_stats', )
class SubmissionGraderSerializer(AplusModelSerializerBase): url = NestedHyperlinkedIdentityField( view_name='api:submission-grader', lookup_map='exercise.api.views.SubmissionViewSet', ) submission = SubmissionInGraderSerializer(source='*') exercise = ExerciseBriefSerializer() class Meta(AplusSerializerMeta): model = Submission fields = ( 'url', 'submission', 'exercise', 'grading_data', 'is_graded', )
class ProjectFileReadSerializer(NestedHyperlinkedModelSerializer): content = NestedHyperlinkedIdentityField( read_only=True, view_name='projectfile-raw', ) class Meta: model = models.ProjectFile fields = ('url', 'project', 'path', 'content', 'file_type', 'last_mod') extra_kwargs = { 'url': { 'lookup_map': 'caviart.viewsets.ProjectFileViewSet' }, 'project': { 'lookup_map': 'caviart.viewsets.ProjectViewSet', }, }
class CourseBriefSerializer(AplusModelSerializer): """ BriefSerializer for course models """ # course model has url var, so we need to redifen the type here url = NestedHyperlinkedIdentityField( view_name='api:course-detail', lookup_map='course.api.views.CourseViewSet') code = serializers.CharField(source='course.code') name = serializers.CharField(source='course.name') class Meta(AplusModelSerializer.Meta): model = CourseInstance fields = ( 'url', 'html_url', 'code', 'name', 'instance_name', )
class CourseModuleSerializer(AplusModelSerializer): url = NestedHyperlinkedIdentityField(view_name='api:course-exercises-detail') exercises = serializers.SerializerMethodField() display_name = serializers.CharField(source='__str__') class Meta(AplusModelSerializer.Meta): model = CourseModule fields = ( 'url', 'html_url', 'display_name', 'is_open', 'exercises', ) def get_exercises(self, obj): # this needs to be method so .as_leaf_class() can be called exercises = obj.learning_objects.all() exercises = (e.as_leaf_class() for e in exercises) serializer = ExerciseBriefSerializer(instance=exercises, many=True, context=self.context) return serializer.data