Skip to content

Commit dd55cd0

Browse files
committed
add -s --tail-slash options for gen cmd: default do not use trailing slash
1 parent da9712e commit dd55cd0

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

hobbit/bootstrap.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ def new(ctx, name, port, dist, template, force, celery):
6969
@click.option('-c', '--csv-path', required=False, type=click.Path(exists=True),
7070
callback=validate_csv_file,
7171
help='A csv file that defines some models.')
72+
@click.option('-s', '--tail-slash', default=False, is_flag=True,
73+
help='Whether to use the trailing slash.')
7274
@click.pass_context
73-
def gen(ctx, name, template, dist, force, csv_path):
75+
def gen(ctx, name, template, dist, force, csv_path, tail_slash):
7476
"""Generator new feature. Auto gen models/{name}.py, schemas/{name}.py,
7577
views/{name}.py, services/{name.py}, tests/test_{name}.py etc.
7678
"""
@@ -88,6 +90,7 @@ def gen(ctx, name, template, dist, force, csv_path):
8890
'name': name,
8991
'module': module,
9092
'metadata': metadata,
93+
'tail_slash': '/' if tail_slash else '',
9194
}
9295

9396
render_project(dist, template)

hobbit/static/feature/rivendell/app/views/{{ name }}.py.jinja2

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@ bp = Blueprint('{{ module }}', __name__)
1515

1616

1717
{% for model_name, Model in metadata.items() -%}
18-
@bp.route('/{{ Model.plural }}/', methods=['GET'])
18+
@bp.route('/{{ Model.plural }}{{ tail_slash }}', methods=['GET'])
1919
@use_kwargs(PageParams)
2020
def list_{{ Model.plural }}(page, page_size, order_by):
2121
page_ret = {{ model_name }}Service.list(page, page_size, order_by)
2222
return jsonify(schemas.paged_{{ Model.singular }}_schemas.dump(page_ret))
2323

2424

25-
@bp.route('/{{ Model.plural }}/', methods=['POST'])
25+
@bp.route('/{{ Model.plural }}{{ tail_slash }}', methods=['POST'])
2626
@use_kwargs(schemas.{{ Model.singular }}_schema)
2727
@transaction(db.session)
2828
def create_{{ Model.singular }}(**kwargs):
2929
instance = {{ model_name }}Service.create(**kwargs)
3030
return jsonify(schemas.{{ Model.singular }}_schema.dump(instance)), 201
3131

3232

33-
@bp.route('/{{ Model.plural }}/<int:pk>/', methods=['GET'])
33+
@bp.route('/{{ Model.plural }}/<int:pk>{{ tail_slash }}', methods=['GET'])
3434
def retrieve_{{ Model.singular }}(pk):
3535
instance = {{ model_name }}Service.get_or_404(pk)
3636
return jsonify(schemas.{{ Model.singular }}_schema.dump(instance))
3737

3838

39-
@bp.route('/{{ Model.plural }}/<int:pk>/', methods=['PUT'])
39+
@bp.route('/{{ Model.plural }}/<int:pk>{{ tail_slash }}', methods=['PUT'])
4040
@use_kwargs(schemas.{{ Model.singular }}_schema)
4141
@transaction(db.session)
4242
def update_{{ Model.singular }}(pk, **kwargs):
4343
instance = {{ model_name }}Service.update(pk, **kwargs)
4444
return jsonify(schemas.{{ Model.singular }}_schema.dump(instance))
4545

4646

47-
@bp.route('/{{ Model.plural }}/', methods=['DELETE'])
47+
@bp.route('/{{ Model.plural }}{{ tail_slash }}', methods=['DELETE'])
4848
@use_kwargs({'ids': DelimitedList(fields.Int())})
4949
@transaction(db.session)
5050
def delete_{{ Model.plural }}(ids):

hobbit/static/feature/rivendell/tests/test_{{ name }}.py.jinja2

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ class Test{{ model_name }}(BaseTest):
2121
return instance
2222

2323
def test_list(self, client):
24-
resp = client.get('/api/{{ Model.plural }}/')
24+
resp = client.get('/api/{{ Model.plural }}{{ tail_slash }}')
2525
assert resp.status_code == 200
2626
assert set(resp.json.keys()) == {'items', 'page', 'page_size', 'total'}
2727

2828
def test_retrieve(self, client, instance):
29-
resp = client.get(f'/api/{{ Model.plural }}/{instance.id}/')
29+
resp = client.get(f'/api/{{ Model.plural }}/{instance.id}{{ tail_slash }}')
3030
assert resp.status_code == 200
3131
assert set(resp.json.keys()) == {
3232
'created_at', 'id', 'updated_at'{%- for c in Model.columns -%}, '{{ c.field }}'{%- endfor -%}}
@@ -37,7 +37,7 @@ class Test{{ model_name }}(BaseTest):
3737
'{{ c.field }}': {{ c.test }}{%- if not loop.last -%}, {% endif -%}
3838
{%- endfor -%}
3939
}
40-
resp = client.post('/api/{{ Model.plural }}/', json=payload)
40+
resp = client.post('/api/{{ Model.plural }}{{ tail_slash }}', json=payload)
4141
assert resp.status_code == 201
4242
assert set(resp.json.keys()) == {
4343
'created_at', 'id', 'updated_at'{%- for c in Model.columns -%}, '{{ c.field }}'{%- endfor -%}}
@@ -50,15 +50,15 @@ class Test{{ model_name }}(BaseTest):
5050
'{{ c.field }}': {{ c.test }}{%- if not loop.last -%}, {% endif -%}
5151
{%- endfor -%}
5252
}
53-
resp = client.put(f'/api/{{ Model.plural }}/{instance.id}/', json=payload)
53+
resp = client.put(f'/api/{{ Model.plural }}/{instance.id}{{ tail_slash }}', json=payload)
5454
assert resp.status_code == 200
5555
assert set(resp.json.keys()) == {
5656
'created_at', 'id', 'updated_at'{%- for c in Model.columns -%}, '{{ c.field }}'{%- endfor -%}}
5757
for k, v in payload.items():
5858
assert resp.json[k] == v
5959

6060
def test_delete(self, client, instance):
61-
resp = client.delete(f'/api/{{ Model.plural }}/?ids={instance.id}')
61+
resp = client.delete(f'/api/{{ Model.plural }}{{ tail_slash }}?ids={instance.id}')
6262
assert resp.status_code == 204
6363
assert {{ model_name }}.query.get(instance.id) is None
6464
{% if not loop.last %}

tests/test_hobbit.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def csv_file(self, runner):
8383
assert result.exit_code == 0
8484

8585
@pytest.mark.parametrize("gen_cmd", [
86-
['--echo', 'gen', '-n', 'user', '-t', 'rivendell'],
86+
['--echo', 'gen', '-n', 'user', '-t', 'rivendell', '-s'],
8787
['--echo', 'gen', '-n', 'user', '-t', 'rivendell', '-f', '--csv-path',
8888
os.path.join(BaseTest.root_path, 'tests', 'models.csv')],
8989
])

0 commit comments

Comments
 (0)