class WallpostSerializerBase(serializers.ModelSerializer): """ Base class serializer for Wallposts. This is not used directly; please subclass it. """ type = serializers.ReadOnlyField(source='wallpost_type', required=False) author = UserPreviewSerializer(read_only=True) parent_type = WallpostContentTypeField(slug_field='model', source='content_type') parent_id = serializers.IntegerField(source='object_id') reactions = ReactionSerializer(many=True, read_only=True, required=False) donation = serializers.PrimaryKeyRelatedField(queryset=Donation.objects, required=False, allow_null=True) def to_representation(self, instance): # We want to connect a donation by just sending the id, # but reading we want an embedded object, so we do a little trick here. response = super(WallpostSerializerBase, self).to_representation(instance) if instance.donation: response['donation'] = WallpostDonationSerializer( instance.donation, context=self.context).data return response class Meta(object): fields = ('id', 'type', 'author', 'created', 'reactions', 'parent_type', 'parent_id', 'pinned', 'donation', 'email_followers', 'share_with_facebook', 'share_with_twitter', 'share_with_linkedin')
class WallpostDonationSerializer(serializers.ModelSerializer): amount = MoneySerializer() user = UserPreviewSerializer() type = serializers.SerializerMethodField() class Meta(object): model = Donation fields = ( 'type', 'id', 'user', 'amount', 'name', 'fundraiser', 'reward', 'anonymous', ) def get_type(self, obj): return 'contributions/donations' def get_fields(self): """ If the donation is anonymous, we do not return the user. """ fields = super(WallpostDonationSerializer, self).get_fields() if isinstance(self.instance, Donation) and self.instance.anonymous: del fields['user'] return fields
class ProjectTaskMemberSerializer(serializers.ModelSerializer): """ Members that joined a task """ user = UserPreviewSerializer(source='member') class Meta: model = TaskMember fields = ('id', 'user', 'created', 'motivation', 'task')
class ProjectPosterSerializer(serializers.ModelSerializer): """ Members that wrote a wallpost """ user = UserPreviewSerializer(source='author') class Meta: model = TextWallpost fields = ('id', 'user', 'created', 'text')
class ProjectDonationSerializer(serializers.ModelSerializer): member = UserPreviewSerializer(source='user') date_donated = serializers.DateTimeField(source='ready') amount = MoneySerializer() siblings = serializers.IntegerField() class Meta: model = Donation fields = ('member', 'date_donated', 'amount',)
class ProjectDonorSerializer(serializers.ModelSerializer): """ Members that made a donation """ user = UserPreviewSerializer() class Meta: model = Donation fields = ('id', 'user', 'created')
class LatestDonationProjectSerializer(BaseProjectPreviewSerializer): task_count = serializers.IntegerField() owner = UserPreviewSerializer() class Meta(BaseProjectPreviewSerializer): model = BaseProjectPreviewSerializer.Meta.model fields = ('allow_overfunding', 'amount_asked', 'amount_donated', 'amount_needed', 'country', 'deadline', 'id', 'image', 'is_campaign', 'owner', 'pitch', 'status', 'status', 'task_count', 'title')
class ReactionSerializer(serializers.ModelSerializer): """ Serializer for Wallpost Reactions. """ author = UserPreviewSerializer() text = ContentTextField() wallpost = serializers.PrimaryKeyRelatedField(queryset=Wallpost.objects) class Meta: model = Reaction fields = ('created', 'author', 'text', 'id', 'wallpost')
class NewsItemSerializer(serializers.ModelSerializer): id = serializers.CharField(source='slug') blocks = BlockSerializer(source='contents.contentitems.all', many=True) main_image = SorlImageField('800x400') author = UserPreviewSerializer() class Meta(object): model = NewsItem fields = ('id', 'title', 'blocks', 'main_image', 'author', 'publication_date', 'allow_comments', 'language', 'main_image')
class NewsItemSerializer(serializers.ModelSerializer): id = serializers.CharField(source='slug') body = NewsItemContentsField(source='contents') main_image = SorlImageField('800x400') author = UserPreviewSerializer() class Meta: model = NewsItem fields = ('id', 'title', 'body', 'main_image', 'author', 'publication_date', 'allow_comments', 'language', 'main_image')
class LatestDonationSerializer(serializers.ModelSerializer): project = LatestDonationProjectSerializer() payment_method = serializers.SerializerMethodField() user = UserPreviewSerializer() amount = MoneySerializer() class Meta: model = Donation fields = ('amount', 'anonymous', 'created', 'fundraiser', 'id', 'name', 'payment_method', 'project', 'user') def get_payment_method(self, obj): return obj.get_payment_method()
class VoteSerializer(serializers.ModelSerializer): voter = UserPreviewSerializer(required=False, default=None) project = serializers.SlugRelatedField(slug_field='slug', queryset=Project.objects) def validate(self, data): data['voter'] = self.context['request'].user if Vote.has_voted(data['voter'], data['project']): raise exceptions.ValidationError('You already voted.') return data class Meta: model = Vote fields = ('id', 'voter', 'project', 'created')
class PreviewDonationSerializer(serializers.ModelSerializer): project = serializers.PrimaryKeyRelatedField(read_only=True) fundraiser = serializers.PrimaryKeyRelatedField( required=False, queryset=Fundraiser.objects) payment_method = serializers.SerializerMethodField() user = UserPreviewSerializer(source='public_user') amount = MoneySerializer() name = serializers.CharField(source='public_name', required=False) class Meta: model = Donation fields = ('amount', 'anonymous', 'created', 'fundraiser', 'id', 'name', 'payment_method', 'project', 'reward', 'user') def get_payment_method(self, obj): return obj.get_payment_method()
class WallpostSerializerBase(serializers.ModelSerializer): """ Base class serializer for Wallposts. This is not used directly; please subclass it. """ type = serializers.ReadOnlyField(source='wallpost_type', required=False) author = UserPreviewSerializer() parent_type = WallpostContentTypeField(slug_field='model', source='content_type') parent_id = WallpostParentIdField(source='object_id') reactions = ReactionSerializer(many=True, read_only=True, required=False) class Meta: fields = ('id', 'type', 'author', 'created', 'reactions', 'parent_type', 'parent_id', 'donation', 'email_followers', 'share_with_facebook', 'share_with_twitter', 'share_with_linkedin')
class WallpostSerializer(serializers.ModelSerializer): type = serializers.ReadOnlyField(source='wallpost_type', required=False) author = UserPreviewSerializer() def to_representation(self, obj): """ Wallpost Polymorphic serialization """ if isinstance(obj, TextWallpost): return TextWallpostSerializer( obj, context=self.context).to_representation(obj) elif isinstance(obj, MediaWallpost): return MediaWallpostSerializer( obj, context=self.context).to_representation(obj) elif isinstance(obj, SystemWallpost): return SystemWallpostSerializer( obj, context=self.context).to_representation(obj) return super(WallpostSerializer, self).to_representation(obj) class Meta(object): model = Wallpost fields = ('id', 'type', 'author', 'created', 'email_followers', 'share_with_facebook', 'share_with_twitter', 'share_with_linkedin')
def get_user(self, obj): user = Member.objects.get(pk=obj['pk']) return UserPreviewSerializer( user, context=self.context).to_representation(user)
class ContactMessageSerializer(serializers.ModelSerializer): author = UserPreviewSerializer() class Meta: model = ContactMessage fields = ('id', 'author', 'name', 'email', 'message', 'creation_date')
class QuoteSerializer(serializers.ModelSerializer): user = UserPreviewSerializer() class Meta: model = Quote fields = ('id', 'quote', 'user')