def summary_for_topic(topic, language): meta = t(language, 'topic.' + topic.meta_id + '.metadata') lower_first = func = lambda s: s[:1].lower() + s[1:] if s else '' return t(language, 'subscription.summary').encode('utf-8').format( meta['description'].encode('utf-8'), meta['resolution'].encode('utf-8'), meta['coverage'].encode('utf-8'), meta['source'].encode('utf-8'), meta['timescale'].encode('utf-8'), lower_first(meta['updates'].encode('utf-8')))
def area_name(self, language='EN'): params = self.data['params'] if 'id1' in params.keys(): return "ID1: " + params.get('id1') elif 'iso' in params.keys(): return "ISO Code: " + params.get('iso') elif 'wdpaid' in params.keys(): return "WDPA ID: " + str(params.get('wdpaid')) else: return t(language, 'subscription.custom_area')
def formatted_value(self, language='EN'): value = map(str, self.value()) template = t(language, 'topic.' + self.topic.meta_id + '.alert_template') return template.format(*value)
def send_for_event(self, event): topic_result = self.subscription.run_analysis(event.begin, event.end) if topic_result.is_zero() == False: topic = Topic.get_by_id(event.topic) subscriptions_url = gfw_url('my_gfw/subscriptions', {}) unsubscribe_url = '%s/v2/subscriptions/%s/unsubscribe' % \ (runtime_config['APP_BASE_URL'], str(self.subscription.key.id())) begin = event.begin.strftime('%d %b %Y') end = event.end.strftime('%d %b %Y') utm_config = { 'utm_source': 'subscription', 'utm_medium': 'email', 'utm_campaign': topic.id, 'utm_content': self.subscription.email } url_params = self.subscription.params url_params['begin'] = event.begin url_params['end'] = event.end url_params['fit_to_geom'] = 'true' url_params['tab'] = 'analysis-tab' alert_link = map_url(self.subscription.params, utm_config) email = self.subscription.email user_profile = self.subscription.user_id.get().get_profile() name = getattr(user_profile, 'name', email) language = self.subscription.language or 'EN' template_params = { 'selected_area': topic_result.area_name(language), 'alert_count': topic_result.formatted_value(language), 'alert_type': description_for_topic(topic, language), 'alert_date': "{} {} {}".format(begin, t(language, 'subscription.date_join'), end), 'alert_summary': summary_for_topic(topic, language), 'alert_name': self.subscription.formatted_name(), 'alert_link': alert_link, 'unsubscribe_url': unsubscribe_url, 'subscriptions_url': subscriptions_url } if topic.id == 'alerts/viirs': map_image = '%s/v2/subscriptions/%s/overview.png' % \ (runtime_config['APP_BASE_URL'], str(self.subscription.key.id())) template_params['map_image'] = map_image urlfetch.fetch(map_image, method=urlfetch.GET) fire_alerts = topic_result.value()[1][:10] for alert in fire_alerts: alert['acq_date'] = alert['acq_date'].split('T')[0] alert['acq_time'] = alert['acq_time'][:2] + ':' + alert[ 'acq_time'][2:] + ' UTC' alert['latitude'] = "{0:.3f}".format(alert['latitude']) alert['longitude'] = "{0:.3f}".format(alert['longitude']) template_params['fire_alerts'] = fire_alerts response = sparkpost.transmissions.send( recipients=[{ 'address': { 'email': email, 'name': name } }], template=template_for_topic(topic, language), substitution_data=template_params) logging.info("Send Subscription Email Result: %s" % response)
def description_for_topic(topic, language): return t(language, 'topic.' + topic.meta_id + '.description')
def send_for_event(self, event): topic_result = self.subscription.run_analysis(event.begin, event.end) if topic_result.is_zero() == False: topic = Topic.get_by_id(event.topic) subscriptions_url = gfw_url('my_gfw/subscriptions', {}) unsubscribe_url = '%s/v2/subscriptions/%s/unsubscribe' % \ (runtime_config['APP_BASE_URL'], str(self.subscription.key.id())) begin = event.begin.strftime('%d %b %Y') end = event.end.strftime('%d %b %Y') utm_config = { 'utm_source': 'subscription', 'utm_medium': 'email', 'utm_campaign': topic.id, 'utm_content': self.subscription.email } url_params = self.subscription.params url_params['begin'] = event.begin url_params['end'] = event.end url_params['fit_to_geom'] = 'true' url_params['tab'] = 'analysis-tab' alert_link = map_url(self.subscription.params, utm_config) email = self.subscription.email user_profile = self.subscription.user_id.get().get_profile() name = getattr(user_profile, 'name', email) language = self.subscription.language or 'EN' template_params = { 'selected_area': topic_result.area_name(language), 'alert_count': topic_result.formatted_value(language), 'alert_type': description_for_topic(topic, language), 'alert_date': "{} {} {}".format(begin, t(language, 'subscription.date_join'), end), 'alert_summary': summary_for_topic(topic, language), 'alert_name': self.subscription.formatted_name(), 'alert_link': alert_link, 'unsubscribe_url': unsubscribe_url, 'subscriptions_url': subscriptions_url } if topic.id == 'alerts/viirs': map_image = '%s/v2/subscriptions/%s/overview.png' % \ (runtime_config['APP_BASE_URL'], str(self.subscription.key.id())) template_params['map_image'] = map_image urlfetch.fetch(map_image, method=urlfetch.GET) fire_alerts = topic_result.value()[1][:10] for alert in fire_alerts: alert['acq_date'] = alert['acq_date'].split('T')[0] alert['acq_time'] = alert['acq_time'][:2] + ':' + alert['acq_time'][2:] + ' UTC' alert['latitude'] = "{0:.3f}".format(alert['latitude']) alert['longitude'] = "{0:.3f}".format(alert['longitude']) template_params['fire_alerts'] = fire_alerts response = sparkpost.transmissions.send( recipients=[{'address': { 'email': email, 'name': name }}], template=template_for_topic(topic, language), substitution_data=template_params ) logging.info("Send Subscription Email Result: %s" % response)