def setUpPloneSite(self, portal): from plone.formwidget.geolocation.geolocation import Geolocation applyProfile(portal, "plone.app.mosaic:default") applyProfile(portal, "collective.geolocationbehavior:default") applyProfile(portal, "collective.collectionfilter:default") applyProfile(portal, "collective.collectionfilter.tests:testing") catalog = api.portal.get_tool(name="portal_catalog") if "exclude_from_nav" not in catalog.indexes(): catalog.addIndex( "exclude_from_nav", BooleanIndex("exclude_from_nav"), ) with api.env.adopt_roles(["Manager"]): portal.invokeFactory( "Collection", id="testcollection", title=u"Test Collection", query=[{ "i": "portal_type", "o": "plone.app.querystring.operation.selection.any", "v": ["Document", "Event"], }], ) if six.PY2: now = datetime.now() else: now = datetime.now(pytz.UTC) portal.invokeFactory( "Event", id="testevent", title=u"Test Event", start=now + timedelta(days=1), end=now + timedelta(days=2), subject=[u"Süper", u"Evänt"], exclude_from_nav=False, ) portal.invokeFactory( "Document", id="testdoc", title=u"Test Document and Document 😉", text=RichTextValue(u"Ein heißes Test Dokument", "text/plain", "text/html"), subject=[u"Süper", u"Dokumänt"], exclude_from_nav=False, ) portal.invokeFactory( "Document", id="testdoc2", title=u"Page 😉", text=RichTextValue(u"Ein heiBes Test Dokument", "text/plain", "text/html"), subject=[u"Dokumänt"], exclude_from_nav=True, ) doc = portal["testdoc"] doc.geolocation = Geolocation(47.4048832, 9.7587760701108) doc.reindexObject()
def geocode_all_contacts(context): default_latitude = api.portal.get_registry_record( "geolocation.default_latitude") default_longitude = api.portal.get_registry_record( "geolocation.default_longitude") # we use OpenCage with a temporary API key for this migration because # Nominatim is too limited for bulk use geolocator = geopy.geocoders.OpenCage( api_key="801c91558c1f4338a62a43561fc961ab", timeout=3) brains = api.content.find(portal_type="imio.directory.Contact") for brain in brains: obj = brain.getObject() coordinates = IGeolocatable(obj).geolocation street_parts = [ obj.number and str(obj.number) or "", obj.street, obj.complement, ] street = " ".join(filter(None, street_parts)) entity_parts = [ obj.zipcode and str(obj.zipcode) or "", obj.city, ] entity = " ".join(filter(None, entity_parts)) country = translate_vocabulary_term( "imio.smartweb.vocabulary.Countries", obj.country) address = " ".join(filter(None, [street, entity, country])) if not address: # if we have no address, clear geolocation if obj.geolocation is not None: obj.geolocation = Geolocation("", "") obj.reindexObject(idxs=["longitude", "latitude"]) logger.info( f"Contact has no address : {obj.absolute_url()} --> cleared geolocation" ) continue location = None if coordinates is None or not all( [coordinates.latitude, coordinates.longitude]): # contact has no geolocation, see if we can find one location = geolocator.geocode(address) logger.info( f"Contact had no location : {obj.absolute_url()} --> {location.latitude} / {location.longitude}" ) elif (coordinates.latitude == default_latitude and coordinates.longitude == default_longitude # NOQA ): # contact was automatically geolocated on IMIO (by default) location = geolocator.geocode(address) logger.info( f"Contact was located on IMIO : {obj.absolute_url()} --> {location.latitude} / {location.longitude}" ) else: # contact already has a geolocation, do nothing logger.info( f"Contact already has its location : {obj.absolute_url()}") continue if location: obj.geolocation = Geolocation(latitude=location.latitude, longitude=location.longitude) obj.reindexObject(idxs=["longitude", "latitude"])
def __iter__(self): for item in self.previous: keys = item.keys() typekey = self.typekey(*keys)[0] pathkey = self.pathkey(*keys)[0] if not (typekey and pathkey): logger.warn('Not enough info for item: {0}'.format(item)) yield item continue type_, path = item[typekey], item[pathkey] # noqa obj = self.context.unrestrictedTraverse( str(item[pathkey]).lstrip('/'), None) if not obj: yield item continue if getattr(obj, 'text', None): raw_text = obj.text.raw if '@@download' in raw_text: fixed_text = re.sub(r'(/@@download/.*?)"', r'"', raw_text) setattr( obj, 'text', RichTextValue(raw=fixed_text, outputMimeType='text/x-html-safe', mimeType=u'text/html')) # da scommentare in caso di migrazione di subsite # if type_ == 'Folder' and item['is_rer_subsite']: # if not IRERSubsiteEnabled.providedBy(obj): # alsoProvides(obj, IRERSubsiteEnabled) # obj.reindexObject(idxs=['object_provides']) # obj.subsite_color = item.get('subsite_color', None) # # if item.get('_datafield_image', None): # image_params = item['_datafield_image'] # image_data = urllib2.urlopen( # image_params['data_uri'] # ).read() # obj.image = namedfile.NamedBlobImage( # image_data, # contentType=image_params['content_type'], # filename=image_params['filename'] # ) if type_ == 'SchedaER': links_info = [ 'toDeepen', 'rulesAndActs', 'modules', 'ongoingProjects', 'initiatives', 'publications', 'usefulLinks', 'seeOther' ] for el in links_info: if item.get(el, ''): link_data = '<ul>' for line in item.get(el): if line['link'].startswith('http'): link_data += '<li><a href="' \ + line['link'] \ + '" data-linktype="external" data-val="' \ + line['link'] + '">' \ + line['title'] \ + '</a></li>' else: link_data += '<li><a href="../resolveuid/' \ + line['uid'] \ + '" data-linktype="internal" data-val="' \ + line['uid'] \ + '">' \ + line['title'] \ + '</a></li>' link_data += '</ul>' if link_data == '<ul></ul>': link_data = '' tmp_text = RichTextValue( link_data, 'text/html', 'text/x-plone-outputfilters-html') setattr(obj, el, tmp_text) del item[el] if type_ == 'LinkNormativa': if getattr(obj, 'lawType', None): setattr(obj, 'lawType', item.get('lawType')) logger.info('Fired event to setup remoteUrl for: {0}'.format( obj.absolute_url())) notify(ObjectAddedEvent(obj)) if type_ == 'Pubblicazione': if item['publicationDate']: obj.publicationDate = DateTime( item['publicationDate']).asdatetime() auth = item.get('_auth_info') if item.get('_datafield_image', None): image_params = item['_datafield_image'] image_data = self.get_file_data(image_params, auth) obj.image = namedfile.NamedBlobImage( image_data, contentType=image_params['content_type'], filename=image_params['filename']) if item.get('_datafield_file', None): file_params = item['_datafield_file'] file_data = self.get_file_data(file_params, auth) obj.publicationFile = namedfile.NamedBlobFile( file_data, filename=file_params['filename']) if type_ == 'Circolare': if getattr(obj, 'file1') and obj.file1.getSize() == 0: del obj.file1 if getattr(obj, 'file2') and obj.file2.getSize() == 0: del obj.file2 if getattr(obj, 'file3') and obj.file3.getSize() == 0: del obj.file3 # bisogna prendere solo le immagini all'interno degli annunci del # mercatino if type_ == 'Image' and \ (obj.aq_parent.portal_type == 'Advertisement' or obj.aq_parent.portal_type == 'BookCrossing'): if item.get('_datafield_image', None): image_params = item['_datafield_image'] request = urllib2.Request(image_params['data_uri']) base64string = base64.encodestring('{0}:{1}'.format( self.remote_username, self.remote_password)).replace('\n', '') request.add_header('Authorization', 'Basic {0}'.format(base64string)) image_data = urllib2.urlopen(request).read() obj.image = namedfile.NamedBlobImage( image_data, contentType=image_params['content_type'], filename=image_params['filename']) if type_ == 'Venue': from plone.formwidget.geolocation.geolocation import Geolocation geo = Geolocation(item['latitude'], item['longitude']) setattr(obj, 'geolocation', geo) yield item