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")
def index(request): """ index home page :param request: :return: """ # PC 版、手机版适配 user_agent = parse(request.META.get('HTTP_USER_AGENT', '')) pc = user_agent.is_pc # 判断是否登录用户 user = get_login_user(request) # 默认的渲染列表,区分是否登录用户 if user is None: sub_feeds = get_visitor_subscribe_feeds('', '') else: sub_feeds = get_user_subscribe_feeds(user.oauth_id, user_level=user.level) pre_load_count = 2 if pc else 10 articles = Article.objects.filter(status='active', site_id__in=sub_feeds).order_by('-id')[:pre_load_count] context = dict() context['articles'] = articles context['user'] = user context['github_oauth_key'] = settings.GITHUB_OAUTH_KEY # 记录访问来源 add_referer_stats(request.META.get('HTTP_REFERER', '')) if pc: return render(request, 'index.html', context) else: return render(request, 'mobile/index.html', context)
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
def get_my_feeds(request): """ 获取我的订阅列表;游客已订阅、推荐订阅;登陆用户已订阅、推荐订阅 """ sub_feeds = json.loads(request.POST.get('sub_feeds') or '[]') unsub_feeds = json.loads(request.POST.get('unsub_feeds') or '[]') user, reach_sub_limit = get_login_user(request), [False, 0] if user is None: visitor_sub_feeds = get_visitor_subscribe_feeds( tuple(sub_feeds), tuple(unsub_feeds)) sub_sites = Site.objects.filter( status='active', pk__in=visitor_sub_feeds).order_by('-star') recom_sites = Site.objects.filter(status='active', star__gte=20).exclude(pk__in=visitor_sub_feeds).\ order_by('-star') if len(visitor_sub_feeds) == settings.VISITOR_SUBS_LIMIT: reach_sub_limit = [True, settings.VISITOR_SUBS_LIMIT] else: user_sub_feeds = get_user_subscribe_feeds(user.oauth_id, user_level=user.level) sub_sites = Site.objects.filter( status='active', pk__in=user_sub_feeds).order_by('-star') recom_sites = Site.objects.filter(status='active', star__gte=20).exclude(pk__in=user_sub_feeds)\ .order_by('-star') if user.level < 10: if len(user_sub_feeds) == settings.USER_SUBS_LIMIT: reach_sub_limit = [True, settings.USER_SUBS_LIMIT] context = dict() context['sub_sites'] = sub_sites context['recom_sites'] = recom_sites context['user'] = user context['reach_sub_limit'] = reach_sub_limit return render(request, 'myfeeds.html', context=context)
def github_callback(request): """ 接收 github 的登录回调 """ try: code = request.GET.get('code') if code: rsp = requests.post('https://github.com/login/oauth/access_token', data={ "client_id": settings.GITHUB_OAUTH_KEY, "client_secret": settings.GITHUB_OAUTH_SECRET, "code": code, }, headers={"Accept": "application/json"}, timeout=10) if rsp.ok: access_token = rsp.json().get('access_token') if access_token: rsp = requests.get('https://api.github.com/user', headers={ "Accept": "application/json", "Authorization": f"token {access_token}", }, timeout=10) if rsp.ok: if rsp.json().get('id'): oauth_id = f'github/{rsp.json()["id"]}' oauth_name = rsp.json().get( 'name') or rsp.json().get('login') oauth_avatar = rsp.json().get('avatar_url') oauth_email = rsp.json().get('email') oauth_blog = rsp.json().get( 'blog') or rsp.json().get('html_url') oauth_ext = json.dumps(rsp.json()) # 用户信息入库 user, created = User.objects.update_or_create( oauth_id=oauth_id, defaults={ "oauth_name": oauth_name, "oauth_avatar": oauth_avatar, "oauth_email": oauth_email, "oauth_blog": oauth_blog, "oauth_ext": oauth_ext, }) if created: logger.warning(f"欢迎新用户登录:`{user.oauth_name}") add_user_sub_feeds( oauth_id, get_visitor_subscribe_feeds('', '', star=28)) add_register_count() # 用户头像存储到本地一份,国内网络会丢图 avatar = save_avatar(oauth_avatar, oauth_id) user.avatar = avatar user.save() response = redirect('index') response.set_signed_cookie('oauth_id', oauth_id, max_age=10 * 365 * 86400) response.set_signed_cookie('toast', 'LOGIN_SUCC_MSG', max_age=20) return response except (ConnectTimeout, HTTPError, ReadTimeout, Timeout, ConnectionError): logger.warning("OAuth 认证网络出现异常!") except: logger.error("OAuth 认证出现未知异常") response = redirect('index') response.set_signed_cookie('toast', 'LOGIN_ERROR_MSG', max_age=20) return response
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")