Exemple #1
0
def get_tags():
    res_list = list()
    for item in BlogTag.objects(delete_time=None).order_by('-count'):
        res_list.append(item.as_dict())

    # 更新实际使用的tag的数量,之前的数量已经不准确
    es = ES.connect_host()
    ret = es.search('simpleblog', 'blogpost', {
        'aggs': {
            'all_tags': {
                'terms': {
                    'field': 'tags',
                    'size': 0
                }
            }
        }
    }, size=1000)

    name_count = dict([(b['key'], b['doc_count']) for b in ret['aggregations']['all_tags']['buckets']])

    for item in res_list:
        if item['name'] in name_count:
            item['count'] = name_count.get(item['name'])
            item['name_count'] = '%s (%d)' % (item['name'], item['count'])
        else:
            item['count'] = 0
            item['name_count'] = '%s (%d)' % (item['name'], 0)

    return res_list
Exemple #2
0
def blog_edit():
    if request.method == 'GET':
        tags = get_tags()
        tags = map(lambda x: x['name'], tags)
        blog = None
        _id = request.args.get('id', '')
        if _id:
            blog = Blog.objects.with_id(_id)
            for tag in blog.tags:
                if tag in tags:
                    tags.remove(tag)
        tag_list = tags
        tags = json.dumps(tags)
        if blog:
            return render_template('home/blog_edit2.html', blog=blog, data=json.dumps(blog.as_dict()), tags=tags,
                                   tag_list=tag_list)
        else:
            return render_template('home/blog_edit2.html', blog=None, data=json.dumps({}), tags=tags, tag_list=tag_list)
    else:
        _id = request.form.get('id', '').strip()
        if _id:
            blog = Blog.objects.with_id(_id)
        else:
            blog = Blog()
        title = request.form.get('title', '').strip()
        content = request.form.get('content', '').strip()
        visible = request.form.get('visible', 1, int)
        tags = request.form.getlist('tags[]')
        blog.title = title
        blog.content = content
        blog.author = current_user.id
        blog.visible = visible
        blog.tags = tags
        if _id:
            blog.update_time = now_lambda()
        else:
            blog.create_time = now_lambda()
            blog.update_time = blog.create_time
        try:
           blog.save()
        except ValidationError, e:
            return jsonify(success=False, error='save failed %s' % e)

        # 保存tag
        for tag in tags:
            blog_tag = BlogTag.objects(name=tag).first()
            if not blog_tag:
                blog_tag = BlogTag(name=tag, count=1, last_use_time=now_lambda(), create_time=now_lambda())
                blog_tag.save()
            else:
                blog_tag.update_time = now_lambda()
                blog_tag.last_use_time = now_lambda()
                blog_tag.count += 1
                blog_tag.save()
        return jsonify(success=True, error='提交成功', blog=blog.reload().as_dict())