summaryrefslogtreecommitdiffstats
path: root/src/collective/dms/batchimport/fileimporter.py
blob: 2a62af9be5a4f1fcad1b563a23bf5403e4eebbb8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import logging
import os
import os.path

from zope import schema

from zope.component import queryUtility
from five import grok
from Products.CMFPlone.interfaces import IPloneSiteRoot
from Products.CMFCore.utils import getToolByName

import z3c.form.button
from plone import api
from plone.directives import form
from plone.i18n.normalizer.interfaces import IIDNormalizer

from plone.namedfile.field import NamedFile, NamedBlobFile

from . import _
from . import utils

log = logging.getLogger('collective.dms.batchimport')

class IImportFileFormSchema(form.Schema):
    file = NamedBlobFile(title=_(u"File"))

    title = schema.Text(required=False)
    portal_type = schema.Text(required=False)
    location = schema.Text(required=False)
    owner = schema.Text(required=False)
    treating_group = schema.Text(required=False)
    keywords = schema.Text(required=False)  # comma separated identifiers


class ImportFileForm(form.SchemaForm):
    schema = IImportFileFormSchema

    # Permission required to
    grok.require("cmf.ManagePortal")

    ignoreContext = True

    grok.context(IPloneSiteRoot)

    # appear as @@fileimport view
    grok.name("fileimport")

    def get_folder(self, foldername):
        folder = getToolByName(self.context, 'portal_url').getPortalObject()
        for part in foldername.split('/'):
            if not part:
                continue
            folder = getattr(folder, part)
        return folder

    def convertTitleToId(self, title):
        """Plug into plone's id-from-title machinery.
        """
        #title = title.decode('utf-8')
        newid = queryUtility(IIDNormalizer).normalize(title)
        return newid

    @z3c.form.button.buttonAndHandler(_('Import'), name='import')
    def import_file(self, action):
        # Extract form field values and errors from HTTP request
        data, errors = self.extractData()
        if errors:
            self.status = self.formErrorsMessage
            return

        portal_type = data['portal_type']
        filename = data['file'].filename
        owner = data['owner']
        treating_group = data['treating_group']
        folder = self.get_folder(data['location'])
        keywords = set([x.strip() for x in data['keywords'].split(',')])

        document_id = self.convertTitleToId(data.get('title') or os.path.splitext(filename)[0])

        utils.createDocument(self, folder, portal_type, document_id,
                filename, data['file'], owner, treating_group=treating_group,
                metadata={
                    'title': data.get('title'),
                    'keywords': keywords,
                    })