mirror of https://gitlab.com/bashrc2/epicyon
Add remove button to reading statuses on profile screen
parent
ce259b74e2
commit
6f4ff8163e
48
reading.py
48
reading.py
|
@ -249,15 +249,20 @@ def get_reading_status(post_json_object: {},
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def _add_book_to_reader(reader_books_json: {}, book_dict: {}) -> bool:
|
def _add_book_to_reader(reader_books_json: {}, book_dict: {},
|
||||||
|
debug: bool) -> bool:
|
||||||
"""Updates reader books
|
"""Updates reader books
|
||||||
"""
|
"""
|
||||||
if not book_dict.get('published'):
|
if not book_dict.get('published'):
|
||||||
|
if debug:
|
||||||
|
print('_add_book_to_reader no published field')
|
||||||
return False
|
return False
|
||||||
book_url = book_dict['href']
|
book_url = book_dict['href']
|
||||||
book_event_type = book_dict['type']
|
book_event_type = book_dict['type']
|
||||||
if not reader_books_json.get(book_url):
|
if not reader_books_json.get(book_url):
|
||||||
reader_books_json[book_url] = {}
|
reader_books_json[book_url] = {}
|
||||||
|
if debug:
|
||||||
|
print('_add_book_to_reader first book')
|
||||||
else:
|
else:
|
||||||
# has this book event already been stored?
|
# has this book event already been stored?
|
||||||
if reader_books_json[book_url].get(book_event_type):
|
if reader_books_json[book_url].get(book_event_type):
|
||||||
|
@ -265,11 +270,18 @@ def _add_book_to_reader(reader_books_json: {}, book_dict: {}) -> bool:
|
||||||
if book_dict.get('updated'):
|
if book_dict.get('updated'):
|
||||||
if prev_book_dict.get('updated'):
|
if prev_book_dict.get('updated'):
|
||||||
if prev_book_dict['updated'] == book_dict['updated']:
|
if prev_book_dict['updated'] == book_dict['updated']:
|
||||||
|
if debug:
|
||||||
|
print('_add_book_to_reader ' +
|
||||||
|
'updated date already seen')
|
||||||
return False
|
return False
|
||||||
else:
|
else:
|
||||||
if prev_book_dict['published'] == book_dict['updated']:
|
if prev_book_dict['published'] == book_dict['updated']:
|
||||||
|
if debug:
|
||||||
|
print('_add_book_to_reader ' +
|
||||||
|
'published date already seen')
|
||||||
return False
|
return False
|
||||||
if prev_book_dict['published'] == book_dict['published']:
|
if prev_book_dict['published'] == book_dict['published']:
|
||||||
|
|
||||||
return False
|
return False
|
||||||
# store the book event
|
# store the book event
|
||||||
reader_books_json[book_url][book_event_type] = book_dict
|
reader_books_json[book_url][book_event_type] = book_dict
|
||||||
|
@ -278,14 +290,19 @@ def _add_book_to_reader(reader_books_json: {}, book_dict: {}) -> bool:
|
||||||
published = book_dict['published']
|
published = book_dict['published']
|
||||||
if book_dict.get('updated'):
|
if book_dict.get('updated'):
|
||||||
published = book_dict['updated']
|
published = book_dict['updated']
|
||||||
|
if '.' in published:
|
||||||
|
published = published.split('.')[0] + 'Z'
|
||||||
post_time_object = \
|
post_time_object = \
|
||||||
date_from_string_format(published, ["%Y-%m-%dT%H:%M:%S%z"])
|
date_from_string_format(published, ["%Y-%m-%dT%H:%M:%S%z",
|
||||||
|
"%Y-%m-%dT%H:%M:%S%Z"])
|
||||||
if post_time_object:
|
if post_time_object:
|
||||||
baseline_time = date_epoch()
|
baseline_time = date_epoch()
|
||||||
days_diff = post_time_object - baseline_time
|
days_diff = post_time_object - baseline_time
|
||||||
post_days_since_epoch = days_diff.total_seconds()
|
post_days_since_epoch = days_diff.total_seconds()
|
||||||
reader_books_json['timeline'][post_days_since_epoch] = book_url
|
reader_books_json['timeline'][post_days_since_epoch] = book_url
|
||||||
return True
|
return True
|
||||||
|
elif debug:
|
||||||
|
print('_add_book_to_reader published date not recognised ' + published)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
|
@ -433,7 +450,7 @@ def store_book_events(base_dir: str,
|
||||||
elif os.path.isfile(reader_books_filename):
|
elif os.path.isfile(reader_books_filename):
|
||||||
# if not in cache then load from file
|
# if not in cache then load from file
|
||||||
reader_books_json = load_json(reader_books_filename)
|
reader_books_json = load_json(reader_books_filename)
|
||||||
if _add_book_to_reader(reader_books_json, book_dict):
|
if _add_book_to_reader(reader_books_json, book_dict, debug):
|
||||||
if not save_json(reader_books_json, reader_books_filename):
|
if not save_json(reader_books_json, reader_books_filename):
|
||||||
if debug:
|
if debug:
|
||||||
print('DEBUG: unable to save reader book event')
|
print('DEBUG: unable to save reader book event')
|
||||||
|
@ -451,6 +468,8 @@ def store_book_events(base_dir: str,
|
||||||
first_actor = books_cache['reader_list'][0]
|
first_actor = books_cache['reader_list'][0]
|
||||||
books_cache['reader_list'].remove(first_actor)
|
books_cache['reader_list'].remove(first_actor)
|
||||||
del books_cache['readers'][actor]
|
del books_cache['readers'][actor]
|
||||||
|
elif debug:
|
||||||
|
print('_add_book_to_reader failed ' + str(book_dict))
|
||||||
|
|
||||||
book_id = book_url.replace('/', '#')
|
book_id = book_url.replace('/', '#')
|
||||||
book_filename = books_path + '/' + book_id + '.json'
|
book_filename = books_path + '/' + book_id + '.json'
|
||||||
|
@ -470,7 +489,9 @@ def store_book_events(base_dir: str,
|
||||||
|
|
||||||
|
|
||||||
def html_profile_book_list(base_dir: str, actor: str, no_of_books: int,
|
def html_profile_book_list(base_dir: str, actor: str, no_of_books: int,
|
||||||
translate: {}) -> str:
|
translate: {},
|
||||||
|
nickname: str, domain: str,
|
||||||
|
authorized: bool) -> str:
|
||||||
"""Returns html for displaying a list of books on a profile screen
|
"""Returns html for displaying a list of books on a profile screen
|
||||||
"""
|
"""
|
||||||
reading_path = base_dir + '/accounts/reading'
|
reading_path = base_dir + '/accounts/reading'
|
||||||
|
@ -544,11 +565,28 @@ def html_profile_book_list(base_dir: str, actor: str, no_of_books: int,
|
||||||
if book_reading:
|
if book_reading:
|
||||||
html_str += ' <br>' + \
|
html_str += ' <br>' + \
|
||||||
translate['reading'].title() + '\n'
|
translate['reading'].title() + '\n'
|
||||||
|
# book star rating
|
||||||
if book_rating is not None:
|
if book_rating is not None:
|
||||||
html_str += ' <br>'
|
html_str += ' <br>'
|
||||||
for _ in range(int(book_rating)):
|
for _ in range(int(book_rating)):
|
||||||
html_str += '⭐'
|
html_str += '⭐'
|
||||||
html_str += ' (' + str(book_rating) + ')'
|
html_str += ' (' + str(book_rating) + ')\n'
|
||||||
|
# remove button
|
||||||
|
if authorized:
|
||||||
|
if actor.endswith('/users/' + nickname) and \
|
||||||
|
'://' + domain in actor:
|
||||||
|
html_str += \
|
||||||
|
' <br>' + \
|
||||||
|
' <form method="POST" action="' + \
|
||||||
|
'/users/' + nickname + '/removereadingstatus">\n' + \
|
||||||
|
' ' + \
|
||||||
|
'<input type="hidden" name="actor" value="' + \
|
||||||
|
actor + '">\n' + \
|
||||||
|
' ' + \
|
||||||
|
'<button type="submit" class="button" ' + \
|
||||||
|
'name="submitRemoveReadingStatus">' + \
|
||||||
|
translate['Remove'] + '</button>\n' + \
|
||||||
|
' </form>\n'
|
||||||
html_str += ' </div>\n'
|
html_str += ' </div>\n'
|
||||||
|
|
||||||
html_str += ' </div>\n'
|
html_str += ' </div>\n'
|
||||||
|
|
14
tests.py
14
tests.py
|
@ -8327,6 +8327,8 @@ def _test_book_link(base_dir: str):
|
||||||
max_recent_books,
|
max_recent_books,
|
||||||
books_cache,
|
books_cache,
|
||||||
max_cached_readers)
|
max_cached_readers)
|
||||||
|
expected_readers = 1
|
||||||
|
print('reader_list 1: ' + str(books_cache['reader_list']))
|
||||||
|
|
||||||
actor = "https://some.instance/users/hiw"
|
actor = "https://some.instance/users/hiw"
|
||||||
id_str = actor + "/statuses/6293"
|
id_str = actor + "/statuses/6293"
|
||||||
|
@ -8442,6 +8444,8 @@ def _test_book_link(base_dir: str):
|
||||||
max_recent_books,
|
max_recent_books,
|
||||||
books_cache,
|
books_cache,
|
||||||
max_cached_readers)
|
max_cached_readers)
|
||||||
|
expected_readers += 1
|
||||||
|
print('reader_list 2: ' + str(books_cache['reader_list']))
|
||||||
|
|
||||||
title = 'The Rise of the Meritocracy'
|
title = 'The Rise of the Meritocracy'
|
||||||
image_url = 'https://bookwyrm.instance/images/previews/covers/6735.jpg'
|
image_url = 'https://bookwyrm.instance/images/previews/covers/6735.jpg'
|
||||||
|
@ -8492,6 +8496,8 @@ def _test_book_link(base_dir: str):
|
||||||
max_recent_books,
|
max_recent_books,
|
||||||
books_cache,
|
books_cache,
|
||||||
max_cached_readers)
|
max_cached_readers)
|
||||||
|
expected_readers += 1
|
||||||
|
print('reader_list 3: ' + str(books_cache['reader_list']))
|
||||||
|
|
||||||
title = 'Pirate Enlightenment, or the Real Libertalia'
|
title = 'Pirate Enlightenment, or the Real Libertalia'
|
||||||
image_url = 'https://bookwyrm.instance/images/previews/covers/5283.jpg'
|
image_url = 'https://bookwyrm.instance/images/previews/covers/5283.jpg'
|
||||||
|
@ -8539,14 +8545,16 @@ def _test_book_link(base_dir: str):
|
||||||
max_recent_books,
|
max_recent_books,
|
||||||
books_cache,
|
books_cache,
|
||||||
max_cached_readers)
|
max_cached_readers)
|
||||||
|
expected_readers += 1
|
||||||
|
print('reader_list 4: ' + str(books_cache['reader_list']))
|
||||||
|
|
||||||
assert books_cache
|
assert books_cache
|
||||||
assert 'reader_list' in books_cache
|
assert 'reader_list' in books_cache
|
||||||
if len(books_cache['reader_list']) != 3:
|
if len(books_cache['reader_list']) != expected_readers:
|
||||||
pprint(books_cache)
|
pprint(books_cache)
|
||||||
print('reader_list: ' + str(books_cache['reader_list']))
|
print('reader_list: ' + str(books_cache['reader_list']))
|
||||||
assert len(books_cache['reader_list']) == 4
|
assert len(books_cache['reader_list']) == expected_readers
|
||||||
assert books_cache['reader_list'][3] == actor
|
assert books_cache['reader_list'][expected_readers - 1] == actor
|
||||||
assert books_cache['readers'].get(actor)
|
assert books_cache['readers'].get(actor)
|
||||||
|
|
||||||
if os.path.isdir(base_dir):
|
if os.path.isdir(base_dir):
|
||||||
|
|
|
@ -585,7 +585,8 @@ def _get_profile_header(base_dir: str, http_prefix: str, nickname: str,
|
||||||
occupation_name: str,
|
occupation_name: str,
|
||||||
actor_proxied: str,
|
actor_proxied: str,
|
||||||
person_url: str,
|
person_url: str,
|
||||||
no_of_books: int) -> str:
|
no_of_books: int,
|
||||||
|
authorized: bool) -> str:
|
||||||
"""The header of the profile screen, containing background
|
"""The header of the profile screen, containing background
|
||||||
image and avatar
|
image and avatar
|
||||||
"""
|
"""
|
||||||
|
@ -716,7 +717,8 @@ def _get_profile_header(base_dir: str, http_prefix: str, nickname: str,
|
||||||
|
|
||||||
# book events for this actor
|
# book events for this actor
|
||||||
html_str += html_profile_book_list(base_dir, person_url, no_of_books,
|
html_str += html_profile_book_list(base_dir, person_url, no_of_books,
|
||||||
translate)
|
translate, nickname, domain,
|
||||||
|
authorized)
|
||||||
return html_str
|
return html_str
|
||||||
|
|
||||||
|
|
||||||
|
@ -870,7 +872,8 @@ def _get_profile_header_after_search(base_dir: str,
|
||||||
translate)
|
translate)
|
||||||
# book events for this actor
|
# book events for this actor
|
||||||
html_str += html_profile_book_list(base_dir, person_url, no_of_books,
|
html_str += html_profile_book_list(base_dir, person_url, no_of_books,
|
||||||
translate)
|
translate,
|
||||||
|
nickname, domain, authorized)
|
||||||
|
|
||||||
return html_str
|
return html_str
|
||||||
|
|
||||||
|
@ -1264,7 +1267,7 @@ def html_profile(signing_priv_key_pem: str,
|
||||||
access_keys, joined_date,
|
access_keys, joined_date,
|
||||||
occupation_name,
|
occupation_name,
|
||||||
actor_proxied, actor,
|
actor_proxied, actor,
|
||||||
no_of_books)
|
no_of_books, authorized)
|
||||||
|
|
||||||
# keyboard navigation
|
# keyboard navigation
|
||||||
user_path_str = '/users/' + nickname
|
user_path_str = '/users/' + nickname
|
||||||
|
|
Loading…
Reference in New Issue