Files
intern2020/fet2020api/__init__.py
2020-12-28 08:13:56 +00:00

70 lines
2.1 KiB
Python

import requests
from .djangoapi import django_crud_api
class fet2020api(django_crud_api):
def __init__(
self,
endpoint="http://localhost:8106/api/posts/",
push_ids=["legacy_id"],
pk="id",
):
super().__init__(endpoint=endpoint)
self.push_ids = push_ids
self.pk = pk
def push(self, d):
"this is a shortcut for pushing content to the database based on filter attributes"
for a in self.push_ids:
if not a in d: # all push attributes must be given
raise AttributeError(
f"{a} must be in data because it is in 'push_ids' = {self.push_ids}."
)
id = {key: d[key] for key in self.push_ids}
p = self.find_one(id)
if p is None:
p = self.create(d)
else:
p = self.update(p[self.pk], d)
return p
class fet2020postapi(django_crud_api):
def __init__(self, endpoint="http://localhost:8106/api/posts/"):
super().__init__(endpoint=endpoint)
def read_post(self, slug=None, legacy_id=None):
return self.find_one({"slug": slug, "legacy_id": legacy_id})
def update_or_write_by_legacy_id(self, d):
if not "legacy_id" in d:
raise AttributeError("legacy_id muss angegeben werden")
p = self.read_post(legacy_id=d["legacy_id"])
if p is None:
p = self.create(d)
else:
p = self.update(p["slug"], d)
return p
def push(self, d):
return self.update_or_write_by_legacy_id(d)
class fet2020memberapi(django_crud_api):
def __init__(self, endpoint="http://localhost:8103/api/members/"):
super().__init__(endpoint=endpoint)
def read_post(self, slug=None, legacy_id=None):
return self.find_one({"nickname": slug, "legacy_id": legacy_id})
def update_or_write_by_legacy_id(self, d):
if not "legacy_id" in d:
raise AttributeError("legacy_id muss angegeben werden")
p = self.read_post(legacy_id=d["legacy_id"])
if p is None:
p = self.create(d)
else:
p = self.update(p["slug"], d)
return p