Skip to content

Commit 3edd760

Browse files
committed
Response refactorization
1 parent 9ccf33d commit 3edd760

3 files changed

Lines changed: 57 additions & 13 deletions

File tree

app/mod_photos/controllers.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from app.data import db
55
from app.mod_photos.models import Photo
66

7+
from factory_responses import FactoryResponse
8+
79
import uuid
810
import os
911

@@ -17,16 +19,14 @@ def allowed_file(filename):
1719

1820
@mod_photos.route('/photos',methods=['POST'])
1921
def photos():
22+
responses = FactoryResponse()
2023
app.logger.debug("Applying post photo...")
2124

2225
data = None
2326
if request.method == 'POST':
2427
file = request.files['file']
2528
if file and not allowed_file(file.filename):
26-
resp = jsonify()
27-
resp.content_type='application/json'
28-
resp.status_code = 202
29-
return resp
29+
return responses.new202()
3030

3131
extension = os.path.splitext(file.filename)[1]
3232
f_name=str(uuid.uuid4()) + extension
@@ -37,13 +37,11 @@ def photos():
3737
file.save(filepath)
3838
data = {'filename':f_name}
3939
#TODO refactor to set up necessary methods to create responses
40-
if data == None:
41-
resp = jsonify()
42-
resp.status_code = 200
43-
else:
44-
resp = jsonify(data)
45-
resp.status_code = 201
46-
#TODO add diferent type of constructors
47-
resp.content_type='application/json'
40+
#TODO is it necessary to return 200 command
41+
resp = None
42+
if data == None:
43+
resp = responses.new200()
44+
else:
45+
resp = responses.new201(data)
4846
return resp
4947

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from flask import jsonify, json
2+
#TODO add unit tests for this class
3+
class FactoryResponse(object):
4+
def new201(self,data):
5+
resp = jsonify(data)
6+
resp.content_type="application/json"
7+
resp.status_code=201
8+
return resp
9+
10+
def new202(self):
11+
resp = jsonify()
12+
resp.content_type="application/json"
13+
resp.status_code=202
14+
return resp
15+
16+
def new200(self):
17+
resp = jsonify()
18+
resp.content_type="application/json"
19+
resp.status_code=200
20+
return resp
21+

app/mod_photos/tests.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,38 @@
55
from app import app
66

77
from .models import Photo
8+
from factory_responses import FactoryResponse
9+
810

911

1012
class TestModelPhoto(BaseTestCase):
1113
def test_new_photo(self):
1214
photo = Photo('1','/test/photo.png')
1315
self.assertTrue(photo.uuid,'1')
1416
self.assertTrue(photo.filepath,'/test/photo.png')
17+
class TestFactoryResponses(BaseTestCase):
18+
responses = None
19+
20+
@classmethod
21+
def setUpClass(cls):
22+
cls.responses = FactoryResponse()
23+
@classmethod
24+
def tearDownClass(cls):
25+
cls.responses = None
26+
def test_200_correct(self):
27+
#TODO add respnse
28+
resp=self.responses.new200()
29+
self.assertTrue(resp.status_code==200)
30+
self.assertTrue(resp.content_type=="application/json")
31+
def test_201_correct(self):
32+
empty_data = {}
33+
resp=self.responses.new201(empty_data)
34+
self.assertTrue(resp.status_code==201)
35+
self.assertTrue(resp.content_type=="application/json")
36+
def test_202_correct(self):
37+
resp=self.responses.new202()
38+
self.assertTrue(resp.status_code==202)
39+
self.assertTrue(resp.content_type=="application/json")
1540

1641
class TestPhotosViews(BaseTestCase):
1742
def test_post_photo_correct(self):
@@ -32,4 +57,4 @@ def post_photo_not_allowed_file(self):
3257
#self.app.logger.debug(response.json)
3358
#TODO add assert for correct response
3459
self.assertTrue(response.status_code == 202)
35-
60+

0 commit comments

Comments
 (0)