Skip to content

Commit ca8f6ba

Browse files
committed
Added pagination to serve different photos
1 parent 781ba15 commit ca8f6ba

2 files changed

Lines changed: 28 additions & 6 deletions

File tree

app/mod_photos/controllers.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def allowed_file(filename):
2121

2222

2323
@mod_photos.route('/photos',methods=['POST'])
24-
def photos_post():
24+
def post_photos():
2525
app.logger.debug("Applying post photo...")
2626

2727
data = None
@@ -48,7 +48,7 @@ def photos_post():
4848
return resp
4949

5050
@mod_photos.route('/photos',methods=['GET'])
51-
def photos_get():
51+
def get_photos():
5252
app.logger.debug("Applying get photos...")
5353
photos = Photo.query.all()
5454
page = request.args.get('page',1,type=int)
@@ -57,12 +57,13 @@ def photos_get():
5757
photos = pagination.items
5858
prev = None
5959
if pagination.has_prev:
60-
prev=url_for('',page=page-1,_external=True)
60+
prev=url_for('photos.get_photos',page=page-1,_external=True)
6161
next = None
6262
if pagination.has_next:
63-
next = url_for('',page=page+1,_external=True)
63+
next = url_for('photos.get_photos',page=page+1,_external=True)
6464

65-
resp = jsonify({'data':
66-
[photo.serialize() for photo in photos]})
65+
resp = jsonify({'data' :[photo.serialize() for photo in photos],
66+
'pagination':{'prev': prev,'next': next,'count':pagination.total}
67+
})
6768
return resp
6869

app/mod_photos/tests.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
from factory_responses import FactoryResponse
99
from app.data import db
1010

11+
def create_photo(number_users):
12+
return [Photo.create(**{'uuid': str(index+1), 'filepath': 'test'+str(index+1)+'.jpg'}) for index in range(0,number_users) ]
1113

1214
class TestModelPhoto(BaseTestCase):
1315
def test_new_photo(self):
@@ -67,5 +69,24 @@ def test_get_photo_correct(self):
6769
with self.app.open_resource("test_resources/photo.jpg") as fp:
6870
with self.client:
6971
response = self.client.get("/photos/v1.0/photos")
72+
app.logger.debug(response.json)
7073
app.logger.debug(response.json['data'][0]['uuid'])
7174

75+
76+
def test_get_photo_correct(self):
77+
#fake information
78+
create_photo(1)
79+
#fake data to test get resource
80+
with self.client:
81+
response = self.client.get("/photos/v1.0/photos")
82+
app.logger.debug(response.json)
83+
app.logger.debug(response.json['data'][0]['uuid'])
84+
85+
def test_get_photos_pagination(self):
86+
create_photo(102)
87+
with self.client:
88+
response = self.client.get("/photos/v1.0/photos")
89+
app.logger.debug(response.json)
90+
app.logger.debug(response.json['data'][0]['uuid'])
91+
92+

0 commit comments

Comments
 (0)