Skip to content

Commit

Permalink
Allow username in /user API
Browse files Browse the repository at this point in the history
  • Loading branch information
anshg1214 committed Apr 7, 2022
1 parent b21b567 commit 298bba0
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
4 changes: 4 additions & 0 deletions critiquebrainz/ws/user/test/views_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ def test_user_addition(self):
def test_user_entity_handler(self):
user = User(db_users.create(
display_name='Tester 1',
musicbrainz_username='tester1',
email='tester1@testing.org',
))
resp = self.client.get('/user/{user_id}'.format(user_id=user.id)).json
self.assertEqual(resp['user']['display_name'], 'Tester 1')

resp2 = self.client.get('/user/{username}'.format(username=user.musicbrainz_username)).json
self.assertEqual(resp2['user']['display_name'], 'Tester 1')
# Test if user with specified ID does not exist
self.assert404(self.client.get('/user/e7aad618-fd86-3983-9e77-405e21796eca'))
15 changes: 9 additions & 6 deletions critiquebrainz/ws/user/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from critiquebrainz.ws.exceptions import NotFound
from critiquebrainz.ws.oauth import oauth
from critiquebrainz.ws.parser import Parser
from critiquebrainz.frontend.views.user import get_user_from_username_or_id

user_bp = Blueprint('ws_user', __name__)

Expand Down Expand Up @@ -197,10 +198,10 @@ def user_delete_handler(user):
return jsonify(message='Request processed successfully')


@user_bp.route('/<uuid:user_id>', methods=['GET', 'OPTIONS'])
@user_bp.route('/<string:user_ref>', methods=['GET', 'OPTIONS'])
@crossdomain(headers="Authorization, Content-Type")
def user_entity_handler(user_id):
"""Get profile of a user with a specified UUID.
def user_entity_handler(user_ref):
"""Get profile of a user with a specified UUID or username.
**Request Example:**
Expand All @@ -225,9 +226,11 @@ def user_entity_handler(user_id):
:resheader Content-Type: *application/json*
"""
user = db_users.get_by_id(str(user_id))
if not user:
raise NotFound("Can't find a user with ID: {user_id}".format(user_id=user_id))
try:
user = get_user_from_username_or_id(user_ref)
except:
raise NotFound("Can't find a user with ID or Username: {user_ref}".format(user_ref=user_ref))

inc = Parser.list('uri', 'inc', User.allowed_includes, optional=True) or []
return jsonify(user=User(user).to_dict(inc))

Expand Down

0 comments on commit 298bba0

Please sign in to comment.