Commit 551452e8 authored by David Haynes's avatar David Haynes
Browse files

signup now supports registereduser

- remove silly helper functions since those are now model vars
- fix associations with ^^
- full_name is now provided/filled in/read-only for non-staff
- correct redirects with signup
parent b21d5173
...@@ -16,32 +16,6 @@ from go.forms import URLForm, SignupForm ...@@ -16,32 +16,6 @@ from go.forms import URLForm, SignupForm
# Other Imports # Other Imports
from datetime import timedelta from datetime import timedelta
##############################################################################
"""
Define useful helper methods here.
"""
def is_approved(user):
"""
This function checks if a user account has a corresponding RegisteredUser,
thus checking if the user is approved.
"""
return user.RegisteredUser
def is_registered(user):
"""
This function checks if a user account has a corresponding RegisteredUser,
thus checking if the user is registered.
"""
return user.registereduser.requested
##############################################################################
"""
Define user views here.
"""
def index(request): def index(request):
""" """
This view handles the homepage that the user is presented with when This view handles the homepage that the user is presented with when
...@@ -54,9 +28,8 @@ def index(request): ...@@ -54,9 +28,8 @@ def index(request):
# If the user is not authenticated, show them a public landing page. # If the user is not authenticated, show them a public landing page.
if not request.user.is_authenticated(): if not request.user.is_authenticated():
return render(request, 'public_landing.html') return render(request, 'public_landing.html')
# If the user isn't approved, don't give them any leeway.
# If the user isn't registered, don't give them any leeway. elif not request.user.registereduser.approved:
if not is_approved(request.user):
return render(request, 'not_registered.html') return render(request, 'not_registered.html')
url_form = URLForm(host=request.META.get('HTTP_HOST')) # unbound form url_form = URLForm(host=request.META.get('HTTP_HOST')) # unbound form
...@@ -140,7 +113,7 @@ def my_links(request): ...@@ -140,7 +113,7 @@ def my_links(request):
obviously need to be logged in to view your URLs. obviously need to be logged in to view your URLs.
""" """
if not is_approved(request.user): if not request.user.registereduser.approved:
return render(request, 'not_registered.html') return render(request, 'not_registered.html')
urls = URL.objects.filter(owner=request.user) urls = URL.objects.filter(owner=request.user)
...@@ -161,7 +134,7 @@ def delete(request, short): ...@@ -161,7 +134,7 @@ def delete(request, short):
logged in and registered, and must also be the owner of the URL. logged in and registered, and must also be the owner of the URL.
""" """
if not is_approved(request.user): if not request.user.registereduser.approved:
return render(request, 'not_registered.html') return render(request, 'not_registered.html')
url = get_object_or_404(URL, short__iexact=short) url = get_object_or_404(URL, short__iexact=short)
...@@ -175,47 +148,33 @@ def delete(request, short): ...@@ -175,47 +148,33 @@ def delete(request, short):
@login_required @login_required
def signup(request): def signup(request):
""" """
This view presents the user with a registration form. You can register This view presents the user with a registration form. You can register yourself.
yourself, or another person.
""" """
# Do not display signup page to registered or approved users (Staff can still see these pages) # Do not display signup page to registered or approved users (Staff can still see these pages)
if is_registered(request.user) and not request.user.is_staff: if request.user.registereduser.registered and not request.user.is_staff:
return render(request, 'core/signup.html', { return render(request, 'registered.html', {})
'registered': True, elif request.user.registereduser.approved and not request.user.is_staff:
'approved': False, return render(request, 'core/index.html', {})
},
) signup_form = SignupForm(request)
elif is_approved(request.user) and not request.user.is_staff:
return render(request, 'core/signup.html', {
'registered': True,
'approved': True,
},
)
signup_form = SignupForm(request, initial={'username': request.user.username})
# Non-staff have the username field read-only and pre-filled
if request.user.is_staff: if request.user.is_staff:
signup_form = SignupForm(request) signup_form = SignupForm(request)
else: else:
signup_form = SignupForm(request, signup_form = SignupForm(request,
initial={'username': request.user.username, 'full_name': request.user.first_name + " " + request.user.last_name}) initial={'full_name': request.user.first_name + " " + request.user.last_name})
signup_form.fields['username'].widget.attrs['readonly'] = 'readonly' signup_form.fields['full_name'].widget.attrs['readonly'] = 'readonly'
if request.method == 'POST': if request.method == 'POST':
signup_form = SignupForm(request, request.POST, signup_form = SignupForm(request, request.POST, instance=request.user.registereduser,
initial={'approved': False, 'username': request.user.username}) initial={'full_name': request.user.first_name + " " + request.user.last_name})
signup_form.fields['username'].widget.attrs['readonly'] = 'readonly' signup_form.fields['full_name'].widget.attrs['readonly'] = 'readonly'
if signup_form.is_valid(): if signup_form.is_valid():
if not request.user.is_staff:
username = request.user.username
else:
username = signup_form.cleaned_data.get('username')
full_name = signup_form.cleaned_data.get('full_name')
description = signup_form.cleaned_data.get('description') description = signup_form.cleaned_data.get('description')
full_name = signup_form.cleaned_data.get('full_name')
organization = signup_form.cleaned_data.get('organization') organization = signup_form.cleaned_data.get('organization')
registered = signup_form.cleaned_data.get('registered')
# Only send mail if we've defined the mailserver # Only send mail if we've defined the mailserver
if settings.EMAIL_HOST and settings.EMAIL_PORT: if settings.EMAIL_HOST and settings.EMAIL_PORT:
...@@ -254,6 +213,7 @@ def signup(request): ...@@ -254,6 +213,7 @@ def signup(request):
settings.EMAIL_FROM, settings.EMAIL_FROM,
[user_mail] [user_mail]
) )
signup_form.save() signup_form.save()
return redirect('registered') return redirect('registered')
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment