Skip to content

Instantly share code, notes, and snippets.

@belyaev-pa
Last active September 17, 2019 09:13
Show Gist options
  • Save belyaev-pa/c4a29da8ddaddf31a583738fdc57b270 to your computer and use it in GitHub Desktop.
Save belyaev-pa/c4a29da8ddaddf31a583738fdc57b270 to your computer and use it in GitHub Desktop.
test task for refactoring fome shit code
from project.strategies import container as strategies_container
from project.schemas import ScorePlayload
from altron.http import response, abort
from flask import request
@app.route('/strategies/<strategy>')
def get_strategy(strategy):
if strategies_container.has(strategy):
strategy = strategies_container.get(strategy)
else:
error = 'Неизвестное имя стратегии: ' + strategy
abort.not_found(error) # raise 404
try:
payload = request.get_json(force=True, selint=True)
except BaseException:
error = 'payload get error'
abort.not_found(error) # raise 404
if ScorePlayload().is_valid(payload):
pass
else:
msg = 'Input payload validation failed'
return response.bad_request(msg, data=payload)
score = strategy.get_score(payload)
return response.ok(data=score.normalize().to_dict())
@app.route('/strategies/<strategy>', methods=['DELETE'])
def post_strategy(strategy):
if strategies_container.has(strategy):
strategy = strategies_container.get(strategy)
else:
error = 'Неизвестное имя стратегии: ' + strategy
abort.not_found(error) # raise 404
strategies_container.remove(strategy)
return response.ok()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment