Skip to content
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

Merged
merged 1 commit into from
Aug 24, 2021

Conversation

groovecoder
Copy link
Member

To test:

  1. http://127.0.0.1:8000/accounts/profile/subdomain?subdomain=
    • Note: if your browser session isn't signed in, it should return an error
  2. Also verify that you can still register a new subdomain

Copy link
Contributor

@maxxcrawford maxxcrawford left a 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?

@groovecoder
Copy link
Member Author

Oh good catch! I didn't include the badwords check in there. Will fix and update.

@groovecoder groovecoder force-pushed the add-domain-check-endpoint branch from 126c0d3 to fd4e305 Compare August 23, 2021 21:26
@groovecoder
Copy link
Member Author

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)

@groovecoder groovecoder merged commit 1335e6e into main Aug 24, 2021
@groovecoder groovecoder deleted the add-domain-check-endpoint branch August 24, 2021 18:36
Copy link
Contributor

@say-yawn say-yawn left a 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
Copy link
Contributor

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:
Copy link
Contributor

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})
Copy link
Contributor

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'))
Copy link
Contributor

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'))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants