Skip to content

Commit c2f4023

Browse files
committed
added preview endpoint
1 parent 86f3a4e commit c2f4023

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

src/repository/profile_repository.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,11 @@ async def fetch_viz_template(geo_level: str, category: str, subcategory: str, to
121121
async def fetch_template_tree(geo_level: str):
122122
query = f"select category, subcategory, name from content where geo_level = '{geo_level}'"
123123
response = fetch_many(query)
124-
return response
124+
return response
125+
126+
async def fetch_single_content(category: str, subcategory: str, topic: str):
127+
log.info(f'Fetching {category}/{subcategory}/{topic} content ...')
128+
query = f"select file from content where category = '{category}' and subcategory = '{subcategory}' and name = '{topic}'"
129+
response = fetch_one(query)
130+
log.info(f'Succesfully fetched /{category}/{subcategory}/{topic} content')
131+
return response['file']

src/routers/content.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
from fastapi import APIRouter
1+
from fastapi import APIRouter, Body
22
from repository.profile_repository import fetch_content_template, fetch_county, fetch_municipality, fetch_region
3-
from services.content import build_content, build_template_tree
3+
from services.content import build_content, build_single_content, build_template_tree
44
from fastapi.responses import HTMLResponse
55
from fastapi.staticfiles import StaticFiles
6-
from fastapi.templating import Jinja2Templates
76

87
router = APIRouter(
98
prefix="/content",
@@ -14,7 +13,6 @@
1413
# profile = build_tract_profile(geoid)
1514
# return profile
1615

17-
templates = Jinja2Templates(directory="content")
1816

1917

2018
@router.get("/municipality/{geoid}")
@@ -37,6 +35,13 @@ async def get_region():
3735
content = await build_content('region', profile)
3836
return content
3937

38+
@router.post('/preview')
39+
async def get_content_template(category: str, subcategory: str, topic: str, body: str = Body(..., media_type="text/plain")):
40+
profile = await fetch_region()
41+
42+
template = await build_single_content(body, profile,category, subcategory, topic)
43+
return template
44+
4045
@router.get('/template/{geo_level}')
4146
async def get_content_template(geo_level: str, category: str, subcategory: str, topic: str):
4247
template = await fetch_content_template(geo_level, category, subcategory, topic)

src/services/content.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import logging
44
import json
55

6-
from repository.profile_repository import fetch_content, fetch_template_tree
6+
from repository.profile_repository import fetch_content, fetch_template_tree, fetch_single_content
77
from jinja.template import env
88
from utils.consts import subcategory_map
99

@@ -73,4 +73,9 @@ async def build_template_tree(geo_level):
7373

7474
nested_dict.setdefault(cat, {}).setdefault(subcat, []).append(name)
7575

76-
return nested_dict
76+
return nested_dict
77+
78+
async def build_single_content(template: str, profile, category: str, subcategory: str, topic: str):
79+
# file = await fetch_single_content(category, subcategory, topic)
80+
populated_content = populate_template(template, profile)
81+
return populated_content

0 commit comments

Comments
 (0)