-
-
Notifications
You must be signed in to change notification settings - Fork 72
/
update.py
36 lines (31 loc) · 1.29 KB
/
update.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from plone.registry import field
from plone.registry.interfaces import IRegistry
from plone.restapi.deserializer import json_body
from plone.restapi.services import Service
from zope.component import getUtility
from zope.interface import alsoProvides
from zope.schema.interfaces import WrongType
import json
import plone.protect.interfaces
class RegistryUpdate(Service):
def reply(self):
records_to_update = json_body(self.request)
registry = getUtility(IRegistry)
# Disable CSRF protection
if "IDisableCSRFProtection" in dir(plone.protect.interfaces):
alsoProvides(self.request, plone.protect.interfaces.IDisableCSRFProtection)
for key, value in records_to_update.items():
if key not in registry:
raise NotImplementedError(
"This endpoint is only intended to update existing "
f"records! Couldn't find key {key}"
)
# Issue 1575: Deal with tuple values
try:
registry[key] = value
except WrongType as exc:
if isinstance(exc.field, field.Tuple) and isinstance(value, list):
registry[key] = tuple(value)
else:
raise exc
return self.reply_no_content()