def parse_organization_sheet(self, sheet, errors): organizations = {} for row in range(1, sheet.nrows): try: values = [cell.value.strip() for cell in sheet.row(row)[:-1]] values = values + [str(sheet.row(row)[-1].value)] assert len(values) == len(TITLES_ORGANIZATION) assert values[0] not in organizations organization = {} for index, field_ in enumerate(FIELDS_ORGANIZATION): organization[field_] = values[2 + index] children = [id.strip() for id in values[1].split(',') if id] organization['_children'] = children organizations[values[0]] = organization except: errors.append(_(u'Invalid row ${row} in sheet ${sheet}', mapping={u'row': row, u'sheet': sheet.name})) found = 0 start = None for key, value in organizations.iteritems(): if sheet.name == value['title']: start = key found += 1 if found != 1: errors.append(_(u'Top level organizations not found')) else: organizations['first'] = start return organizations
class IMembership(IPeopleMembership): start = schema.TextLine(title=_(u"Start of membership"), required=False) prefix = schema.TextLine(title=_(u"Prefix"), required=False) postfix = schema.TextLine(title=_(u"Postfix"), required=False)
def parse_register_sheet(self, sheet, errors): members = [] for row in range(1, sheet.nrows): try: values = [cell.value.strip() for cell in sheet.row(row)] assert len(values) == len(TITLES_REGISTER) member = {} for index, field_ in enumerate(FIELDS_REGISTER): member[field_] = values[index] pattern = re.compile( r'\((.*)\)\((.*)\)\((.*)\)\((.*)\)\((.*)\)') strings = [ membership.strip() for membership in values[len(FIELDS_REGISTER)].split('//') if membership ] memberships = [pattern.match(str).groups() for str in strings] member['_memberships'] = memberships members.append(member) except Exception: errors.append( _(u'Invalid row ${row} in sheet ${sheet}', mapping={ u'row': row, u'sheet': sheet.name })) return members
def parse_register_sheet(self, sheet, errors): members = [] for row in range(1, sheet.nrows): try: values = [cell.value.strip() for cell in sheet.row(row)] assert len(values) == len(TITLES_REGISTER) member = {} for index, field_ in enumerate(FIELDS_REGISTER): member[field_] = values[index] pattern = re.compile( r'\((.*)\)\((.*)\)\((.*)\)\((.*)\)\((.*)\)' ) strings = [ membership.strip() for membership in values[len(FIELDS_REGISTER)].split('//') if membership ] memberships = [pattern.match(str).groups() for str in strings] member['_memberships'] = memberships members.append(member) except Exception: errors.append(_(u'Invalid row ${row} in sheet ${sheet}', mapping={u'row': row, u'sheet': sheet.name})) return members
def get_print_date_text(self): date_text = api.portal.get_localized_time( datetime=datetime.combine( date.today(), datetime.min.time() ) ) return self.translate( _(u'Print date: ${date}', mapping={'date': date_text}) )
def import_xls(self, action): """ Create and handle form button.""" # Extract form field values and errors from HTTP request data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: io = StringIO(data["xls_file"].data) workbook = xlrd.open_workbook(file_contents=io.read()) except (KeyError, TypeError, xlrd.XLRDError): self.abort_action(action, (_(u'Invalid XLS file'), )) return errors = self._import_xls(workbook) if errors: self.abort_action(action, errors) else: self.status = _(u'Items imported. Please reindex.')
def import_xls(self, action): """ Create and handle form button.""" # Extract form field values and errors from HTTP request data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: io = StringIO(data["xls_file"].data) workbook = xlrd.open_workbook(file_contents=io.read()) except (KeyError, TypeError, xlrd.XLRDError): self.abort_action(action, (_(u'Invalid XLS file'),)) return errors = self._import_xls(workbook) if errors: self.abort_action(action, errors) else: self.status = _(u'Items imported. Please reindex.')
def parse_organization_sheet(self, sheet, errors): organizations = {} for row in range(1, sheet.nrows): try: values = [cell.value.strip() for cell in sheet.row(row)[:-1]] values = values + [str(sheet.row(row)[-1].value)] assert len(values) == len(TITLES_ORGANIZATION) assert values[0] not in organizations organization = {} for index, field_ in enumerate(FIELDS_ORGANIZATION): organization[field_] = values[2 + index] children = [id.strip() for id in values[1].split(',') if id] organization['_children'] = children organizations[values[0]] = organization except: errors.append( _(u'Invalid row ${row} in sheet ${sheet}', mapping={ u'row': row, u'sheet': sheet.name })) found = 0 start = None for key, value in organizations.iteritems(): if sheet.name == value['title']: start = key found += 1 if found != 1: errors.append(_(u'Top level organizations not found')) else: organizations['first'] = start return organizations
class ImportView(form.Form): fields = field.Fields(IImportSchema) grok.context(IPloneSiteRoot) grok.require('cmf.ManagePortal') grok.name('import-agencies') ignoreContext = True def abort_action(self, action, messages): """ Aborts the given action and adds the list of messages as error-widgets to the form.""" form = action.form formcontent = form.getContent() request = action.request for msg in messages: args = (Invalid(msg), request, None, None, form, formcontent) err = InvalidErrorViewSnippet(*args) err.update() form.widgets.errors += (err, ) form.status = form.formErrorsMessage transaction.abort() @buttonAndHandler(_(u'Import'), name='import') def import_xls(self, action): """ Create and handle form button.""" # Extract form field values and errors from HTTP request data, errors = self.extractData() if errors: self.status = self.formErrorsMessage return try: io = StringIO(data["xls_file"].data) workbook = xlrd.open_workbook(file_contents=io.read()) except (KeyError, TypeError, xlrd.XLRDError): self.abort_action(action, (_(u'Invalid XLS file'), )) return errors = self._import_xls(workbook) if errors: self.abort_action(action, errors) else: self.status = _(u'Items imported. Please reindex.') def parse_organization_sheet(self, sheet, errors): organizations = {} for row in range(1, sheet.nrows): try: values = [cell.value.strip() for cell in sheet.row(row)[:-1]] values = values + [str(sheet.row(row)[-1].value)] assert len(values) == len(TITLES_ORGANIZATION) assert values[0] not in organizations organization = {} for index, field_ in enumerate(FIELDS_ORGANIZATION): organization[field_] = values[2 + index] children = [id.strip() for id in values[1].split(',') if id] organization['_children'] = children organizations[values[0]] = organization except: errors.append( _(u'Invalid row ${row} in sheet ${sheet}', mapping={ u'row': row, u'sheet': sheet.name })) found = 0 start = None for key, value in organizations.iteritems(): if sheet.name == value['title']: start = key found += 1 if found != 1: errors.append(_(u'Top level organizations not found')) else: organizations['first'] = start return organizations def parse_register_sheet(self, sheet, errors): members = [] for row in range(1, sheet.nrows): try: values = [cell.value.strip() for cell in sheet.row(row)] assert len(values) == len(TITLES_REGISTER) member = {} for index, field_ in enumerate(FIELDS_REGISTER): member[field_] = values[index] pattern = re.compile( r'\((.*)\)\((.*)\)\((.*)\)\((.*)\)\((.*)\)') strings = [ membership.strip() for membership in values[len(FIELDS_REGISTER)].split('//') if membership ] memberships = [pattern.match(str).groups() for str in strings] member['_memberships'] = memberships members.append(member) except Exception: errors.append( _(u'Invalid row ${row} in sheet ${sheet}', mapping={ u'row': row, u'sheet': sheet.name })) return members def publish_content(self, content): wftool = getToolByName(self.context, 'portal_workflow') with unrestricted.run_as('Manager'): for action in KNOWN_PUBLISH_ACTIONS: try: wftool.doActionFor(content, action) break except WorkflowException: pass def create_organisation(self, context, id, organizations, memberships, count=0): organization = organizations[id] kwargs = dict((key, unicode(value)) for (key, value) in organization.iteritems() if not key.startswith('_')) da = kwargs['display_alphabetically'] if da == u'0' or da == u'0.0': kwargs['display_alphabetically'] = False else: kwargs['display_alphabetically'] = True content = createContentInContainer(context, "seantis.agencies.organization", **kwargs) self.publish_content(content) count += 1 log.info('added organization %s (%i/%i)' % (organization['title'], count, len(organizations) - 1)) if id in memberships: for membership in sorted(memberships[id], key=lambda i: i[3]): person = RelationValue(membership[4]) m_content = createContentInContainer( content, "seantis.agencies.membership", role=unicode(membership[0]), start=unicode(membership[1]), prefix=unicode(membership[2]), person=person) self.publish_content(m_content) log.info('added %s to %s' % (person.to_object.title, organization['title'])) for child in organization['_children']: count = self.create_organisation(content, child, organizations, memberships, count) return count def _import_xls(self, workbook): errors = [] organizations = {} registers = {} for sheet in workbook.sheets(): titles = [] if sheet.nrows > 1: titles = [cell.value for cell in sheet.row(0)] if titles == TITLES_ORGANIZATION: organizations[sheet.name] = self.parse_organization_sheet( sheet, errors) elif titles == TITLES_REGISTER: registers[sheet.name] = self.parse_register_sheet( sheet, errors) else: log.warn('ignored sheet %s' % sheet.name) if errors: return errors intids = getUtility(IIntIds) memberships = {} for name, members in registers.iteritems(): directory = createContentInContainer(self.context, "seantis.people.list", title=name) self.publish_content(directory) log.info('added directory %s' % name) for index, member in enumerate(members): kwargs = dict((key, unicode(value)) for (key, value) in member.iteritems() if not key.startswith('_')) content = createContentInContainer(directory, "seantis.agencies.member", exclude_from_nav=True, **kwargs) self.publish_content(content) uid = intids.getId(content) log.info('added person %s %s (%i/%i)' % (member['firstname'], member['lastname'], index + 1, len(members))) for membership in member['_memberships']: if membership[0] not in memberships: memberships[membership[0]] = [] memberships[membership[0]].append( (membership[1], membership[2], membership[3], membership[4], uid)) for name, organization in organizations.iteritems(): self.create_organisation(self.context, organization['first'], organization, memberships) log.info('import finished')
class IImportSchema(form.Schema): xls_file = NamedFile(title=_(u"XLS file"))
def get_print_date_text(self): date_text = api.portal.get_localized_time( datetime=datetime.combine(date.today(), datetime.min.time())) return self.translate( _(u'Print date: ${date}', mapping={'date': date_text}))
class IOrganization(form.Schema): searchable('title') title = schema.TextLine(title=_(u'Title')) searchable('description') description = schema.Text(title=_(u'Description'), required=False) searchable('portrait') form.widget(portrait=WysiwygFieldWidget) portrait = schema.Text(title=_(u'Portrait'), required=False) display_alphabetically = schema.Bool( title=_(u'Display memberships alphabetically'), description=_( u'Tick this box to sort memberships alphabetically by name rather ' u'than by their position in the folder.'), default=False) export_fields = schema.List( title=_(u"Fields to export"), description=_(u"Fields to include in the PDF export"), required=False, value_type=schema.Choice(vocabulary=SimpleVocabulary(terms=[ SimpleTerm(value=u'role', title=_('Role')), SimpleTerm(value=u'title', title=_('Last Name First Name')), SimpleTerm(value=u'start', title=_('Start of membership')), SimpleTerm(value=u'postfix', title=_('Postfix')), SimpleTerm(value=u'lastname', title=_('Last Name')), SimpleTerm(value=u'firstname', title=_('First Name')), SimpleTerm(value=u'year', title=_('Year')), SimpleTerm(value=u'academic_title', title=_('Academic Title')), SimpleTerm(value=u'occupation', title=_('Occupation')), SimpleTerm(value=u'address', title=_('Address')), SimpleTerm(value=u'political_party', title=_('Political Party')), SimpleTerm(value=u'phone', title=_('Phone')), SimpleTerm(value=u'direct_number', title=_('Direct number')), ])), default=['role', 'title']) organigram = NamedImage(title=_(u'Organigram'), required=False)