Beispiel #1
0
def get_article_update_view(request):
    """
    获取更新的文章列表视图;登录用户展示其订阅内容
    """
    # 请求参数获取
    sub_feeds = json.loads(request.POST.get('sub_feeds') or '[]')
    unsub_feeds = json.loads(request.POST.get('unsub_feeds') or '[]')
    page_size = int(request.POST.get('page_size', 10))
    page = int(request.POST.get('page', 1))
    mobile = request.POST.get('mobile', False)
    onlyunread = request.POST.get('onlyunread', 'no') == 'yes'

    user = get_login_user(request)

    # 我的订阅源
    if user is None:
        my_sub_feeds = get_visitor_subscribe_feeds(tuple(sub_feeds),
                                                   tuple(unsub_feeds))
    else:
        my_sub_feeds = get_user_subscribe_feeds(user.oauth_id,
                                                user_level=user.level)

    # 获取文章索引列表
    my_articles = set()
    for site_id in my_sub_feeds:
        my_articles.update(get_recent_site_articles(site_id))

    # 过滤
    if user and onlyunread:
        my_articles = get_user_unread_articles(user.oauth_id, my_articles)

    my_articles = sorted(my_articles, reverse=True)

    if my_articles:
        # 分页处理
        try:
            paginator_obj = Paginator(my_articles, page_size)
        except:
            logger.warning(
                f"分页参数错误:`{page}`{page_size}`{sub_feeds}`{unsub_feeds}")
            return HttpResponseForbidden("Page Number Error")

        pg = paginator_obj.page(page)
        num_pages = paginator_obj.num_pages

        articles = Article.objects.filter(
            uindex__in=pg.object_list).order_by('-id')[:50]

        context = dict()
        context['articles'] = articles
        context['num_pages'] = num_pages
        context['user'] = user
        context['pg'] = pg

        if mobile:
            return render(request, 'mobile/list.html', context=context)
        else:
            return render(request, 'left/list_view.html', context=context)

    return HttpResponseForbidden("No Feeds Subscribed")
Beispiel #2
0
def get_lastweek_articles(request):
    """
    游客用户返回过去一周的文章 id 列表;登录用户返回过去一周的未读数
    """
    uid = request.POST.get('uid', '')
    user = get_login_user(request)
    sub_feeds = json.loads(request.POST.get('sub_feeds') or '[]')
    unsub_feeds = json.loads(request.POST.get('unsub_feeds') or '[]')
    ext = request.POST.get('ext', '')

    reach_sub_limit = False

    logger.info(f"查询未读数:`{uid}`{ext}")

    if user is None:
        my_sub_feeds = get_visitor_subscribe_feeds(tuple(sub_feeds),
                                                   tuple(unsub_feeds))
        reach_sub_limit = len(my_sub_feeds) == settings.VISITOR_SUBS_LIMIT
    else:
        my_sub_feeds = get_user_subscribe_feeds(user.oauth_id,
                                                user_level=user.level)

        if user.level < 10:
            reach_sub_limit = len(my_sub_feeds) == settings.USER_SUBS_LIMIT

    # 异步更新任务
    django_rq.enqueue(update_sites_async, list(my_sub_feeds))

    # 获取文章索引列表
    my_toread_articles = set()
    for site_id in my_sub_feeds:
        my_toread_articles.update(get_recent_site_articles(site_id))

    my_toread_articles = list(my_toread_articles)

    if user:
        my_unread_count = get_user_unread_count(user.oauth_id,
                                                my_toread_articles)

        # 标记用户登陆
        set_user_visit_day(user.oauth_id)

        response = JsonResponse({"result": my_unread_count})
        if reach_sub_limit:
            response.set_signed_cookie('toast',
                                       'SUBS_LIMIT_ERROR_MSG',
                                       max_age=20)

        return response
    else:
        response = JsonResponse({"result": my_toread_articles})
        if reach_sub_limit:
            response.set_signed_cookie('toast',
                                       'SUBS_LIMIT_ERROR_MSG',
                                       max_age=20)

        return response
Beispiel #3
0
def get_site_article_update_view(request):
    """
    获取某个站点的更新文章列表视图
    """
    # 请求参数获取
    site_id = request.POST.get('site_id', 0)
    page_size = int(request.POST.get('page_size', 10))
    page = int(request.POST.get('page', 1))

    user = get_login_user(request)

    site = Site.objects.get(pk=site_id, status='active')
    recent_articles = list(get_recent_site_articles(site.pk))

    # 查看文章数限制
    site_articles_limit = 9999
    if user:
        if user.level < 10:
            site_articles_limit = settings.USER_SITE_ARTICLES_LIMIT
    else:
        site_articles_limit = settings.VISITOR_SITE_ARTICLES_LIMIT

    site_articles = Article.objects.filter(
        site=site, status='active').order_by('-id')[:site_articles_limit]

    if site_articles:
        # 分页处理
        paginator_obj = Paginator(site_articles, page_size)
        try:
            # 页面及数据
            pg = paginator_obj.page(page)
            num_pages = paginator_obj.num_pages

            context = dict()
            context['pg'] = pg
            context['num_pages'] = num_pages
            context['site'] = site
            context['user'] = user
            context['articles'] = json.dumps(recent_articles)

            return render(request, 'left/list2_view.html', context=context)
        except:
            logger.warning(f"分页参数错误:`{page}`{page_size}`{site_id}")
            return HttpResponseForbidden("Page Number Error")

    return HttpResponseForbidden("No Sites Data")
Beispiel #4
0
def user_mark_read_all(request):
    """
    设置批量已读,如不传 ids 则设置全部已读
    """
    ids = json.loads(request.POST.get('ids') or '[]')
    user = get_login_user(request)

    if user:
        if not ids:
            my_sub_feeds = get_user_subscribe_feeds(user.oauth_id,
                                                    user_level=user.level)

            ids = set()
            for site_id in my_sub_feeds:
                ids.update(get_recent_site_articles(site_id))

        set_user_read_articles(user.oauth_id, ids)

        if ids:
            return get_lastweek_articles(request)
        else:
            return JsonResponse({})

    return HttpResponseNotFound("Param Error")
Beispiel #5
0
def get_site_update_view(request):
    """
    获取更新的全局站点视图,游客 100 个,登陆用户 200 个站点
    """
    sub_feeds = json.loads(request.POST.get('sub_feeds') or '[]')
    unsub_feeds = json.loads(request.POST.get('unsub_feeds') or '[]')
    page_size = int(request.POST.get('page_size', 10))
    page = int(request.POST.get('page', 1))
    onlyunread = request.POST.get('onlyunread', 'no') == 'yes'

    user = get_login_user(request)

    if user is None:
        my_feeds = get_visitor_subscribe_feeds(tuple(sub_feeds),
                                               tuple(unsub_feeds))
    else:
        my_feeds = get_user_subscribe_feeds(user.oauth_id,
                                            user_level=user.level)

    # 过滤有内容更新的
    if user and onlyunread:
        my_feeds = get_user_unread_sites(user.oauth_id, my_feeds)

    my_feeds = sorted(my_feeds,
                      key=lambda t: get_site_last_id(t),
                      reverse=True)

    if my_feeds:
        # 分页处理
        try:
            paginator_obj = Paginator(my_feeds, page_size)
        except:
            logger.warning(
                f"分页参数错误:`{page}`{page_size}`{sub_feeds}`{unsub_feeds}")
            return HttpResponseNotFound("Page Number Error")

        pg = paginator_obj.page(page)
        num_pages = paginator_obj.num_pages
        sites = Site.objects.filter(pk__in=pg.object_list,
                                    status='active').order_by('-star')[:50]

        for site in sites:
            recent_articles = get_recent_site_articles(site.pk)

            site.update_count = len(recent_articles)
            site.update_ids = json.dumps(list(recent_articles))
            site.update_time = get_site_last_id(site.pk)

            if user:
                site.unread_count = get_user_unread_count(
                    user.oauth_id, recent_articles)

        context = dict()
        context['pg'] = pg
        context['sites'] = sites
        context['num_pages'] = num_pages
        context['user'] = user

        return render(request, 'left/site_view.html', context=context)

    return HttpResponseNotFound("No Feeds Subscribed")