import logging
import urllib

from google.appengine.ext import webapp
log = logging.getLogger(__name__)
from CommandFactory import CommandFactory
from orangescape.model.server.UserAuthentication import UserAuthentication

class ECommandFactory(webapp.RequestHandler,CommandFactory):

    def getParams(self):
        url=self.request.path
        url=urllib.unquote(url)
        path=url[1:len(url)].split('/')
        urlList = path[1:len(path)]
        urlList.insert(1,self.getSession().getApplication().getSheetId())
        return urlList

    def logout(self,metadatatype,applicationid):
        UserAuthentication(self.getSession()).logOutUser(self.userid)
        self.redirect('/'+self.getSession().getApplication().getSheetId()+'/1/signin')

    def updateAttachment(self,metadatatype,applicationid,parentsheetmetadataid,taskid,sheetmetadataid,url,fileName,fileContent):
        """POST /Instances: Create a new item."""
        updateOutput = self.performUpdateAttachment(fileContent)
        if updateOutput:
            self.response.out.write(updateOutput)
        else:
            self.error(500)

    def updateAttachmentWithId(self,metadatatype,applicationid,parentsheetmetadataid,taskid,sheetmetadataid,url, fileName, fileContent):
        """POST /Instances: Create a new item."""
        updateOutput=self.perfromUpdateAttachmentWithId(fileName,fileContent,self.request.query_string)
        if updateOutput:
            self.response.out.write(updateOutput)
        else:
            self.error(500)