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: Provide add_or_edit root-only endpoint for importers #1380

Merged
merged 6 commits into from
Jan 23, 2025
Merged
Changes from 1 commit
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
Next Next commit
feat: Provide add_or_edit root-only endpoint for importers
This endpoint allows for an `add_or_edit`-action with the ability to provide a key as well.
It is restricted to 'root'-users.
  • Loading branch information
phorward committed Jan 23, 2025
commit 7e96db9330248d0dd2ecaa93197becb0d12802eb
46 changes: 46 additions & 0 deletions src/viur/core/prototypes/skelmodule.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import yaml
import logging
from viur.core import Module, db, current
from viur.core.decorators import *
from viur.core.config import conf
from viur.core.skeleton import skeletonByKind, Skeleton, SkeletonInstance
import typing as t
Expand Down Expand Up @@ -147,3 +148,48 @@ def _apply_default_order(self, query: db.Query):
query.order(default_order)
else:
query.order(*default_order)

@force_ssl
@force_post
@exposed
@skey
@access("root")
def add_or_edit(self, key: db.Key | int | str, **kwargs) -> t.Any:
"""
This function is intended to be used by importers.
Only "root"-users are allowed to use it.
"""
db_key = db.keyHelper(key, targetKind=self.kindName, adjust_kind=self.kindName)
is_add = not bool(db.Get(db_key))

if is_add:
skel = self.addSkel()
else:
skel = self.editSkel()

skel["key"] = db_key

if (
not kwargs # no data supplied
or not skel.fromClient(kwargs) # failure on reading into the bones
):
# render the skeleton in the version it could as far as it could be read.
return self.render.render("add_or_edit", skel)

if is_add:
self.onAdd(skel)
else:
self.onEdit(skel)

skel.toDB()

if is_add:
self.onAdded(skel)
else:
self.onEdited(skel)

print(is_add)
if is_add:
return self.render.addSuccess(skel)

return self.render.editSuccess(skel)
Loading