Exemple #1
0
def links(request):
    """
    友情链接
    """
    links = get_links('tmp_links')
    random.shuffle(links)  # 友情链接随机排序
    new_post = get_popular_top10_blogs('tmp_new_post')
    classification = get_classifications('tmp_classification')
    tag_list, music_list = get_tags_and_musics('tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
    ads_imgs = get_carousel_imgs('tmp_ads_imgs', CarouselImgType.ADS)  # 广告轮播图
    date_list = get_date_list('tmp_date_list')
    return render(request, 'blog/links.html', locals())
Exemple #2
0
def message(request):
    """
    主人寄语
    """
    own_messages = OwnerMessage.objects.all()
    own_message = random.sample(list(own_messages), 1)[0] if own_messages else ""  # 随机返回一个主人寄语
    date_list = get_date_list('tmp_date_list')
    classification = get_classifications('tmp_classification')
    new_post = get_popular_top10_blogs('tmp_new_post')
    tag_list, music_list = get_tags_and_musics('tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
    ads_imgs = get_carousel_imgs('tmp_ads_imgs', CarouselImgType.ADS)  # 广告轮播图
    return render(request, 'blog/message.html', locals())
Exemple #3
0
def archive(request):
    """
    文章归档
    """
    archive = get_archieve('tmp_archive')
    new_post = get_popular_top10_blogs('tmp_new_post')
    classification = get_classifications('tmp_classification')
    tag_list, music_list = get_tags_and_musics('tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
    ads_imgs = get_carousel_imgs('tmp_ads_imgs', CarouselImgType.ADS)  # 广告轮播图
    date_list = get_date_list('tmp_date_list')

    return render(request, 'blog/archive.html', locals())
Exemple #4
0
def about(request):
    """
    关于我
    """
    new_post = get_popular_top10_blogs('tmp_new_post')
    comments = get_cache_comments(request.path)
    page_num = request.GET.get("page") or 1
    comments, total = paginate(comments, page_num=page_num)
    classification = get_classifications('tmp_classification')
    tag_list, music_list = get_tags_and_musics('tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
    ads_imgs = get_carousel_imgs('tmp_ads_imgs', CarouselImgType.ADS)  # 广告轮播图
    date_list = get_date_list('tmp_date_list')

    return render(request, 'blog/about.html', locals())
Exemple #5
0
def archive_month(request, year, month):
    is_arch_month = True

    articles = Article.objects.filter(publish_time__year=year, publish_time__month=month, status=BlogStatus.PUBLISHED)  # 当前日期下的文章列表
    page_num = request.GET.get("page") or 1
    page_size = request.GET.get("page_size") or 5
    articles, total = paginate(articles, page_num=page_num, page_size=page_size)

    new_post = get_popular_top10_blogs('tmp_new_post')
    classification = get_classifications('tmp_classification')
    tag_list, music_list = get_tags_and_musics('tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
    ads_imgs = get_carousel_imgs('tmp_ads_imgs', CarouselImgType.ADS)  # 广告轮播图
    date_list = get_date_list('tmp_date_list')

    return render(request, 'blog/index.html', locals())
Exemple #6
0
def tagDetail(request, tag):
    is_tag, articles, total = True, [], 0
    temp = Tag.objects.filter(name=tag).first()
    if temp:
        articles = temp.article_set.filter(status=BlogStatus.PUBLISHED)
    page_num = request.GET.get("page") or 1
    page_size = request.GET.get("page_size") or 5
    articles, total = paginate(articles, page_num=page_num, page_size=page_size)

    new_post = get_popular_top10_blogs('tmp_new_post')
    classification = get_classifications('tmp_classification')
    tag_list, music_list = get_tags_and_musics('tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
    ads_imgs = get_carousel_imgs('tmp_ads_imgs', CarouselImgType.ADS)  # 广告轮播图
    date_list = get_date_list('tmp_date_list')

    return render(request, 'blog/index.html', locals())
Exemple #7
0
def home(request):
    """
    博客首页
    """
    is_home = True
    articles = get_articles('tmp_articles')
    page_num = request.GET.get("page") or 1
    page_size = request.GET.get("page_size") or 5
    articles, total = paginate(articles, page_num=page_num, page_size=page_size)

    new_post = get_popular_top10_blogs('tmp_new_post')  # 阅读量最高的十篇文章
    classification = get_classifications('tmp_classification')  # 分类,以及对应的数目
    tag_list, music_list = get_tags_and_musics('tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
    date_list = get_date_list('tmp_date_list')  # 按月归档,以及对应的文章数目
    carouse_imgs = get_carousel_imgs('tmp_carouse_imgs', CarouselImgType.BANNER)  # 轮播图
    ads_imgs = get_carousel_imgs('tmp_ads_imgs', CarouselImgType.ADS)  # 广告轮播图
    return render(request, 'blog/index.html', locals())
Exemple #8
0
def detail(request, year, month, day, id):
    """
    博客详情
    """
    try:
        article = Article.objects.filter(status=BlogStatus.PUBLISHED).get(
            id=id)
        # 文章阅读量统计,12小时内连续访问的IP只记录一次
        ip_address = get_clientip(request)
        key = ip_address + '_' + str(id)
        if cache.get(key) is None:
            article.count += 1
            article.save(update_fields=['count'])
            cache.set(key, str(datetime.now() + timedelta(hours=12)),
                      12 * 60 * 60)  # 设置12小时过期

        statics_count = Article.objects.filter(
            status=BlogStatus.PUBLISHED).aggregate(
                blog_count=Count('id', distinct=True),
                read_count=Sum('count'),
                tags_count=Count('tags', distinct=True))
        print(statics_count)
        # 生成文章目录
        if article.editor == EditorKind.Markdown:
            md = markdown.Markdown(extensions=[
                'markdown.extensions.extra', 'markdown.extensions.codehilite',
                'markdown.extensions.toc', 'markdown.extensions.tables'
            ])
            article.content = md.convert(article.content)
            toc = md.toc
        tag_list, music_list = get_tags_and_musics(
            'tmp_tags', 'tmp_musics')  # 获取所有标签,并随机赋予颜色
        not_update_days = (datetime.now() -
                           article.last_update).days  # 计算文章距现在多长时间没有更新
        return render(request, 'blog/content.html', locals())
    except Article.DoesNotExist:
        raise Http404