Skip to content

Commit

Permalink
Fix #52
Browse files Browse the repository at this point in the history
  • Loading branch information
sirex committed Jul 19, 2022
1 parent 5190220 commit e3d7a29
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 18 deletions.
27 changes: 24 additions & 3 deletions spinta/commands/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,32 @@ async def getone(
params: UrlParams,
):
if params.prop and params.propref:
return await commands.getone(context, request, params.prop, params.model.backend, action=action, params=params)
return await commands.getone(
context,
request,
params.prop,
params.model.backend,
action=action,
params=params,
)
elif params.prop:
return await commands.getone(context, request, params.prop, params.prop.dtype.backend or params.model.backend, action=action, params=params)
return await commands.getone(
context,
request,
params.prop,
params.prop.dtype.backend or params.model.backend,
action=action,
params=params,
)
else:
return await commands.getone(context, request, params.model, params.model.backend, action=action, params=params)
return await commands.getone(
context,
request,
params.model,
params.model.backend,
action=action,
params=params,
)


@commands.changes.register(Context, Model, Request)
Expand Down
15 changes: 12 additions & 3 deletions spinta/formats/ascii/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import sys

from typing import Optional
from typing import List

from starlette.requests import Request
from starlette.responses import StreamingResponse
Expand All @@ -26,7 +25,16 @@ def render(
status_code: int = 200,
headers: Optional[dict] = None,
):
return _render(context, model, fmt, action, params, data, status_code, headers)
return _render(
context,
model,
fmt,
action,
params,
data,
status_code,
headers,
)


def _render(
Expand All @@ -44,7 +52,8 @@ def _render(
colwidth = params.formatparams.get('colwidth')

if width is None and colwidth is None and sys.stdin.isatty():
_, width = map(int, subprocess.run(['stty', 'size'], capture_output=True).stdout.split())
proc = subprocess.run(['stty', 'size'], capture_output=True)
_, width = map(int, proc.stdout.split())
elif width is None and colwidth is None:
colwidth = 42

Expand Down
3 changes: 3 additions & 0 deletions spinta/formats/ascii/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def __call__(
):
manifest: Manifest = context.get('store').manifest

if action == Action.GETONE:
data = [data]

data = iter(data)
peek = next(data, None)

Expand Down
29 changes: 26 additions & 3 deletions spinta/utils/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,38 @@ async def create_http_response(
prop = params.prop
dtype = prop.dtype
backend = model.backend
return await commands.getone(context, request, prop, dtype, backend, action=action, params=params)
return await commands.getone(
context,
request,
prop,
dtype,
backend,
action=action,
params=params,
)
elif params.prop:
prop = params.prop
dtype = prop.dtype
backend = dtype.backend or model.backend
return await commands.getone(context, request, prop, dtype, backend, action=action, params=params)
return await commands.getone(
context,
request,
prop,
dtype,
backend,
action=action,
params=params,
)
else:
backend = model.backend
return await commands.getone(context, request, model, backend, action=action, params=params)
return await commands.getone(
context,
request,
model,
backend,
action=action,
params=params,
)

else:
search = any((
Expand Down
35 changes: 26 additions & 9 deletions tests/formats/test_ascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,16 +233,23 @@ def _build_context(
manifest: Manifest,
url: str,
row: Dict[str, Any],
*,
headers: Optional[Dict[str, str]] = None,
) -> Optional[Dict[str, Any]]:
context.set('auth.token', AdminToken())

if '?' in url:
model, query = url.split('?', 1)
path, query = url.split('?', 1)
else:
model, query = url, None

model = manifest.models[model]
request = make_get_request(model.name, query, {'accept': 'text/html'})
action = Action.GETONE
path, query = url, None
if headers is None:
headers = {
'Accept': 'text/html',
}
request = make_get_request(path, query, headers)
params = commands.prepare(context, UrlParams(), Version(), request)
action = Action.GETONE
model = params.model

select_tree = get_select_tree(context, action, params.select)
prop_names = get_select_prop_names(
Expand All @@ -266,7 +273,8 @@ def _build_context(
return render(context, request, model, params, row, action=action)


def test_ascii_getone(
@pytest.mark.asyncio
async def test_ascii_getone(
rc: RawConfig,
):
context, manifest = load_manifest_and_context(rc, '''
Expand All @@ -278,8 +286,17 @@ def test_ascii_getone(

_id = '19e4f199-93c5-40e5-b04e-a575e81ac373'
result = _build_context(context, manifest, f'example/City/{_id}', {
'_type': 'example/City',
'_id': _id,
'_revision': 'b6197bb7-3592-4cdb-a61c-5a618f44950c',
'name': 'Vilnius',
})
assert result == ''
}, headers={'Accept': 'text/plain'})
result = ''.join([x async for x in result.body_iterator]).splitlines()
assert result == [
'',
'',
'Table: example/City',
' _type _id _revision name ',
'====================================================================================================',
'example/City 19e4f199-93c5-40e5-b04e-a575e81ac373 b6197bb7-3592-4cdb-a61c-5a618f44950c Vilnius',
]

0 comments on commit e3d7a29

Please sign in to comment.