from Products.CMFCore.WorkflowCore import WorkflowException from Products.Five.browser import BrowserView from z3c.sqlalchemy import getSAWrapper, createSAWrapper from Products.CMFCore.utils import getToolByName from zope import component from zope.app.intid.interfaces import IIntIds from z3c.relationfield import RelationValue class SyncDeputiesFromPcfDbView(BrowserView): def __call__(self): portal = getToolByName(self.context, 'portal_url').getPortalObject() plone_tool = getToolByName(self.context, 'plone_utils') intids = component.getUtility(IIntIds) workflowTool = getToolByName(self.context, 'portal_workflow') c = portal.db._wrapper.connection cursor = c.cursor() cursor.execute('''SELECT t_pers.id, t_pers.nom, prenom, sexe, datenaiss, t_comppol.abbr, t_arrond.nom FROM t_pers, t_pershistoline, t_comppol, t_parl, t_arrond WHERE t_pers.st = 'S_PARL' and t_pers.prenom is not NULL and t_pers.id = t_pershistoline.pers and t_pershistoline.description = t_comppol.id and t_pershistoline.type = 'P_CMPL' and t_pershistoline.fin IS NULL and t_pers.id = t_parl.id and t_parl.arrond = t_arrond.id ''') polgroup_ids = {} while True: row = cursor.fetchone() if row is None: break pers_id, lastname, firstname, sex, birthdate, polgroup, district = row fullname = '%s %s' % (firstname, lastname) new_id = plone_tool.normalizeString(fullname) if not hasattr(self.context, new_id): self.context.invokeFactory('themis.datatypes.deputy', new_id, firstname=firstname, lastname=lastname) object = getattr(self.context, new_id) object.firstname = firstname object.lastname = lastname object.sex = sex object.birthdate = birthdate object.district = district if not polgroup in polgroup_ids: polgroup_id = plone_tool.normalizeString(polgroup) polgroupdir = getattr(portal, 'groupes-politiques') if not hasattr(polgroupdir, polgroup_id): polgroupdir.invokeFactory('themis.datatypes.polgroup', polgroup_id, title=polgroup) try: workflowTool.doActionFor(getattr(polgroupdir, polgroup_id), 'publish') except WorkflowException: pass polgroup_intid = intids.getId(getattr(polgroupdir, polgroup_id)) polgroup_ids[polgroup] = polgroup_intid object.polgroup = RelationValue(polgroup_ids.get(polgroup)) try: workflowTool.doActionFor(object, 'publish') except WorkflowException: pass #self.request.response.redirect('./')