-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added ability for username
to act as identifier for API user detail endpoint in addition to primary key (/api/users/<userID>
OR /api/users/<username>
)
#321
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
1ba3983
changed User detail URL to accept `slug` #320
abrahmasandra da1fb7d
changed get_object of the detail view #320
abrahmasandra b0aa9dc
Merge branch 'dev' into apis/user-detail-slugs
abrahmasandra 8078803
Merge branch 'dev' into apis/user-detail-slugs
abrahmasandra ffb316a
Merge branch 'dev' into apis/user-detail-slugs
abrahmasandra 1d5cba7
Merge branch 'dev' into apis/user-detail-slugs
abrahmasandra 9ec5c1f
Merge branch 'dev' into apis/user-detail-slugs
abrahmasandra 328a464
Merge branch 'dev' into apis/user-detail-slugs
majorsylvie 0546104
Merge branch 'dev' into apis/user-detail-slugs
majorsylvie File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
# from django.shortcuts import render | ||
from django.shortcuts import get_object_or_404 | ||
from django_filters.rest_framework import DjangoFilterBackend | ||
from rest_framework import generics, status | ||
from rest_framework.pagination import PageNumberPagination | ||
|
@@ -35,7 +36,7 @@ class UserFriendsAPIView(generics.RetrieveAPIView): | |
|
||
def get_queryset(self): | ||
user_id = self.kwargs["pk"] | ||
user_profile = UserProfile.objects.get(user=user_id) | ||
user_profile = get_object_or_404(UserProfile, user=user_id) | ||
return user_profile.friends.all() | ||
|
||
|
||
|
@@ -60,6 +61,17 @@ class UserListView(generics.ListCreateAPIView): | |
class UserDetailView(generics.RetrieveUpdateDestroyAPIView): | ||
queryset = User.objects.all() | ||
serializer_class = UserSerializer | ||
lookup_field = "slug" | ||
|
||
def get_object(self): | ||
lookup_value = self.kwargs.get(self.lookup_field) | ||
|
||
# If the lookup_value is an integer, use the id field | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good code comment! |
||
if lookup_value.isdigit(): | ||
return get_object_or_404(User, pk=lookup_value) | ||
else: | ||
# Otherwise, use the slug field | ||
return get_object_or_404(User, username=lookup_value) | ||
|
||
|
||
class MessageView(generics.CreateAPIView): | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if a user goes by the name "1234"? That could cause an issue with searching for the correct user. A better implementation could have you search through user name first and store usernames as "User#user_id" That way you can query id numbers by using the #, and we can exclude # from potential username characters.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just made a commit that validates the User's
username
, and ensures that it cannot be fully numeric. This way, we will not have this problem, as a given URL parameter can only be interpreted as either ausername
OR anid
but not BOTH.NOTE: This commit is in PR #291, because in that PR, I created the username field for the
User
model, so it is only consistent that I put restrictions on theusername
field in that PR.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You must reach out to the users team before I will approve any changes to the
User
model.