Exemple #1
0
 def post(self, url_user=''):
   curr_user_db = auth.AuthUser(users.get_current_user())
   if not curr_user_db:
     HandleAuthFailure(self)
     return
   new_allowed_device_ids = self.request.get('device_checkbox',
                                             allow_multiple=True)
   curr_user_owned_devices = (model.RelayDevice
                              .query(ancestor=curr_user_db.key)
                              .fetch())
   
   url_user_db = model.AuthorizedUser.get_by_id(url_user)
   if not url_user_db:
       request_handler.response.write(
         'Bad request -- url_user %s not found.' % url_user)
       request_handler.response.set_status(400)
       return
       
   url_user_db.may_post_to = []
   for device_id in new_allowed_device_ids:
     try:
       device = next(device for device in curr_user_owned_devices
                     if device.key.id() == device_id)
     except StopIteration:
       request_handler.response.write(
         'Bad request -- device_id %s not found.' % device_id)
       request_handler.response.set_status(400)
       return
     url_user_db.may_post_to.append(device.key)
   url_user_db.put()
   self.redirect('/user/%s' % url_user)
Exemple #2
0
  def get(self, owner_email='', device_id=''):
    # TODO: restructure to have 2 URL parameters: owner first, then ID.
    # Construct the parent, child key from this. We can't do get_by_id without
    # the parent key.
    logging.info('%s %s', owner_email, device_id)
    curr_user_db = auth.AuthUser(users.get_current_user())
    if not curr_user_db:
      HandleAuthFailure(self)
      return
    device_db = (model.RelayDevice
                 .get_by_id(device_id,
                            parent=ndb.Key(model.AuthorizedUser,
                                           owner_email)))
    if (device_db.key not in curr_user_db.may_post_to
        and device_db.key.parent() != curr_user_db.key):
      HandleAuthFailure(self)
      return      
    messages = (model.Message
                .query(ancestor=device_db.key)
                .order(-model.Message.date)
                .fetch())
    
    sender_keys = []
    for message in messages:
      sender_keys.append(message.sender)
    senders_db = ndb.get_multi(sender_keys)
    
    for message, sender_db in zip(messages, senders_db):
      message.sender_email = sender_db.key.id()
      message.acknowledged = (device_db.acked_until
                              and device_db.acked_until >= message.date)
      
    # TODO: More robust timezone support once pytz lands.
    # local_tz = pytz.timezone('America/New_York')
    # message.date.replace(tzinfo=pytz.utc).astimezone(local_tz).replace(tzinfo=None)

    authorized_users_this_device = (
        model.AuthorizedUser
        .query()
        .filter(model.AuthorizedUser.may_post_to == device_db.key)
        .fetch())
    
    logging.info('authorized_users_this_device %s', authorized_users_this_device)
    
    user, url, url_linktext = GetLoginLinks(self)
    template_values = {
        'user': user,
        'url': url,
        'url_linktext': url_linktext,
        'authorized_users_this_device': authorized_users_this_device,
        'messages': messages,
        'owner_email': owner_email,
        'device_id': device_id,
    }
    template = JINJA_ENVIRONMENT.get_template('device.html')
    self.response.write(template.render(template_values))
Exemple #3
0
 def post(self):
   if not auth.AuthUser(users.get_current_user()):
     HandleAuthFailure(self)
     return
   email = self.request.get('email_to_authorize')
   # TODO: validate that this is a real email address
   if email:
   	user_db = model.AuthorizedUser(id=email)
   	user_db.put()
   self.redirect('/register_users')
Exemple #4
0
 def get(self):
   if not auth.AuthUser(users.get_current_user()):
     HandleAuthFailure(self)
     return
   user, url, url_linktext = GetLoginLinks(self)
   template_values = {
       'user': user,
       'url': url,
       'url_linktext': url_linktext,
       'authorized_users': model.AuthorizedUser.query().fetch(),
   }
   template = JINJA_ENVIRONMENT.get_template('register_users.html')
   self.response.write(template.render(template_values))
Exemple #5
0
 def get(self):
   curr_user_db = auth.AuthUser(users.get_current_user())
   if curr_user_db:
     self.redirect('/')
     return
   user, url, url_linktext = GetLoginLinks(self)
   template_values = {
       'user': user,
       'url': url,
       'url_linktext': url_linktext,
   }
   template = JINJA_ENVIRONMENT.get_template('login.html')
   self.response.write(template.render(template_values))
Exemple #6
0
  def get(self):
    curr_user_db = auth.AuthUser(users.get_current_user())
    if not curr_user_db:
      HandleAuthFailure(self)
      return
    owned_devices = model.RelayDevice.query(ancestor=curr_user_db.key).fetch()
    may_post_to_devices = ndb.get_multi(curr_user_db.may_post_to)
    user, url, url_linktext = GetLoginLinks(self)
    template_values = {
      'user': user,
      'url': url,
      'url_linktext': url_linktext,
      'owned_devices': owned_devices,
      'may_post_to_devices': may_post_to_devices,
    }

    template = JINJA_ENVIRONMENT.get_template('index.html')
    self.response.write(template.render(template_values))
Exemple #7
0
 def post(self, owner_email='', device_id=''):
   curr_user_db = auth.AuthUser(users.get_current_user())
   if not curr_user_db:
     HandleAuthFailure(self)
     return
   device_db = (model.RelayDevice
                .get_by_id(device_id,
                           parent=ndb.Key(model.AuthorizedUser,
                                          owner_email)))
   if (device_db.key not in curr_user_db.may_post_to
       and device_db.key.parent() != curr_user_db.key):
     HandleAuthFailure(self)
     return
   message = model.Message(parent=device_db.key)
   message.body = self.request.get('message_text')
   message.sender = curr_user_db.key
   message.put()
   if device_db.fcm_id:
     SendPushNotification(device_db.fcm_id)
   self.redirect('/device/%s/%s' % (owner_email, device_id))
Exemple #8
0
 def get(self, url_user=''):
   curr_user_db = auth.AuthUser(users.get_current_user())
   if not curr_user_db:
     HandleAuthFailure(self)
     return
   user, url, url_linktext = GetLoginLinks(self)
   url_user_db = model.AuthorizedUser.get_by_id(url_user)
   # TODO: bad url_user
   curr_user_owned_devices = (model.RelayDevice
                              .query(ancestor=curr_user_db.key)
                              .fetch())
   for device in curr_user_owned_devices:
     device.url_user_may_post_to = device.key in url_user_db.may_post_to
   template_values = {
       'user': user,
       'url': url,
       'url_linktext': url_linktext,
       'url_user': url_user,
       'curr_user_owned_devices': curr_user_owned_devices,
   }
   logging.info('%s %s' % (url_user, url_user_db))
   template = JINJA_ENVIRONMENT.get_template('user.html')
   self.response.write(template.render(template_values))
Exemple #9
0
  def device_request(self, request):
    """Upserts a device, then returns the least recent unacknowledged message."""
    logging.info('User %s', endpoints.get_current_user())
    user_model = auth.AuthUser(endpoints.get_current_user())
    if not user_model:
      raise endpoints.ForbiddenException('Unauthorized client device')

    # Get the device and upsert its info if needed
    device = model.RelayDevice.get_or_insert('%s%s' % (request.device_id,
                                                       user_model.key.id()),
                                             parent=user_model.key)
    modified = False
    if device.fcm_id != request.fcm_id:
      device.fcm_id = request.fcm_id
      modified = True
    acked_until_utc = datetime.datetime.utcfromtimestamp(request.acked_until)
    if device.acked_until != acked_until_utc:
      device.acked_until = acked_until_utc
      modified = True
    if modified:
      device.put()

    # Get the oldest unacked message on the device and return
    oldest_unacked = (model.Message
                     .query(ancestor=device.key)
                     .filter(model.Message.date > acked_until_utc)
                     .order(model.Message.date)
                     .get())
    response = DeviceResponse()
    if oldest_unacked:
      response.message = oldest_unacked.body
      response.sender = oldest_unacked.sender.get().key.id()
      # We add one since the timegm() function always rounds down any
      # sub-seconds. This guarantees that the returned time is >= the message
      # time in the datastore.
      response.date = calendar.timegm(oldest_unacked.date.timetuple()) + 1
    return response
Exemple #10
0
def get_text_messages(message):
    print(message.text)
    AUTH.AuthUser(message, bot)