Repos / pytaku / 06bebfeedd
commit 06bebfeedd869487fe892510e4b65244daee9865
Author: Bùi Thành Nhân <hi@imnhan.com>
Date:   Sat Aug 8 08:56:07 2020 +0700

    support unread, and "follows" view improvements

diff --git a/pyproject.toml b/pyproject.toml
index 2dafa4e..0b6e855 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -1,6 +1,6 @@
 [tool.poetry]
 name = "pytaku"
-version = "0.1.9"
+version = "0.2.0"
 description = ""
 authors = ["Bùi Thành Nhân <hi@imnhan.com>"]
 license = "AGPL-3.0-only"
diff --git a/src/pytaku/decorators.py b/src/pytaku/decorators.py
index 6863355..05a9a86 100644
--- a/src/pytaku/decorators.py
+++ b/src/pytaku/decorators.py
@@ -2,7 +2,7 @@
 
 from flask import redirect, request, session, url_for
 
-from .persistence import read
+from .persistence import read, unread
 
 
 def ensure_session_version(f, CURRENT_VERSION=1):
@@ -36,9 +36,9 @@ def decorated_function(*args, **kwargs):
     return decorated_function
 
 
-def trigger_has_read(f):
+def toggle_has_read(f):
     """
-    Augments a view with the ability to mark a chapter as read if there's a
+    Augments a view with the ability to toggle a chapter's read status if there's a
     `?has_read=<chapter_id>` url param.
     """
 
@@ -46,10 +46,16 @@ def trigger_has_read(f):
     def decorated_function(*args, **kwargs):
         assert "site" in kwargs  # only use on site-specific views
         has_read_chapter_id = request.args.get("has_read")
-        if has_read_chapter_id:
-            if session.get("user"):
+        unread_chapter_id = request.args.get("unread")
+        assert not (has_read_chapter_id and unread_chapter_id)  # can't do both
+
+        if session.get("user"):
+            if has_read_chapter_id:
                 read(session["user"]["id"], kwargs["site"], has_read_chapter_id)
                 return redirect(request.url[: request.url.rfind("?")])
+            elif unread_chapter_id:
+                unread(session["user"]["id"], kwargs["site"], unread_chapter_id)
+                return redirect(request.url[: request.url.rfind("?")])
         return f(*args, **kwargs)
 
     return decorated_function
diff --git a/src/pytaku/main.py b/src/pytaku/main.py
index a1903a8..a5d7ef6 100644
--- a/src/pytaku/main.py
+++ b/src/pytaku/main.py
@@ -17,7 +17,7 @@
 
 from . import mangadex
 from .conf import config
-from .decorators import ensure_session_version, require_login, trigger_has_read
+from .decorators import ensure_session_version, require_login, toggle_has_read
 from .persistence import (
     follow,
     get_followed_titles,
@@ -162,7 +162,7 @@ def auth_view():
 
 @app.route("/title/<site>/<title_id>")
 @ensure_session_version
-@trigger_has_read
+@toggle_has_read
 def title_view(site, title_id):
     user = session.get("user", None)
     user_id = user["id"] if user else None
@@ -180,7 +180,7 @@ def title_view(site, title_id):
 
 @app.route("/chapter/<site>/<chapter_id>")
 @ensure_session_version
-@trigger_has_read
+@toggle_has_read
 def chapter_view(site, chapter_id):
     chapter = load_chapter(site, chapter_id)
     if not chapter:
diff --git a/src/pytaku/persistence.py b/src/pytaku/persistence.py
index 32e16a7..f5ed0b3 100644
--- a/src/pytaku/persistence.py
+++ b/src/pytaku/persistence.py
@@ -52,8 +52,7 @@ def load_title(site, title_id, user_id=None):
         SELECT id, name, site, cover_ext, chapters, alt_names, descriptions
         FROM title
         WHERE id = ?
-          AND site = ?
-          AND datetime(updated_at) > datetime('now', '-6 hours');
+          AND site = ?;
         """,
         (title_id, site),
     )
diff --git a/src/pytaku/templates/follows.html b/src/pytaku/templates/follows.html
index a3ccba1..ac6dda3 100644
--- a/src/pytaku/templates/follows.html
+++ b/src/pytaku/templates/follows.html
@@ -45,6 +45,10 @@
 .chapter:hover {
   background-color: #eee;
 }
+.chapter:last-child::after {
+  content: '← resume here';
+  background-color: cornsilk;
+}
 
 .group {
   font-size: .9em;
@@ -74,7 +78,10 @@
     </a>
   </div>
   <div class="chapters">
-    {% for ch in title['chapters'][:6] %}
+    {% if title['chapters']|length > 4 %}
+    <a class="more chapter" href="{{ title_url }}">and {{ title['chapters']|length - 4 }} more...</a>
+    {% endif %}
+    {% for ch in title['chapters'][-4:] %}
     <a class="chapter" href="{{ url_for('chapter_view', site=title['site'], chapter_id=ch['id']) }}">
       Chapter {{ ch['num_major'] }}{% if ch['num_minor'] %}.{{ ch['num_minor'] }}{% endif %}
               {% if ch['volume'] %}Volume {{ ch['volume'] }} {% endif %}
@@ -84,9 +91,6 @@
         {% endfor %}
     </a>
     {% endfor %}
-    {% if title['chapters']|length > 6 %}
-    <a class="more chapter" href="{{ title_url }}">and more...</a>
-    {% endif %}
   </div>
 </div>
 {% endfor %}
diff --git a/src/pytaku/templates/title.html b/src/pytaku/templates/title.html
index c5ecc47..97151c8 100644
--- a/src/pytaku/templates/title.html
+++ b/src/pytaku/templates/title.html
@@ -50,7 +50,11 @@ <h1>{{ name }}</h1>
   </tr>
   {% for chapter in chapters %}
   <tr>
-    <td>{% if chapter['is_read'] %}yes{% endif %}</td>
+    <td>
+      {% if chapter['is_read'] %}
+      {{ ibutton(href='?unread=' + chapter['id'], text='✓', color='green', title='Click to unread') }}
+      {% endif %}
+    </td>
     <td>
       <a href="{{ url_for('chapter_view', chapter_id=chapter['id'], site=site) }}">
         Chapter {{ chapter['number'] }}