def _enhance_event_item(self, doc): plannings = self.get_plannings_for_event(doc) if plannings.count() > 0: doc['planning_ids'] = [ planning.get('_id') for planning in plannings ] for location in (doc.get('location') or []): format_address(location) # this is to fix the existing events have original creator as empty string if not doc.get('original_creator'): doc.pop('original_creator', None)
def export_events_to_text(self, items, format='utf-8', template=None, tz_offset=None): for item in items: item['formatted_state'] = item['state'] if item.get('state') in [WORKFLOW_STATE.CANCELLED, WORKFLOW_STATE.RESCHEDULED, WORKFLOW_STATE.POSTPONED] else None location = item['location'][0] if len(item.get('location') or []) > 0 else None if location: format_address(location) item['formatted_location'] = location.get('name') if not location.get('formatted_address') else \ '{0}, {1}'.format(location.get('name'), location['formatted_address']) item['contacts'] = [] for contact in get_contacts_from_item(item): contact_info = ['{0} {1}'.format(contact.get('first_name'), contact.get('last_name'))] phone = None if (contact.get('job_title')): contact_info[0] = contact_info[0] + ' ({})'.format(contact['job_title']) if (len(contact.get('contact_email') or [])) > 0: contact_info.append(contact['contact_email'][0]) if (len(contact.get('contact_phone') or [])) > 0: phone = next((p for p in contact['contact_phone'] if p.get('public')), None) elif len(contact.get('mobile') or []) > 0: phone = next((m for m in contact['mobile'] if m.get('public')), None) if phone: contact_info.append(phone.get('number')) item['contacts'].append(", ".join(contact_info)) date_time_format = "%a %d %b %Y, %H:%M" item['dates']['start'] = utc_to_local(config.DEFAULT_TIMEZONE, item['dates']['start']) item['dates']['end'] = utc_to_local(config.DEFAULT_TIMEZONE, item['dates']['end']) item['schedule'] = "{0}-{1}" .format(item['dates']['start'].strftime(date_time_format), item['dates']['end'].strftime("%H:%M")) if ((item['dates']['end'] - item['dates']['start']).total_seconds() / 60) >= (24 * 60): item['schedule'] = "{0} to {1}".format(item['dates']['start'].strftime(date_time_format), item['dates']['end'].strftime(date_time_format)) if tz_offset: tz_browser = tz.tzoffset('', int(tz_offset)) item['browser_start'] = (item['dates']['start']).astimezone(tz_browser) item['browser_end'] = (item['dates']['end']).astimezone(tz_browser) set_item_place(item) return str.encode(render_template(template, items=items), format)
def test_format_address(self): location = { 'address': { 'postal_code': '2150', 'line': ['The Pub'], 'area': 'Parramatta', 'locality': 'Sydney', 'country': 'Australia' }, 'name': 'Parramatta', 'location': { 'lat': -33.8139843, 'lon': 151.002666 }, 'qcode': 'urn:newsml:localhost:2017-11-28T13:21:06.571812:1ce975e9-19c2-4fad-9cd6-8cda4020e565' } format_address(location) self.assertEqual(location['formatted_address'], 'The Pub Parramatta Sydney 2150 Australia') location = { "address": { "line": [""], }, "name": "Parramatta", "location": { "lat": -33.8139843, "lon": 151.002666 }, "qcode": "urn:newsml:localhost:2017-11-28T13:21:06.571812:1ce975e9-19c2-4fad-9cd6-8cda4020e565" } format_address(location) self.assertEqual(location['formatted_address'], '') location = { "address": {}, "name": "Parramatta", "location": { "lat": -33.8139843, "lon": 151.002666 }, "qcode": "urn:newsml:localhost:2017-11-28T13:21:06.571812:1ce975e9-19c2-4fad-9cd6-8cda4020e565" } format_address(location) self.assertEqual(location['formatted_address'], '') location = { "address": { "line": [] }, "name": "Parramatta", "location": { "lat": -33.8139843, "lon": 151.002666 }, "qcode": "urn:newsml:localhost:2017-11-28T13:21:06.571812:1ce975e9-19c2-4fad-9cd6-8cda4020e565" } format_address(location) self.assertEqual(location['formatted_address'], '')
def _enhance_item(self, doc): format_address(doc)