Skip to content

Commit a0f3262

Browse files
committed
api: refactor and de-duplicate data merging logic (database + data from disk)
1 parent ad21d41 commit a0f3262

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

hhub/views.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,8 @@ def entry_manifest(request, pk):
3030
data = open(f"db-sources/{entry.basepath}/entries/{pk}/game.json").read()
3131
json_data = json.loads(data)
3232

33-
# Enrich the manifest with some values available only in the (postgres) database
34-
json_data["devtoolinfo"] = entry.devtoolinfo
35-
json_data["basepath"] = entry.basepath
36-
json_data["baserepo"] = entry.baserepo
37-
return JsonResponse(json_data)
33+
merged_json_data = merge_manifest_data(json_data, entry)
34+
return JsonResponse(merged_json_data)
3835

3936

4037
def search_entries(request):
@@ -130,12 +127,10 @@ def search_entries(request):
130127
f"db-sources/{entry.basepath}/entries/{entry.slug}/game.json"
131128
).read()
132129
json_data = json.loads(data)
133-
# Enrich the manifest with some values available only in the (postgres) database
134-
additional_json_data = {
135-
"basebath": entry.basepath,
136-
"firstadded_date": entry.firstadded_date,
137-
}
138-
json_entries.append({**json_data, **additional_json_data})
130+
131+
merged_json_data = merge_manifest_data(json_data, entry)
132+
133+
json_entries.append(merged_json_data)
139134

140135
# Prepare final JSON response
141136
return JsonResponse(
@@ -191,6 +186,18 @@ def stats(request):
191186
return JsonResponse(data)
192187

193188

189+
def merge_manifest_data(data, entry):
190+
# Enrich the manifest with some values available only in the (postgres) database
191+
additional_json_data = {
192+
"basebath": entry.basepath,
193+
"baserepo": entry.baserepo,
194+
"firstadded_date": entry.firstadded_date,
195+
"devtoolinfo": entry.devtoolinfo,
196+
}
197+
198+
return {**data, **additional_json_data}
199+
200+
194201
# Utils
195202
def sort_entries(entries, sort_key, direction="asc"):
196203
# regardless what user has submitted, we lowercase the input

0 commit comments

Comments
 (0)