76 lines
3.0 KiB
Python
76 lines
3.0 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
import cPickle
|
|
import os
|
|
import datetime
|
|
import cStringIO
|
|
|
|
from Products.Five.browser import BrowserView
|
|
from Products.CMFCore.utils import getToolByName
|
|
from plone.dexterity.factory import DexterityFactory
|
|
from plone.dexterity.utils import addContentToContainer
|
|
from plone.namedfile.file import NamedFile
|
|
from Products.CMFCore.WorkflowCore import WorkflowException
|
|
|
|
class Migrate(BrowserView):
|
|
def __call__(self):
|
|
if self.request['REQUEST_METHOD'] == 'GET':
|
|
filename = self.request.form.get('filename')
|
|
if not filename:
|
|
return 'Missing filename\n'
|
|
fd = file(os.path.join('/tmp/', filename))
|
|
else:
|
|
fd = cStringIO.StringIO(self.request.form['data'])
|
|
|
|
doc = cPickle.load(fd)
|
|
portal = getToolByName(self.context, 'portal_url').getPortalObject()
|
|
workflow_tool = getToolByName(self.context, 'portal_workflow')
|
|
plone_tool = getToolByName(self.context, 'plone_utils')
|
|
|
|
if doc.get('state') not in ('filed',):
|
|
return 'Unhandled state (%s)\n' % (doc.get('state'))
|
|
|
|
if doc.get('meta_type') == 'PFBArbitralCourt' and doc.get('id').startswith('cour-d-arbitrage'):
|
|
return 'Arbitral Court documents are not to be imported\n'
|
|
|
|
# first step is to create a "courrier entrant" object
|
|
typename = 'courrier_entrant'
|
|
factory = DexterityFactory(portal_type=typename)
|
|
if hasattr(self.context, doc.get('id')):
|
|
ob = getattr(self.context, doc.get('id'))
|
|
else:
|
|
ob = factory(id=doc.get('id'), title=doc.get('title'))
|
|
addContentToContainer(self.context, ob)
|
|
|
|
ob.date_reception = datetime.datetime.strptime(doc.get('deliverydate'), '%Y-%m-%d').date()
|
|
ob.numero_courrier = doc.get('mailnumber')
|
|
|
|
ob.fichier = NamedFile(fd.read(), filename=unicode(doc.get('filename')))
|
|
|
|
ob.categorie_de_courrier = [{
|
|
'PFBAddressChange': u'Changement de coordonnées',
|
|
'PFBGeneralMail': u'Courrier général divers',
|
|
'PFBArbitralCourt': u'Relations publiques',
|
|
'PFBSubventionRequest': u'Demande de subvention',
|
|
'PFBActivityAndExpertReport': u'''Rapport d'activités/d'experts''',
|
|
'PFBCollegialNotification': u'Notifications du Collège',
|
|
'PFBMeetingExcuses': u'Excusés',
|
|
'PFBAuditingCommitteeCommunication': u'Communication gouvernementale',
|
|
'PFBGeneralCommission': u'Commissions divers',
|
|
}.get(doc.get('meta_type'))]
|
|
|
|
ob.expediteur = []
|
|
for shipper in doc.get('shippers')[:1]:
|
|
shipper_id = plone_tool.normalizeString(shipper)
|
|
if not portal.contacts.has_key(shipper_id):
|
|
portal.contacts.invokeFactory('themis.datatypes.contact', shipper_id, title=shipper)
|
|
#ob.expediteur.append('contact:%s' % shipper_id)
|
|
ob.expediteur.append(shipper)
|
|
|
|
try:
|
|
workflow_tool.doActionFor(ob, 'publish')
|
|
except WorkflowException:
|
|
pass
|
|
|
|
return u'→ OK\n'
|