def delete_model(self, request, obj): """ TODO 清理相关tag的缓存 删除tag,重量级操作 1、找到所有包含这个tag的图集,修改tag字段 2、删除redis中的tag索引 3、删除该tag,刷新缓存 :param request: :param obj: :return: """ tag_name = obj.tag_name logger.info("delete tag {tag}".format(tag=ensure_utf8(tag_name))) # 存在tag包含的情况 gs = Gallery.objects.filter(tags__contains=tag_name + ",") | Gallery.objects.filter( tags__contains="," + tag_name) for gallery in gs: logger.info("delete tag for gallery : {g_id}".format( g_id=gallery.gallery_id)) tag_list = gallery.tags.split(",") if tag_name in tag_list: tag_list.remove(tag_name) gallery.tags = ",".join(tag_list) gallery.save() logger.info("delete from tag_cache {tag_id}".format( tag_id=ensure_utf8(obj.tag_id))) tag_cache.delete_tag(obj.tag_id) logger.info( "delete from db {tag_id}".format(tag_id=ensure_utf8(obj.tag_id))) obj.delete()
def save_model(self, request, obj, form, change): """ 新增或更新tag,重量级操作 1、数据库中查找tag_name不存在,存在则直接返回 2、数据库中插入tag 3、按照gallery 标题进行查找,标题中存在的则加入该tag 4、新建索引 """ if change: obj.save() tags = Tag.objects.filter(tag_name=obj.tag_name) if len(tags) == 0: obj.tag_id = get_pinyin(obj.tag_name) obj.save() gs = Gallery.objects.filter( title__contains=ensure_utf8(obj.tag_name)) for gallery in gs: logger.info("add tag for gallery : {gid}".format( gid=gallery.gallery_id)) print tag_cache.add_new_gallery(gallery.gallery_id, [obj.tag_id]) tag_list = gallery.tags.split(",") tag_list.append(obj.tag_name) gallery.tags = ",".join(tag_list) gallery.save() else: obj = tags[0]
def _get_random_galleries_by_tag(tag, count): """ 获取与tag相关的最多count个图集 :param tag: :param count: :return: """ cache_id = md5(ensure_utf8(tag.get("tag_id") + str(count))).hexdigest() if cache.get(cache_id) is None: cache.set(cache_id, tag_cache.get_random_top10000_by_tag(tag.get("tag_id"), count), timeout=15 * 60) return cache.get(cache_id)
def tag_page(request, tag_name, page_num=1): """ 包含查询tag和query数据库两种,会优先查询tag :param request: :param tag_name: :param page_num: :return: """ page_num = int(page_num) try: tag_id = get_pinyin(tag_name) tags = Tag.objects.filter(tag_id=tag_id) if len(tags) == 0: raise Tag.DoesNotExist tag = tags[0] galleries = __get_galleries_by_tag(tag_id, page_size=20, page=page_num, max_pages=100) except Tag.DoesNotExist: logger.info("tag not exist , need query {query}".format( query=ensure_utf8(tag_name))) # 每次都要扫表,很慢 gs = Gallery.objects.filter(title__contains=tag_name) g_page = Paginator(gs, 20) g_list = g_page.page(page_num) galleries = Page(object_list=g_list.object_list, page_size=20, num_pages=g_page.num_pages, number=g_list.number) tag = {"tag_name": tag_name, "tag_id": tag_name} context = { 'page_content': galleries, 'tag': tag, 'relate_tags': __get_relate_tags(galleries, tag_name) } tag_view = tag_info.info(get_pinyin(tag_name)) if tag_view: context['tag_view'] = tag_view return render(request, 'beauty/tag_page.html', __with_tag_seo(__with_normal_field(context)))
def save_gallery_item(data): """ 保存传进来的图集以及图片内容,格式如下 { gallery : { gallery_field }, images : [ image image image ] } :param data: :return: """ logger.debug(json.dumps(data)) priority = data.get("priority", None) ori_gallery = data.get("gallery", {}) title = ori_gallery['title'] if not add_title_to_md5(ensure_utf8(title)): logger.info("dedup old title") return images = data.get("images", []) _gallery = Gallery.objects.create_item( gallery_id=ori_gallery['gallery_id'], title=ensure_unicode(ori_gallery['title']), tags=ensure_unicode(ori_gallery.get("tags", "")), cover_id=ori_gallery.get("cover_id"), publish_time=ori_gallery.get("publish_time", int(time.time())), insert_time=ori_gallery.get("insert_time", int(time.time())), page_count=ori_gallery.get("page_count", len(images)) ) try: _gallery.save() except Exception, e: print e
def get_all_tag_page(): base = "http://www.meizibar.com/{tag}/1" for t in Tag.objects.all(): url = base.format(tag=ensure_utf8(t.tag_id)) yield url
def __str__(self): return "{name} -- {_id}".format(name=ensure_utf8(self.tag_name), _id=ensure_utf8(self.tag_id))
def __str__(self): return ensure_utf8(self.title)