Ejemplo n.º 1
0
 def get(self):
     user = users.get_current_user()
     
     user_prefs_query = UserPrefs().all()
     user_prefs_query.filter('google_account =', user)
     
     user_prefs = user_prefs_query.get()
     
     if user is not None and user_prefs is not None:
         color_name_list = ColorName.all().filter('user_prefs =', user_prefs).fetch(100)
         color_list = []
         for color_name in color_name_list:
             color_list.append({'name': color_name.name,
                                'name_yomi': color_name.name_yomi,
                                'red': color_name.red,
                                'green': color_name.green,
                                'blue': color_name.blue,
                                'rank': color_name.rank})
             
         data = json.dumps(color_list, ensure_ascii=False)
         
     else:
         self.response.set_status(401)
         data = json.dumps({'state': 'failed'}, ensure_ascii=False)
         
     self.response.content_type = 'application/json'
     self.response.out.write(data)
Ejemplo n.º 2
0
 def get(self):
     
     page_id = self.request.get('page_id')
     if page_id != '':
         page_id = int(page_id)
     else:
         page_id = 1
         
     user_list_query = UserPrefs.all().filter('activate_flg =', True).filter('schedule_list =', datetime.datetime.now().hour)
     
     last_cursor = memcache.get('notify_cursor_%d' % page_id)
     if last_cursor:
         user_list_query.with_cursor(last_cursor)
     
     user_list = user_list_query.fetch(10)
     if user_list is not None:
         logging.info('count: %d' % len(user_list))
         for user in user_list:
             rand = random.random() * 100
             #rand = 0
             logging.info('random: %f, probability: %f' % (rand, user.notify_probability))
             if rand < user.notify_probability:
                 logging.info('Add notify task.')
                 try:
                     #bot_id=1
                     bot_id = user.bot_prefs_key.key().id()
                     taskqueue.add(url = '/task/find_device', params = {'user_id': user.key().id(), 'bot_id': bot_id})
                 except:
                     logging.error('Add task failed.')
             else:
                 logging.info('Boo...')
         
     cursor = user_list_query.cursor()
     
     user_list_query = UserPrefs.all().filter('activate_flg =', True).filter('schedule_list =', datetime.datetime.now().hour)
     user_list_query.with_cursor(cursor)
     user_list = user_list_query.fetch(10)
     logging.info('count: %d' % len(user_list))
     if memcache.get('notify_cursor_%d' % (page_id+1)):
         memcache.delete('notify_cursor_%d' % (page_id+1))
         
     memcache.add('notify_cursor_%d' % (page_id+1), cursor, 30)
     
     if len(user_list) <= 0:
         next_page_id = None
     else:
         next_page_id = page_id + 1
         logging.info('recursive...')
         try:
             taskqueue.add(url = '/cron/notify', params = {'page_id': next_page_id}, method='GET')
         except:
             logging.error('Add task failed.')
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 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)
        
        store_tweet_history = StoreTweetHistory.all().filter('google_account =', user).filter('expired_at >', datetime.datetime.now()).get()
        if store_tweet_history is None:
            user_prefs.free_quantity = user_prefs.free_quantity + 100
            user_prefs.activate_flg = True
            user_prefs.put()
            
            store_tweet_history = StoreTweetHistory()
            store_tweet_history.google_account = user
            store_tweet_history.expired_at = datetime.datetime.now() + datetime.timedelta(days=7)
            store_tweet_history.put()
            
            data = {'status': True}
        else:
            data = {'status': False}
        
        json = simplejson.dumps(data, ensure_ascii=False)
        self.response.content_type = 'application/json'
        self.response.out.write(json)
Ejemplo n.º 5
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.º 6
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)
     
     receipt_data = self.request.get('receipt_data')
     logging.info('Row: %s' % receipt_data)
     
     store_purchase_tmp_history = StorePurchaseTmpHistory()
     store_purchase_tmp_history.user_prefs = user_prefs.key()
     store_purchase_tmp_history.platform = 'ios'
     store_purchase_tmp_history.receipt_data = receipt_data
     store_purchase_tmp_history.status = 'pending'
     store_purchase_tmp_history.secret_key = uuid.uuid4().hex
     store_purchase_tmp_history.put()
     
     try:
         taskqueue.add(url = '/store_api/task/verify_receipt', params = {'id': store_purchase_tmp_history.key().id()})
     
         json = simplejson.dumps({'status': True, 'key': store_purchase_tmp_history.secret_key}, ensure_ascii=False)
         self.response.content_type = 'application/json'
         return self.response.out.write(json)
         
     except:
         logging.error('Add task failed.')
     
     json = simplejson.dumps({'status': False, 'key': None}, ensure_ascii=False)
     self.response.content_type = 'application/json'
     self.response.out.write(json)
Ejemplo n.º 7
0
 def get(self):
     
     user = users.get_current_user()
     if user:
         user_prefs = UserPrefs.all().filter('google_account =', user).get()
         if user_prefs is None:
             return self.redirect('/user/home')
         
     store_tweet_history = StoreTweetHistory.all().filter('google_account =', user).filter('expired_at >', datetime.datetime.now()).get()
     if store_tweet_history is None:
         can_tweet = True
         expired_at = None
     else:
         can_tweet = False
         user_timezone = timezone(user_prefs.timezone)
         loc_dt = user_timezone.localize(store_tweet_history.expired_at)
         expired_at = loc_dt.strftime('%Y-%m-%d %H:%M:%S %Z%z')
             
     template_values = {
         'can_tweet': can_tweet,
         'expired_at': expired_at
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/store/tweet.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 8
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)
     
     mode = self.request.get('mode')
     
     if mode == 'add':
         name = self.request.get('name')
         icon = self.request.get('icon')
         
         marker_icon = MarkerIcon()
         marker_icon.name = name
         marker_icon.icon = db.Blob(icon)
         marker_icon.user_prefs = user_prefs.key()
         marker_icon.visible = True
         marker_icon.put()
         
     elif mode == 'delete':
         id = self.request.get('id')
         
         marker_icon = MarkerIcon.get_by_id(int(id))
         if marker_icon is not None:
             marker_icon.visible = False
             marker_icon.put()
     
     self.redirect('/user/icon')
Ejemplo n.º 9
0
 def post(self, map_id):
     
     user = users.get_current_user()
     if user is None:
         return self.redirect(users.create_login_url(self.request.uri))
     
     user_prefs = UserPrefs.all().filter('google_account =', user).get()
     if user_prefs is None:
         return self.redirect('/map/%s' % map_id)
     
     user_maps = UserMaps.all().filter('map_id =', map_id).filter('visible =', True).filter('user_prefs =', user_prefs.key()).get()
     if user_maps is None:
         return self.redirect('/map/%s' % map_id)
     
     mode = self.request.get('mode')
     
     if mode == 'set_home':
         lat = self.request.get('lat')
         lng = self.request.get('lng')
         
         user_maps.home_geo = db.GeoPt(lat, lon=lng)
         user_maps.put()
     elif mode == 'set_marker':
         name = self.request.get('name')
         tags = self.request.get('tags')
         icon = self.request.get('icon')
         
         marker_icon = None
         if icon != '':
             marker_icon = MarkerIcon.get_by_id(int(icon))
         
         tags = tags.replace(u'、', ',').replace(' ', ',').replace(u' ', ',')
         
         lat = self.request.get('lat')
         lng = self.request.get('lng')
         
         user_activity = UserActivity()
         user_activity.name = name
         user_activity.tags = tags
         user_activity.geo = db.GeoPt(lat, lon=lng)
         user_activity.user_prefs = user_prefs.key()
         user_activity.user_maps = user_maps.key()
         
         if marker_icon is not None:
             user_activity.icon = marker_icon.key()
         
         user_activity.put()
         
         try:
             taskqueue.add(url='/task/build_tag_index', params={'map_id': map_id})
         except:
             logging.error('Taskqueue add failed.')
             
     elif mode == 'delete':
         user_maps.visible = False
         user_maps.put()
         return self.redirect('/user/home')
         
     return self.redirect('/map/%s' % map_id)
Ejemplo n.º 10
0
 def post(self):
     user = users.get_current_user()
     
     user_prefs_query = UserPrefs().all()
     user_prefs_query.filter('google_account =', user)
     
     user_prefs = user_prefs_query.get()
     
     if user is not None and user_prefs is not None:
         name = self.request.get('name')
         name_yomi = self.request.get('name_yomi')
         red = self.request.get('red')
         green = self.request.get('green')
         blue = self.request.get('blue')
         rank = self.request.get('rank')
         
         color_name = ColorName().all()\
                         .filter('user_prefs =', user_prefs)\
                         .filter('name =', name)\
                         .filter('name_yomi =', name_yomi)\
                         .filter('red =', int(red))\
                         .filter('green =', int(green))\
                         .filter('blue =', int(blue))\
                         .get()
                         
         if color_name is None:
             color_name = ColorName()
             color_name.user_prefs = user_prefs
             color_name.name = name
             color_name.name_yomi = name_yomi
             color_name.red = int(red)
             color_name.green = int(green)
             color_name.blue = int(blue)
         
         color_name.rank = int(rank)
         color_name.put()
         
         taskqueue.add(url='/task/create_crayon', params={'id': color_name.key().id()})
 
         data = json.dumps({'state': 'ok'}, ensure_ascii=False)
         
     else:
         data = json.dumps({'state': 'failed'}, ensure_ascii=False)
         
     self.response.content_type = 'application/json'
     self.response.out.write(data)
Ejemplo n.º 11
0
 def get(self):
     
     user = users.get_current_user()
     if user:
         user_prefs = UserPrefs.all().filter('google_account =', user).get()
         if user_prefs is not None:
             return self.redirect('/user/home')
             
     template_values = {
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/user/welcome.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 12
0
    def get(self, user_id):
        user_prefs = UserPrefs().all().filter('user_id =', user_id).get()

        if user_prefs is None:
            return self.error(404)

        color_name_list = ColorName.all()\
                            .filter('user_prefs =', user_prefs)\
                            .order('rank')\
                            .fetch(100, 0)

        color_list = []
        #color_list.append({'name': 'test',
        #                   'name_yomi': 'test',
        #                   'hex': '%02x%02x%02x' % (128,
        #                                             128,
        #                                             128)
        #                   })
        for color_name in color_name_list:
            is_crayon_available = CrayonData.all().filter(
                'color_name =', color_name).get()

            show_new_icon = False
            if color_name.created_at > (datetime.datetime.now() -
                                        datetime.timedelta(days=1)):
                show_new_icon = True

            color_list.append({
                'id':
                color_name.key().id(),
                'name':
                color_name.name,
                'name_yomi':
                color_name.name_yomi,
                'is_crayon_available':
                True if is_crayon_available else False,
                'show_new_icon':
                show_new_icon,
                'hex':
                '%02x%02x%02x' %
                (color_name.red, color_name.green, color_name.blue)
            })

        template_values = {
            'nick_name': user_prefs.nick_name,
            'user_id': user_prefs.user_id,
            'color_list': color_list
        }

        path = os.path.join(os.path.dirname(__file__), 'templates/user.html')
        self.response.out.write(template.render(path, template_values))
Ejemplo n.º 13
0
 def post(self):
     
     map_id = self.request.get('map_id')
     map_title = self.request.get('map_title')
     
     if map_title == '':
         map_title = map_id
     
     user = users.get_current_user()
     
     user_prefs = UserPrefs.all().filter('google_account =', user).get()
     if user_prefs is None:
         user_prefs = UserPrefs()
         user_prefs.google_account = user
         user_prefs.put()
     
     user_maps = UserMaps.all().filter('visible =', True).filter('map_id =', map_id).get()
     if user_maps is None and map_id != '':
         user_maps = UserMaps()
         user_maps.map_id = map_id
         user_maps.map_title = map_title
         user_maps.user_prefs = user_prefs
         user_maps.visible = True
         user_maps.put()
         
         self.redirect('/map/%s' % map_id)
         
     else:
         template_values = {
             'map_id': map_id,
             'map_title': map_title,
             'message': 'そのマップIDは使用できませんできした。'
         }
     
         path = os.path.join(os.path.dirname(__file__), 'templates/user/create.html')
         self.response.out.write(template.render(path, template_values))
Ejemplo n.º 14
0
 def get(self):
     
     user = users.get_current_user()
     if user:
         user_prefs = UserPrefs.all().filter('google_account =', user).get()
         if user_prefs is None:
             return self.redirect('/user/home')
         
     quantity = user_prefs.free_quantity + user_prefs.paid_quantity
     
     template_values = {
         'quantity': quantity
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/store/buy_success.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 15
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')
     
     max_notify_list = [3, 5, 10, 15, 20]
     
     template_values = {
         'user_prefs': user_prefs,
         'max_notify_list': max_notify_list,
         'timezones': pytz.common_timezones
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/user/settings.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 16
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)
        
        try:
            bot_id = user_prefs.bot_prefs_key.key().id()
            taskqueue.add(url = '/task/find_device', params = {'user_id': user_prefs.key().id(), 'bot_id': bot_id})
            data = {'status': True}
        except:
            logging.error('Add task failed.')
            data = {'status': False}
        
        json = simplejson.dumps(data, ensure_ascii=False)
        self.response.content_type = 'application/json'
        self.response.out.write(json)
Ejemplo n.º 17
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.error(404)
        
        marker_icon_list = MarkerIcon.all().filter('user_prefs =', user_prefs.key()).filter('visible =', True).fetch(100)
        
        icon_list = []
        #icon_list.append({'id':None, 'name':'Default'})
        for marker_icon in marker_icon_list:
            icon_list.append({'id':marker_icon.key().id(), 'name':marker_icon.name})

        template_values = {
            'icon_list': icon_list
        }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/user/icon.html')
        self.response.out.write(template.render(path, template_values))
Ejemplo n.º 18
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.error(404)
        
        user_maps = UserMaps.all().filter('visible =', True).filter('user_prefs =', user_prefs.key()).fetch(100)

        nickname = user.nickname()
        logout_url = users.create_logout_url('/')

        template_values = {
            'map_list': user_maps,
            'nickname': nickname,
            'logout_url': logout_url
        }
        
        path = os.path.join(os.path.dirname(__file__), 'templates/user/home.html')
        self.response.out.write(template.render(path, template_values))
Ejemplo n.º 19
0
 def get(self):
     id = self.request.get('id')
     
     cache_key = 'cached_avatar_image_%s' % id
     
     image = memcache.get(cache_key)
     if image is None:
         user_prefs = UserPrefs.all().filter('user_id =', id).get()
         if user_prefs is None:
             return self.error(404)
         
         image = user_prefs.avatar
         
         memcache.add(cache_key, image, 3600)
         
         logging.info('create cache.')
     else:
         logging.info('load cache.')
         
     self.response.headers['Content-Type'] = 'image/png'
     self.response.out.write(image)
Ejemplo n.º 20
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.º 21
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')
     
     debug_flg = self.request.get('debug')
     if debug_flg == 'true':
         debug_flg = True
     else:
         debug_flg = False
     
     quantity = user_prefs.free_quantity + user_prefs.paid_quantity
     
     try:
         bot_id = user_prefs.bot_prefs_key.bot_id
         bot_nickname = user_prefs.bot_prefs_key.nickname
     except:
         bot_id = '-'
         bot_nickname = 'Not found'
     
     logout_url = users.create_logout_url('/user/welcome')
     
     template_values = {
         'quantity': quantity,
         'user_prefs': user_prefs,
         'bot_id': bot_id,
         'bot_nickname': bot_nickname,
         'logout_url': logout_url,
         'debug_flg': debug_flg
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/user/home.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 22
0
 def post(self):
     user = users.get_current_user()
     
     user_prefs_query = UserPrefs().all()
     user_prefs_query.filter('google_account =', user)
     
     user_prefs = user_prefs_query.get()
     
     if user is not None and user_prefs is not None:
         nick_name = self.request.get('nick_name')
         user_id = self.request.get('user_id')
         avatar = self.request.get('avatar')
         
         is_id_available = True
         id_check = UserPrefs.all().filter('user_id =', user_id).get()
         if id_check is not None:
             if id_check.google_account != user:
                 is_id_available = False
                 
         avatar_result = images.resize(db.Blob(avatar), 300, 300, output_encoding=images.PNG)
                 
         if is_id_available:
             user_prefs.user_id = user_id
             user_prefs.nick_name = nick_name
             user_prefs.avatar = db.Blob(avatar_result)
             user_prefs.put()
             
             data = json.dumps({'state': 'ok', 'user_id': user_prefs.user_id}, ensure_ascii=False)
         
         else:
             logging.info('nick name is not available.')
             
             self.response.set_status(401)
             data = json.dumps({'state': 'nick name is not available.'}, ensure_ascii=False)
         
     else:
         logging.info('unauthorized.')
         
         self.response.set_status(401)
         data = json.dumps({'state': 'failed'}, ensure_ascii=False)
         
     self.response.content_type = 'application/json'
     self.response.out.write(data)
Ejemplo n.º 23
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)
     
     daily_max_notify_count = self.request.get('daily_max_notify_count')
     schedule_0000_0300 = self.request.get('0000_0300')
     schedule_0300_0600 = self.request.get('0300_0600')
     schedule_0600_0900 = self.request.get('0600_0900')
     schedule_0900_1200 = self.request.get('0900_1200')
     schedule_1200_1500 = self.request.get('1200_1500')
     schedule_1500_1800 = self.request.get('1500_1800')
     schedule_1800_2100 = self.request.get('1800_2100')
     schedule_2100_2400 = self.request.get('2100_2400')
     logging.info(schedule_2100_2400)
     
     current_timezone = timezone(self.request.get('timezone'))
     utc_timezone = timezone('UTC')
     
     #schedule = Schedule.all().filter('user_prefs_key =', user_prefs.key()).get()
     #if schedule is None:
     #    schedule = Schedule()
     schedule_list = []
     
     user_prefs.daily_max_notify_count = int(daily_max_notify_count)
     
     if schedule_0000_0300 == '1':
         user_prefs.schedule_0000_0300 = True
         utc = datetime.datetime(2011, 01, 01, 0, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 1, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 2, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_0000_0300 = False
     
     if schedule_0300_0600 == '1':
         user_prefs.schedule_0300_0600 = True
         
         utc = datetime.datetime(2011, 01, 01, 3, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 4, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 5, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_0300_0600 = False
         
     if schedule_0600_0900 == '1':
         user_prefs.schedule_0600_0900 = True
         
         utc = datetime.datetime(2011, 01, 01, 6, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 7, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 8, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_0600_0900 = False
         
     if schedule_0900_1200 == '1':
         user_prefs.schedule_0900_1200 = True
         
         utc = datetime.datetime(2011, 01, 01, 9, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 10, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 11, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_0900_1200 = False
         
     if schedule_1200_1500 == '1':
         user_prefs.schedule_1200_1500 = True
         
         utc = datetime.datetime(2011, 01, 01, 12, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 13, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 14, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_1200_1500 = False
         
     if schedule_1500_1800 == '1':
         user_prefs.schedule_1500_1800 = True
         
         utc = datetime.datetime(2011, 01, 01, 15, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 16, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 17, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_1500_1800 = False
         
     if schedule_1800_2100 == '1':
         user_prefs.schedule_1800_2100 = True
         
         utc = datetime.datetime(2011, 01, 01, 18, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 19, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 20, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_1800_2100 = False
         
     if schedule_2100_2400 == '1':
         user_prefs.schedule_2100_2400 = True
         
         utc = datetime.datetime(2011, 01, 01, 21, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 22, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
         
         utc = datetime.datetime(2011, 01, 01, 23, 0, 0, tzinfo=current_timezone)
         schedule_list.append(utc.astimezone(utc_timezone).hour)
     else:
         user_prefs.schedule_2100_2400 = False
         
     user_prefs.schedule_list = schedule_list
     user_prefs.timezone = current_timezone.zone
     
     if len(schedule_list) > 0:
         user_prefs.notify_probability = (float(user_prefs.daily_max_notify_count)  / (float(len(schedule_list)) * 6)) * 100
     else:
         user_prefs.notify_probability = 0.0
         
     user_prefs.activate_flg = True
     user_prefs.put()
     
     json = simplejson.dumps({'status': True}, ensure_ascii=False)
     self.response.content_type = 'application/json'
     self.response.out.write(json)
Ejemplo n.º 24
0
 def get(self, map_id):
     
     user_maps = UserMaps.all().filter('map_id =', map_id).filter('visible =', True).get()
     if user_maps is None:
         return self.error(404)
     
     map_title = user_maps.map_title
     
     lat = ''
     lng = ''
     if user_maps.home_geo is not None:
         lat = user_maps.home_geo.lat
         lng = user_maps.home_geo.lon
     
     user_activity_list = UserActivity.all().filter('user_maps =', user_maps.key()).order('-created_at').fetch(200)
     activity_list = []
     activity_id_list = []
     available_icon_list = []
     for user_activity in user_activity_list:
         
         icon_id = ''
         if user_activity.icon is not None:
             icon_id = user_activity.icon.key().id()
             available_icon_list.append({'id': icon_id, 'name': user_activity.icon.name})
         
         created_at = user_activity.created_at + datetime.timedelta(hours=9)
             
         activity_list.append({'id': user_activity.key().id(), 'name': user_activity.name, 'tags': user_activity.tags, 'icon': icon_id, 'lat': user_activity.geo.lat, 'lng': user_activity.geo.lon, 'created_at': created_at.strftime('%Y-%m-%d %H:%M:%S (JST)')})
         
         activity_id_list.append(str(user_activity.key().id()))
     
     user = users.get_current_user()
     if user:
         url = users.create_logout_url('/map/%s' % map_id)
     else:
         url = users.create_login_url('/map/%s' % map_id)
     
     icon_list = None
     map_owner = False
     if user is not None:
         user_prefs = UserPrefs.all().filter('google_account =', user).get()
         if user_prefs is not None:
             user_maps = UserMaps.all().filter('map_id =', map_id).filter('user_prefs =', user_prefs.key()).get()
             if user_maps is not None:
                 map_owner = True
                 marker_icon_list = MarkerIcon.all().filter('user_prefs =', user_prefs.key()).filter('visible =', True).fetch(100)
                 icon_list = []
                 for marker_icon in marker_icon_list:
                     icon_list.append({'id': marker_icon.key().id(), 'name': marker_icon.name})
     
     mode = self.request.get('mode')
     
     template_values = {
         'map_title': map_title,
         'map_id': map_id,
         'lat': lat,
         'lng': lng,
         'activity_list': activity_list,
         'activity_id_list': ','.join(activity_id_list),
         'icon_list': icon_list,
         'available_icon_list': available_icon_list,
         'map_owner': map_owner,
         'login': user and True or False,
         'url': url,
         'mode': mode
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/map/map.html')
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 25
0
 def get(self):
     user = users.get_current_user()
     if user is None:
         login_url = users.create_login_url('/api/v1/login?first=true')
         return self.redirect(login_url)
     
     continue_login = self.request.get('continue')
     first_login = self.request.get('first')
     
     user_id = gen_userid()
     
     user_prefs_query = UserPrefs().all()
     user_prefs_query.filter('google_account =', user)
     
     template_name = 'login.html'
     
     is_previously_saved = False
     user_prefs = user_prefs_query.get()
     if user_prefs is None:
         user_prefs = UserPrefs()
         user_prefs.user_id = user_id
         user_prefs.google_account = user
         user_prefs.put()
         
     else:
         color_name = ColorName.all().filter('user_prefs =', user_prefs).count(10)
         if color_name > 0:
             is_previously_saved = True
             
         if continue_login != 'true' and first_login != 'true':
             template_name = 'confirm_login.html'
     
     template_values = {
         'account': user.email(),
         'user_id': user_prefs.user_id,
         'avatar_available': True if user_prefs.avatar else False,
         'is_previously_saved': is_previously_saved
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/api/%s' % template_name)
     self.response.out.write(template.render(path, template_values))
Ejemplo n.º 26
0
 def post(self):
     user = users.get_current_user()
     
     user_prefs_query = UserPrefs().all()
     user_prefs_query.filter('google_account =', user)
     
     user_prefs = user_prefs_query.get()
     
     if user is not None and user_prefs is not None:
         raw_data = self.request.get('raw_data')
         
         if raw_data != '':
             json_data = json.loads(raw_data)
             
             saved_color_name_list = ColorName().all()\
                                 .filter('user_prefs =', user_prefs)\
                                 .fetch(100, 0)
                                 
             for saved_color_name in saved_color_name_list:
                 is_saved = False
                 for data in json_data:
                     if data['name'] == saved_color_name.name and \
                         data['red'] == saved_color_name.red and \
                         data['green'] == saved_color_name.green and \
                         data['blue'] == saved_color_name.blue:
                         
                         is_saved = True
                         continue
                     
                 if is_saved == False:
                     logging.info('delete.')
                     saved_color_name.delete()
                 else:
                     logging.info('not delete.')
             
             new_color_name_list = []
             
             for data in json_data:
                 name = data['name']
                 name_yomi = data['name_yomi']
                 red = data['red']
                 green = data['green']
                 blue = data['blue']
                 rank = data['rank']
             
                 color_name = ColorName().all()\
                                 .filter('user_prefs =', user_prefs)\
                                 .filter('name =', name)\
                                 .filter('name_yomi =', name_yomi)\
                                 .filter('red =', red)\
                                 .filter('green =', green)\
                                 .filter('blue =', blue)\
                                 .get()
                                 
                 do_create_crayon = False
                 if color_name is None:
                     color_name = ColorName()
                     color_name.user_prefs = user_prefs
                     color_name.name = name
                     color_name.name_yomi = name_yomi
                     color_name.red = int(red)
                     color_name.green = int(green)
                     color_name.blue = int(blue)
                     
                     new_color_name_list.append({'name': color_name.name,
                                'name_yomi': color_name.name_yomi,
                                'red': color_name.red,
                                'green': color_name.green,
                                'blue': color_name.blue})
                     
                     do_create_crayon = True
                 
                 color_name.rank = int(rank)
                 color_name.put()
                 
                 if do_create_crayon:
                     taskqueue.add(url='/task/create_crayon', params={'id': color_name.key().id()})
 
         data = json.dumps({'state': 'ok',
                            'user_id': user_prefs.user_id,
                            'new_color': new_color_name_list}, ensure_ascii=False)
         
     else:
         self.response.set_status(401)
         data = json.dumps({'state': 'failed'}, ensure_ascii=False)
         
     self.response.content_type = 'application/json'
     self.response.out.write(data)
Ejemplo n.º 27
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)
Ejemplo n.º 28
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()
         else:
             device_prefs.google_account = user
             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 = 'Asia/Tokyo'
             user_prefs.notify_probability = 0.0
             user_prefs.activate_flg = False
             user_prefs.delete_flg = False
             user_prefs.debug_flg = False
             
             if bot is not None:
                 user_prefs.bot_prefs_key = bot.key()
                 
             user_prefs.put()
         
     self.redirect('/user/auth/update?login=true')
Ejemplo n.º 29
0
 def get(self, map_id):
     
     user_maps = UserMaps.all().filter('visible =', True).filter('map_id =', map_id).get()
     if user_maps is None:
         return self.error(404)
     
     lat = ''
     lng = ''
     if user_maps.home_geo is not None:
         lat = user_maps.home_geo.lat
         lng = user_maps.home_geo.lon
     
     marker_id = self.request.get('marker_id')
     user = users.get_current_user()
     
     user_activity = UserActivity.get_by_id(int(marker_id))
     if user_activity is None:
         return self.error(404)
     
     if user_activity.user_prefs.google_account != user:
         return self.redirect('/map/%s' % map_id)
     
     icon_id = ''
     if user_activity.icon is not None:
         icon_id = user_activity.icon.key().id()
     
     activity_list = []
     activity_list.append({'id': user_activity.key().id(), 'name': user_activity.name, 'tags': user_activity.tags, 'icon': icon_id, 'lat': user_activity.geo.lat, 'lng': user_activity.geo.lon})
     
     if user:
         url = users.create_logout_url('/map/%s' % map_id)
     else:
         url = users.create_login_url('/map/%s' % map_id)
     
     icon_list = None
     map_owner = False
     if user is not None:
         user_prefs = UserPrefs.all().filter('google_account =', user).get()
         if user_prefs is not None:
             user_maps = UserMaps.all().filter('map_id =', map_id).filter('user_prefs =', user_prefs.key()).get()
             if user_maps is not None:
                 map_owner = True
                 marker_icon_list = MarkerIcon.all().filter('user_prefs =', user_prefs.key()).filter('visible =', True).fetch(100)
                 icon_list = []
                 for marker_icon in marker_icon_list:
                     icon_list.append({'id': marker_icon.key().id(), 'name': marker_icon.name})
     
     template_values = {
         'map_title': user_maps.map_title,
         'map_id': map_id,
         'lat': lat,
         'lng': lng,
         'activity_list': activity_list,
         'icon_list': icon_list,
         'map_owner': map_owner,
         'login': user and True or False,
         'url': url
     }
     
     path = os.path.join(os.path.dirname(__file__), 'templates/map/map_edit.html')
     self.response.out.write(template.render(path, template_values))