Exemple #1
0
def report(begin=None,end=None):
  if not end:
    end = arrow.now().format('YYYY-MM-DD')
  rpt = {}
  confirmed = Subscription.query(Subscription.confirmed==True)
  unconfirmed = Subscription.query(ndb.OR(Subscription.confirmed==False,Subscription.confirmed==None))
  rpt['total'] = {
    'confirmed': confirmed.count(),
    'unconfirmed': unconfirmed.count()
  }
  def sub_tuple(sub):
    params=sub.params
    typ = params.get('iso','geojson')
    date = arrow.get(sub.created).format("YYYY-MM-DD")
    return (typ,date,sub.email)

  if begin:
    filtered_confirmed = filter_subscriptions(confirmed,begin,end)
    filtered_unconfirmed = filter_subscriptions(unconfirmed,begin,end)
    rpt['filtered'] = {
      'begin': begin,
      'end': end
    }
    rpt['filtered']['confirmed'] = {
      'count': filtered_confirmed.count(),
      'subscriptions': map(sub_tuple,filtered_confirmed)
    }
    rpt['filtered']['unconfirmed'] = {
      'count': filtered_unconfirmed.count(),
      'subscriptions': map(sub_tuple,filtered_unconfirmed)
    }
  return rpt
Exemple #2
0
def resetUpdates():
  subs = Subscription.query()
  for s in subs:
    if s.updates:
      print("s %s" % s.email)
      s.updates = None
      s.put()
Exemple #3
0
def clearSubs(email=None):
  if not email:
    email = 'bguzder.wri.org'
  subs = Subscription.query(Subscription.email==email)
  for s in subs:
      print("clear sub:  %s" % s)
      s.updates = None
      s.put()
Exemple #4
0
 def receive(self, message):
     if message.to.find('<') > -1:
         token = message.to.split('<')[1].split('+')[1].split('@')[0]
     else:
         token = message.to.split('+')[1].split('@')[0]
     token = self.request.get('token')
     if Subscription.confirm_by_token(token):
         self.response.write(json.dumps(dict(confirmed=True)))
     else:
       self.error(404)  
Exemple #5
0
    def unsubscribe(self):
        try:
            params = self._get_params(body=True)
            token = params.get('token')
            if token:
                Subscription.unsubscribe_by_token(token)
                self.response.set_status(201)
                self._send_response(json.dumps(dict(unsubscribe=True)))
            else:
                self.error(404) 

        except Exception, e:
            name = e.__class__.__name__
            msg = 'Error: PubSub API (%s)' % name
            monitor.log(
                self.request.url, 
                msg, 
                error=e,        
                headers=self.request.headers
            )
Exemple #6
0
 def _updateSubscriptionDates(self, subscription_id):
     subscription = Subscription.get_by_id(subscription_id)
     updated = None
     for key in self.sqls:
         date = self._recentDate(key, 0)
         if date:
             updated = True
             if not subscription.updates:
                 subscription.updates = {}
             subscription.updates[key] = date
     if updated:
         subscription.put()
Exemple #7
0
 def _updateSubscriptionDates(self,subscription_id):
     subscription = Subscription.get_by_id(subscription_id)
     updated = None
     for key in self.sqls:
         date = self._recentDate(key,0)
         if date:
             updated = True
             if not subscription.updates:
                 subscription.updates = {}
             subscription.updates[key] = date
     if updated:
         subscription.put()
Exemple #8
0
    def post(self):
        """Publish notifications to all event subscribers."""
        e = ndb.Key(urlsafe=self.request.get('event')).get()

        if not e.multicasted:
            for s in Subscription.with_confirmation():
                n = Notification.get(e, s)
                if not n:
                    n = Notification.create(e, s)
                    n.put()

                taskqueue.add(
                    url='/pubsub/notify',
                    queue_name='pubsub-notify',
                    params=dict(notification=n.key.urlsafe()))
        e.multicasted = True
        e.put()
Exemple #9
0
    def subscribe(self):
        try:
            params = self._get_params(body=True)
            subscription = Subscription.subscribe(params)
            if subscription:
                token = subscription.key.urlsafe()
                self.response.set_status(201)
                self._send_response(json.dumps(dict(subscribe=True,token=token)))
            else:
                self.error(404)  

        except Exception, e:
            name = e.__class__.__name__
            msg = 'Error: PubSub API (%s)' % name
            monitor.log(
                self.request.url, 
                msg, 
                error=e,
                headers=self.request.headers
            )
Exemple #10
0
def subscribe(**params):
  auto_confirm = params.pop('auto_confirm',False)
  token = Subscription.subscribe(params)
  if token and auto_confirm:
    confirm(token)
  return token
Exemple #11
0
def printUpdates():
  subs = Subscription.query()
  for s in subs:
    print("s %s" % s.updates)
Exemple #12
0
def unconfirmed():
  subs = Subscription.without_confirmation()
  for s in subs:
    print("%s" % s)
Exemple #13
0
 def get(self):
     token = self.request.get('token')
     if Subscription.confirm_by_token(token):
         self.response.write(json.dumps(dict(confirmed=True)))
     else:
         self.error(404)        
Exemple #14
0
def checkSubs(email=None):
  if not email:
    email = 'bguzder.wri.org'
  subs = Subscription.query(Subscription.email==email)
  for s in subs:
      print("check sub:  %s" % s)
Exemple #15
0
def unsubscribe(token):
  return Subscription.unsubscribe(token)
Exemple #16
0
def confirm(token):
  return Subscription.confirm(token)
Exemple #17
0
 def get(self):
     email = self.request.get('email')
     subs = [x.to_dict(exclude=['created']) for x in Subscription.with_email(email)]
     self.response.headers.add_header('charset', 'utf-8')
     self.response.headers["Content-Type"] = "application/json"
     self.response.out.write(json.dumps(subs, sort_keys=True))