MCPcopy Create free account
hub / github.com/internetarchive/openlibrary / POST

Method POST

openlibrary/plugins/upstream/code.py:270–328  ·  view source on GitHub ↗
(self, key)

Source from the content-addressed store, hash-verified

268 raise web.seeother(web.changequery(m=None))
269
270 def POST(self, key):
271 i = web.input("v", _comment=None)
272 v = i.v and safeint(i.v, None)
273
274 if v is None:
275 raise web.seeother(web.changequery({}))
276
277 if not web.ctx.site.can_write(key) or not user_can_revert_records():
278 return render.permission_denied(web.ctx.fullpath, "Permission denied to edit " + key + ".")
279
280 thing = web.ctx.site.get(key, i.v)
281
282 if not thing:
283 raise web.notfound()
284
285 def revert(thing):
286 if thing.type.key == "/type/delete" and thing.revision > 1:
287 prev = web.ctx.site.get(thing.key, thing.revision - 1)
288 if prev.type.key in ["/type/delete", "/type/redirect"]:
289 return revert(prev)
290 else:
291 prev._save("revert to revision %d" % prev.revision, action="revert-version")
292 return prev
293 elif thing.type.key == "/type/redirect":
294 redirect = web.ctx.site.get(thing.location)
295 if redirect and redirect.type.key not in [
296 "/type/delete",
297 "/type/redirect",
298 ]:
299 return redirect
300 else:
301 # bad redirect. Try the previous revision
302 prev = web.ctx.site.get(thing.key, thing.revision - 1)
303 return revert(prev)
304 else:
305 return thing
306
307 def process(value):
308 if isinstance(value, list):
309 return [process(v) for v in value]
310 elif isinstance(value, client.Thing):
311 if value.key:
312 if value.type.key in ["/type/delete", "/type/revert"]:
313 return revert(value)
314 else:
315 return value
316 else:
317 for k in value:
318 value[k] = process(value[k])
319 return value
320 else:
321 return value
322
323 for k in thing:
324 thing[k] = process(thing[k])
325
326 comment = i._comment or "reverted to revision %d" % v
327 thing._save(comment, action="revert-version")

Callers

nothing calls this directly

Calls 6

user_can_revert_recordsFunction · 0.85
notfoundMethod · 0.80
_saveMethod · 0.80
processFunction · 0.70
safeintFunction · 0.50
getMethod · 0.45

Tested by

no test coverage detected