class CategorySerializer(ModelSerializer): articles = fields.Nested('ArticleListSerializer', many=True) series = fields.Nested('SeriesListSerializer', many=True) class Meta: model = Category fields = CATEGORY_FIELDS + ('articles', 'series')
class TagSerializer(ModelSerializer): articles = fields.Nested('ArticleListSerializer', many=True) series = fields.Nested('SeriesListSerializer', many=True) class Meta: model = Tag fields = TAG_FIELDS + ('articles', 'series')
class SeriesArticleSerializer(ModelSerializer): slug = fields.Nested('ArticleSerializer', attribute='article', only='slug') title = fields.Nested('ArticleSerializer', attribute='article', only='title') class Meta: model = SeriesArticle fields = ARTICLE_SERIES_FIELDS
class SeriesSerializer(ModelSerializer): articles = fields.Nested('ArticleListSerializer', many=True) category = fields.Nested('CategorySerializer', only=('name', 'slug')) tags = fields.Nested('TagSerializer', only=('name', 'slug'), many=True) class Meta: model = Series fields = SERIES_FIELDS
class ArticleSerializer(ModelSerializer): author = fields.Nested('UserSerializer', only=('first_name', 'last_name')) category = fields.Nested('CategorySerializer', only=('name', 'slug')) series = fields.Nested('ArticleSeriesDetailSerializer', attribute='article_series') tags = fields.Nested('TagSerializer', only=('name', 'slug'), many=True) class Meta: model = Article fields = ARTICLE_FIELDS + ('html', )
class CategorySerializer(ModelSerializer): id = fields.String(required=False) articles = fields.Nested('ArticleListSerializer', many=True) class Meta: model = Category fields = CATEGORY_FIELDS + ('articles', )
class UserSerializer(ModelSerializer): email = fields.Email(required=True) #roles = fields.Nested('RoleSerializer', only='name', many=True) # FIXME: Why 'only' has to be used? roles = fields.Nested('RoleSerializer', many=True) class Meta: model = User exclude = ('confirmed_at', 'created_at', 'updated_at', 'user_roles') dump_only = ('active', 'roles') load_only = ('password', ) @validates('email') def validate_email(self, email): existing = User.get_by(email=email) if existing and (self.is_create() or existing != self.instance): raise ValidationError('Sorry, that email is already taken.') @validates('username') def validate_username(self, username): if re.search(NON_ALPHANUMERIC_RE, username): raise ValidationError( 'Username should only contain letters, numbers and/or the underscore character.' ) existing = User.get_by(username=username) if existing and (self.is_create() or existing != self.instance): raise ValidationError('Sorry, that username is already taken.') @validates('password') def validate_password(self, value): if not value or len(value) < 8: raise ValidationError( 'Password must be at least 8 characters long.')
class ArticleListSerializer(ArticleSerializer): series = fields.Nested('ArticleSeriesSerializer', attribute='article_series') class Meta: model = Article fields = ARTICLE_FIELDS
class ArticleSeriesDetailSerializer(ArticleSeriesSerializer): articles = fields.Nested('SeriesArticleSerializer', attribute='series.series_articles', many=True) class Meta: model = SeriesArticle fields = ARTICLE_SERIES_FIELDS + ('articles',)
class ArticleSerializer(ModelSerializer): category = fields.Nested('CategorySerializer', only=('id', 'name', 'slug')) class Meta: model = Article fields = ARTICLE_FIELDS + ('html', )