def map_url(params, utm={}): if not params: return BASE_PATH if 'begin' in params: params['begin'] = params['begin'].strftime('%Y-%m-%d') if 'end' in params: params['end'] = params['end'].strftime('%Y-%m-%d') if 'geostore' in params and params['geostore'] is None: del params['geostore'] if 'geostore' not in params and 'geom' in params: geojson = json.dumps(params['geom']) if len(geojson) <= 1000: params['geojson'] = geojson if 'topic' in params: topic = Topic.get_by_id(params['topic']) else: topic = Topic.all()[0] baselayer = topic.baselayer url_params = {k: params[k] for k in ALLOWED_PARAMS if k in params} url_params = dict(url_params.items() + utm.items()) query_string = urllib.urlencode(url_params, doseq=True) return BASE_URL + BASE_PATH + '/-1/0/0/' + iso(params) + '/grayscale/' + \ baselayer + '?' + query_string
def post(self): params = self.args() if 'topic' in params: selected_topic = params['topic'] else: selected_topic = Topic.all()[0].id previous_event = Event.latest_for_topic(selected_topic) event = Event(topic=selected_topic) if previous_event != None: event.begin = previous_event.end if 'begin' in params: event.begin = datetime.datetime.strptime(params['begin'], '%Y-%m-%d') if 'end' in params: event.end = datetime.datetime.strptime(params['end'], '%Y-%m-%d') if 'send' in params: event.put() send_subscriptions(event) self.redirect('/manage/pubsub?success=true') alerts = [] if 'preview' in params: alerts = get_subscriptions(event) alerts = map(set_url_factory(event), alerts) emails = [] if 'preview_emails' in params: emails = get_subscription_emails(event) template_values = { 'topics': Topic.all(), 'selected_topic': selected_topic, 'begin_date': event.begin.strftime('%Y-%m-%d'), 'end_date': event.end.strftime('%Y-%m-%d'), 'alerts': alerts, 'emails': emails, 'preview': 'preview' in params, 'preview_emails': 'preview_emails' in params, 'success': 'success' in params } template_path = os.path.join(os.path.dirname(__file__), 'templates/index.html') self.response.out.write(template.render(template_path, template_values))
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') 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) email = self.subscription.email user_profile = self.subscription.user_id.get().get_profile() name = getattr(user_profile, 'name', email) template_params = { 'selected_area': topic_result.area_name(), 'alert_count': topic_result.formatted_value(), 'alert_type': topic.description, 'alert_date': begin + " to " + end, 'alert_summary': summary_for_topic(topic), '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), substitution_data=template_params ) logging.info("Send Subscription Email Result: %s" % response)
def run_analysis(self, begin, end): params = copy.copy(self.params) params['begin'] = begin params['end'] = end if 'geom' in params: geom = params['geom'] if 'geometry' in geom: geom = geom['geometry'] params['geojson'] = json.dumps(geom) topic = Topic.get_by_id(self.topic) return topic.execute(params)
def __init__(self, subscription): self.subscription = subscription self.topic = Topic.get_by_id(subscription.topic)