Esempio n. 1
0
    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
Esempio n. 2
0
    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')
Esempio n. 3
0
    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')
Esempio n. 4
0
    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')
Esempio n. 5
0
    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')
Esempio n. 6
0
    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')
Esempio n. 7
0
    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')
Esempio n. 8
0
    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')
Esempio n. 9
0
    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())
Esempio n. 10
0
    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())
Esempio n. 11
0
    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')
Esempio n. 12
0
    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')
Esempio n. 13
0
    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()))
Esempio n. 14
0
    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')
Esempio n. 15
0
    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')
Esempio n. 16
0
    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')
Esempio n. 17
0
    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)
Esempio n. 18
0
    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
        })
Esempio n. 19
0
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
Esempio n. 20
0
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
Esempio n. 21
0
    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
Esempio n. 22
0
    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)
Esempio n. 23
0
def get_subscription_emails(event):
    subscriptions = Subscription.query(Subscription.topic == event.topic,
                                       Subscription.confirmed == True)
    return [s.email for s in subscriptions.iter()]
Esempio n. 24
0
    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)
Esempio n. 25
0
 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')
Esempio n. 26
0
 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)
Esempio n. 27
0
def get_subscription_emails(event):
    subscriptions = Subscription.query(Subscription.topic ==
            event.topic, Subscription.confirmed == True)
    return [s.email for s in subscriptions.iter()]
Esempio n. 28
0
def count_subscriptions(topic=None):
    if topic:
        print Subscription.query(Subscription.topic == topic).count()
    else:
        print Subscription.query().count()
Esempio n. 29
0
 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')
Esempio n. 30
0
    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)
Esempio n. 31
0
 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)