Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

moved apis into codebase; updated to latest django #2

Merged
merged 6 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions .env
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
APIS_DB_NAME=pmb
APIS_DB_USER=pmb
APIS_DB_PASSWORD=pmb
APIS_DB_HOST=172.17.0.1
POSTGRES_DB=pmb
POSTGRES_USER=pmb
POSTGRES_PASSWORD=pmb
POSTGRES_HOST=172.17.0.1
DEBUG=True
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ requirements_dev.txt
media/pmb-log.csv
media/professions.csv
hansi.csv
pmb_dump.sql
apis_core/.DS_Store
2 changes: 2 additions & 0 deletions apis_core/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
}
1 change: 1 addition & 0 deletions apis_core/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__version__ = '0.10.2'
54 changes: 54 additions & 0 deletions apis_core/api_renderers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import re
from collections import OrderedDict

from rest_framework import renderers


class NetJsonRenderer(renderers.JSONRenderer):
media_type = "application/json"
format = "json+net"

def render(self, data, media_type=None, renderer_context=None):
try:
test = re.search(r"/relations/([^/]+)/", data["results"][0]["url"])
except IndexError:
return super().render(
data, accepted_media_type=media_type, renderer_context=renderer_context
)
if test:
rel = test.group(1)
for r in data["results"][0].keys():
if r.startswith("related_"):
r2 = r.split("_")[1]
rel2 = re.match("^{}[a-z]*".format(r2), rel)
if rel2:
source = r
elif r.endswith('A'):
source = r
elif r.endswith('B'):
target = r
rel2 = re.match("^[a-z]*?{}$".format(r2), rel)
if rel2:
target = r
results2 = []
for d in data["results"]:
d2 = OrderedDict(
[
("target", v)
if k == target
else ("source", v)
if k == source
else (k, v)
for k, v in d.items()
]
)
if d2["source"] is None or d2["target"] is None: #fix needed to not get 500s if relations without source or target exist in the data
continue
target_type = d2["target"]["url"].split("/")[-3].title()
source_type = d2["source"]["url"].split("/")[-3].title()
d2["target"]["type"] = target_type
d2["source"]["type"] = source_type
results2.append(d2)
data["results"] = results2
res3 = super().render(data, accepted_media_type=media_type, renderer_context=renderer_context)
return res3
Loading
Loading