Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
await db.async_set(self.arguments.container, container)
await container.install()
# Local Roles assign owner as the creator user
roleperm = IPrincipalRoleManager(container)
roleperm.assign_role_to_principal("guillotina.Owner", "root")
await notify(ObjectAddedEvent(container, db, container.__name__))
try:
await tm.commit(txn=txn)
except ConflictIdOnContainer:
# ignore id conflicts
await tm.abort(txn=txn)
txn = await tm.begin()
task_vars.registry.set(None)
task_vars.container.set(container)
api = WikipediaAPI()
folder_count = 0
async for page_data in api.iter_pages():
await self.import_folder(api, tm, txn, container, page_data)
folder_count += 1
if folder_count >= self.arguments.per_node:
break
try:
await tm.commit(txn=txn)
except ConflictIdOnContainer:
# ignore id conflicts
await tm.abort(txn=txn)
api.close()
async def __aenter__(self):
task_vars.request.set(self.request)
task_vars.db.set(self.db)
set_authenticated_user(self.user)
return self
def get_transaction() -> typing.Optional[ITransaction]:
"""
Return the current active transaction.
"""
return task_vars.txn.get()
def __call__(self, relative=False, container_url=False):
if container_url:
# we want the url relative to container so remove the container
path = [x for x in get_physical_path(self.context)]
path.pop(1)
path = "/".join(path)
else:
path = "/".join(get_physical_path(self.context))
if container_url:
return path
elif relative:
db = task_vars.db.get()
return "/" + db.id + path
else:
db = task_vars.db.get()
return get_url(self.request, db.id + path)
This is used together with "with" syntax for wrapping mutating
code into a request owned transaction.
:param request: request owning the transaction
Example::
with get_tm().transaction() as txn: # begin transaction txn
# do something
# transaction txn commits or raises ConflictError
"""
return task_vars.tm.get()
def __enter__(self):
task_vars.request.set(self)
def __init__(self, txn, container):
self.txn = txn
self.container = container
self.index = weakref.WeakValueDictionary()
self.deleted = {}
task_vars.registry.set(None)
task_vars.container.set(container)
task_vars.txn.set(txn)
self.sync()
def find_container(context=None) -> typing.Optional[IContainer]:
"""
Find container based on contextvar or by looking up the
container from the provided context parameter
"""
container = task_vars.container.get()
if container is None:
while context is not None:
if IContainer.providedBy(context):
container = context
break
context = getattr(context, "__parent__", None)
return container
context = None
try:
request = get_current_request()
except RequestNotFound:
request = None
try:
url = request.url.human_repr()
except AttributeError:
# older version of aiohttp
url = ""
info = {
"url": url,
"container": getattr(task_vars.container.get(), "id", None),
"user": get_authenticated_user_id(),
"db_id": getattr(task_vars.db.get(), "id", None),
"request_uid": getattr(request, "_uid", None),
"method": getattr(request, "method", None),
"subscribers": [],
"context": context,
"event": event,
}
start = time.time() * 1000
subscriptions = sorted(
self.subscriptions(map(providedBy, objects), provided),
key=lambda sub: getattr(sub, "priority", 100),
)
info["lookup_time"] = (time.time() * 1000) - start
info["found"] = len(subscriptions)
results = []
for subscription in subscriptions:
def __call__(self, relative=False, container_url=False):
if container_url:
# we want the url relative to container so remove the container
path = [x for x in get_physical_path(self.context)]
path.pop(1)
path = "/".join(path)
else:
path = "/".join(get_physical_path(self.context))
if container_url:
return path
elif relative:
db = task_vars.db.get()
return "/" + db.id + path
else:
db = task_vars.db.get()
return get_url(self.request, db.id + path)