def create_for_email(cls, email): migration = cls.query(cls.email == email).fetch() if len(migration) > 0: migration = migration[0] else: migration = cls() migration.email = email if migration.complete == True: return subscriptions = Subscription.query(Subscription.email == email) for subscription in subscriptions.iter(): if hasattr(subscription, 'user_id') and subscription.user_id != None: continue if subscription.topic in IGNORED_TOPICS: continue migration.subscriptions.append(subscription.key) if len(migration.subscriptions) > 0: migration.complete = True migration.put() return migration
def delete(self, subscription_id): subscription = Subscription.get_by_id(int(subscription_id)) if subscription: subscription.unsubscribe() self.complete('respond', subscription.to_dict()) else: self.write_error(404, 'Not found')
def unsubscribe(self, subscription_id): subscription = Subscription.get_by_id(int(subscription_id)) if subscription: subscription.unsubscribe() self.redirect(gfw_url('my_gfw/subscriptions', {'unsubscribed': 'true'})) else: self.write_error(404, 'Not found')
def send_confirmation(self, subscription_id): subscription = Subscription.get_by_id(int(subscription_id)) if subscription: subscription.unconfirm(); self.redirect(gfw_url('my_gfw/subscriptions', {'subscription_confirmation_sent': 'true'})) else: self.write_error(404, 'Not Found')
def create(self): subscription = Subscription.subscribe(self.__get_params(), self.user) if subscription: token = subscription.key.urlsafe() self.response.set_status(201) self.complete('respond', {"subscribe": True, "key": token}) else: self.write_error(400, 'Bad Request')
def testUpdateSubscriptions(self): subscription = Subscription() subscription.params = {} subscription.put() migration = Migration() migration.subscriptions = [subscription.key] migration.put() user = GFWUser() user.auth_ids = ['123'] user.put() migration.update_subscriptions(user) self.assertIsNotNone(subscription.key.get().user_id) self.assertEqual(subscription.key.get().user_id.urlsafe(), user.key.urlsafe())
def put(self, subscription_id): subscription = Subscription.get_by_id(int(subscription_id)) if subscription: subscription.populate(**self.__get_params()) subscription.put() self.complete('respond', subscription.to_dict()) else: self.write_error(404, 'Not found')
def publish_subscriptions(self): event = ndb.Key(urlsafe=self.args().get('event')).get() subscriptions = Subscription.query(Subscription.topic == event.topic, Subscription.confirmed == True) for subscription in subscriptions.iter(): taskqueue.add(url='/manage/pubsub/tasks/publish_subscription', queue_name='pubsub-publish-sub', params=dict(event=event.key.urlsafe(), subscription=subscription.key.urlsafe()))
def overview(self, subscription_id): subscription = Subscription.get_by_id(int(subscription_id)) if subscription: if (not hasattr(subscription, 'overview_image')) or (subscription.overview_image is None): subscription.overview_image = SubscriptionOverviewService.overview_image(subscription) subscription.put() self.response.headers['Content-Type'] = 'image/png' self.response.out.write(subscription.overview_image) else: self.write_error(404, 'Not found')
def testCreateMigrations(self): user = GFWUser() user.auth_ids = ['123'] user.put() new_subscription = Subscription() new_subscription.params = {} new_subscription.user_id = user.key new_subscription.put() subscription = Subscription() subscription.params = {'iso': 'ALB'} subscription.email = '*****@*****.**' subscription.put() Migration.create_from_subscriptions() migration_count = Migration.query().count() self.assertEqual(migration_count, 1) migration = Migration.query().fetch()[0] self.assertEqual(migration.email, '*****@*****.**') self.assertEqual(len(migration.subscriptions), 1) self.assertEqual(migration.subscriptions[0], subscription.key)
def subscriptions(self): cursor_id = self.args().get('cursor') cursor = Cursor(urlsafe=cursor_id) subscriptions, next_cursor, more = Subscription.query().fetch_page(PER_PAGE, start_cursor=cursor) to_dict = lambda g: g.to_dict() subscriptions_as_dicts = map(to_dict, subscriptions) if more: next_cursor = next_cursor.urlsafe() else: next_cursor = None self.complete('respond', { "subscriptions": subscriptions_as_dicts, "cursor": next_cursor })
def get_subscriptions(event): subscriptions = Subscription.query(Subscription.topic == event.topic, Subscription.confirmed == True) alerts = [] for subscription in subscriptions.iter(): try: topic_result = subscription.run_analysis(event.begin, event.end) if (topic_result.is_zero() == False): alerts.append({ 'count': topic_result.formatted_value(), 'subscription': subscription }) except Exception: pass return alerts
def get_subscription_emails(event): subscriptions = Subscription.query(Subscription.topic == event.topic, Subscription.confirmed == True) return [s.email for s in subscriptions.iter()]
def create_from_subscriptions(cls): query_set = Subscription.query(projection=["email"], distinct=True) unique_emails = [data.email for data in query_set] for email in unique_emails: cls.create_for_email(email)
def confirm(self, subscription_id): if Subscription.confirm_by_id(subscription_id): self.redirect(gfw_url('my_gfw/subscriptions', {'subscription_confirmed': 'true'})) else: self.write_error(404, 'Not Found')
def index(self): subscriptions = Subscription.query(Subscription.user_id==self.user.key).fetch() subscriptions = [s.to_dict() for s in subscriptions] self.complete('respond', subscriptions)
def count_subscriptions(topic=None): if topic: print Subscription.query(Subscription.topic == topic).count() else: print Subscription.query().count()