Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(BACKEND): allow bypass of revision protection
Add context_manager `unprotected_content_revision` allowing to bypass revision deletion protection. Ref: #2589
- Loading branch information
Showing
4 changed files
with
97 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from contextlib import contextmanager | ||
import typing | ||
|
||
from sqlalchemy.orm import Session | ||
|
||
|
||
class TracimSession(Session): | ||
""" | ||
Subclass of Sqlalchemy session to add tracim specific stuff | ||
""" | ||
|
||
def __init__(self, *args, **kwargs) -> None: | ||
super().__init__(*args, **kwargs) | ||
self._allow_revision_deletion = False # type: bool | ||
|
||
def set_allowed_revision_deletion(self, value: bool) -> None: | ||
self._allow_revision_deletion = value | ||
|
||
def get_allowed_revision_deletion(self) -> bool: | ||
return self._allow_revision_deletion | ||
|
||
|
||
@contextmanager | ||
def unprotected_content_revision( | ||
session: TracimSession, | ||
) -> typing.Generator[TracimSession, None, None]: | ||
""" | ||
allow to bypass protection on tracim revisions | ||
""" | ||
original_allow_revision_deletion_status = session.get_allowed_revision_deletion() | ||
try: | ||
session.set_allowed_revision_deletion(True) | ||
yield session | ||
finally: | ||
session.set_allowed_revision_deletion(original_allow_revision_deletion_status) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters