<filename>app/management/schemas.py
from marshmallow import fields
from marshmallow.validate import Length
from app.extensions import ma, db

from app.forum.models import Forum, Category, Report
from app.user.models import Group, User
from app.plugins.models import PluginRegistry


class PaginationFields():
    has_next = fields.Boolean()
    has_prev = fields.Boolean()
    page = fields.Integer(data_key='current_page')
    pages = fields.Integer(data_key='total_pages')
    total = fields.Integer(data_key='count')


class ForumInputSchema(ma.Schema):
    category = fields.Integer(required=True)
    title = fields.String(required=True, validate=Length(3))
    description = fields.String(required=True, validate=Length(5))
    position = fields.Integer(default=1)
    locked = fields.Boolean(default=False)
    show_moderators = fields.Boolean(default=False)
    external = fields.String()
    moderators = fields.List(
        fields.Integer(), required=True, validate=Length(min=1))
    groups = fields.List(fields.Integer(), required=True,
                         validate=Length(min=1))


class ForumUpdateSchema(ma.Schema):
    id = fields.Integer(required=True)
    category = fields.Integer()
    title = fields.String(validate=Length(3))
    description = fields.String(validate=Length(5))
    position = fields.Integer()
    locked = fields.Boolean()
    show_moderators = fields.Boolean()
    external = fields.String()
    moderators = fields.List(
        fields.Integer(), validate=Length(min=1))
    groups = fields.List(fields.Integer(), validate=Length(min=1))


class ForumSchema(ma.Schema):
    id = fields.Integer()
    title = fields.String()
    description = fields.String()
    position = fields.Integer()
    locked = fields.Boolean()
    show_moderators = fields.Boolean()
    post_count = fields.Integer()
    topic_count = fields.Integer()
    last_post_title = fields.String()
    last_post_username = fields.String()
    last_post_created = fields.DateTime()


class CategoryBaseSchema(ma.Schema):
    title = fields.String(required=True)
    description = fields.String(required=True)
    position = fields.Integer(default=1)


class CategorySchema(CategoryBaseSchema):
    id = fields.Integer()
    forums = fields.List(fields.Nested(ForumSchema))


class CategoryUpdateSchema(CategoryBaseSchema):
    id = fields.Integer(required=True)


class GroupSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Group
        load_instance = True
        sqla_session = db.session


class GroupsSchema(ma.Schema, PaginationFields):
    items = fields.List(fields.Nested(GroupSchema), data_key='groups')


class UserSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = User
        load_instance = True
        sqla_session = db.session
        exclude = ("_password",)


class UsersSchema(ma.Schema, PaginationFields):
    items = fields.List(fields.Nested(UserSchema), data_key='users')


class ReportSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Report
        load_instance = True
        sqla_session = db.session


class ReportsSchema(ma.Schema, PaginationFields):
    items = fields.List(fields.Nested(ReportSchema), data_key='reports')


class UserInputBase(ma.Schema):
    birthday = fields.Date()
    gender = fields.String()
    location = fields.String()
    website = fields.String()
    avatar = fields.String()
    signature = fields.String()
    notes = fields.String()
    activated = fields.Boolean()
    secondary_groups = fields.List(fields.Integer())


class UserAddSchema(UserInputBase):
    username = fields.String(required=True)
    email = fields.String(required=True)
    password = fields.String(required=True, load_only=True)
    primary_group = fields.Integer(required=True)


class UserUpdateSchema(UserInputBase):
    id = fields.Integer(required=True)
    username = fields.String()
    email = fields.String()
    primary_group = fields.Integer()


class PluginSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = PluginRegistry

<filename>app/management/schemas.py
from marshmallow import fields
from marshmallow.validate import Length
from app.extensions import ma, db

from app.forum.models import Forum, Category, Report
from app.user.models import Group, User
from app.plugins.models import PluginRegistry


class PaginationFields():
    has_next = fields.Boolean()
    has_prev = fields.Boolean()
    page = fields.Integer(data_key='current_page')
    pages = fields.Integer(data_key='total_pages')
    total = fields.Integer(data_key='count')


class ForumInputSchema(ma.Schema):
    category = fields.Integer(required=True)
    title = fields.String(required=True, validate=Length(3))
    description = fields.String(required=True, validate=Length(5))
    position = fields.Integer(default=1)
    locked = fields.Boolean(default=False)
    show_moderators = fields.Boolean(default=False)
    external = fields.String()
    moderators = fields.List(
        fields.Integer(), required=True, validate=Length(min=1))
    groups = fields.List(fields.Integer(), required=True,
                         validate=Length(min=1))


class ForumUpdateSchema(ma.Schema):
    id = fields.Integer(required=True)
    category = fields.Integer()
    title = fields.String(validate=Length(3))
    description = fields.String(validate=Length(5))
    position = fields.Integer()
    locked = fields.Boolean()
    show_moderators = fields.Boolean()
    external = fields.String()
    moderators = fields.List(
        fields.Integer(), validate=Length(min=1))
    groups = fields.List(fields.Integer(), validate=Length(min=1))


class ForumSchema(ma.Schema):
    id = fields.Integer()
    title = fields.String()
    description = fields.String()
    position = fields.Integer()
    locked = fields.Boolean()
    show_moderators = fields.Boolean()
    post_count = fields.Integer()
    topic_count = fields.Integer()
    last_post_title = fields.String()
    last_post_username = fields.String()
    last_post_created = fields.DateTime()


class CategoryBaseSchema(ma.Schema):
    title = fields.String(required=True)
    description = fields.String(required=True)
    position = fields.Integer(default=1)


class CategorySchema(CategoryBaseSchema):
    id = fields.Integer()
    forums = fields.List(fields.Nested(ForumSchema))


class CategoryUpdateSchema(CategoryBaseSchema):
    id = fields.Integer(required=True)


class GroupSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Group
        load_instance = True
        sqla_session = db.session


class GroupsSchema(ma.Schema, PaginationFields):
    items = fields.List(fields.Nested(GroupSchema), data_key='groups')


class UserSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = User
        load_instance = True
        sqla_session = db.session
        exclude = ("_password",)


class UsersSchema(ma.Schema, PaginationFields):
    items = fields.List(fields.Nested(UserSchema), data_key='users')


class ReportSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = Report
        load_instance = True
        sqla_session = db.session


class ReportsSchema(ma.Schema, PaginationFields):
    items = fields.List(fields.Nested(ReportSchema), data_key='reports')


class UserInputBase(ma.Schema):
    birthday = fields.Date()
    gender = fields.String()
    location = fields.String()
    website = fields.String()
    avatar = fields.String()
    signature = fields.String()
    notes = fields.String()
    activated = fields.Boolean()
    secondary_groups = fields.List(fields.Integer())


class UserAddSchema(UserInputBase):
    username = fields.String(required=True)
    email = fields.String(required=True)
    password = fields.String(required=True, load_only=True)
    primary_group = fields.Integer(required=True)


class UserUpdateSchema(UserInputBase):
    id = fields.Integer(required=True)
    username = fields.String()
    email = fields.String()
    primary_group = fields.Integer()


class PluginSchema(ma.SQLAlchemyAutoSchema):
    class Meta:
        model = PluginRegistry
