Example #1
0
def blogs():
    if request.method == 'GET':
        page = request.args.get('page', 1, int)
        per_page = 10
        if current_user.is_authenticated is False:
            visible = [Blog.VISIBLE_ALL]
        else:
            visible = [Blog.VISIBLE_ALL, Blog.VISIBLE_LOGIN, Blog.VISIBLE_OWNER]
        blogs = list()
        for item in Blog.objects(delete_time=None, visible__in=visible):
            if item.visible == Blog.VISIBLE_OWNER and item.author != current_user.id:
                continue
            else:
                blogs.append(item.as_dict())
        # blogs = [item.as_dict() for item in Blog.objects(delete_time=None)]
        blogs.sort(key=lambda item: item['create_time'], reverse=True)
        total = len(blogs)
        blogs = blogs[per_page * (page - 1): per_page * page]
    else:
        pass

    monthes = get_all_month()

    return render_template('home/blog.html', blogs=blogs, monthes=monthes, total=total, per_page=per_page, page=page,
                           keyword='', tags=get_tags_cloud(), blog_count=get_blog_count(), next_page_url='blogs')
Example #2
0
def get_all_month():
    res_list = set()
    for item in Blog.objects(delete_time=None):
        res_list.add(format_datetime(item.create_time, '%Y-%m'))
    res_list = list(res_list)
    res_list.sort(reverse=True)
    return res_list
Example #3
0
def get_blog_count():
    """
    获得自己的博客数量
    user_id 是ObjectId
    """
    if not current_user.is_active:
        return 0
    count = Blog.objects(author=current_user.id, delete_time=None).count()
    return count
Example #4
0
def show_blog(id):
    blog = Blog.objects.with_id(ObjectId(id))
    if not blog:
        return jsonify(success=False, error='不存在该日志')
    if (blog.visible in [Blog.VISIBLE_LOGIN, Blog.VISIBLE_OWNER] and current_user.is_authenticated is False) \
        or (blog.visible == Blog.VISIBLE_OWNER and current_user.id != blog.author):
        return abort(403)

    # 增加观看量
    Blog.objects(id=ObjectId(id)).update(inc__view_count=1)
    # 设置上个观察者的ip
    Blog.objects(id=ObjectId(id)).update(set__last_view_ip=request.remote_addr)
    # 设置上个观看者的用户id
    Blog.objects(id=ObjectId(id)).update(set__last_view_user=current_user.id if current_user.is_active and current_user.is_anounymous is False else None)
    # 设置上次观看的时间
    Blog.objects(id=ObjectId(id)).update(set__last_view_time=now_lambda())

    prev, next = Blog.get_prev_next(unicode(blog.id), current_user)
    monthes = get_all_month()
    return render_template('home/blog_detail.html', blog=blog.as_dict(), prev=prev, next=next, monthes=monthes, tags=get_tags_cloud())
Example #5
0
def get_archive_blogs(month):
    """返回某一月的blog"""
    page = request.args.get('page', 1, int)
    per_page = 10
    if current_user.is_authenticated is False:
        visible = [Blog.VISIBLE_ALL]
    else:
        visible = [Blog.VISIBLE_ALL, Blog.VISIBLE_LOGIN, Blog.VISIBLE_OWNER]
    blogs = list()
    for item in Blog.objects(delete_time=None, visible__in=visible,
                             create_time__gte=to_datetime(month + '-01 00:00:00', '%Y-%m-%d %H:%M:%S'),
                             create_time__lt=to_datetime(get_next_month(month) + '-01 00:00:00', '%Y-%m-%d %H:%M:%S')):
        if item.visible == Blog.VISIBLE_OWNER and item.author != current_user.id:
            continue
        else:
            blogs.append(item.as_dict())

    blogs.sort(key=lambda item: item['create_time'], reverse=True)
    total = len(blogs)
    blogs = blogs[per_page * (page - 1): per_page * page]
    monthes = get_all_month()
    return render_template('home/blog.html', blogs=blogs, monthes=monthes, total=total, per_page=per_page, page=page,
                           keyword='', tags=get_tags_cloud(), blog_count=get_blog_count(), next_page_url='blog-archive/%s' % month)