#
#   Python backend and Angular frontend code generation by gencrud
#   Copyright (C) 2018-2021 Marc Bertens-Nguyen m.bertens@pe2mbs.nl
#
#   This library is free software; you can redistribute it and/or modify
#   it under the terms of the GNU Library General Public License GPL-2.0-only
#   as published by the Free Software Foundation.
#
#   This library is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
#   Library General Public License for more details.
#
#   You should have received a copy of the GNU Library General Public
#   License GPL-2.0-only along with this library; if not, write to the
#   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
#   Boston, MA 02110-1301 USA
#
#   gencrud: 2021-04-04 08:26:09 version 2.1.680 by user mbertens
#
import webapp2.api as API
from marshmallow import fields, pre_load, post_dump
from webapp2.common.convert import value2Label, utcDateString2Local


class NewsSchema( API.mm.SQLAlchemySchema ):
    """Schema for the news table, this is generated by the gencrud.py module
    When modifing the file make sure that you remove the table from the configuration.
    """
    N_ID    = fields.Integer()
    N_MESSAGE    = fields.String()
    N_ACTIVE    = fields.Boolean()
    N_ALERT    = fields.Boolean()
    N_KEEP    = fields.Boolean()
    N_START_DATE    = fields.Date()
    N_END_DATE    = fields.Date()
    N_REMARK    = fields.String()

    @post_dump
    def post_dump_process( self, in_data, **kwargs ):
        in_data[ 'N_ACTIVE_LABEL' ] = value2Label( {True: 'Yes', False: 'No'}, in_data[ 'N_ACTIVE' ] )
        in_data[ 'N_ALERT_LABEL' ] = value2Label( {True: 'Yes', False: 'No'}, in_data[ 'N_ALERT' ] )
        in_data[ 'N_KEEP_LABEL' ] = value2Label( {True: 'Yes', False: 'No'}, in_data[ 'N_KEEP' ] )
        return in_data

    @pre_load
    def pre_load_process( self, out_data, **kwargs ):
        out_data[ 'N_START_DATE' ] = utcDateString2Local( out_data[ 'N_START_DATE' ], '%Y-%m-%d' )
        out_data[ 'N_END_DATE' ] = utcDateString2Local( out_data[ 'N_END_DATE' ], '%Y-%m-%d' )
        return out_data


newsSchema   = NewsSchema()
newssSchema  = NewsSchema( many = True )

