# -*- coding: UTF-8 -*-
#    This file is part of arena.
#
#    arena is free software: you can redistribute it and/or modify it
#    under the terms of the GNU General Public License as published by the
#    Free Software Foundation; either version 3 of the License or, at your
#    option, any later version.
#
#    arena is distributed in the hope that it will be useful, but
#    WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABLITY
#    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
#    License for more details.
#
#    You should have received a copy of the GNU General Public License along
#    with arena.  If not, see <http://www.gnu.org/licenses/>.

## @file
#  This file extends AnArenaParsedContentProvider to allow users with the 
#  appropriate permissions to add and edit content (posts) within channels.

from Plugins.Content.PerUser.Arena.ContentProviders import \
    AnArenaParsedContentProvider, AnArenaContentProviderError
from Plugins.Content.PerUser.Arena.Posts import AnArenaPost

from Local.Configuration import Site

## The name of the query key that specifies the action to take with regard to
#  a post
apeActionQueryKey = "do"
## The query key value that specifies a new post is to be published
apeActionPublish = "publish"
## The query key that specifies an existing post is to be updated
apeActionUpdate = "update"
## The query key that specifies an existing post is to be deleted
apeActionDelete = "delete"
## The query key value that specifies the ID of ap ost
apeIdQueryKey = "id"
## The name of the view to use for editing
apeViewName = "post/edit"

## The default title of a new post
apePostDefaultTitle = "Post Title"
## The default content of a new post
apePostDefaultContent = "Enter the content of your post and click 'Save' " + \
    "when finished, or 'Cancel' to go back to the previous page."
    
## The default fields and values to use for a new arena post
apePostNewFieldsValues = {
    "id": 0,
    "title": apePostDefaultTitle,
    "description": "",
    "author": "",
    "editor": "",
    "reviewer": "",
    "icon": "",
    "keywords": "",
    "created": 0.0,
    "published": 0.0,
    "ip": "",
    "sticky": "",
    "draft": "",
    "channels": "",
    "meta": "",
    "content": apePostDefaultContent
}

## A content provider that allows posts to be edited
class AnArenaPostEditor(AnArenaParsedContentProvider):
    # Properties ---------------------------------------------------------------
    ## Fetch content
    def content(Self):       
        # After this call, if a post ID was specified by the client, the 
        # post will have been retrieved from the database by the code
        # snippet contained in the channel's "onFetchContent" meta 
        # attribute.
        Posts = AnArenaParsedContentProvider.content(Self)      
        
        Self._contentParameters.update({
            "DataSource": Self._Mediator.DataSource(),
            "User": Self._User
        })        
        
        # If a post was retrieved, then display it
        for Post in Posts:            
            # If an action was specified by the client, then act accordingly
            if apeActionQueryKey in Self._Request:            
                action = Self._Request[apeActionQueryKey].value.lower()
            
                if action in (apeActionPublish, apeActionUpdate):
                    Post.publish()
                    Post.readOnly = False
                    Post.flush()
                
                elif action == apeActionDelete:
                   Post.delete()
            
            return (Post,)
        
        # Otherwise, allow the user to edit a new post        
        else:
            Post = Self._ContentHandler(apePostNewFieldsValues,
                **Self._contentParameters)
                
            # If an action was specified by the client, then act accordingly
            if apeActionQueryKey in Self._Request:            
                action = Self._Request[apeActionQueryKey].value.lower()
            
                if action in (apeActionPublish, apeActionUpdate):
                    Post.publish()
                    Post.readOnly = False
                    Post.flush()
                    
            return (Post, )
