This repository has been archived on 2023-02-21. You can view files and clone it, but cannot push or open issues or pull requests.
themis.libellioimport/themis/libellioimport/migration.py

248 lines
11 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):
self.logfile = file('/tmp/migration.log', 'a+')
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')
print 'Processing', doc.get('id')
print >> self.logfile, 'I: Processing', doc.get('id')
if doc.get('meta_type') == 'PFBArbitralCourt' and doc.get('id').startswith('cour-d-arbitrage'):
print >> self.logfile, 'I: skipped, arbitral court document'
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 not hasattr(self.context, doc.get('id')):
ob = factory(id=doc.get('id'), title=doc.get('title'))
self.context._setObject(ob.id, ob)
ob = getattr(self.context, doc.get('id'))
ob.date_reception = datetime.datetime.strptime(doc.get('deliverydate'), '%Y-%m-%d').date()
ob.numero_courrier = doc.get('mailnumber')
if ob.date_reception < (datetime.datetime.today()-datetime.timedelta(days=90)).date():
# all documents created more than three months ago are considered
# done. (XXX: maybe this should not happen for published
# documents?)
doc['state'] = 'filed'
ob.fichier = NamedFile(fd.read(), filename=unicode(doc.get('filename')))
ob.categorie_de_courrier = [{
'PFBActivityAndExpertReport': u'''Rapport d'activités/d'experts''',
'PFBAddressChange': u'Changement de coordonnées',
'PFBArbitralCourt': u'Relations publiques',
'PFBAuditingCommitteeCommunication': u'Communication gouvernementale',
'PFBChallenge': u'Interpellation',
'PFBCollegialNotification': u'Notifications du Collège',
'PFBCurrentEventsQuestion': u'''Question d'actualité''',
'PFBGeneralCommission': u'Commissions divers',
'PFBGeneralMail': u'Courrier général divers',
'PFBMeetingExcuses': u'Excusés',
'PFBOralRequest': u'Question orale',
'PFBProject': u'Projet',
'PFBProposal': u'Proposition',
'PFBSubventionRequest': u'Demande de subvention',
'PFBWriteAnswer': u'Réponse écrite',
'PFBWriteRequest': u'Question écrite',
}.get(doc.get('meta_type'))]
if ob.categorie_de_courrier is None:
print >> self.logfile, 'W: failed to get a category'
ob.expediteur = []
shipper_id = None
if doc.get('shippers'):
shipper = unicode(doc.get('shippers')[0], 'utf-8')
shipper_id = plone_tool.normalizeString(shipper)
if not portal.contacts.has_key(shipper_id):
portal.contacts.invokeFactory('themis.datatypes.contact', shipper_id, title=shipper)
shipper_id = 'contact:' + shipper_id
else:
if doc.get('meta_type') in ('PFBWriteAnswer', 'PFBProject'):
# shipper is a ministry
if doc.get('authors'):
shipper_id = self.get_ministry(doc.get('authors')[0])
else:
print >> self.logfile, 'W: document without an author'
elif doc.get('meta_type') in ('PFBChallenge',
'PFBCurrentEventsQuestion', 'PFBOralRequest',
'PFBProposal', 'PFBWriteRequest'):
# shipper is a deputy
if doc.get('authors'):
shipper_id = self.get_deputy(doc.get('authors')[0])
else:
print >> self.logfile, 'W: document without an author'
if shipper_id:
ob.expediteur.append(shipper_id)
if doc.get('type'):
ob.sous_categorie_de_courrier = {
'proj_decret': u'Projet de décret',
'proj_reglement': u'Projet de règlement',
'prop_decret': u'Proposition de décret',
'prop_reglement': u'Proposition de règlement',
'prop_resolution': u'Proposition de résolution',
'prop_modif_regl': u'Proposition de modification du Règlement du PFB',
'prop_modif_statut': u'Proposition de modification du statut du personnel'
}.get(doc.get('type'))
if doc.get('state') in ('filed', 'preDocumented'):
try:
workflow_tool.doActionFor(ob, 'publish')
except WorkflowException:
pass
except Exception, e:
print >> self.logfile, 'E: Exception %r' % e
raise
second_object_typename = {
'PFBChallenge': 'interpellationD',
'PFBWriteAnswer': 'reponse_a_question_ecriteD',
'PFBCurrentEventsQuestion': 'questionactualiteD',
'PFBOralRequest': 'QuestionoraleD',
'PFBProject': 'ProjetD',
}.get(doc.get('meta_type'))
if doc.get('meta_type') == 'PFBProposal':
second_object_typename = {
'prop_decret': 'proposition_decret_reglementD',
'prop_reglement': 'proposition_decret_reglementD',
'prop_resolution': 'proposition_resolution_modificationD',
'prop_modif_regl': 'proposition_resolution_modificationD',
'prop_modif_statut': 'proposition_resolution_modificationD'
}.get(doc.get('type'))
if second_object_typename is None:
print >> self.logfile, 'W: proposal of unknown type'
if second_object_typename:
# XXX: this is a temporary location, no decision has been taken yet
# on the location of those documents
docdir = getattr(self.context.aq_parent, 'documents-diffusables')
if not hasattr(docdir, doc.get('id')):
factory = DexterityFactory(portal_type=second_object_typename)
ob2 = factory(id=doc.get('id'), title=doc.get('title'))
docdir._setObject(ob2.id, ob2)
else:
ob2 = getattr(docdir, doc.get('id'))
ob2.date_reception = datetime.datetime.strptime(doc.get('deliverydate'), '%Y-%m-%d').date()
ob2.mail_ref_id = ob.numero_courrier
ob2.session = doc.get('session')
if second_object_typename == 'reponse_a_question_ecriteD':
if doc.get('authors'):
ob2.ministre_auteur_reponse = self.get_ministry(doc.get('authors')[0])
elif second_object_typename in ('interpellationD',
'questionactualiteD', 'QuestionoraleD'):
if doc.get('authors'):
ob2.auteur = [self.get_deputy(x) for x in doc.get('authors')]
if doc.get('recipients'):
ob2.ministres_concernes = [self.get_ministry(x) for x in doc.get('recipients')]
elif second_object_typename == 'ProjetD':
ob2.type_de_projet = ob.sous_categorie_de_courrier
if doc.get('authors'):
ob2.auteur = [self.get_ministry(x) for x in doc.get('authors')]
elif second_object_typename in ('proposition_decret_reglementD',
'proposition_resolution_modificationD'):
ob2.type_de_proposition = ob.sous_categorie_de_courrier
if doc.get('authors'):
ob2.auteur = [self.get_deputy(x) for x in doc.get('authors')]
if doc.get('state') in ('filed', 'preDocumented'):
try:
workflow_tool.doActionFor(ob2, 'publish')
except WorkflowException:
pass
except Exception, e:
print >> self.logfile, 'E: Exception(b) %r' % e
raise
return u'→ OK\n'
def get_ministry(self, author):
if not author:
return None
plone_tool = getToolByName(self.context, 'plone_utils')
portal = getToolByName(self.context, 'portal_url').getPortalObject()
author = unicode(author, 'utf-8')
author_id = plone_tool.normalizeString(author)
if author_id == 'college':
return 'ministry:college'
if not portal.ministres.has_key(author_id):
workflow_tool = getToolByName(self.context, 'portal_workflow')
try:
lastname, firstname = author.split(',')
except ValueError:
print >> self.logfile, 'E: ministry??? (%r)' % author
raise
portal.ministres.invokeFactory('themis.datatypes.ministry', author_id,
firstname=firstname.strip(),
lastname=lastname.strip())
ministry = getattr(portal.ministres, author_id)
try:
workflow_tool.doActionFor(ministry, 'publish')
except WorkflowException:
pass
return 'ministry:'+author_id
def get_deputy(self, author):
plone_tool = getToolByName(self.context, 'plone_utils')
portal = getToolByName(self.context, 'portal_url').getPortalObject()
author = unicode(author, 'utf-8')
if len(author.split()) > 3:
if not 'Warnaffe' in author and \
not 'Jonghe' in author:
print >> self.logfile, 'W: author with (too?) many parts: %r' % author
author_id = plone_tool.normalizeString(author)
if not portal.deputes.has_key(author_id):
workflow_tool = getToolByName(self.context, 'portal_workflow')
try:
lastname, firstname = author.split(',')
except ValueError:
print >> self.logfile, 'E: deputy??? (%r)' % author
raise
portal.deputes.invokeFactory('themis.datatypes.deputy', author_id,
firstname=firstname.strip(),
lastname=lastname.strip())
deputy = getattr(portal.deputes, author_id)
try:
workflow_tool.doActionFor(deputy, 'publish')
except WorkflowException:
pass
return 'deputy:'+author_id