예제 #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
예제 #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')
예제 #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')
예제 #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')
예제 #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')
예제 #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')
예제 #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')
예제 #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')
예제 #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())
예제 #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())
예제 #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')
예제 #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')
예제 #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()))
예제 #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')
예제 #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')
예제 #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')
예제 #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)
예제 #18
0
파일: api.py 프로젝트: suryatapavi/gfw-api
    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
        })
예제 #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
예제 #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
예제 #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
예제 #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)
예제 #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()]
예제 #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)
예제 #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')
예제 #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)
예제 #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()]
예제 #28
0
def count_subscriptions(topic=None):
    if topic:
        print Subscription.query(Subscription.topic == topic).count()
    else:
        print Subscription.query().count()
예제 #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')
예제 #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)
예제 #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)