From 4fb25ba55e2cb9a134041adccf035f8b06d51832 Mon Sep 17 00:00:00 2001 From: doubaniux Date: Thu, 7 May 2020 01:10:08 +0800 Subject: [PATCH] fix following$followers page bug --- boofilsic/settings.py | 8 +- books/templates/books/list.html | 234 ++++++++++++++++++++++++++++++ books/templates/books/scrape.html | 2 +- common/mastodon/auth.py | 6 +- users/static/js/followers_list.js | 10 +- users/static/js/following_list.js | 7 +- users/templates/users/list.html | 138 ++++-------------- users/views.py | 8 +- 8 files changed, 291 insertions(+), 122 deletions(-) diff --git a/boofilsic/settings.py b/boofilsic/settings.py index 39ba74fb..1215db03 100644 --- a/boofilsic/settings.py +++ b/boofilsic/settings.py @@ -86,7 +86,7 @@ if DEBUG: 'NAME': 'test', 'USER': 'donotban', 'PASSWORD': 'donotbansilvousplait', - 'HOST': '192.168.13.70', + 'HOST': '192.168.13.72', 'OPTIONS': { 'client_encoding': 'UTF8', # 'isolation_level': psycopg2.extensions.ISOLATION_LEVEL_DEFAULT, @@ -166,8 +166,8 @@ AUTH_USER_MODEL = 'users.User' MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR, 'media/') -CLIENT_ID = '3U57sjR7uvCu8suyFlp-fiBVj9-pKt3-jd7F2gLF6EE' -CLIENT_SECRET = 'HZohdI-xR8lUyTs_bM0G3l9Na0W6bZ6DfMK3b84_E0g' +CLIENT_ID = 'kEbwT9Je5HHg4FoLx4nb0tNaIrPNs5Mw6AYlQlsj2_4' +CLIENT_SECRET = 'xwmEvlmudLCkBmvdzGf8m41Ug5o5di9xnDqeVLrcKSg' # Path to save report related images, ends without slash REPORT_MEDIA_PATH_ROOT = 'report/' @@ -176,7 +176,7 @@ BOOK_MEDIA_PATH_ROOT = 'book/' DEFAULT_BOOK_IMAGE = os.path.join(MEDIA_ROOT, BOOK_MEDIA_PATH_ROOT, 'default.jpg') # Mastodon domain name -MASTODON_DOMAIN_NAME = 'cmx-im.work' +MASTODON_DOMAIN_NAME = 'donotban.com' # Timeout of requests to Mastodon, in seconds MASTODON_TIMEOUT = 30 diff --git a/books/templates/books/list.html b/books/templates/books/list.html index e69de29b..201139b5 100644 --- a/books/templates/books/list.html +++ b/books/templates/books/list.html @@ -0,0 +1,234 @@ +{% load static %} +{% load i18n %} +{% load admin_url %} +{% load mastodon %} +{% load oauth_token %} +{% load truncate %} +{% load highlight %} + + + + + + + {% trans 'Boofilsic - 书' %} + + + + + + + + + + + +
+
+ + +
+
+
+
    + + {% for mark in marks %} + +
  • + + + +
    + + + {{ mark.book.title }} + + {% if mark.book.rating %} + +
    + + {{ mark.book.rating }} + + {% else %} + {% trans '暂无评分' %} + {% endif %} + + {% if mark.book.pub_year %} + {{ mark.book.pub_year }}{% trans '年' %} / + {% if mark.book.pub_month %} + {{ mark.book.pub_month }}{% trans '月' %} / + {% endif %} + {% endif %} + + {% if mark.book.author %} + {% trans '作者' %} + {% for author in mark.book.author %} + {{ author }}{% if not forloop.last %},{% endif %} + {% endfor %}/ + {% endif %} + + {% if mark.book.translator %} + {% trans '译者' %} + {% for translator in mark.book.translator %} + {{ translator }}{% if not forloop.last %},{% endif %} + {% endfor %}/ + {% endif %} + + {% if mark.book.orig_title %} +  {% trans '原名' %} + {{ mark.book.orig_title }} + {% endif %} + +

    + {{ mark.book.brief | truncate:170 }} +

    +
    +
  • + {% empty %} + {% trans '无结果' %} + {% endfor %} + +
+ +
+ +
+
+
+ {{ user.username }} + +
+
+
+

+ + + {% if request.user != user %} + {% trans '举报用户' %} + {% endif %} + +
+
+
+ {% trans '关注的人' %} +
+ {% trans '更多' %} +
    +
  • + + +
  • +
+
+ {% trans '被他们关注' %} +
+ {% trans '更多' %} +
    +
  • + + +
  • +
+
+
+
+
+ +
+ + + + + + + + + + + + + diff --git a/books/templates/books/scrape.html b/books/templates/books/scrape.html index 259adfd4..fa3f0b5e 100644 --- a/books/templates/books/scrape.html +++ b/books/templates/books/scrape.html @@ -61,7 +61,7 @@
- {% trans '根据豆瓣内容填写!' %} + {% trans '根据豆瓣内容填写下方表格!' %}
{% trans '剽取!' %} diff --git a/common/mastodon/auth.py b/common/mastodon/auth.py index 1ebe99b8..676145b7 100644 --- a/common/mastodon/auth.py +++ b/common/mastodon/auth.py @@ -6,14 +6,18 @@ from .api import * def obtain_token(request, code): """ Returns token if success else None. """ + # TODO change http! payload = { 'client_id': CLIENT_ID, 'client_secret': CLIENT_SECRET, - 'redirect_uri': f"http://{request.get_host()}{reverse('users:OAuth2_login')}", + 'redirect_uri': f"https://{request.get_host()}{reverse('users:OAuth2_login')}", 'grant_type': 'authorization_code', 'code': code, 'scope': 'read write' } + from boofilsic.settings import DEBUG + if DEBUG: + payload['redirect_uri']= f"http://{request.get_host()}{reverse('users:OAuth2_login')}", url = 'https://' + MASTODON_DOMAIN_NAME + API_OBTAIN_TOKEN response = post(url, data=payload) if response.status_code != 200: diff --git a/users/static/js/followers_list.js b/users/static/js/followers_list.js index 569380a8..e80de2b0 100644 --- a/users/static/js/followers_list.js +++ b/users/static/js/followers_list.js @@ -43,16 +43,17 @@ $(document).ready( function() { mast_uri, token, function(userList, request) { + let subUserList = null; if (userList.length == 0) { $(".mast-followers").hide(); } else { if (userList.length > 4){ - userList = userList.slice(0, 4); + subUserList = userList.slice(0, 4); $(".mast-followers-more").show(); } let template = $(".mast-followers li").clone(); $(".mast-followers").html(""); - userList.forEach(data => { + subUserList.forEach(data => { temp = $(template).clone(); temp.find("img").attr("src", data.avatar); if (data.display_name) { @@ -68,8 +69,9 @@ $(document).ready( function() { $(followersSpinner).remove(); // main let template = $("#main .user").clone().show(); + userList.forEach(data => { - temp = $(template).clone() + temp = $(template).clone(); temp.find(".avatar").attr("src", data.avatar); if (data.display_name) { temp.find(".user-name").html(translateEmojis(data.display_name, data.emojis)); @@ -147,7 +149,7 @@ $(document).ready( function() { mainSpinner.hide(); return; } - let template = $("#main .user:first").clone(); + let template = $("#main .user:first").clone().show(); let newUrlFlag = false; request.getResponseHeader('link').split(',').forEach(link => { if (link.includes('next')) { diff --git a/users/static/js/following_list.js b/users/static/js/following_list.js index 05791e22..6abda5c1 100644 --- a/users/static/js/following_list.js +++ b/users/static/js/following_list.js @@ -75,16 +75,17 @@ $(document).ready( function() { token, function(userList, request) { // aside + let subUserList = null; if (userList.length == 0) { $("#aside .mast-following").hide(); } else { if (userList.length > 4){ - userList = userList.slice(0, 4); + subUserList = userList.slice(0, 4); $("#aside .mast-following-more").show(); } let template = $("#aside .mast-following li").clone(); $("#aside .mast-following").html(""); - userList.forEach(data => { + subUserList.forEach(data => { temp = $(template).clone() temp.find("img").attr("src", data.avatar); if (data.display_name) { @@ -148,7 +149,7 @@ $(document).ready( function() { mainSpinner.hide(); return; } - let template = $("#main .user:first").clone(); + let template = $("#main .user:first").clone().show(); let newUrlFlag = false; request.getResponseHeader('link').split(',').forEach(link => { if (link.includes('next')) { diff --git a/users/templates/users/list.html b/users/templates/users/list.html index 201139b5..0362791f 100644 --- a/users/templates/users/list.html +++ b/users/templates/users/list.html @@ -11,14 +11,20 @@ - {% trans 'Boofilsic - 书' %} + {% if is_followers_page %} + {% trans 'Boofilsic - 被他们关注' %} + {% else %} + {% trans 'Boofilsic - 关注的人' %} + {% endif %} - - - + + {% if is_followers_page %} + + {% else %} + + {% endif %} - @@ -45,109 +51,25 @@
-
    - - {% for mark in marks %} - -
  • - - - -
    - - - {{ mark.book.title }} - - {% if mark.book.rating %} - -
    - - {{ mark.book.rating }} - - {% else %} - {% trans '暂无评分' %} - {% endif %} - - {% if mark.book.pub_year %} - {{ mark.book.pub_year }}{% trans '年' %} / - {% if mark.book.pub_month %} - {{ mark.book.pub_month }}{% trans '月' %} / - {% endif %} - {% endif %} - - {% if mark.book.author %} - {% trans '作者' %} - {% for author in mark.book.author %} - {{ author }}{% if not forloop.last %},{% endif %} - {% endfor %}/ - {% endif %} - - {% if mark.book.translator %} - {% trans '译者' %} - {% for translator in mark.book.translator %} - {{ translator }}{% if not forloop.last %},{% endif %} - {% endfor %}/ - {% endif %} - - {% if mark.book.orig_title %} -  {% trans '原名' %} - {{ mark.book.orig_title }} - {% endif %} - -

    - {{ mark.book.brief | truncate:170 }} -

    -
    -
  • - {% empty %} - {% trans '无结果' %} - {% endfor %} - -
- +
+
+ {% if is_followers_page %} + {% trans ' 被他们关注' %} + {% else %} + {% trans '关注的人' %} + {% endif %} +
+
+ + +
+
diff --git a/users/views.py b/users/views.py index a23f6b5e..4f88b6a8 100644 --- a/users/views.py +++ b/users/views.py @@ -53,6 +53,12 @@ def login(request): f"client_id={CLIENT_ID}&scope=read+write&" +\ f"redirect_uri=https://{request.get_host()}{reverse('users:OAuth2_login')}" +\ "&response_type=code" + from boofilsic.settings import DEBUG + if DEBUG: + auth_url = f"https://{MASTODON_DOMAIN_NAME}{API_OAUTH_AUTHORIZE}?" +\ + f"client_id={CLIENT_ID}&scope=read+write&" +\ + f"redirect_uri=http://{request.get_host()}{reverse('users:OAuth2_login')}" +\ + "&response_type=code" return render( request, @@ -290,7 +296,7 @@ def book_list(request, id, status): marks = paginator.get_page(page_number) return render( request, - 'users/list.html', + 'books/list.html', { 'marks': marks, 'user': user,