Auto delete old esign sessions#28
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds a registry boolean to enable/disable throttled auto-cleanup of e-sign sessions older than 90 days; SessionsListingView triggers the cleanup after authorization. Changes
Sequence DiagramsequenceDiagram
actor Client
participant View as SessionsListingView
participant Utils as cleanup_expired_sessions()
participant Config as get_esign_registry_auto_cleanup_enabled()
participant Registry as Plone Registry
participant Store as Session Store
Client->>View: __call__()
View->>View: available() (auth)
View->>Utils: cleanup_expired_sessions()
Utils->>Config: get_esign_registry_auto_cleanup_enabled()
Config->>Registry: read `imio.esign.auto_cleanup_enabled`
Registry-->>Config: value
Config-->>Utils: enabled/disabled
alt enabled
Utils->>Store: read annot["last_cleanup"]
Store-->>Utils: last_cleanup
alt within throttle window
Utils->>Utils: return (throttled)
else
Utils->>Store: list sessions with last_update < now - 90d
Store-->>Utils: expired sessions
loop per expired session
Utils->>Utils: log session metadata
Utils->>Store: remove_session(id)
end
Utils->>Store: annot["last_cleanup"] = now
end
else disabled
Utils->>Utils: return (disabled)
end
Utils-->>View: done
View->>View: delegate to parent __call__
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Comment |
| def __call__(self): | ||
| if not self.available(): | ||
| raise Unauthorized | ||
| cleanup_expired_sessions() |
There was a problem hiding this comment.
Ici, je lance le cleanup à chaque fois que quelqu'un visite la vue @@parapheo (et qu'il y a pas eu de cleanup dans les 24 dernières heures). Est-ce que c'est la meilleure manière de faire ? Je pensais aussi dans la fonction get_session_annotation mais ça fait beaucoup de calls.
Coverage Report for CI Build 24141156622Coverage increased (+0.08%) to 76.308%Details
Uncovered Changes
Coverage RegressionsNo coverage regressions found. Coverage Stats
💛 - Coveralls |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/imio/esign/utils.py`:
- Around line 487-495: The expiration scan currently does session["last_update"]
and will raise on malformed records; in the cleanup code (referencing annot,
annot["sessions"], last_cleanup, SESSION_EXPIRY_DAYS and CLEANUP_THROTTLE_HOURS
called from SessionsListingView.__call__), change the comprehension to
defensively handle bad entries: iterate items(), ensure each session is a dict
(or has .get), fetch last_update via session.get("last_update") and verify it's
a datetime (or comparable) before comparing to cutoff, skipping (and optionally
logging) any records that are missing/invalid so a single bad session cannot
abort the whole cleanup.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 478db144-55c2-4d21-bd28-fae58ca22793
📒 Files selected for processing (4)
src/imio/esign/browser/settings.pysrc/imio/esign/browser/views.pysrc/imio/esign/config.pysrc/imio/esign/utils.py
5f777d5 to
0da791b
Compare
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
src/imio/esign/tests/test_utils.py (1)
844-852: Restore previous registry value instead of hardcodingTruein test cleanup.Current cleanup can overwrite pre-existing suite state. Capture the original value and restore that exact value for better test isolation.
♻️ Suggested test hardening
-from imio.esign.config import set_registry_auto_cleanup_enabled +from imio.esign.config import get_registry_auto_cleanup_enabled +from imio.esign.config import set_registry_auto_cleanup_enabled ... - set_registry_auto_cleanup_enabled(False) - self.addCleanup(set_registry_auto_cleanup_enabled, True) + previous_auto_cleanup = get_registry_auto_cleanup_enabled() + self.addCleanup(set_registry_auto_cleanup_enabled, previous_auto_cleanup) + set_registry_auto_cleanup_enabled(False)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/imio/esign/tests/test_utils.py` around lines 844 - 852, The test currently hardcodes restoring registry auto-cleanup with set_registry_auto_cleanup_enabled(True), which can overwrite prior suite state; before mutating it call set_registry_auto_cleanup_enabled to read and store the original value (e.g. orig_auto_cleanup = ...), use set_registry_auto_cleanup_enabled(False) for the test, register self.addCleanup(set_registry_auto_cleanup_enabled, orig_auto_cleanup) to restore the exact previous value, and replace any later hardcoded set_registry_auto_cleanup_enabled(True) calls with set_registry_auto_cleanup_enabled(orig_auto_cleanup) or rely on the registered cleanup to restore state; update references around the existing set_registry_auto_cleanup_enabled, addCleanup, and annot["last_cleanup"] lines.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@src/imio/esign/browser/views.py`:
- Around line 61-65: The call to cleanup_expired_sessions() in
SessionsListingView.__call__ can raise and abort the request; wrap that call in
a try/except that catches all exceptions, logs the error (or records it via
existing logger) and continues without re-raising so the view returns normally
(still enforcing the Unauthorized guard and then calling
super(SessionsListingView, self).__call__()). Ensure the exception handling is
minimal and does not change the control flow on success.
---
Nitpick comments:
In `@src/imio/esign/tests/test_utils.py`:
- Around line 844-852: The test currently hardcodes restoring registry
auto-cleanup with set_registry_auto_cleanup_enabled(True), which can overwrite
prior suite state; before mutating it call set_registry_auto_cleanup_enabled to
read and store the original value (e.g. orig_auto_cleanup = ...), use
set_registry_auto_cleanup_enabled(False) for the test, register
self.addCleanup(set_registry_auto_cleanup_enabled, orig_auto_cleanup) to restore
the exact previous value, and replace any later hardcoded
set_registry_auto_cleanup_enabled(True) calls with
set_registry_auto_cleanup_enabled(orig_auto_cleanup) or rely on the registered
cleanup to restore state; update references around the existing
set_registry_auto_cleanup_enabled, addCleanup, and annot["last_cleanup"] lines.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 2dfc6f98-837d-4c5f-8981-f30e12395d02
📒 Files selected for processing (6)
CHANGES.rstsrc/imio/esign/browser/settings.pysrc/imio/esign/browser/views.pysrc/imio/esign/config.pysrc/imio/esign/tests/test_utils.pysrc/imio/esign/utils.py
✅ Files skipped from review due to trivial changes (2)
- CHANGES.rst
- src/imio/esign/browser/settings.py
🚧 Files skipped from review as they are similar to previous changes (2)
- src/imio/esign/config.py
- src/imio/esign/utils.py
e2a06f1 to
313b75d
Compare
There was a problem hiding this comment.
♻️ Duplicate comments (1)
src/imio/esign/utils.py (1)
492-495:⚠️ Potential issue | 🟠 MajorHarden against malformed session records.
Direct dictionary access to
session["last_update"]andsession["state"]will raiseKeyErrorif any session record is missing these keys, aborting the entire cleanup. Since this is called fromSessionsListingView.__call__, a single corrupt record can break the listing page.🛠️ Proposed defensive fix
cutoff = now - timedelta(days=SESSION_EXPIRY_DAYS) - expired = [ - sid for sid, session in annot["sessions"].items() - if session["last_update"] < cutoff and session["state"] != "draft" - ] + expired = [] + for sid, session in annot["sessions"].items(): + last_update = session.get("last_update") + state = session.get("state") + if not isinstance(last_update, datetime): + logger.warning( + "Auto-cleanup: skipping session %s with invalid last_update=%r", + sid, last_update + ) + continue + if state == "draft": + continue + if last_update < cutoff: + expired.append(sid)🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@src/imio/esign/utils.py` around lines 492 - 495, The list comprehension that builds expired uses direct indexing of session["last_update"] and session["state"], which can raise KeyError for malformed records; update the logic in the SessionsListingView.__call__ path (the code creating expired from sid, session in annot["sessions"].items()) to use session.get("last_update") and session.get("state"), validate the last_update value (e.g., ensure it's not None and comparable to cutoff) and treat missing/invalid keys as non-expired (skip them) so a single corrupt session record won't abort the cleanup.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@src/imio/esign/utils.py`:
- Around line 492-495: The list comprehension that builds expired uses direct
indexing of session["last_update"] and session["state"], which can raise
KeyError for malformed records; update the logic in the
SessionsListingView.__call__ path (the code creating expired from sid, session
in annot["sessions"].items()) to use session.get("last_update") and
session.get("state"), validate the last_update value (e.g., ensure it's not None
and comparable to cutoff) and treat missing/invalid keys as non-expired (skip
them) so a single corrupt session record won't abort the cleanup.
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro
Run ID: 78ea0695-49c7-4aa5-860a-769d73595bda
📒 Files selected for processing (6)
CHANGES.rstsrc/imio/esign/browser/settings.pysrc/imio/esign/browser/views.pysrc/imio/esign/config.pysrc/imio/esign/tests/test_utils.pysrc/imio/esign/utils.py
✅ Files skipped from review due to trivial changes (1)
- CHANGES.rst
🚧 Files skipped from review as they are similar to previous changes (4)
- src/imio/esign/browser/settings.py
- src/imio/esign/config.py
- src/imio/esign/browser/views.py
- src/imio/esign/tests/test_utils.py
There was a problem hiding this comment.
tester si tout est fonctionnel après effacement d'une session. Quid viewlet d'info sur un élément associé.
Il pourrait être intéressant de déplacer l'info sur chaque élément de la session pour pouvoir toujours indiquer que cet élément a été signé dans la session xx à la date du yy, et l'afficher dans le viewlet.
313b75d to
6413f78
Compare
|
@sgeulette J'ai mis à jour selon test commentaires. J'ai aussi ajouté un mot dans la description de l'état pour indiquer qu'une session sera supprimée. Jean m'a informé qu'ils ont changé le délai de suppression à 100 jours au lieu de 90. Il m'a aussi dit que le délai est compté à partir de la création de la session. Peut-être aussi voir avec @gbastien si on veut garder une trace quelque part des sessions supprimées |
6413f78 to
a7b41c3
Compare
Summary by CodeRabbit
New Features
Documentation
Tests