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

290 lines
13 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
try:
from plone.namedfile.file import NamedBlobFile as NamedFile
except ImportError:
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:
try:
fd = cStringIO.StringIO(self.request.form['data'])
except KeyError:
return 'Missing data\n'
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'):
for shipper in doc.get('shippers'):
shipper = unicode(shipper, '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
ob.expediteur.append(shipper_id)
else:
if doc.get('meta_type') in ('PFBWriteAnswer', 'PFBProject'):
# shipper is a ministry
if doc.get('authors'):
for author in doc.get('authors'):
shipper_id = self.get_ministry(author)
ob.expediteur.append(shipper_id)
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'):
for author in doc.get('authors'):
shipper_id = self.get_deputy(author)
ob.expediteur.append(shipper_id)
else:
print >> self.logfile, 'W: document without an author'
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 not doc.get('authors') and doc.get('state') == 'initial':
# this document was not completed, do not create a secondary object
second_object_typename = None
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)
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'):
if len(doc.get('authors')) > 1:
print >> self.logfile, 'W: answer to written question with more than one author'
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'):
if len(doc.get('authors')) > 1:
print >> self.logfile, 'W: project with more than one author'
ob2.auteur = [self.get_ministry(x) for x in doc.get('authors')][0]
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.auteurs = [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 = {
u'de Donn\xe9a Fran\xe7ois-Xavier': u'de Donn\xe9a, Fran\xe7ois-Xavier',
u'Doulkeridis Christos': u'Doulkeridis, Christos',
}.get(author, author)
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')
author = {
u'Brotchi Jacques': u'Brothi, Jacques',
u'De Bock Emmanuel': u'De Bock, Emmanuel',
u'Defoss\xe9 Jean-Claude': u'Defoss\xe9, Jean-Claude',
u"d'Ursel Anne Charlotte": u"d'Ursel, Anne Charlotte",
u'El Khannouss Ahmed': u'El Khannouss, Ahmed',
u'Ikazban Jamal': u'Ikazban, Jamal',
u'Lurquin Vincent': u'Lurquin, Vincent',
u'Mandaila Gis\xe8le': u'Mandaila, Gis\xe8le',
u'Maron Alain': u'Maron, Alain',
u'Migisha Pierre': u'Migisha, Pierre',
u'Morel Jacques': u'Morel, Jacques',
u'Mouhssin Ahmed': u'Mouhssin, Ahmed',
u'Moureaux Catherine': u'Moureaux, Catherine',
u'Ouriaghli Mohamed': u'Ouriaghli, Mohamed',
u'Ozdemir Mahinur': u'Ozdemir, Mahinur',
u'Pinxteren Arnaud': u'Pinxteren, Arnaud',
u'Sessler Patrick': u'Sessler, Patrick',
u'Sidibe Fatoumata': u'Sidibe, Fatoumata',
u'Trachte Barbara': u'Trachte, Barbara',
u'Van Goidsenhoven Ga\xebtan': u'Van Goidsenhoven, Ga\xebtan',
u'Vanhalewyn Vincent': u'Vanhalewyn, Vincent',
u'Willame Boonen Magdeleine': u'Willame Boonen, Magdeleine',
u'Govers, Pascale, ': u'Govers, Pascale',
}.get(author, 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