class ImageDetailSerializer(ImageShortSerializer): created = DateTimeUTCField(read_only=True) updated = DateTimeUTCField(read_only=True) updated_by = serializers.SlugRelatedField(slug_field='username', queryset=User.objects.all()) class Meta(ImageShortSerializer.Meta): fields = ImageShortSerializer.Meta.fields + ( 'created', 'updated', 'updated_by', 'description', 'is_draft', 'creator')
class ImageDetailSerializer(ImageShortSerializer): created = DateTimeUTCField(read_only=True) updated = DateTimeUTCField(read_only=True) updated_by = serializers.SlugRelatedField(slug_field="username", queryset=User.objects.all()) class Meta(ImageShortSerializer.Meta): fields = ImageShortSerializer.Meta.fields + ( "created", "updated", "updated_by", "description", "is_draft", "creator", )
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 RealtimeActionSerializer(serializers.ModelSerializer): action_object = GenericRelatedField(serializer_type='full') actor = GenericRelatedField(serializer_type='full') target = GenericRelatedField(serializer_type='full') verb = serializers.CharField() timestamp = DateTimeUTCField() class Meta: model = PushNotificationRegistration fields = ( 'action_object', 'actor', 'id', 'target', 'timestamp', 'verb', )
class RealtimeActionSerializer(serializers.ModelSerializer): action_object = GenericRelatedField(serializer_type="full") actor = GenericRelatedField(serializer_type="full") target = GenericRelatedField(serializer_type="full") verb = serializers.CharField() timestamp = DateTimeUTCField() class Meta: model = PushNotificationRegistration fields = ( "action_object", "actor", "id", "target", "timestamp", "verb", )
class NotificationSerializer(serializers.ModelSerializer): is_read = serializers.ReadOnlyField() timestamp = DateTimeUTCField(source='action.timestamp') actor = GenericRelatedField(source='action.actor') verb = serializers.CharField(source='action.verb') action_object = GenericRelatedField(source='action.action_object') target = GenericRelatedField(source='action.target') class Meta: model = PushNotificationRegistration fields = ( 'action_object', 'actor', 'id', 'is_read', 'target', 'timestamp', 'verb', )
class NotificationSerializer(serializers.ModelSerializer): is_read = serializers.ReadOnlyField() timestamp = DateTimeUTCField("%Y-%m-%dT%H:%M:%SZ", source="action.timestamp") actor = GenericRelatedField(source="action.actor") verb = serializers.CharField(source="action.verb") action_object = GenericRelatedField(source="action.action_object") target = GenericRelatedField(source="action.target") class Meta: model = PushNotificationRegistration fields = ( "action_object", "actor", "id", "is_read", "target", "timestamp", "verb", )
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 ProfileSerializer(serializers.ModelSerializer): id = serializers.IntegerField(source="user.id", read_only=True) username = serializers.CharField(source="user.username") display_name = serializers.CharField(source="name", required=False) date_joined = DateTimeUTCField(source="user.date_joined", read_only=True) avatar = serializers.SerializerMethodField("get_avatar_url") email = PermissionMod(serializers.EmailField, permissions=[OnlySelf])(source="user.email", required=True) settings = PermissionMod(UserSettingSerializer, permissions=[OnlySelf])(many=True, read_only=True) helpfulness = serializers.ReadOnlyField(source="answer_helpfulness") answer_count = serializers.SerializerMethodField() question_count = serializers.SerializerMethodField() solution_count = serializers.SerializerMethodField() last_answer_date = serializers.SerializerMethodField() is_active = serializers.BooleanField(source="user.is_active", read_only=True) timezone = TimezoneField(required=False) class Meta: model = Profile fields = [ "answer_count", "avatar", "bio", "city", "country", "date_joined", "display_name", "email", "facebook", "helpfulness", "id", "irc_handle", "is_active", "last_answer_date", "locale", "mozillians", "question_count", "settings", "solution_count", "timezone", "twitter", "username", "website", ] def get_avatar_url(self, profile): request = self.context.get("request") size = request.GET.get("avatar_size", 200) if request else 200 return profile_avatar(profile.user, size=size) def get_question_count(self, profile): return num_questions(profile.user) def get_answer_count(self, profile): return num_answers(profile.user) def get_solution_count(self, profile): return num_solutions(profile.user) def get_last_answer_date(self, profile): last_answer = profile.user.answers.order_by("-created").first() return last_answer.created if last_answer else None
class ProfileSerializer(serializers.ModelSerializer): id = serializers.IntegerField(source='user.id', read_only=True) username = serializers.CharField(source='user.username') display_name = serializers.CharField(source='name', required=False) date_joined = DateTimeUTCField(source='user.date_joined', read_only=True) avatar = serializers.SerializerMethodField('get_avatar_url') email = (PermissionMod(serializers.EmailField, permissions=[OnlySelf])(source='user.email', required=True)) settings = (PermissionMod(UserSettingSerializer, permissions=[OnlySelf])(many=True, read_only=True)) helpfulness = serializers.ReadOnlyField(source='answer_helpfulness') answer_count = serializers.SerializerMethodField() question_count = serializers.SerializerMethodField() solution_count = serializers.SerializerMethodField() last_answer_date = serializers.SerializerMethodField() is_active = serializers.BooleanField(source='user.is_active', read_only=True) # This is a write only field. It is very important it stays that way! password = serializers.CharField(source='user.password', write_only=True) class Meta: model = Profile fields = [ 'answer_count', 'avatar', 'bio', 'city', 'country', 'date_joined', 'display_name', 'email', 'facebook', 'helpfulness', 'id', 'irc_handle', 'is_active', 'last_answer_date', 'locale', 'mozillians', # Password is here so it can be involved in write operations. It is # marked as write-only above, so will not be visible. 'password', 'question_count', 'settings', 'solution_count', 'timezone', 'twitter', 'username', 'website', ] def get_avatar_url(self, profile): request = self.context.get('request') size = request.REQUEST.get('avatar_size', 48) if request else 48 return profile_avatar(profile.user, size=size) def get_question_count(self, profile): return num_questions(profile.user) def get_answer_count(self, profile): return num_answers(profile.user) def get_solution_count(self, profile): return num_solutions(profile.user) def get_last_answer_date(self, profile): last_answer = profile.user.answers.order_by('-created').first() return last_answer.created if last_answer else None def validate(self, data): if data.get('name') is None: username = data.get('user', {}).get('username') data['name'] = username return data def create(self, validated_data): user_data = validated_data.pop('user') u = RegistrationProfile.objects.create_inactive_user( user_data['username'], user_data['password'], user_data['email']) p = u.profile for key, val in validated_data.items(): setattr(p, key, val) p.save() return p def update(self, instance, validated_data): if 'user' in validated_data: user_data = validated_data.pop('user') for key, val in user_data.items(): setattr(instance.user, key, val) instance.user.save() return super(ProfileSerializer, self).update(instance, validated_data) def validate_username(self, username): if re.match(r'^[\w.-]{4,30}$', username) is None: raise ValidationError( 'Usernames may only be letters, numbers, "." and "-".') if self.instance: # update if username != self.instance.user.username: raise ValidationError("Can't change this field.") else: # create if User.objects.filter(username=username).exists(): raise ValidationError('A user with that username exists') return username def validate_email(self, email): if not self.instance: # create if User.objects.filter(email=email).exists(): raise ValidationError( 'A user with that email address already exists.') return email
class ProfileSerializer(serializers.ModelSerializer): id = serializers.IntegerField(source="user.id", read_only=True) username = serializers.CharField(source="user.username") display_name = serializers.CharField(source="name", required=False) date_joined = DateTimeUTCField(source="user.date_joined", read_only=True) avatar = serializers.SerializerMethodField("get_avatar_url") email = PermissionMod(serializers.EmailField, permissions=[OnlySelf])(source="user.email", required=True) settings = PermissionMod(UserSettingSerializer, permissions=[OnlySelf])(many=True, read_only=True) helpfulness = serializers.ReadOnlyField(source="answer_helpfulness") answer_count = serializers.SerializerMethodField() question_count = serializers.SerializerMethodField() solution_count = serializers.SerializerMethodField() last_answer_date = serializers.SerializerMethodField() is_active = serializers.BooleanField(source="user.is_active", read_only=True) # This is a write only field. It is very important it stays that way! password = serializers.CharField(source="user.password", write_only=True) timezone = TimezoneField(required=False) class Meta: model = Profile fields = [ "answer_count", "avatar", "bio", "city", "country", "date_joined", "display_name", "email", "facebook", "helpfulness", "id", "irc_handle", "is_active", "last_answer_date", "locale", "mozillians", # Password is here so it can be involved in write operations. It is # marked as write-only above, so will not be visible. "password", "question_count", "settings", "solution_count", "timezone", "twitter", "username", "website", ] def get_avatar_url(self, profile): request = self.context.get("request") size = request.GET.get("avatar_size", 200) if request else 200 return profile_avatar(profile.user, size=size) def get_question_count(self, profile): return num_questions(profile.user) def get_answer_count(self, profile): return num_answers(profile.user) def get_solution_count(self, profile): return num_solutions(profile.user) def get_last_answer_date(self, profile): last_answer = profile.user.answers.order_by("-created").first() return last_answer.created if last_answer else None def validate(self, data): if data.get("name") is None: username = data.get("user", {}).get("username") data["name"] = username return data def create(self, validated_data): user_data = validated_data.pop("user") u = RegistrationProfile.objects.create_inactive_user( user_data["username"], user_data["password"], user_data["email"]) p = u.profile for key, val in validated_data.items(): setattr(p, key, val) p.save() return p def update(self, instance, validated_data): if "user" in validated_data: user_data = validated_data.pop("user") for key, val in user_data.items(): setattr(instance.user, key, val) instance.user.save() return super(ProfileSerializer, self).update(instance, validated_data) def validate_username(self, username): if re.match(r"^[\w.-]{4,30}$", username) is None: raise ValidationError( 'Usernames may only be letters, numbers, "." and "-".') if self.instance: # update if username != self.instance.user.username: raise ValidationError("Can't change this field.") else: # create if User.objects.filter(username=username).exists(): raise ValidationError("A user with that username exists") return username def validate_email(self, email): if not self.instance: # create if User.objects.filter(email=email).exists(): raise ValidationError( "A user with that email address already exists.") return email
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