# -*- 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 post attachments to
#  be managed and searched.
from Plugins.Content.PerUser.Arena.ContentProviders import \
    AnArenaParsedContentProvider, AnArenaContentProviderError
from Plugins.Content.PerUser.Arena.Attachments import AnArenaAttachment

from Local.Configuration import Site

## The name of the query key that specifies the action to take with regard to
#  an attachment
aamActionQueryKey = "do"
## The query key value that specifies a new attachment is to be uploaded
aamActionUpload = "upload"
## The query key that specifies an existing attachment is to be deleted
aamActionDelete = "delete"

## The default fields and values to use for a new arena attachment
aamAttachmentNewFieldsValues = {
    "id": 0,
    "attachmentName": "",
    "category": "",
    "localPath": "", 
    "clientPath": "",
    "mimeType": "",
    "fileSize": 0,
    "icon": "",
    "description": "",
    "keywords": "",
    "author": "",
    "editor": "",
    "reviewer": "",
    "created": 0.0,
    "published": 0.0,
    "ip": "",
    "meta": ""
}

# A content provider that allows attachments to be uploaded
class AnArenaAttachmentManager(AnArenaParsedContentProvider):
    # Properties ---------------------------------------------------------------
    ## Fetch content
    def content(Self):
        # After this call, if an attachment ID was specified by the client, the 
        # attachment will have been retrieved from the database by the code
        # snippet contained in the channel's "onFetchContent" meta 
        # attribute.
        Attachments = AnArenaParsedContentProvider.content(Self)
        
        Self._contentParameters.update({
            "Mediator": Self._Mediator,
            "DataSource": Self._Mediator.DataSource(),
            "User": Self._User
        })        
        
        # If an action was specified by the client, then act accordingly
        if aamActionQueryKey in Self._Request:
            action = Self._Request[aamActionQueryKey].value.lower()
            # Upload a new attachment
            if action == aamActionUpload:
                Attachment = AnArenaAttachment(aamAttachmentNewFieldsValues,
                    Mediator = Self._Mediator,
                    Channel = Self._Channel,
                    User = Self._User
                )
                    
                Attachment.upload()
                return (Attachment,)
                    
            # Delete an existing attachment
            elif action == aamActionDelete:
                for Attachment in Attachments:
                    Attachment.delete()
                    return (Attachment,)
            
        # Otherwise, return all attachments found to the view, which will 
        # format and display them to the client
        return Attachments
