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))
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)
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))
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)
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)