示例#1
0
 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)
示例#2
0
    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))