def _readable_text(content_item): body = content_item.title + ' ' + content_item.description if content_item.text: html = content_item.text.raw transforms = api.portal.get_tool(name='portal_transforms') stream = transforms.convertTo('text/plain', html, mimetype='text/html') text = stream.getData().strip() body = safe_utf8(body) + ' ' + safe_utf8(text) return body
def _getFieldContent(self, field): tags = getattr(aq_base(self.context), field, None) if not tags: raise AttributeError if field not in self.context.contentIds(): encodedTags = tuple(safe_utf8(s) for s in tags) return encodedTags
def listCreators(self): # List Dublin Core Creator elements - resource authors. if self.creators is None: return () if six.PY2: return tuple(safe_utf8(c) for c in self.creators) return self.creators
def Subject(self): # Dublin Core Subject element - resource keywords. if self.subject is None: return () if six.PY2: return tuple(safe_utf8(s) for s in self.subject) return tuple(self.subject)
def get_media_container(self): container = None config_media_path = api.portal.get_registry_record( 'collective.behavior.relatedmedia.media_container_path') nav_root = api.portal.get_navigation_root(self.context) media_path = "{}{}".format( '/'.join(nav_root.getPhysicalPath()), config_media_path) try: container = self.context.restrictedTraverse(safe_utf8(media_path)) except: # try to create media folder container = nav_root for f_id in config_media_path.split('/'): if not f_id: continue if not hasattr(container, f_id): container = createContentInContainer( container, 'Folder', id=f_id, title=f_id, exclude_from_nav=True, checkConstraints=False) else: container = container[f_id] if container is None: container = aq_inner(self.context) while not IFolderish.providedBy(container): container = aq_parent(container) return container
def correct_archetypes(objects): for obj in objects: desc = safe_utf8(obj.Description()) obj.setDescription(desc) obj.description = desc obj.reindexObject() obj.processForm() transaction.commit() logger.info('{0} updated'.format(obj.absolute_url()))
def _get_uri_response(external_link_obj, timeout): """This is the multiprocessing worker function. It has to be top level in order to be pickable. """ logger = logging.getLogger(LOGGER_NAME) logger.info( safe_utf8(u'Head request to {}'.format(external_link_obj.link_target))) headers = { 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36' } error = None response = None start_time = _millis() try: response = requests.head(safe_utf8(external_link_obj.link_target), timeout=timeout, headers=headers, allow_redirects=False, verify=False) except requests.exceptions.Timeout: error = 'Timeout' except requests.exceptions.TooManyRedirects: error = 'Too many redirects' except requests.exceptions.ConnectionError: error = 'Connection Error' except Exception as e: error = e.message time = _millis() - start_time if response and response.status_code == 200 \ or 'resolveuid' in external_link_obj.link_target: external_link_obj.is_broken = False else: external_link_obj.is_broken = True external_link_obj.status_code = getattr(response, 'status_code', None) external_link_obj.content_type = headers.get('Content-Type', None) external_link_obj.response_time = time external_link_obj.error_message = error return external_link_obj
def __init__(self, context, request, view, manager, data): self.context = context self.data = data self.request = request tags = getTagRootTags(context) tag_root = getInterfaceRoot(context, ITagRoot) root_path ='/'.join(tag_root.getPhysicalPath()) catalog_tool = getToolByName(context, "portal_catalog") query = {} query['object_provides'] = ITaggable.__identifier__ query['path'] = root_path tag_occurrence = {} for tag in tags: query['tags'] = safe_utf8(tag) tag_occurrence[tag] = len(catalog_tool(query)) weight_list = tag_occurrence.values() weight_list.sort() if weight_list: minimal = weight_list[:1][0] maximal = weight_list[-1:][0] maxsize = float(self.data.maxsize) minsize = float(self.data.minsize) tag_cloud = [] for tag in tags: try: size = float((maxsize * \ (tag_occurrence[tag] - minimal))) / \ float((maximal - minimal)) except ZeroDivisionError: size = 1 if tag_occurrence[tag] <= minimal or size < minsize: size = float(self.data.minsize) info = dict(title=tag, font_size=round(size, 1)) tag_cloud.append(info) tag_cloud.sort(lambda x, y: cmp(x['title'].lower(), y['title'].lower())) self.tag_cloud = tag_cloud else: self.tag_cloud = [] self.tag_root_url = tag_root.absolute_url()
def get_link(self, link): internal_link = link['internal_link'] external_link = link['external_url'] if internal_link: try: internal_link = api.portal.get().unrestrictedTraverse( safe_utf8(internal_link.lstrip('/'))) except KeyError: internal_link = None if internal_link: internal_link = internal_link.absolute_url() return external_link or internal_link
def complete_information_with_internal_path(self, obj_having_path, path): # relation not broken if possible to traverse to try: path = urlparse( safe_utf8(path).rstrip('/view').rstrip('/download')).path storage = getUtility(IRedirectionStorage) path = storage.get(path, path) api.portal.get().unrestrictedTraverse(path) self.is_broken = False self.is_internal = True except Exception: self.is_broken = True self.is_internal = True self.link_origin = '/'.join(obj_having_path.getPhysicalPath()) self.source_state = self.get_review_state(obj_having_path) self.link_target = path self.creator = obj_having_path.Creator()
def filtered_result(self, **kwargs): if 'default_values' in kwargs: fdata = kwargs.pop('default_values') else: fdata = self.default_values pquery = kwargs.pop('pquery', self.parsed_query) # Subject Index has to be utf8 encoded if 'Subject' in pquery: subjects = pquery['Subject'].get('query', []) if not isinstance(subjects, list): subjects = list(subjects) pquery['Subject'] = dict(query=[safe_utf8(s) for s in subjects]) try: return self.filtered_query(pquery, fdata, kwargs.get('batch', False), kwargs.get('b_size', 100), kwargs.get('b_start', 0)) except Exception, msg: logger.info("Could not apply filtered search: %s, %s %s", msg, fdata, pquery)
def send_feedback(self, email_subject, email_message, receiver_email_address, xlsx_file_content, file_name): """Send an email including an excel workbook attached. """ mh = plone.api.portal.get_tool('MailHost') from_name = self.PLONE.getProperty('email_from_name', '') from_email = self.PLONE.getProperty('email_from_address', '') sender = 'Linkcheck Reporter' recipient = from_email msg = MIMEMultipart() msg['From'] = "%s <%s>" % (from_name, from_email) msg['reply-to'] = "%s <%s>" % (sender, recipient) msg['To'] = receiver_email_address msg['Date'] = formatdate(localtime=True) msg['Subject'] = Header(email_subject, 'windows-1252') msg.attach( MIMEText(email_message.encode('windows-1252'), 'plain', 'windows-1252')) part = MIMEBase('application', "octet-stream") part.set_payload(xlsx_file_content) encoders.encode_base64(part) part.add_header('Content-Disposition', 'attachment; filename="' + file_name + '"') msg.attach(part) try: mh.send(msg, mto=receiver_email_address, mfrom=from_email, immediate=True) self.logger.info( safe_utf8(u'Sent email to {}'.format(receiver_email_address))) except SMTPServerDisconnected: self.logger.error( 'Email could not be sent. SMTP connection could not be established.' )
def listCreators(self): # List Dublin Core Creator elements - resource authors. if self.creators is None: return () return tuple(safe_utf8(c) for c in self.creators)
def Rights(self): # Dublin Core Rights element - resource copyright. return safe_utf8(self.rights)
def listContributors(self): # Dublin Core Contributor elements - resource collaborators. return tuple(safe_utf8(c) for c in self.contributors)
def Title(self): return safe_utf8(self.title or self.fallback_title)
def data_geojson(self): style = {} global_style = utils.get_feature_styles(self.context) style['fill'] = global_style['polygoncolor'] style['stroke'] = global_style['linecolor'] style['width'] = global_style['linewidth'] if global_style.get('marker_image', None): img = get_marker_image(self.context, global_style['marker_image']) style['image'] = img else: style['image'] = None json_result = [] self.pc = api.portal.get_tool('portal_catalog') for contact in self.get_contacts(): brain = self.pc.unrestrictedSearchResults(UID=contact.UID())[0] if contact.use_parent_address: brain = self.pc.unrestrictedSearchResults( UID=contact.aq_parent.UID())[0] if brain.zgeo_geometry == Missing.Value: continue if brain.collective_geo_styles == Missing.Value: continue if brain.collective_geo_styles.get('use_custom_styles', False) and \ brain.collective_geo_styles.get('marker_image', None): img = get_marker_image(self.context, brain.collective_geo_styles['marker_image']) style['image'] = img geom = {'type': brain.zgeo_geometry['type'], 'coordinates': brain.zgeo_geometry['coordinates']} if geom['coordinates']: if geom['type']: classes = geom['type'].lower() + ' ' else: classes = '' address = get_address(contact) number = '' if address.get('number', None): number = ', {0}'.format(address['number']) formated_address = '{0}{1}<br />{2} {3}'.format( safe_utf8(address.get('street') or ''), number, address.get('zip_code') or '', safe_utf8(address.get('city') or '') ) img = '' if self.context.see_logo_in_popup: acc = getattr(contact, 'logo', None) if acc and acc.filename: img = '{0}/@@images/logo/thumb'.format( contact.absolute_url() ) classes += brain.getPath().split('/')[-2].replace('.', '-') json_result.append( geojson.Feature( id=contact.id.replace('.', '-'), geometry=as_shape(geom), style=style, properties={ 'title': brain.Title, 'description': brain.Description, 'style': style, 'url': brain.getURL(), 'classes': classes, 'image': img, 'address': formated_address })) feature_collection = geojson.FeatureCollection(json_result) feature_collection.update({'title': self.context.title}) return geojson.dumps(feature_collection)
def is_view_available(self, name): return self.context.restrictedTraverse(safe_utf8(name), None) is not None
def data_geojson(self): style = {} global_style = utils.get_feature_styles(self.context) style["fill"] = global_style["polygoncolor"] style["stroke"] = global_style["linecolor"] style["width"] = global_style["linewidth"] if global_style.get("marker_image", None): img = get_marker_image(self.context, global_style["marker_image"]) style["image"] = img else: style["image"] = None json_result = [] self.pc = api.portal.get_tool("portal_catalog") for contact in self.get_contacts(): brain = self.pc.unrestrictedSearchResults(UID=contact.UID())[0] if contact.use_parent_address: brain = self.pc.unrestrictedSearchResults(UID=contact.aq_parent.UID())[ 0 ] if brain.zgeo_geometry == Missing.Value: continue if brain.collective_geo_styles == Missing.Value: continue if brain.collective_geo_styles.get( "use_custom_styles", False ) and brain.collective_geo_styles.get("marker_image", None): img = get_marker_image( self.context, brain.collective_geo_styles["marker_image"] ) style["image"] = img geom = { "type": brain.zgeo_geometry["type"], "coordinates": brain.zgeo_geometry["coordinates"], } if geom["coordinates"]: if geom["type"]: classes = geom["type"].lower() + " " else: classes = "" address = get_address(contact) number = "" if address.get("number", None): number = ", {0}".format(address["number"]) formated_address = "{0}{1}<br />{2} {3}".format( safe_utf8(address.get("street") or ""), number, address.get("zip_code") or "", safe_utf8(address.get("city") or ""), ) img = "" if self.context.see_logo_in_popup: acc = getattr(contact, "logo", None) if acc and acc.filename: img = "{0}/@@images/logo/thumb".format(contact.absolute_url()) classes += brain.getPath().split("/")[-2].replace(".", "-") json_result.append( geojson.Feature( id=contact.id.replace(".", "-"), geometry=as_shape(geom), style=style, properties={ "title": brain.Title, "description": brain.Description, "style": style, "url": brain.getURL(), "classes": classes, "image": img, "address": formated_address, }, ) ) feature_collection = geojson.FeatureCollection(json_result) feature_collection.update({"title": self.context.title}) return geojson.dumps(feature_collection)
def safe_subject_encode(self, k, v): return k == 'Subject' and safe_utf8(v) or safe_unicode(v)
def _getFieldContent(self, field): tags = getattr(self.context, field) if not tags: return "" encodedTags = tuple(safe_utf8(s) for s in tags) return encodedTags
def listContributors(self): # Dublin Core Contributor elements - resource collaborators. if six.PY2: return tuple(safe_utf8(c) for c in self.contributors) return tuple(self.contributors)
def keyword_indexer(obj): if obj.subjects is None: return () return tuple(safe_utf8(s) for s in obj.subjects)
def Subject(self): # Dublin Core Subject element - resource keywords. if self.subject is None: return () return tuple(safe_utf8(s) for s in self.subject)
def Rights(self): # Dublin Core Rights element - resource copyright. if six.PY2: return safe_utf8(self.rights) return self.rights
def taxonomy_filter(self): if len(self.sub_path): requested_subject = self.traverse_subpath[0] self.subject = safe_utf8(requested_subject) return self.subject return None