class AnswerSerializer(serializers.ModelSerializer): content = SplitSourceField(read_source='content_parsed', write_source='content') created = DateTimeUTCField(read_only=True) creator = serializers.SerializerMethodField() num_helpful_votes = serializers.ReadOnlyField() num_unhelpful_votes = serializers.ReadOnlyField() updated = DateTimeUTCField(read_only=True) updated_by = serializers.SerializerMethodField() class Meta: model = Answer fields = ( 'id', 'question', 'content', 'created', 'creator', 'updated', 'updated_by', 'is_spam', 'num_helpful_votes', 'num_unhelpful_votes', ) def get_creator(self, obj): return ProfileFKSerializer(Profile.objects.get(user=obj.creator)).data def get_updated_by(self, obj): updated_by = Profile.objects.get( user=obj.updated_by) if obj.updated_by else None return ProfileFKSerializer(updated_by).data if updated_by else None def validate(self, data): user = getattr(self.context.get('request'), 'user') if user and not user.is_anonymous and data.get('creator') is None: data['creator'] = user return data
class AnswerSerializer(serializers.ModelSerializer): content = SplitSourceField(read_source="content_parsed", write_source="content") created = DateTimeUTCField(read_only=True) creator = serializers.SerializerMethodField() num_helpful_votes = serializers.ReadOnlyField() num_unhelpful_votes = serializers.ReadOnlyField() updated = DateTimeUTCField(read_only=True) updated_by = serializers.SerializerMethodField() class Meta: model = Answer fields = ( "id", "question", "content", "created", "creator", "updated", "updated_by", "is_spam", "num_helpful_votes", "num_unhelpful_votes", ) def get_creator(self, obj): return ProfileFKSerializer(Profile.objects.get(user=obj.creator)).data def get_updated_by(self, obj): updated_by = Profile.objects.get( user=obj.updated_by) if obj.updated_by else None return ProfileFKSerializer(updated_by).data if updated_by else None def validate(self, data): user = getattr(self.context.get("request"), "user") if user and not user.is_anonymous and data.get("creator") is None: data["creator"] = user return data
class QuestionSerializer(serializers.ModelSerializer): answers = serializers.PrimaryKeyRelatedField(many=True, read_only=True) content = SplitSourceField(read_source='content_parsed', write_source='content') created = DateTimeUTCField(read_only=True) creator = serializers.SerializerMethodField() involved = serializers.SerializerMethodField() is_solved = serializers.ReadOnlyField() is_taken = serializers.ReadOnlyField() metadata = QuestionMetaDataSerializer(source='metadata_set', read_only=True, many=True) num_votes = serializers.ReadOnlyField() product = serializers.SlugRelatedField( required=True, slug_field='slug', queryset=Product.objects.all()) tags = serializers.SerializerMethodField() solution = serializers.PrimaryKeyRelatedField(read_only=True) solved_by = serializers.SerializerMethodField() taken_by = serializers.SerializerMethodField() topic = TopicField(required=True, queryset=Topic.objects.all()) updated = DateTimeUTCField(read_only=True) updated_by = serializers.SerializerMethodField() class Meta: model = Question fields = ( 'answers', 'content', 'created', 'creator', 'id', 'involved', 'is_archived', 'is_locked', 'is_solved', 'is_spam', 'is_taken', 'last_answer', 'locale', 'metadata', 'tags', 'num_answers', 'num_votes_past_week', 'num_votes', 'product', 'solution', 'solved_by', 'taken_until', 'taken_by', 'title', 'topic', 'updated_by', 'updated', ) def get_involved(self, obj): involved = set([obj.creator.profile]) involved.update(a.creator.profile for a in obj.answers.all()) return ProfileFKSerializer(involved, many=True).data def get_solved_by(self, obj): if obj.solution: return ProfileFKSerializer(obj.solution.creator.profile).data else: return None def get_creator(self, obj): return ProfileFKSerializer(obj.creator.profile).data def get_taken_by(self, obj): if obj.taken_by: return ProfileFKSerializer(obj.taken_by.profile).data else: return None def get_updated_by(self, obj): if obj.updated_by: return ProfileFKSerializer(obj.updated_by.profile).data else: return None def get_tags(self, obj): return [{'name': tag.name, 'slug': tag.slug} for tag in obj.tags.all()] def validate(self, data): user = getattr(self.context.get('request'), 'user') if user and not user.is_anonymous() and data.get('creator') is None: data['creator'] = user return data
class QuestionSerializer(serializers.ModelSerializer): answers = serializers.PrimaryKeyRelatedField(many=True, read_only=True) content = SplitSourceField(read_source="content_parsed", write_source="content") created = DateTimeUTCField(read_only=True) creator = serializers.SerializerMethodField() involved = serializers.SerializerMethodField() is_solved = serializers.ReadOnlyField() is_taken = serializers.ReadOnlyField() metadata = QuestionMetaDataSerializer(source="metadata_set", read_only=True, many=True) num_votes = serializers.ReadOnlyField() product = serializers.SlugRelatedField(required=True, slug_field="slug", queryset=Product.objects.all()) tags = serializers.SerializerMethodField() solution = serializers.PrimaryKeyRelatedField(read_only=True) solved_by = serializers.SerializerMethodField() taken_by = serializers.SerializerMethodField() topic = TopicField(required=True, queryset=Topic.objects.all()) updated = DateTimeUTCField(read_only=True) updated_by = serializers.SerializerMethodField() class Meta: model = Question fields = ( "answers", "content", "created", "creator", "id", "involved", "is_archived", "is_locked", "is_solved", "is_spam", "is_taken", "last_answer", "locale", "metadata", "tags", "num_answers", "num_votes_past_week", "num_votes", "product", "solution", "solved_by", "taken_until", "taken_by", "title", "topic", "updated_by", "updated", ) def get_involved(self, obj): involved = {obj.creator.profile} involved.update(a.creator.profile for a in obj.answers.all()) return ProfileFKSerializer(involved, many=True).data def get_solved_by(self, obj): if obj.solution: return ProfileFKSerializer(obj.solution.creator.profile).data else: return None def get_creator(self, obj): return ProfileFKSerializer(obj.creator.profile).data def get_taken_by(self, obj): if obj.taken_by: return ProfileFKSerializer(obj.taken_by.profile).data else: return None def get_updated_by(self, obj): if obj.updated_by: return ProfileFKSerializer(obj.updated_by.profile).data else: return None def get_tags(self, obj): return [{"name": tag.name, "slug": tag.slug} for tag in obj.tags.all()] def validate(self, data): user = getattr(self.context.get("request"), "user") if user and not user.is_anonymous and data.get("creator") is None: data["creator"] = user return data