rewrite djangoapi

This commit is contained in:
www
2020-12-28 08:13:56 +00:00
parent 31b3384bd5
commit 5158e4b8da
2 changed files with 102 additions and 67 deletions

View File

@@ -1,38 +1,69 @@
import requests
from .djangoapi import django_crud_api
class fet2020postapi(django_crud_api):
def __init__(self,endpoint="http://localhost:8106/api/posts/"):
class fet2020api(django_crud_api):
def __init__(
self,
endpoint="http://localhost:8106/api/posts/",
push_ids=["legacy_id"],
pk="id",
):
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'])
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)
p = self.create(d)
else:
p=self.update(p['slug'], d)
p = self.update(p[self.pk], d)
return p
def push(self,d):
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/"):
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'])
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)
p = self.create(d)
else:
p=self.update(p['slug'], d)
p = self.update(p["slug"], d)
return p