Skip to content

Commit

Permalink
Added user search functionallity
Browse files Browse the repository at this point in the history
  • Loading branch information
ineentho committed May 18, 2015
1 parent b06b107 commit 7a5ba4d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
29 changes: 29 additions & 0 deletions server/api_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from flask import jsonify
from server import app, db
from server.channel import Video, Channel
from server.user import User


@app.route('/api/channels/')
Expand Down Expand Up @@ -67,3 +68,31 @@ def list_videos(page=1):
'url': video.channel.url + ('/' if video.channel.url[-1] != '/' else '') + video.slug
})
return jsonify(dict(base_resp, videos=video_list))


@app.route('/api/users/<string:query>/')
@app.route('/api/users/<string:query>/<int:page>')
def list_users(query, page=1):
"""
A paginated search for users by name
"""
pagination = User.query.filter(User.name.ilike("%" + query + "%")).paginate(page, error_out=False)

# Base response will always be used as the base, even if
# the request fails
base_resp = {
'page': page,
'total-pages': pagination.pages,
'total-users': pagination.total
}

if len(pagination.items) == 0 and page != 1:
return jsonify(dict(base_resp, error='Page not found')), 404

user_list = []
for user in pagination.items:
user_list.append({
'id': user.id,
'name': user.name
})
return jsonify(dict(base_resp, users=user_list))
2 changes: 1 addition & 1 deletion tests/base_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ def create_video(self, slug='test-channel', password='test123', video_slug='vide

def post_json(self, url, data):
return self.client.post(url, data=json.dumps(data),
headers={'content-type': 'application/json'})
headers={'content-type': 'application/json'})
17 changes: 17 additions & 0 deletions tests/test_api_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,20 @@ def test_one_video(self):
response = self.client.get('/api/videos/')
self.assertEquals(response.json['total-videos'], 1)
self.assertEquals(len(response.json['videos']), 1)


class UserSearchTestCase(BaseTestCase):
def test_no_users(self):
"""
When searching for anything with no users, there should be 0 results
"""

resp = self.client.get('/api/users/Test Search/')
self.assertEquals(resp.json['total-users'], 0)

def test_correct_user(self):
"""
When there is a user that matches, there should be 1 result
"""

#self.create_dummy_user()

0 comments on commit 7a5ba4d

Please sign in to comment.