def get(self):
     
     map_id = self.request.get('id')
     
     data = memcache.get('tag_index_%s' % map_id)
     if data is None:
         user_maps = UserMaps.all().filter('visible =', True).filter('map_id =', map_id).get()
         if user_maps is None:
             logging.error('map_id not found.')
             return self.error(404)
         
         user_activity_tag_index_list = UserActivityTagIndex.all().filter('user_maps =', user_maps.key()).fetch(100)
         if len(user_activity_tag_index_list) <= 0:
             logging.error('Tag index not found.')
             return self.error(404)
         
         data = []
         for user_activity_tag_index in user_activity_tag_index_list:
             data.append({'id': user_activity_tag_index.key().id(), 'tag': user_activity_tag_index.tag, 'count': user_activity_tag_index.count})
         
         memcache.add('tag_index_%s' % map_id, data, 3600)
         logging.info('Add memcache.')
         
     else:
         logging.info('Load memcache.')
     
     json = simplejson.dumps(data, ensure_ascii=False)
     self.response.content_type = 'application/json'
     self.response.out.write(json)
    def get(self):
        
        index_id = self.request.get('id')
        
        index_id_list = index_id.split(',')
        logging.info(index_id_list)
        
        tmp_data = []
        
        for index_id in index_id_list:
            # workaround
            try:
                int(index_id)
            except:
                continue
            
            data = memcache.get('tag_id_list_%s' % index_id)
            if data is None:
                user_activity_tag_index = UserActivityTagIndex.get_by_id(int(index_id))
                if user_activity_tag_index is None:
                    logging.error('Tag index not found.')
                    return self.error(404)
                
                data = []
                for marker_id in user_activity_tag_index.user_activity_id_list.split(','):
                    data.append(marker_id)
                
                memcache.add('tag_id_list_%s' % index_id, data, 3600)
                logging.info('Add memcache.')
                
            else:
                logging.info('Load memcache.')
            
            tmp_data.append(data)
        
        sorted_data = []

        if len(tmp_data) != 1:
            id_count = {}
            for tmp_list in tmp_data:
                for tmp_id in tmp_list:
                    if tmp_id not in id_count:
                        id_count[tmp_id] = 1
                    else:
                        id_count[tmp_id] += 1
                        
            for key, value in id_count.items():
                if value == len(tmp_data):
                    sorted_data.append(key)

        else:
            sorted_data = tmp_data[0]
        
        logging.info(sorted_data)
        
        json = simplejson.dumps(sorted_data, ensure_ascii=False)
        self.response.content_type = 'application/json'
        self.response.out.write(json)
    def post(self):
        map_id = self.request.get("map_id")

        user_maps = UserMaps.all().filter("visible =", True).filter("map_id =", map_id).get()
        if user_maps is None:
            logging.error("map_id not found.")
            return

        user_activity_list = UserActivity.all().filter("user_maps =", user_maps.key()).fetch(100)
        if len(user_activity_list) > 0:
            index = {}
            for user_activity in user_activity_list:
                if user_activity.tags is not None:
                    for tag in user_activity.tags.split(","):
                        if len(tag) > 0:
                            # logging.info('Tag: %s' % tag)
                            if tag in index:
                                activity_id_list = index[tag]
                                activity_id_list.append(str(user_activity.key().id()))
                            else:
                                activity_id_list = []
                                activity_id_list.append(str(user_activity.key().id()))
                                index[tag] = activity_id_list

            user_activity_tag_index_list = UserActivityTagIndex.all().filter("user_maps =", user_maps.key()).fetch(500)
            if len(user_activity_tag_index_list) > 0:
                logging.info("Clear Index: %d" % len(user_activity_tag_index_list))
                for user_activity_tag_index in user_activity_tag_index_list:
                    memcache.delete("tag_id_list_%f" % user_activity_tag_index.key().id())
                    user_activity_tag_index.delete()

                memcache.delete("tag_index_%s" % map_id)

            for tag, activity_id_list in index.items():
                user_activity_tag_index_list = UserActivityTagIndex()
                user_activity_tag_index_list.user_maps = user_maps.key()
                user_activity_tag_index_list.tag = tag
                user_activity_tag_index_list.user_activity_id_list = ",".join(activity_id_list)
                user_activity_tag_index_list.count = len(activity_id_list)
                user_activity_tag_index_list.put()

            logging.info("Add Index: %d" % len(index))