Ejemplo n.º 1
0
 def post(self):
     
     user = users.get_current_user()
     
     user_prefs = UserPrefs.all().filter('google_account =', user).get()
     if user_prefs is None:
         return self.error(404)
     
     bot_id = self.request.get('bot_id')
     if bot_id == '_custom':
         custom_bot_id = self.request.get('custom_bot_id')
         bot = BotPrefs.all().filter('bot_id =', custom_bot_id).get()
         logging.info('Custom Bot ID: %s' % custom_bot_id)
     else:
         bot = BotPrefs.all().filter('bot_id =', bot_id).get()
         logging.info('Bot ID: %s' % bot_id)
         
     if bot is not None:
         user_prefs.bot_prefs_key = bot.key()
         user_prefs.put()
         
         data = {'status': True, 'bot_id': bot.bot_id, 'nickname': bot.nickname}
     else:
         logging.error('bot_id is invalid.')
         data = {'status': False, 'message': 'Bot ID is invalid.'}
         
     json = simplejson.dumps(data, ensure_ascii=False)
     self.response.content_type = 'application/json'
     self.response.out.write(json)
Ejemplo n.º 2
0
 def post(self):
     
     user = users.get_current_user()
     
     device_token = self.request.get('device_token')
     if device_token != '':
         logging.info('device_token: %s' % device_token)
         
         device_prefs = DevicePrefs.all().filter('device_token =', device_token).get()
         if device_prefs is None:
             device_prefs = DevicePrefs()
             device_prefs.google_account = user
             device_prefs.device_token = device_token
             device_prefs.delete_flg = False
             device_prefs.put()
             
         user_prefs = UserPrefs.all().filter('google_account =', user).get()
         if user_prefs is None:
             bot = BotPrefs.all().filter('bot_id =', 'test').get()
             
             user_prefs = UserPrefs()
             user_prefs.google_account = user
             user_prefs.daily_max_notify_count = 3
             user_prefs.paid_quantity = 0
             user_prefs.free_quantity = 100
             user_prefs.timezone = 0.0
             user_prefs.notify_probability = 0.0
             user_prefs.delete_flg = False
             user_prefs.bot_prefs_key = bot.key()
             user_prefs.put()
         
         return self.redirect('/user/home')
         
     self.redirect('/user/home')
Ejemplo n.º 3
0
 def post(self):
     user_id = self.request.get('user_id')
     bot_id = self.request.get('bot_id')
     
     bot_prefs = BotPrefs.get_by_id(int(bot_id))
     if bot_prefs is None:
         logging.error('bot_id is invalid.')
         return
     
     user_prefs = UserPrefs.get_by_id(int(user_id))
     if user_prefs is None:
         logging.error('user_id is invalid.')
         return
     
     if user_prefs.free_quantity <= 0 and user_prefs.paid_quantity <= 0:
         user_prefs.activate_flg = False
         user_prefs.put()
         
         logging.info('quantity is invalid.')
         return
     
     device_list = DevicePrefs.all().filter('google_account =', user_prefs.google_account).fetch(10)
     for device in device_list:
         logging.info('Add notify task.')
         try:
             bot_id = user_prefs.bot_prefs_key.key().id()
             taskqueue.add(url = '/task/send_notify', params = {'user_id': user_prefs.key().id(), 'device_token': device.device_token, 'bot_id': bot_id})
         except:
             logging.error('Add task failed.')
Ejemplo n.º 4
0
    def get(self):

        bot_id = self.request.get('bot_id')
        
        bot_prefs = BotPrefs.all().filter('bot_id =', bot_id).get()
        if bot_prefs is None:
            return self.error(404)
        
        advance_flg = self.request.get('advance')
        if advance_flg == 'True':
            advance_flg = True
        else:
            advance_flg = False
        
        schedule_list = []
        
        if advance_flg:
            for i in range(24):
                time_key = '%02d00-%02d00' % (i, i+1)
                time = '%02d:00 - %02d:00' % (i, i+1)
                bot_message_list = BotMessage.all().filter('bot_prefs_key =', bot_prefs.key()).filter('time =', time_key).order('created_at').fetch(20)
                
                schedule_list.append({'time':time, 'list': bot_message_list, 'hour': 0})
        else:
            for i in [0, 3, 6, 9, 12, 15, 18, 21]:
                time = '%02d:00 - %02d:00' % (i, i+3)
                
                hour = i
                bot_message_list = []
                message_list = []
                for n in range(3):
                    time_key = '%02d00-%02d00' % (hour, hour+1)
                    bot_message = BotMessage.all().filter('bot_prefs_key =', bot_prefs.key()).filter('time =', time_key).order('created_at').fetch(20)
                    
                    for row in bot_message:
                        if row.message not in message_list:
                            bot_message_list.append(row)
                            message_list.append(row.message)
                    
                    hour = hour + 1
                    
                schedule_list.append({'time':time, 'list': bot_message_list, 'hour': i})

        
        template_values = {
            'bot_id': bot_id,
            'nickname': bot_prefs.nickname,
            'public_flg': bot_prefs.public_flg,
            'schedule_list': schedule_list,
            'advance_flg': advance_flg
        }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/bot/schedule_list.html')
        self.response.out.write(template.render(path, template_values))
Ejemplo n.º 5
0
    def get(self):
        user = users.get_current_user()
        
        bot_list = BotPrefs.all().filter('google_account =', user).fetch(20)

        template_values = {
            'bot_list': bot_list
        }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/bot/list.html')
        self.response.out.write(template.render(path, template_values))
Ejemplo n.º 6
0
 def get(self):
     
     user = users.get_current_user()
     
     user_prefs = UserPrefs.all().filter('google_account =', user).get()
     if user_prefs is None:
         return self.redirect('/user/welcome')
     
     new_bot_list = []
     bot_id_list = []
     
     bot_list = BotPrefs.all().filter('public_flg =', True).fetch(20)
     for row in bot_list:
         if row.bot_id not in bot_id_list:
             bot_id_list.append(row.bot_id)
             new_bot_list.append({'bot_id': row.bot_id, 'nickname': row.nickname})
     
     bot_list = BotPrefs.all().filter('google_account =', user).fetch(20)
     for row in bot_list:
         if row.bot_id not in bot_id_list:
             bot_id_list.append(row.bot_id)
             new_bot_list.append({'bot_id': row.bot_id, 'nickname': row.nickname})
             
     try:
         bot_id = user_prefs.bot_prefs_key.bot_id
     except:
         bot_id = ''
         
     template_values = {
         'bot_list': new_bot_list,
         'user_prefs': user_prefs,
         'bot_id': bot_id
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/user/bot.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 7
0
    def get(self):

        bot_id = self.request.get('bot_id')
        
        bot_prefs = BotPrefs.all().filter('bot_id =', bot_id).get()
        if bot_prefs is None:
            return self.error(404)
        
        template_values = {
            'bot_id': bot_id,
            'nickname': bot_prefs.nickname,
            'public_flg': bot_prefs.public_flg,
        }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/bot/edit.html')
        self.response.out.write(template.render(path, template_values))
Ejemplo n.º 8
0
 def post(self):
     user = users.get_current_user()
     
     bot_id = self.request.get('bot_id')
     nickname = self.request.get('nickname')
     
     if nickname == '':
         nickname = bot_id
     
     message = ''
     
     bot_prefs = BotPrefs.all().filter('bot_id =', bot_id).get()
     if bot_prefs is not None:
         message = '%s is already exists.' % bot_id
         data = {'status': False, 'message': message}
     else:
         bot_prefs = BotPrefs()
         bot_prefs.google_account = user
         bot_prefs.bot_id = bot_id
         bot_prefs.nickname = nickname
         bot_prefs.public_flg = False
         bot_prefs.delete_flg = False
         bot_prefs.put()
         
         data = {'status': True, 'message': None}
         
     
     template_values = {
         'bot_id': bot_id,
         'nickname': nickname,
         'message': message
     }
     
     json = simplejson.dumps(data, ensure_ascii=False)
     self.response.content_type = 'application/json'
     self.response.out.write(json)
Ejemplo n.º 9
0
 def get(self):
     
     user= users.get_current_user()
     
     id = self.request.get('id')
     bot_id = self.request.get('bot_id')
     
     bot_prefs = BotPrefs.all().filter('google_account =', user).filter('bot_id =', bot_id).get()
     if bot_prefs is None:
         return self.error(404)
     
     bot_message = BotMessage.get_by_id(int(id))
     if bot_message is None:
         return self.error(404)
     
     if bot_message.bot_prefs_key.key() != bot_prefs.key():
         return self.error(404)
     
     advance_flg = self.request.get('advance')
     if advance_flg == 'True':
         advance_flg = True
     else:
         advance_flg = False
         
     hour = self.request.get('hour')
     try:
         hour = int(hour)
     except:
         hour = 0
     
     template_values = {
         'bot_message': bot_message,
         'bot_id': bot_id,
         'advance_flg': advance_flg,
         'hour': hour
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/bot/show_message.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 10
0
    def post(self):

        user = users.get_current_user()
        bot_id = self.request.get('bot_id')
        
        bot_prefs = BotPrefs.all().filter('bot_id =', bot_id).filter('google_account =', user).get()
        if bot_prefs is None:
            return self.error(404)
        
        mode = self.request.get('mode')
        if mode == 'delete_message':
            message_id = self.request.get('message_id')
            bot_message = BotMessage.get_by_id(int(message_id))
                
            if bot_message is not None:
                advance_flg = self.request.get('advance')
                if advance_flg == 'True':
                    bot_message.delete()
                else:
                    try:
                        hour = int(self.request.get('hour'))
                        
                        for i in range(3):
                            time = '%02d00-%02d00' % (hour, hour + 1)
                            
                            bot_message_list = BotMessage.all().filter('time =', time).filter('message =', bot_message.message).fetch(20)
                            if bot_message_list is not None:
                                for row in bot_message_list:
                                    logging.info('Delete: %s -> %s' % (time, bot_message.message))
                                    row.delete()
                            
                            hour = hour + 1
                            
                    except:
                        pass
                
        json = simplejson.dumps({'status': True}, ensure_ascii=False)
        self.response.content_type = 'application/json'
        self.response.out.write(json)
Ejemplo n.º 11
0
 def post(self):
     user = users.get_current_user()
     bot_id = self.request.get('bot_id')
     
     bot_prefs = BotPrefs.all().filter('bot_id =', bot_id).filter('google_account =', user).get()
     if bot_prefs is None:
         return self.error(404)
     
     mode = self.request.get('mode')
     if mode == 'add_message':
         time = self.request.get('time')
         message = self.request.get('message')
         
         logging.info('time: %s' % time)
         
         if time.find('simple_') > -1:
             logging.info('Simple mode')
             hour = int(time[7:])
             for i in range(3):
                 time = '%02d00-%02d00' % (hour, hour + 1)
                 logging.info('time: %s' % time)
                 bot_message = BotMessage()
                 bot_message.bot_prefs_key = bot_prefs.key()
                 bot_message.time = time
                 bot_message.message = message
                 bot_message.put()
                 
                 hour = hour + 1
             
         else:
             bot_message = BotMessage()
             bot_message.bot_prefs_key = bot_prefs.key()
             bot_message.time = time
             bot_message.message = message
             bot_message.put()
         
     json = simplejson.dumps({'status': True}, ensure_ascii=False)
     self.response.content_type = 'application/json'
     self.response.out.write(json)
Ejemplo n.º 12
0
    def get(self):

        bot_id = self.request.get('bot_id')
        
        bot_prefs = BotPrefs.all().filter('bot_id =', bot_id).get()
        if bot_prefs is None:
            return self.error(404)
        
        advance_flg = self.request.get('advance')
        if advance_flg == 'True':
            advance_flg = True
        else:
            advance_flg = False
        
        hour_list = []
        if advance_flg:
            for i in range(24):
                key = '%02d00-%02d00' % (i, i+1)
                value = '%02d:00 - %02d:00' % (i, i+1)
                logging.info('%s'%value)
                hour_list.append({'key':key, 'value':value})
            
        else:
            for i in [0, 3, 6, 9, 12, 15, 18, 21]:
                key = 'simple_%d' % i
                value = '%02d:00 - %02d:00' % (i, i+3)
                logging.info('%s'%value)
                hour_list.append({'key':key, 'value':value})
        
        template_values = {
            'hour_list': hour_list,
            'bot_id': bot_id,
            'advance_flg': advance_flg
        }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/bot/add_message.html')
        self.response.out.write(template.render(path, template_values))
Ejemplo n.º 13
0
 def post(self):
     user = users.get_current_user()
     bot_id = self.request.get('bot_id')
     
     bot_prefs = BotPrefs.all().filter('bot_id =', bot_id).filter('google_account =', user).get()
     if bot_prefs is None:
         return self.error(404)
     
     mode = self.request.get('mode')
     if mode == 'modify':
         nickname = self.request.get('nickname')
         public_flg = self.request.get('public_flg')
         bot_prefs.nickname = nickname
         
         if public_flg == '1':
             bot_prefs.public_flg = True
         else:
             bot_prefs.public_flg = False
         
         bot_prefs.put()
         
     json = simplejson.dumps({'status': True}, ensure_ascii=False)
     self.response.content_type = 'application/json'
     self.response.out.write(json)
Ejemplo n.º 14
0
    def post(self):
        device_token = self.request.get('device_token')
        user_id = self.request.get('user_id')
        bot_id = self.request.get('bot_id')
        logging.info('SendNotifyTask')
        
        bot_prefs = BotPrefs.get_by_id(int(bot_id))
        if bot_prefs is None:
            logging.error('bot_id is invalid.')
            return
        
        user_prefs = UserPrefs.get_by_id(int(user_id))
        if user_prefs is None:
            logging.error('user_id is invalid.')
            return
        
        if user_prefs.free_quantity <= 0 and user_prefs.paid_quantity <= 0:
            user_prefs.activate_flg = False
            user_prefs.put()
            
            logging.info('quantity is invalid.')
            return
        
        date = datetime.datetime.now(tz=timezone(user_prefs.timezone))
        logging.info('Date: %s' % date)
        hour = date.hour
        logging.info('Hour: %d' % hour)
        
        time = '%02d00-%02d00' % (hour, hour+1)
        
        logging.info('time: %s' % time)
        
        bot_message_list = BotMessage.all().filter('bot_prefs_key =', bot_prefs.key()).filter('time =', time).fetch(10)
        
        message_list = []
        
        for bot_message in bot_message_list:
            message_list.append(bot_message.message)
            
        if len(message_list) > 0:
            rand = random.randint(0, len(message_list)-1)
            message = message_list[rand]
        else:
            logging.error('message is empty.')
            return
        
        logging.info('message: %s' % message)
        
        url = 'https://go.urbanairship.com/api/push/'
        
        data = {
            'device_tokens': [device_token],
            'aps': {
                'alert': message,
                'sound': 'default'
            }
        }
        
        if user_prefs.debug_flg:
            base64string = b64encode('%s:%s' % (UA_APPLICATION_KEY, UA_APPLICATION_MASTER_SECRET))
            logging.info('Debug Mode: %s' % device_token[0:4])
            logging.info('Key: %s' % UA_APPLICATION_KEY[0:4]);
        else:
            base64string = b64encode('%s:%s' % (UA_PROD_APPLICATION_KEY, UA_PROD_APPLICATION_MASTER_SECRET))
            logging.info('Production Mode: %s' % device_token[0:4])
            logging.info('Key: %s' % UA_PROD_APPLICATION_KEY[0:4]);
        
        headers = {'Authorization': 'Basic %s' % base64string}
        headers.update({'Content-Type': 'application/json'})

        result = urlfetch.fetch(url = url,
            payload = simplejson.dumps(data),
            method = urlfetch.POST,
            headers = headers)
        
        if result.status_code == 200:
            logging.info('Success')
            
            if user_prefs.free_quantity > 0:
                user_prefs.free_quantity = user_prefs.free_quantity - 1
            else:
                user_prefs.paid_quantity = user_prefs.paid_quantity - 1
                
            if (user_prefs.free_quantity + user_prefs.paid_quantity) <= 0:
                user_prefs.activate_flg = False
                
            user_prefs.put()
            
        else:
            logging.error('invalid status code. code: %d' % result.status_code)