def _printLog(self):
     """
     """
     #
     tabHead = (
         ' <table class="listing" \
                tal:attributes="summary string:Import results" \
                tal:condition="python:results"> \
           <tr> \
             <th>'
         + translate(_(u"Original file name"))
         + "</th>\
             <th>"
         + translate(_(u"Direct Link"))
         + "</th>\
             <th>"
         + translate(_(u"Size"))
         + "</th>\
             <th>"
         + translate(_(u"Status"))
         + "</th>\
             <th>"
         + translate(_(u"Notes"))
         + "</th>\
           </tr>\
     "
     )
     #
     tabBody = ""
     tabLine = '     <tr>\
               <td>%s</td>\
               <td>\
                 <a href="%s">%s</a>\
               </td>\
               <td>%s</td>\
               <td>%s</td>\
               <td>%s</td>\
             </tr>\
     '
     for entry in self.log:
         values = [entry[item] for item in ["filename", "url", "title", "size", "status", "info"]]
         tabBody += tabLine % tuple(values)
     #
     tabFooter = "</table>\
     "
     #
     return u"" + tabHead + tabBody + tabFooter
Beispiel #2
0
 def _printLog(self):
     """
     """
     #
     tabHead = ' <table class="listing" \
                tal:attributes="summary string:Import results" \
                tal:condition="python:results"> \
           <tr> \
             <th>'+self._translate(_(u"Original file name"))+'</th>\
             <th>'+self._translate(_(u"Direct Link"))+'</th>\
             <th>'+self._translate(_(u"Size"))+'</th>\
             <th>'+self._translate(_(u"Status"))+'</th>\
             <th>'+self._translate(_(u"Notes"))+'</th>\
           </tr>\
     '
     #
     tabBody = ''
     tabLine = '     <tr>\
               <td>%s</td>\
               <td>\
                 <a href="%s">%s</a>\
               </td>\
               <td>%s</td>\
               <td>%s</td>\
               <td>%s</td>\
             </tr>\
     '
     for entry in self.log:
         values = [entry[item] for item in ['filename', 'url', 'title',
                                            'size', 'status', 'info']]
         tabBody += tabLine % tuple(values)
     #
     tabFooter = '</table>\
     '
     #
     return u"" + tabHead + tabBody + tabFooter
    def _getContainer(self, path, item):
        """ Return the container object
        """
        # We get the index of item in path
        lenPath = len(path)
        if path[lenPath - 1]!='':
            ind = lenPath - 1
        else:
            ind = lenPath - 2

        # Index is 0, container is the context
        if not ind:
            return self.context
        # Index is not 0, search of container
        else:
            # We construct the query
            query = {}
            # We normalize the path
            normPath = [self._safeNormalize(item) for item in path[:ind]]
            queryPath = '/'.join(list(self.context.getPhysicalPath())+normPath)
            query['path'] = {'query': queryPath, 'depth': 0}
            # Id of the container, previous element in path
            query['getId'] = self._safeNormalize(path[ind-1])
            # We search an object with path and id
            brain = self.pc(query)
            # One match : everything is ok, we return the object container
            if len(brain) == 1:
                return brain[0].getObject()
            # We have to create the parents
            else:
                container = self._getContainer(path[:ind], path[ind-1])
                id = self._safeNormalize(path[ind-1])
                title = self._reencode(path[ind-1])
                isFolder = True
                rt, code, url, size = self._createObject(id,
                                                         isFolder,
                                                         title,
                                                         container,
                                                         id)
                if not rt:
                    self._log(self._reencode('/'.join(path[:ind]))+'/',
                              info=code)
                else:
                    self._log(self._reencode('/'.join(path[:ind]))+'/', title,
                              status=_(u"Ok"), url=url, info=code)
                return self._getContainer(path, item)
    def _log(self, filename, title=(u"N/A"), size='0', url='',
             status=_(u"Failed"), info=None):
        """
        """
        #
        if info is not None:
            info = self.msg.get(info, u"")
        else:
            info = u""

        #
        entry = {'filename': filename,
                 'title': title,
                 'size': size,
                 'url': url,
                 'status': translate(status),
                 'info': translate(info), }

        #
        self.log.append(entry)
    def __call__(self):
        """
        """
        # if form is not submitted return
        if not self.request.has_key('form.button.submit'):
            return self.template()

        #
        if self.request.has_key('up_file') and self.request.form['up_file']:
            up_file = self.request.form['up_file']
            build_report = self.request.has_key('build_report')
            status, self.log = IMassLoader(self.context).process(up_file,
                                                                 build_report)
            IStatusMessage(self.request).addStatusMessage(status, type='info')
            return self.template()
        else:
            status = _(u"You have to select a file.")
            redirecturl = self.context.absolute_url()+'/@@massloader'
            self.request.response.redirect(redirecturl)
            IStatusMessage(self.request).addStatusMessage(status, type='info')
    def _log(self, filename, title=(u"N/A"), size="0", url="", status=_(u"Failed"), info=None):
        """
        """
        #
        if info is not None:
            info = self.msg.get(info, u"")
        else:
            info = u""

        #
        entry = {
            "filename": filename,
            "title": title,
            "size": size,
            "url": url,
            "status": translate(status),
            "info": translate(info),
        }

        #
        self.log.append(entry)
    def process(self, fileupload=None, wantreport=False):
        """
        """
        #
        error = False

        #
        self.archive = queryUtility(IArchiveUtility).initialize(fileupload)
        if self.archive is None:
            return self.msg[NOCORRECTFILE], ""

        #
        for item in self.archive.listContent():
            #
            normalizeItem = self._reencode(item)

            #
            path = item.split("/")
            if item.endswith("/"):
                title = path[len(path) - 2]
                isFolder = True
            else:
                title = path[len(path) - 1]
                isFolder = False

            # We check the size of the file
            if not isFolder and not self._checkFileSize(item):
                self._log(normalizeItem, info=EXCEEDEDFILESIZE)
                error = True
                continue

            # We check the container
            container = self._getContainer(path, title)
            if not container:
                self._log(normalizeItem, info=NOPARENT)
                error = True
                continue

            # We create or update the object
            id = self._safeNormalize(title)
            title = self._reencode(title)
            rt, code, url, size = self._createObject(id, isFolder, title, container, item)
            if not rt:
                self._log(normalizeItem, info=code)
                error = True
                continue
            self._log(normalizeItem, status=_(u"Ok"), title=title, size=size, url=url, info=code)

        # We create or update the report
        if wantreport:
            filename = fileupload.filename
            id = "report-" + self._safeNormalize(filename)
            if not self.context.hasObject(id):
                alreadyexists = False
                self.ptypes.constructContent(type_name="Document", container=self.context, id=id)
            else:
                alreadyexists = True
            report = self.context[id]
            text = self._printLog()
            if alreadyexists:
                text += report.getText().decode("utf-8")
            else:
                title = translate(_("Report"))
                report.setTitle(title + " " + filename)
                desc = translate(_("Import report for the zip file"))
                report.setDescription(desc + " " + filename)
            report.setText(text)
            report.reindexObject()

        # Return
        if error:
            return self.msg[GENERALERROR], self._printLog()
        else:
            return self.msg[GENERALOK], self._printLog()