def migrate(self): """ Migrate types and categories of bookables self is the booking center """ bookables = self.objectValues() charset = self.getCharset() count = 0 for bookable in bookables: try: if bookable.portal_type != "BookableObject": continue except AttributeError: ## external methods don't have any portal_type... continue oldCat = bookable.getCategory() cat = normalizeString(oldCat, encoding=charset) bookable.setCategory(cat) oldType = bookable.getType() type = normalizeString(oldType, encoding=charset) bookable.setType(type) count += 1 return "%d bookables updated\ndon't forget to update catalog..." % count
def membershipSearch(self, searchString='', searchUsers=True, searchGroups=True, ignore=[]): """Search for users and/or groups, returning actual member and group items Replaces the now-deprecated prefs_user_groups_search.py script""" groupResults = userResults = [] gtool = getToolByName(self, 'portal_groups') mtool = getToolByName(self, 'portal_membership') searchView = getMultiAdapter( (aq_inner(self.context), self.request), name='pas_search') if searchGroups: groupResults = searchView.merge(chain( *[searchView.searchGroups(**{field: searchString}) for field in ['id', 'title']]), 'groupid') groupResults = [gtool.getGroupById(g['id']) for g in groupResults if g[ 'id'] not in ignore] groupResults.sort(key=lambda x: x is not None and normalizeString( x.getGroupTitleOrName())) if searchUsers: userResults = searchView.merge(chain(*[searchView.searchUsers( **{field: searchString}) for field in ['login', 'fullname', 'email']]), 'userid') userResults = [mtool.getMemberById(u['id']) for u in userResults if u[ 'id'] not in ignore] userResults.sort(key=lambda x: x is not None and x.getProperty( 'fullname') is not None and normalizeString(x.getProperty('fullname')) or '') return groupResults + userResults
def membershipSearch(self, searchString="", searchUsers=True, searchGroups=True, ignore=None): """Search for users and/or groups, returning actual member and group items Replaces the now-deprecated prefs_user_groups_search.py script""" if ignore is None: ignore = [] groupResults = userResults = [] gtool = getToolByName(self, "portal_groups") mtool = getToolByName(self, "portal_membership") searchView = getMultiAdapter((aq_inner(self.context), self.request), name="pas_search") if searchGroups: groupResults = searchView.merge( chain(*[searchView.searchGroups(**{field: searchString}) for field in ["id", "title"]]), "groupid" ) groupResults = [gtool.getGroupById(g["id"]) for g in groupResults if g["id"] not in ignore] groupResults.sort(key=lambda x: x is not None and normalizeString(x.getGroupTitleOrName())) if searchUsers: userResults = searchView.merge( chain(*[searchView.searchUsers(**{field: searchString}) for field in ["name", "fullname", "email"]]), "userid", ) userResults = [mtool.getMemberById(u["id"]) for u in userResults if u["id"] not in ignore] userResults.sort( key=lambda x: x is not None and x.getProperty("fullname") is not None and normalizeString(x.getProperty("fullname")) or "" ) return groupResults + userResults
def render(self): res = { "title": self.title(), "url": self.heading_link_target(), "has_custom_name": bool(self.hasName()), "items": [], "available": self.available, } if not res["available"]: return res if self.include_top(): root = self.navigation_root() root_is_portal = self.root_is_portal() if root is None: root = self.urltool.getPortalObject() root_is_portal = True if utils.safe_hasattr(self.context, "getRemoteUrl"): root_url = root.getRemoteUrl() else: # cid, root_url = get_view_url(root) # cid = get_id(root) root_url = get_url(root) root_title = "Home" if root_is_portal else root.pretty_title_or_id( ) root_type = ("plone-site" if root_is_portal else utils.normalizeString(root.portal_type, context=root)) normalized_id = utils.normalizeString(root.Title(), context=root) if root_is_portal: state = "" else: state = api.content.get_state(root) res["items"].append({ "@id": root.absolute_url(), "description": root.Description() or "", "href": root_url, "icon": "", "is_current": bool(self.root_item_class()), "is_folderish": True, "is_in_path": True, "items": [], "normalized_id": normalized_id, "thumb": "", "title": root_title, "type": root_type, "review_state": state, }) res["items"].extend(self.createNavTree()) return res
def getSortableName(self): """Get the name in reverse for sorting""" name = None first = self.first_name last = self.last_name if first: name = normalizeString(last + " " + first) else: name = normalizeString(last) self.plone_log("SORTABLE NAME: %s (%s, %s)" % (name, first, last)) return name
def getMembers(self): searchResults = self.gtool.getGroupMembers(self.groupname) groupResults = [self.gtool.getGroupById(m) for m in searchResults] groupResults.sort(key=lambda x: x is not None and normalizeString( x.getGroupTitleOrName())) userResults = [self.mtool.getMemberById(m) for m in searchResults] userResults.sort(key=lambda x: x is not None and x.getProperty( 'fullname') is not None and normalizeString(x.getProperty('fullname')) or '') mergedResults = groupResults + userResults return [i for i in mergedResults if i]
def addDefinition(self, name, style, columns, id=None): """ Adds a new global definition """ for column in columns: column['name'] = normalizeString(column['name'], self.context) value = dumps({'columns': columns, 'style': style, 'name': name}) if id is None: id = normalizeString(name, self.context) if self.properties.hasProperty(id): self.properties._updateProperty(id, value) else: self.properties._setProperty(id, value)
def get_mimetype_icon_klass(item): # It just makes sense to guess the mimetype of documents if not item.portal_type == 'opengever.document.document': return 'contenttype-%s' % normalizeString(item.portal_type) icon = item.getIcon # Fallback for unknown file type if icon == '': return "icon-document_empty" # Strip '.gif' from end of icon name and remove # leading 'icon_' filetype = icon[:icon.rfind('.')].replace('icon_', '') return 'icon-%s' % normalizeString(filetype)
def render(self): res = { 'title': self.title(), 'url': self.heading_link_target(), 'has_custom_name': bool(self.hasName()), 'items': [], } if self.include_top(): root = self.navigation_root() if utils.safe_hasattr(self.context, 'getRemoteUrl'): root_url = root.getRemoteUrl() else: cid, root_url = get_view_url(root) root_is_portal = self.root_is_portal() root_title = ('Home' if root_is_portal else root.pretty_title_or_id()) root_type = ('plone-site' if root_is_portal else utils.normalizeString( root.portal_type, context=root)) normalized_id = utils.normalizeString(root.Title(), context=root) if root_is_portal: state = '' else: state = api.content.get_state(root) res['items'].append({ '@id': root.absolute_url(), 'description': root.Description() or '', 'href': root_url, 'icon': '', 'is_current': bool(self.root_item_class()), 'is_folderish': True, 'is_in_path': True, 'items': [], 'normalized_id': normalized_id, 'thumb': '', 'title': root_title, 'type': root_type, 'review_state': state, }) res['items'].extend(self.createNavTree()) return res
def iter_results(self): document = self._get_results() for result in document.findall(".//R"): mimetype = result.attrib.get("MIME") if mimetype: mimetype = mimetype.split("/")[1].upper() metadata = dict((result.attrib["N"], result.attrib["V"]) for result in result.xpath("MT")) portal_type = metadata.get("DC.type", "missing") normalized_type = normalizeString(portal_type) heading = result.xpath("T/text()") if heading: heading = heading[0] url = result.xpath("UD|U/text()")[0] description = result.xpath("S/text()") if description: description = description[0] yield { # standard fields "description": description or "", "heading": heading or url, "url": url, # standard Plone DC metadata "creator": metadata.get("DC.creator"), "normalized_content_type": "contenttype-" + normalized_type, # include metadata dictionary for 3rd party # customization "metadata": metadata, "mimetype": mimetype, }
def doSearch(self, searchString): mtool = getToolByName(self, 'portal_membership') searchView = getMultiAdapter((aq_inner(self.context), self.request), name='pas_search') if searchString: explicit_users = list() for user in searchView.searchUsers(account_locked=True): added = False for field in ['login', 'fullname', 'email']: if not added: if searchString in user.get(field, '').lower(): explicit_users.append(user) added = True else: explicit_users = searchView.searchUsers(account_locked=True) explicit_users = searchView.merge(explicit_users, 'userid') results = [] for user_info in explicit_users: userId = user_info['id'] user = mtool.getMemberById(userId) if user is None: continue user_info['password_date'] = \ user.getProperty('password_date', '2000/01/01') user_info['last_notification_date'] = \ user.getProperty('last_notification_date', '2000/01/01') user_info['fullname'] = user.getProperty('fullname', '') results.append(user_info) results.sort(key=lambda x: x is not None and x['fullname'] is not None and \ normalizeString(x['fullname']) or '') return results
def load_portalmembers(self, ignore=[]): """Return all members of the portal""" mtool = getToolByName(self.context, "portal_membership") searchString = self.request.form.get("searchstring") searchView = getMultiAdapter((aq_inner(self.context), self.request), name="pas_search") if self.request.form.get("form.button.FindAll"): userResults = searchView.searchUsers(sort_by="userid") elif self.request.form.get("form.button.Search") and searchString: userResults = searchView.merge( chain(*[ searchView.searchUsers(**{field: searchString}) for field in ["login", "fullname", "email"] ]), "userid", ) else: return [] userResults = [ mtool.getMemberById(u["id"]) for u in userResults if u["id"] not in ignore ] userResults.sort( key=lambda x: x is not None and x.getProperty("fullname") is not None and normalizeString(x.getProperty("fullname")) or "") return userResults
def render(self): context = aq_inner(self.context) pw = getToolByName(context, 'portal_workflow') templates = crearObjecte( context, 'templates', 'Folder', 'Templates', 'Plantilles per defecte administrades per l\'SCP.', constrains=(['Document'])) plantilles = crearObjecte( context, 'plantilles', 'Folder', 'Plantilles', 'En aquesta carpeta podeu posar les plantilles per ser usades a l\'editor.', constrains=(['Document'])) try: pw.doActionFor(templates, "restrict") except: pass for plt in get_plantilles(): plantilla = crearObjecte(templates, normalizeString(plt['titol']), 'Document', plt['titol'], plt['resum'], '') plantilla.setText(plt['cos'], mimetype="text/html") return "OK"
def addEvenementActeDocument(self, evenementActePk, dossierDisciplinairePk): """ ajout d'un fichier dans le localfs 'localfs_ism_event_act' comme document lié à une eventment acte """ ismTools = getMultiAdapter((self.context, self.request), name="manageISM") dateCreation = ismTools.getTimeStamp() auteurPk = self.getAuteurPk() fields = self.context.REQUEST evenementActeDocument = getattr(fields, 'fichierAttache') dossierDisciplinairePk = getattr(fields, 'dossierDisciplinairePk') lfsEventActe = getattr(self.context, 'localfs_ism_event_act') lfs = getattr(lfsEventActe, 'localfs_ism_event_act') filename, ext = os.path.splitext(evenementActeDocument.filename) normalizedFilename = normalizeString(filename, encoding='utf-8') nomFichier = '%s_dd%s_ev%s%s' % (normalizedFilename, dossierDisciplinairePk, evenementActePk, ext) lfs.manage_upload(evenementActeDocument, id=nomFichier) wrapper = getSAWrapper('cesstex') session = wrapper.session newEntry = EvenementActeDocument(eventactdoc_date_creation=dateCreation, eventactdoc_auteur_creation=auteurPk, eventactdoc_nom_fichier=nomFichier, eventactdoc_auteur_creation_fk=auteurPk, eventactdoc_eventact_fk=evenementActePk, eventactdoc_dossier_disciplinaire_fk=dossierDisciplinairePk) session.add(newEntry) session.flush() self.insertLogOperation('addEvenementActeDocument', auteurPk, dossierDisciplinairePk, evenementActePk)
def _roles(self): """plone.app.workflow SharingView original roles method, but with conditionally permission check. """ is_permission_check_disabled = IDisabledPermissionCheck.providedBy( self.request) context = self.context portal_membership = api.portal.get_tool('portal_membership') pairs = [] for name, utility in getUtilitiesFor(ISharingPageRole): permission = utility.required_permission if not is_permission_check_disabled and permission is not None: if not portal_membership.checkPermission(permission, context): continue # be friendly to utilities implemented without required_interface iface = getattr(utility, 'required_interface', None) if iface is not None and not iface.providedBy(context): continue pairs.append(dict(id=name, title=utility.title)) pairs.sort(key=lambda x: normalizeString( translate(x["title"], context=self.request))) return pairs
def _initVocabs(self, node): for objnode in node.getElementsByTagName('object'): filename = objnode.getAttribute('name') filepath = os.path.join(self.name, filename) data = self.environ.readDataFile(filepath) if filename.endswith('.vdex') or filename.endswith('.xml'): # VDEX file try: vdex = VDEXManager(data) except VDEXError, e: self._logger.error('Problem with vdex-file: %s' % filepath) raise vocabid = vdex.getVocabIdentifier() if not vocabid: vocabid = filename[:filename.rfind('.')] vocabname = vocabid if vocabname in self.context.objectIds(): self.context.manage_delObjects([vocabname]) try: self._logger.info( 'Import VDEX file %s with identifier %s' % \ (filename, vocabname)) self.context.invokeFactory('VdexFileVocabulary', vocabname) except BadRequest, e: self._logger.warning( 'Import VDEX file %s with identifier %s renamed as %s' % \ (filename, vocabid, vocabname)) vocabname = normalizeString(vocabid, context=self.context) if vocabname in self.context.objectIds(): self.context.manage_delObjects([vocabname]) self.context.invokeFactory('VdexFileVocabulary', vocabname) self.context[vocabname].importXMLBinding(data)
def getDefinition(self, name): """ Returns the definition """ name = normalizeString(name, self.context) definition = loads(self.properties.getProperty(name, dumps({'columns': [], 'style': ''}))) return definition
def iter_results(self): document = self._get_results() for result in document.findall('.//R'): metadata = dict( (result.attrib['N'], result.attrib['V']) for result in result.xpath('MT') ) portal_type = metadata.get('DC.type', 'missing') normalized_type = normalizeString(portal_type) heading = result.xpath('T/text()') if heading: heading = heading[0] url = result.xpath('UD|U/text()')[0] description = result.xpath('S/text()') if description: description = description[0] yield { # standard fields 'description': description or "", 'heading': heading or url, 'url': url, # standard Plone DC metadata 'creator': metadata.get('DC.creator'), 'normalized_content_type': 'contenttype-' + normalized_type, # include metadata dictionary for 3rd party # customization 'metadata': metadata, }
def create(self, kwargs, container, line): searchview = self.site.restrictedTraverse('searchparcels') for k, v in kwargs.iteritems(): searchview.context.REQUEST[k] = v #check if we can find a parcel in the db cadastre with these infos found = searchview.findParcel(**kwargs) if not found: found = searchview.findParcel(browseoldparcels=True, **kwargs) if len(found) == 1: kwargs['divisionCode'] = kwargs['division'] kwargs['division'] = kwargs['division'] else: self.logError( self, 'Too much parcels found or not enough parcels found', { 'kwargs': kwargs, 'search result': len(found) }) kwargs['id'] = ''.join( [''.join(cleanAndSplitWord(ref)) for ref in kwargs.values()]) kwargs['id'] = normalizeString(kwargs['id'].replace('/', '')) if kwargs['id'] in container.objectIds(): return None return super(ParcelFactory, self).create(kwargs, container=container, line=line)
def mapParcellings(self, line): title = self.getData('Lotis') parcelling_id = normalizeString(title) catalog = api.portal.get_tool('portal_catalog') brains = catalog(id=parcelling_id) parcelling_uids = [brain.getObject().UID() for brain in brains] return parcelling_uids
def map(self, line): licence = self.importer.current_containers_stack[-1] path_mapping = self.getValueMapping('documents_map') documents_path = '{base}/documents/{folder}/DOSSIERS/{id}/'.format( base=IMPORT_FOLDER_PATH, folder=path_mapping.get(licence.portal_type), id=licence.id[1:] ) documents_args = [] try: doc_names = os.listdir(documents_path) except: return documents_args for doc_name in doc_names: doc = open(documents_path + doc_name, 'rb') doc_content = doc.read() doc.close() doc_args = { 'portal_type': 'File', 'id': normalizeString(safe_unicode(doc_name)), 'title': doc_name, 'file': doc_content, } documents_args.append(doc_args) return documents_args
def person_sortable_title(obj): if obj.firstname is None: fullname = obj.lastname else: fullname = u"%s %s" % (obj.lastname, obj.firstname) return normalizeString(fullname, context=obj)
def mapId(self, line): name = '%s' % self.getData('Nom') if not name: name = '%s' % self.getData('Société') name = name.replace(' ', '').replace('-', '') contact_id = normalizeString(self.site.portal_urban.generateUniqueId(name)) return contact_id
def __call__(self, fname=None, fset=None): res = {'errmsg': ''} if fname is None: return json.dumps(res) form = self.context if hasattr(aq_base(form), 'form_instance'): form = form.form_instance if not hasattr(form, 'update'): return form.update() if getattr(form, "extractData", None): data, errors = form.extractData() else: return #if we validate a field in a group we operate on the group if fset is not None: try: fset = int(fset) # integer-indexed fieldset names form = form.groups[fset] except ValueError, TypeError: # try to match fieldset on group name _name = lambda g: getattr(g, '__name__', None) or g.label group_match = filter( lambda group: normalizeString(_name(group)) == fset, form.groups, ) if not group_match: raise ValueError('Fieldset specified, but not found.') form = group_match[0]
def load_portalmembers(self, ignore=[]): """Return all members of the portal""" mtool = getToolByName(self.context, 'portal_membership') searchString = self.request.form.get('searchstring') searchView = getMultiAdapter( (aq_inner(self.context), self.request), name='pas_search') if self.request.form.get('form.button.FindAll'): userResults = searchView.searchUsers(sort_by='userid') elif self.request.form.get('form.button.Search') and searchString: userResults = searchView.merge( chain( *[searchView.searchUsers( **{field: searchString} ) for field in ['login', 'fullname', 'email']] ), 'userid' ) else: return [] userResults = [mtool.getMemberById(u['id']) for u in userResults if u['id'] not in ignore] userResults.sort(key=lambda x: x is not None and x.getProperty( 'fullname') is not None and normalizeString( x.getProperty('fullname') ) or '') return userResults
def get_zip_filename(filename): zip_filename = None if '.' in filename: filenamepref, filenamesuf = ( ''.join(filename.split('.')[:-1]), ''.join(filename.split('.')[-1]) ) ufilename = '' try: ufilename = unicode(filenamepref) except: ufilename = unicode(filenamepref.decode('utf-8')) zip_filename = '%s%s%s' % ( ufilename, '.', filenamesuf) else: ufilename = '' try: ufilename = unicode(filename) except: ufilename = unicode(filename.decode('utf-8')) zip_filename = normalizeString( ufilename, encoding='utf-8') return zip_filename
def __call__(self, context): site = getSite() groups_tool = getToolByName(site, "portal_groups") is_zope_manager = getSecurityManager().checkPermission(ManagePortal, context) groups = groups_tool.listGroups() # Get group id, title tuples for each, omitting virtual group # 'AuthenticatedUsers' terms = [] for g in groups: if g.id == "AuthenticatedUsers": continue if "Manager" in g.getRoles() and not is_zope_manager: continue group_title = safe_unicode(g.getGroupTitleOrName()) if group_title != g.id: title = u"%s (%s)" % (group_title, g.id) else: title = group_title terms.append(SimpleTerm(g.id, g.id, title)) # Sort by title terms.sort(key=lambda x: normalizeString(x.title)) return SimpleVocabulary(terms)
def execute(self, context, data): RetrieveFieldData(self, data) groupstool = getToolByName(context, 'portal_groups') name = normalizeString(self.name, context) if not groupstool.getGroupById(name): groupstool.addGroup(name, self.roles) return context
def getVocabularyValues(self, values): """Get all vocabulary values if a vocabulary is defined""" for field in values.get(self.prefix + '.field').values(): field['values'] = OrderedDict() vocabulary = field.get('vocabulary', []) if not vocabulary: continue utility = queryUtility(IVocabularyFactory, vocabulary) if utility is None: logger.info("%s is missing, ignored." % vocabulary) continue translated = [] for item in utility(self.context): if isinstance(item.title, Message): title = translate(item.title, context=self.request) else: title = item.title translated.append((title, item.value)) translated = sorted( translated, key=lambda x: normalizeString(safe_unicode(x[0]))) for (title, value) in translated: field['values'][value] = {'title': title} return values
def roles(self): """Get a list of roles that can be managed. Returns a list of dicts with keys: - id - title """ context = self.context portal_membership = getToolByName(context, 'portal_membership') pairs = [] for name, utility in getUtilitiesFor(ISharingPageRole): permission = utility.required_permission if permission is not None: if not portal_membership.checkPermission(permission, context): continue # be friendly to utilities implemented without required_interface iface = getattr(utility, 'required_interface', None) if iface is not None and not iface.providedBy(context): continue pairs.append(dict(id = name, title = utility.title)) pairs.sort(key=lambda x: normalizeString(translate(x["title"], context=self.request))) return pairs
def __call__(self, context): site = getSite() groups_tool = getToolByName(site, 'portal_groups') is_zope_manager = getSecurityManager().checkPermission( ManagePortal, context) groups = groups_tool.listGroups() # Get group id, title tuples for each, omitting virtual group # 'AuthenticatedUsers' terms = [] for g in groups: if g.id == 'AuthenticatedUsers': continue if 'Manager' in g.getRoles() and not is_zope_manager: continue group_title = safe_unicode(g.getGroupTitleOrName()) if group_title != g.id: title = u'%s (%s)' % (group_title, g.id) else: title = group_title terms.append(SimpleTerm(g.id, g.id, title)) # Sort by title terms.sort(key=lambda x: normalizeString(x.title)) return SimpleVocabulary(terms)
def getVocabularyValues(self, values): """Get all vocabulary values if a vocabulary is defined""" for field in values.get(self.prefix + '.field').values(): field['values'] = OrderedDict() vocabulary = field.get('vocabulary', []) if not vocabulary: continue utility = queryUtility(IVocabularyFactory, vocabulary) if utility is None: logger.info("%s is missing, ignored." % vocabulary) continue translated = [] for item in utility(self.context): if isinstance(item.title, Message): title = translate(item.title, context=self.request) else: title = item.title translated.append((title, item.value)) translated = sorted( translated, key=lambda x: normalizeString(safe_unicode(x[0])) ) for (title, value) in translated: field['values'][value] = {'title': title} return values
def processInput(self, strValue): """ """ # only called in during validation if not strValue: return None strValue = normalizeString(strValue) return {strValue: 'application/unknow'}
def getGroups(self): groupResults = [ self.gtool.getGroupById(m) for m in self.gtool.getGroupsForPrincipal(self.member) ] groupResults.sort(key=lambda x: x is not None and normalizeString( x.getGroupTitleOrName())) return filter(None, groupResults)
def processInput(self, strValue): """ """ # only called in during validation if not strValue: return None strValue = normalizeString(strValue) return {strValue: 'application/unknown'}
def load_group_members(self, group): """Load member from a group""" if group: users = group.getGroupMembers() users.sort( key=lambda x: normalizeString(x.getProperty('fullname') or '')) return users return []
def mapId(self, line): name = '%s' % self.getData('Nom') if not name: name = '%s' % self.getData('Société') name = name.replace(' ', '').replace('-', '') contact_id = normalizeString( self.site.portal_urban.generateUniqueId(name)) return contact_id
def membershipSearch(self, searchString='', searchUsers=True, searchGroups=True, ignore=None): """Search for users and/or groups, returning actual member and group items Replaces the now-deprecated prefs_user_groups_search.py script""" if ignore is None: ignore = [] groupResults = userResults = [] gtool = getToolByName(self, 'portal_groups') mtool = getToolByName(self, 'portal_membership') searchView = getMultiAdapter((aq_inner(self.context), self.request), name='pas_search') if searchGroups: groupResults = searchView.merge( chain(*[ searchView.searchGroups(**{field: searchString}) for field in ['id', 'title'] ]), 'groupid') groupResults = [ gtool.getGroupById(g['id']) for g in groupResults if g['id'] not in ignore ] groupResults.sort(key=lambda x: x is not None and normalizeString( x.getGroupTitleOrName())) if searchUsers: userResults = searchView.merge( chain(*[ searchView.searchUsers(**{field: searchString}) for field in ['name', 'fullname', 'email'] ]), 'userid') userResults = [ mtool.getMemberById(u['id']) for u in userResults if u['id'] not in ignore ] userResults.sort( key=lambda x: x is not None and x.getProperty('fullname') is not None and normalizeString(x.getProperty('fullname')) or '') return groupResults + userResults
def mapId(self, line): parcel_id = '{ID}{part_1}{part_2}{part_3}'.format( ID=self.getData('ID'), part_1=self.getData('CODE_COMMU'), part_2=self.getData('REFDIREXT'), part_3=self.getData('SOUSCOM'), ) parcel_id = normalizeString(parcel_id) return parcel_id
def test_user_show_all(self): self.browser.open(self.usergroups_url) self.browser.getControl(name='form.button.FindAll').click() # Check that first 10 members (sorted by fullname) are shown. for member in sorted( self.members, key=lambda k: normalizeString(k['fullname']) )[:10]: self.assertIn(member['fullname'], self.browser.contents)
def execute(self, context, data): RetrieveFieldData(self, data) user = normalizeString(self.user, context) roles = dict(context.get_local_roles()).get(user, []) if not self.role in roles: roles.append(self.role) context.manage_addLocalRoles(user, tuple(roles)) context.reindexObjectSecurity() return context
def test_user_show_all(self): self.browser.open(self.usergroups_url) self.browser.getControl(name='form.button.FindAll').click() # Check that first 10 members (sorted by fullname) are shown. for member in sorted( self.members, key=lambda k: normalizeString(k['fullname']))[:10]: self.assertIn(member['fullname'], self.browser.contents)
def setfile( self, submittedValue, filename='', overwrite=False, contenttype='' ): """ Store `submittedValue` (assumed to be a file) as a blob (or FSS). The name is normalized before storing. Return the normalized name and the guessed content type. (The `contenttype` parameter is ignored.) """ # TODO: does the `contenttype` parameter exist for BBB? # If so, mention it. If not, can it go? if filename == '': filename = submittedValue.filename if filename: if """\\""" in filename: filename = filename.split("\\")[-1] filename = '.'.join( [normalizeString(s, encoding='utf-8') for s in filename.split('.')]) if overwrite and filename in self.objectIds(): self.deletefile(filename) try: self._checkId(filename) except BadRequest: # # If filename is a reserved id, we rename it # # Rather than risk dates going back in time when timezone is # changed, always use UTC. I.e. here we care more about # ordering and uniqueness than about the time (which can be # found elsewhere on the object). # filename = '%s_%s' % ( DateTime().toZone('UTC').strftime("%Y%m%d%H%M%S"), filename) if (isinstance(submittedValue, FileUpload) or type(submittedValue) == file): submittedValue.seek(0) contenttype = guessMimetype(submittedValue, filename) submittedValue = submittedValue.read() elif submittedValue.__class__.__name__ == '_fileobject': submittedValue = submittedValue.read() blob = BlobWrapper(contenttype) file_obj = blob.getBlob().open('w') file_obj.write(submittedValue) file_obj.close() blob.setFilename(filename) blob.setContentType(contenttype) self._setObject(filename, blob) return (filename, contenttype) else: return (None, "")
def person_sortable_title(obj): """ Override default sortable title to invert firstname and lastname """ if obj.firstname is None: fullname = obj.lastname else: fullname = u"%s %s" % (obj.firstname, obj.lastname) return normalizeString(fullname, context=obj)
def normalizeString(self, text): """Normalizes a title to an id. The relaxed mode was removed in Plone 4.0. You should use either the url or file name normalizer from the plone.i18n package instead. normalizeString() converts a whole string to a normalized form that should be safe to use as in a url, as a css id, etc. """ return utils.normalizeString(text, context=self)
def setfile(self, submittedValue, filename='', overwrite=False, contenttype=''): """ Store `submittedValue` (assumed to be a file) as a blob (or FSS). The name is normalized before storing. Return the normalized name and the guessed content type. (The `contenttype` parameter is ignored.) """ # TODO: does the `contenttype` parameter exist for BBB? # If so, mention it. If not, can it go? if filename == '': filename = submittedValue.filename if filename: if """\\""" in filename: filename = filename.split("\\")[-1] filename = '.'.join([ normalizeString(s, encoding='utf-8') for s in filename.split('.') ]) if overwrite and filename in self.objectIds(): self.deletefile(filename) try: self._checkId(filename) except BadRequest: # if filename is a reserved id, we rename it filename = '%s_%s' % (DateTime().toZone('UTC').strftime( "%Y%m%d%H%M%S"), filename) if HAS_BLOB: if (isinstance(submittedValue, FileUpload) or type(submittedValue) == file): submittedValue.seek(0) contenttype = guessMimetype(submittedValue, filename) submittedValue = submittedValue.read() elif submittedValue.__class__.__name__ == '_fileobject': submittedValue = submittedValue.read() try: blob = BlobWrapper(contenttype) except: # XXX Except what? # BEFORE PLONE 4.0.1 blob = BlobWrapper() file_obj = blob.getBlob().open('w') file_obj.write(submittedValue) file_obj.close() blob.setFilename(filename) blob.setContentType(contenttype) self._setObject(filename, blob) else: self.manage_addFile(filename, submittedValue) contenttype = self[filename].getContentType() return (filename, contenttype) else: return (None, "")
def normalizeString(self, text): """Normalizes a title to an id. The relaxed mode was removed in Plone 4.0. You should use either the url or file name normalizer from the plone.i18n package instead. normalizeString() converts a whole string to a normalized form that should be safe to use as in a url, as a css id, etc. >>> ptool = self.portal.plone_utils >>> ptool.normalizeString("Foo bar") 'foo-bar' >>> ptool.normalizeString("Some!_are allowed, others&?:are not") 'some-_are-allowed-others-are-not' >>> ptool.normalizeString("Some!_are allowed, others&?:are not") 'some-_are-allowed-others-are-not' all punctuation and spacing is removed and replaced with a '-': >>> ptool.normalizeString("a string with spaces") 'a-string-with-spaces' >>> ptool.normalizeString("p.u,n;c(t)u!a@t#i$o%n") 'p-u-n-c-t-u-a-t-i-o-n' strings are lowercased: >>> ptool.normalizeString("UppERcaSE") 'uppercase' punctuation, spaces, etc. are trimmed and multiples are reduced to just one: >>> ptool.normalizeString(" a string ") 'a-string' >>> ptool.normalizeString(">here's another!") 'heres-another' >>> ptool.normalizeString("one with !@#$!@#$ stuff in the middle") 'one-with-stuff-in-the-middle' the exception to all this is that if there is something that looks like a filename with an extension at the end, it will preserve the last period. >>> ptool.normalizeString("this is a file.gif") 'this-is-a-file-gif' >>> ptool.normalizeString("this is. also. a file.html") 'this-is-also-a-file-html' """ return utils.normalizeString(text, context=self)
def __call__(self, fname=None, fset=None): self.request.response.setHeader('Content-Type', 'application/json') res = {'errmsg': ''} if fname is None: return json.dumps(res) try: form = aq_base(self.context).form_instance except AttributeError: form = self.context try: aq_base(form).update() except (AttributeError, TypeError): return json.dumps(res) if getattr(form, 'extractData', None): data, errors = form.extractData() else: return json.dumps(res) # if we validate a field in a group we operate on the group if fset is not None: try: fset = int(fset) # integer-indexed fieldset names form = form.groups[fset] except (ValueError, TypeError): # try to match fieldset on group name def _name(group): return getattr(group, '__name__', group.label) group_match = list( filter( lambda group: normalizeString(_name(group)) == fset, form.groups, )) if not group_match: raise ValueError('Fieldset specified, but not found.') form = group_match[0] index = len(form.prefix) + len(form.widgets.prefix) raw_fname = fname[index:] validationError = None for error in errors: if error.widget == form.widgets.get(raw_fname, None): validationError = error.message break if isinstance(validationError, Message): validationError = translate(validationError, context=self.request) res['errmsg'] = validationError or '' return json.dumps(res)