-
Notifications
You must be signed in to change notification settings - Fork 185
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
add endpoint for checking subdomain availability #1031
Conversation
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 tested the bad word filter with "butt" (http://127.0.0.1:8000/accounts/profile/subdomain?subdomain=butt) and it returned true
. Should it return false, or is it baked into another query?
Oh good catch! I didn't include the badwords check in there. Will fix and update. |
126c0d3
to
fd4e305
Compare
Okay I moved the "already taken" AND the "has bad word" check into the method that checks for availability. Should be fixed now. (Also added a couple tests) |
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.
Some changes recommended!
@staticmethod | ||
def subdomain_available(subdomain): | ||
bad_word = has_bad_words(subdomain) | ||
taken = len(Profile.objects.filter(subdomain=subdomain)) > 0 |
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.
use Profile.objects.filter(subdomain=subdomain).count()
instead of len
because that will load all the objects which is not as efficient as count()
subdomain_exists = Profile.objects.filter(subdomain=subdomain) | ||
if not subdomain or has_bad_words(subdomain) or subdomain_exists: | ||
subdomain_available = Profile.subdomain_available(subdomain) | ||
if not subdomain or not subdomain_available: |
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.
Hmm, wondering if we should just early return in this function a if subdomain
argument is a falsey-value?
if request.method == 'GET': | ||
subdomain = request.GET.get('subdomain', None) | ||
available = Profile.subdomain_available(subdomain) | ||
return JsonResponse({'available':available}) |
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.
return JsonResponse({'available': available})
def profile_subdomain(request): | ||
if (not request.user or request.user.is_anonymous): | ||
return redirect(reverse('fxa_login')) | ||
profile = request.user.profile_set.first() | ||
if not profile.has_unlimited: | ||
raise CannotMakeSubdomainException(NOT_PREMIUM_USER_ERR_MSG.format('check a subdomain')) |
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.
catch the exception and redirect to profile like we did below:
messages.error(request, e.message)
return redirect(reverse('profile'))
To test: