Created
May 25, 2013 11:16
-
-
Save methane/5648735 to your computer and use it in GitHub Desktop.
http://blog.generace.co.jp/2013/05/24/723 を勝手にリファクタリング
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TestAction | |
{ | |
public function register() | |
{ | |
$testId = $this->getPostRequestParam('testId', 'Test'); | |
if (!$this->isValidId($testId)) { | |
$this->redirect(array('controller' => 'test', 'action' => 'index')); | |
} | |
$sampleList = $this->Sample->getList($testId); | |
if (!$sampleList) { | |
return $this->render('error'); | |
} | |
$nameIds = array(); | |
foreach ($sampleList as $sample) { | |
$nameIds[] = $sample['name_id']; | |
} | |
$nameList = $this->Name->getList($dataNameIdList); | |
$nameMap = array(); | |
foreach ($nameList as $name) { | |
$nameMap[$name['name_id']] = $name['name']; | |
} | |
foreach ($sampleList as $sample) { | |
$sample['name'] = $nameMap[$sample['name_id']]; | |
$sample['category'] = self::CATEGORY_TEST; | |
$this->Sample->save($sample); | |
} | |
return $this->render('complete'); | |
} | |
private function isValidId($id) | |
{ | |
if (!is_int($id) || $id < 0) { | |
return false; | |
} | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import flask | |
from TestApp.models import Sample, Name | |
from TestApp import app, db | |
@app.route('/register', methods=['POST']) | |
def register(): | |
test_id = _validate_test_id(flask.request.form['testId']) | |
samples = Sample.query.find_by(test_id=test_id).all() | |
if not samples: | |
return flask.render_template('error.html') | |
name_ids = [s.name_id for s in samples] | |
names = Name.query.filter(Name.id.in_(name_ids)).all() | |
name_map = {n.name_id: n for n in names] | |
for sample in samples: | |
sample.name = name_map[sample.name_id].name | |
sample.category = Sample.CATEGORY_TEST | |
db.session.commit() | |
return flask.render_template('complete.html') | |
def _validate_test_id(test_id): | |
test_id = int(test_id) | |
if test_id <= 0: | |
raise ValueError("test id must be bigger than 1") | |
return test_id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment