예제 #1
0
def truncatechars_content(content):
    """
    获得文章内容的摘要
    :param content:
    :return:
    """
    from django.template.defaultfilters import truncatechars_html
    from colosseum.utils import get_blog_setting
    blogsetting = get_blog_setting()
    return truncatechars_html(content, blogsetting.cnfreport_sub_length)
예제 #2
0
def load_breadcrumb(cnfreport):
    """
    获得文章面包屑
    :param cnfreport:
    :return:
    """
    names = cnfreport.get_category_tree()
    from colosseum.utils import get_blog_setting
    blogsetting = get_blog_setting()
    site = get_current_site().domain
    names.append((blogsetting.sitename, '/'))
    names = names[::-1]

    return {'names': names, 'title': cnfreport.title}
예제 #3
0
def load_sidebar(user, linktype):
    """
    加载侧边栏
    :return:
    """
    logger.info('load sidebar')
    from colosseum.utils import get_blog_setting
    blogsetting = get_blog_setting()
    recent_cnfreports = CNFReport.objects.filter(
        status='p')[:blogsetting.sidebar_report_count]
    sidebar_categorys = Category.objects.all()
    extra_sidebars = SideBar.objects.filter(
        is_enable=True).order_by('sequence')
    most_read_cnfreports = CNFReport.objects.filter(
        status='p').order_by('-views')[:blogsetting.sidebar_report_count]
    dates = CNFReport.objects.datetimes('created_time', 'month', order='DESC')
    links = Links.objects.filter(
        is_enable=True).filter(Q(show_type=str(linktype)) | Q(show_type='a'))
    commment_list = Comment.objects.filter(
        is_enable=True).order_by('-id')[:blogsetting.sidebar_comment_count]
    # 标签云 计算字体大小
    # 根据总数计算出平均值 大小为 (数目/平均值)*步长
    increment = 5
    tags = Tag.objects.all()
    sidebar_tags = None
    if tags and len(tags) > 0:
        s = [t for t in [(t, t.get_report_count()) for t in tags] if t[1]]
        count = sum([t[1] for t in s])
        dd = 1 if (count == 0 or not len(tags)) else count / len(tags)
        import random
        sidebar_tags = list(
            map(lambda x: (x[0], x[1], (x[1] / dd) * increment + 10), s))
        random.shuffle(sidebar_tags)

    return {
        'recent_cnfreports': recent_cnfreports,
        'sidebar_categorys': sidebar_categorys,
        'most_read_cnfreports': most_read_cnfreports,
        'cnfreport_dates': dates,
        'sidebar_comments': commment_list,
        'user': user,
        'sidabar_links': links,
        'show_google_adsense': blogsetting.show_google_adsense,
        'google_adsense_codes': blogsetting.google_adsense_codes,
        'open_site_comment': blogsetting.open_site_comment,
        'show_gongan_code': blogsetting.show_gongan_code,
        'sidebar_tags': sidebar_tags,
        'extra_sidebars': extra_sidebars
    }
예제 #4
0
def load_report_detail(cnfreport, isindex, user):
    """
    加载文章详情
    :param carts figures report:
    :param isindex:是否列表页,若是列表页只显示摘要
    :return:
    """
    from colosseum.utils import get_blog_setting
    blogsetting = get_blog_setting()

    return {
        'cnfreport': cnfreport,
        'isindex': isindex,
        'user': user,
        'open_site_comment': blogsetting.open_site_comment,
    }
예제 #5
0
def fileupload(request):
    """
    该方法需自己写调用端来上传图片,该方法仅提供图床功能
    :param request:
    :return:
    """
    if request.method == 'POST':
        sign = request.GET.get('sign', None)
        if not sign:
            return HttpResponseForbidden()
        if not sign == get_md5(get_md5(settings.SECRET_KEY)):
            return HttpResponseForbidden()
        response = []
        for filename in request.FILES:
            timestr = datetime.datetime.now().strftime('%Y/%m/%d')
            imgextensions = ['jpg', 'png', 'jpeg', 'bmp']
            fname = u''.join(str(filename))
            isimage = len([i for i in imgextensions if fname.find(i) >= 0]) > 0
            websitesetting = get_blog_setting()

            basepath = r'{basedir}/{type}/{timestr}'.format(
                basedir=websitesetting.resource_path,
                type='files' if not isimage else 'image',
                timestr=timestr)
            if settings.TESTING:
                basepath = settings.BASE_DIR + '/uploads'
            url = 'https://resource.lylinux.net/{type}/{timestr}/{filename}'.format(
                type='files' if not isimage else 'image',
                timestr=timestr,
                filename=filename)
            if not os.path.exists(basepath):
                os.makedirs(basepath)
            savepath = os.path.join(basepath, filename)
            with open(savepath, 'wb+') as wfile:
                for chunk in request.FILES[filename].chunks():
                    wfile.write(chunk)
            if isimage:
                from PIL import Image
                image = Image.open(savepath)
                image.save(savepath, quality=20, optimize=True)
            response.append(url)
        return HttpResponse(response)

    else:
        return HttpResponse("only for post")