Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TaskClearKind #1361

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies = [
"gunicorn~=21.0",
"jinja2~=3.0",
"jsonschema~=4.0",
"logics-py~=0.0.11",
"pillow~=10.0",
"pyotp~=2.0",
"pytz~=2023.0",
Expand Down
103 changes: 102 additions & 1 deletion src/viur/core/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import time
import typing as t
import warnings
import logics
from deprecated.sphinx import deprecated
from functools import partial
from itertools import chain
Expand All @@ -18,7 +19,9 @@
BaseBone,
DateBone,
KeyBone,
RawBone,
ReadFromClientException,
RecordBone,
RelationalBone,
RelationalConsistency,
RelationalUpdateLevel,
Expand All @@ -33,7 +36,13 @@
ReadFromClientErrorSeverity,
getSystemInitialized,
)
from viur.core.tasks import CallDeferred, CallableTask, CallableTaskBase, QueryIter
from viur.core.tasks import (
CallableTask,
CallableTaskBase,
CallDeferred,
DeleteEntitiesIter,
QueryIter,
)

_UNDEFINED = object()
ABSTRACT_SKEL_CLS_SUFFIX = "AbstractSkel"
Expand Down Expand Up @@ -631,6 +640,8 @@
complete = True
skel.errors = []

print(data)

for key, bone in skel.items():
if (ignore is None and bone.readOnly) or key in (ignore or ()):
continue
Expand Down Expand Up @@ -2093,6 +2104,96 @@
logging.exception(f"Failed to notify {notify}")


### TaskClearKind

Check failure on line 2107 in src/viur/core/skeleton.py

View workflow job for this annotation

GitHub Actions / linter (3.12)

E266: too many leading '#' for block comment

@CallableTask
class TaskClearKind(CallableTaskBase):
key = "TaskClearKind"
name = "Clear all entities of a kind"
descr = "This task can be called to clean your database from a specific kind."

def canCall(self):
user = current.user.get()
return user and "root" in user["access"]

class dataSkel(RelSkel):
kinds = SelectBone(
descr="Kind",
values=listKnownSkeletons,
required=True,
multiple=True
)

# class FilterRow(RelSkel):
# name = StringBone(
# required=True,
# )
#
# op = SelectBone(
# required=True,
# values={
# " ": "=",
# "$lt": "<",
# "$gt": ">",
# "$lk": "like",
# },
# defaultValue=" ",
# )
#
# value = StringBone(
# required=True,
# )
#
# filters = RecordBone(
# descr="Filter",
# using=FilterRow,
# multiple=True,
# format="$(name)$(op)=$(value)"
# )

condition = RawBone(
descr="Condition",
required=True,
defaultValue="False # Fused, doesn't delete anything!\n",
params={
"tooltip": "Enter a Logics expression here to filter entries by specific skeleton values."
},
)

def execute(self, **kwargs):
user = current.user.get()
if not (user and "root" in user["access"]):
raise errors.Unauhtorized()

skel = self.dataSkel()
if not skel.fromClient(kwargs):
return self.render.action("add", skel)

try:
condition = logics.Logics(skel["condition"])
except logics.ParseException as e:
raise errors.BadRequest(f"Error parsing condition {e}")

class TaskClearKindIter(DeleteEntitiesIter):
@classmethod
def handleEntry(cls, skel, data):
if condition.run(skel):
logging.warning(f"{skel['key']!r} is a candidate for being deleted")
# super().handleEntry(skel, data)
else:
logging.info(f"{skel['key']!r} is ingored")

for kind in skel["kinds"]:
q = skeletonByKind(kind)().all()

# print(skel["filters"])
# for flt in skel["filters"]:
# print(flt)
# q.mergeExternalFilter({flt["name"] + flt["op"]: flt["value"]})

TaskClearKindIter.startIterOnQuery(q)


# Forward our references to SkelInstance to the database (needed for queries)
db.config["SkeletonInstanceRef"] = SkeletonInstance

Expand Down
1 change: 0 additions & 1 deletion src/viur/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,6 @@ def execute(self, taskID, *args, **kwargs):
return self.render.addSuccess(skel)


TaskHandler.admin = True
TaskHandler.vi = True
TaskHandler.html = True

Expand Down
Loading