Commit 7afb7430 authored by Matthew Rodgers's avatar Matthew Rodgers

Converted Signup.html to Crispy Form. Terms of Service link needs to be set. Closing issue #28.

parent 6ef58d70
...@@ -7,7 +7,7 @@ class URLAdmin(admin.ModelAdmin): ...@@ -7,7 +7,7 @@ class URLAdmin(admin.ModelAdmin):
class RegisteredUserAdmin(admin.ModelAdmin): class RegisteredUserAdmin(admin.ModelAdmin):
list_display = ("username", "full_name", "approved") list_display = ("username", "full_name", "organization", "approved")
admin.site.register(URL, URLAdmin) admin.site.register(URL, URLAdmin)
admin.site.register(RegisteredUser, RegisteredUserAdmin) admin.site.register(RegisteredUser, RegisteredUserAdmin)
from django import forms from django import forms
from go.models import URL, RegisteredUser from go.models import URL, RegisteredUser
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.safestring import mark_safe
from crispy_forms.helper import FormHelper from crispy_forms.helper import FormHelper
from crispy_forms.layout import Layout, Fieldset, Submit, HTML, Div, Field from crispy_forms.layout import Layout, Fieldset, Submit, HTML, Div, Field
from crispy_forms.bootstrap import StrictButton, PrependedText, Accordion, AccordionGroup from crispy_forms.bootstrap import StrictButton, PrependedText, Accordion, AccordionGroup
...@@ -144,9 +145,41 @@ class SignupForm(forms.ModelForm): ...@@ -144,9 +145,41 @@ class SignupForm(forms.ModelForm):
except RegisteredUser.DoesNotExist: except RegisteredUser.DoesNotExist:
return return
def __init__(self, request, *args, **kwargs):
# Necessary to call request in forms.py, is otherwise restricted to views.py and models.py
self.request = request
super(SignupForm, self).__init__(*args, **kwargs)
self.helper = FormHelper(form=self)
self.helper.form_method = 'POST'
self.helper.form_class = 'form-horizontal'
self.helper.label_class = 'col-md-4'
self.helper.field_class = 'col-md-6'
self.helper.layout = Layout(
Fieldset(
'',
HTML("""<p>In order to succesfully provide this service, users must be manually approved. This prevents misuse of the URL shortener. Please indicate below if you are interested.</p><br>"""),
Div(
Div(
'username',
'full_name',
'organization',
'description',
'tos_box',
css_class='well',
),
StrictButton('Submit',css_class='btn btn-primary btn-md col-md-6', type='submit'),
css_class='col-md-6',
)
)
,
)
username = forms.CharField( username = forms.CharField(
required=True, required=True,
label='Mason NetID', label='Mason NetID (Required)',
max_length=30, max_length=30,
validators=[validate_username], validators=[validate_username],
widget=forms.TextInput(attrs={ widget=forms.TextInput(attrs={
...@@ -154,11 +187,18 @@ class SignupForm(forms.ModelForm): ...@@ -154,11 +187,18 @@ class SignupForm(forms.ModelForm):
) )
full_name = forms.CharField( full_name = forms.CharField(
required=True, required=True,
label='Full Name', label='Full Name (Required)',
max_length=100, max_length=100,
widget=forms.TextInput(attrs={ widget=forms.TextInput(attrs={
}), }),
) )
organization = forms.CharField(
required=True,
label='Organization (Required)',
max_length=100,
widget=forms.TextInput(attrs={
})
)
description = forms.CharField( description = forms.CharField(
required=False, required=False,
label='Description (Optional)', label='Description (Optional)',
...@@ -166,6 +206,21 @@ class SignupForm(forms.ModelForm): ...@@ -166,6 +206,21 @@ class SignupForm(forms.ModelForm):
widget=forms.Textarea(attrs={ widget=forms.Textarea(attrs={
}), }),
) )
tos_box = forms.BooleanField(
required=True,
#Need to add a Terms of Service Page and replace the href below
label = mark_safe('Do you accept the <a href="#" target="_blank">Terms of Service</a>?'),
)
def clean_username(self):
# Prevent hax: (non-staff) Users cannot signup for other users
cleaned_data = super(SignupForm, self).clean()
data_username = cleaned_data.get("username")
if not self.request.user.is_staff:
if self.request.user.username not in data_username:
self.add_error('username', "This is not your NetID!")
return data_username
class Meta: class Meta:
model = RegisteredUser model = RegisteredUser
......
...@@ -68,6 +68,11 @@ class RegisteredUser(models.Model): ...@@ -68,6 +68,11 @@ class RegisteredUser(models.Model):
max_length=100, max_length=100,
) )
organization = models.CharField(
blank=False,
max_length=100,
)
description = models.TextField(blank=True) description = models.TextField(blank=True)
approved = models.BooleanField() approved = models.BooleanField()
......
{% extends 'layouts/base.html' %} {% extends 'layouts/base.html' %}
{% load crispy_forms_tags %}
{% crispy SignupForm SignupForm.helper %}
{% block title %} {% block title %}
Go - Signup Go - Apply
{% endblock %} {% endblock %}
...@@ -11,119 +12,13 @@ Go - Signup ...@@ -11,119 +12,13 @@ Go - Signup
<div class="row"> <div class="row">
<div class="col-md-12"> <div class="col-md-12">
<h1><strong>Signup</strong></h1> <h1><strong>Apply</strong></h1>
</div> </div>
</div> </div>
</div> </div>
<div class="row"> {% crispy form %}
{% if not registered %}
<div class="col-md-12">
<p>
In order to succesfully provide this service, users must be manually
approved. This prevents misuse of the URL shortener. Please indicate below
if you are interested.
<br/><br/>
</p>
</div>
<div class="col-md-8">
<div class="well">
<form class="form-horizontal" method="post" action="{% url 'signup' %}">
{% csrf_token %}
<fieldset>
{% if form.non_field_errors %}
<div class="form-group has-error">
<div class="col-md-12">
{% for error in form.non_field_errors %}
<span class="help-block">{{ error }}</p>
{% endfor %}
</div>
</div>
{% endif %}
{% if form.username.errors %}
<div class="form-group has-error">
{% else %}
<div class="form-group">
{% endif %}
<div class="col-md-4">
<label class="control-label" for="{{ form.username.id_for_label }}">
{{ form.username.label }}</label>
</div>
<div class="col-md-8">
{{ form.username }}
{% for error in form.username.errors %}
<span class="help-block">{{error}}</span>
{% endfor %}
</div>
</div>
{% if form.full_name.errors %}
<div class="form-group has-error">
{% else %}
<div class="form-group">
{% endif %}
<div class="col-md-4">
<label class="control-label" for="{{ form.full_name.id_for_label }}">
{{ form.full_name.label }}</label>
</div>
<div class="col-md-8">
{{ form.full_name }}
{% for error in form.full_name.errors %}
<span class="help-block">{{error}}</span>
{% endfor %}
</div>
</div>
{% if form.description.errors %}
<div class="form-group has-error">
{% else %}
<div class="form-group">
{% endif %}
<div class="col-md-4">
<label class="control-label" for="{{ form.description.id_for_label }}">
{{ form.description.label }}</label>
</div>
<div class="col-md-8">
{{ form.description }}
{% for error in form.description.errors %}
<span class="help-block">{{error}}</span>
{% endfor %}
</div>
</div>
<div class="form-group">
<div class="col-md-12">
<input type="submit" value="Submit"
class="btn btn-primary btn-lg btn-block" />
</div>
</div>
</fieldset>
</form>
</div>
</div>
{% else %}
{% if not approved %}
<div class="col-md-12">
<p>You have already submitted a request for approval to use Go.</p>
</div>
{% else %}
<div class="col-md-12">
<p>You are already approved to use Go.</p>
</div>
{% endif %}
{% endif %}
</div>
{% endblock %} {% endblock %}
...@@ -225,33 +225,37 @@ def signup(request): ...@@ -225,33 +225,37 @@ def signup(request):
}, },
) )
signup_form = SignupForm(initial={'username': request.user.username}) signup_form = SignupForm(request, initial={'username': request.user.username})
# Non-staff have the username field read-only and pre-filled # Non-staff have the username field read-only and pre-filled
if request.user.is_staff: if request.user.is_staff:
signup_form = SignupForm() signup_form = SignupForm(request)
else: else:
signup_form = SignupForm(initial={'username': request.user.username, 'full_name': request.user.first_name + " " + request.user.last_name}) signup_form = SignupForm(request,
initial={'username': request.user.username, 'full_name': request.user.first_name + " " + request.user.last_name})
signup_form.fields['username'].widget.attrs['readonly'] = 'readonly' signup_form.fields['username'].widget.attrs['readonly'] = 'readonly'
if request.method == 'POST': if request.method == 'POST':
signup_form = SignupForm(request.POST, initial={'approved': False, signup_form = SignupForm(request, request.POST,
'username': request.user.username}) initial={'approved': False, 'username': request.user.username})
signup_form.fields['username'].widget.attrs['readonly'] = 'readonly'
if signup_form.is_valid(): if signup_form.is_valid():
# Prevent hax: if not staff, force the username back to the request username.
if not request.user.is_staff: if not request.user.is_staff:
username = request.user.username username = request.user.username
else: else:
username = signup_form.cleaned_data.get('username') username = signup_form.cleaned_data.get('username')
full_name = signup_form.cleaned_data.get('full_name') full_name = signup_form.cleaned_data.get('full_name')
description = signup_form.cleaned_data.get('description') description = signup_form.cleaned_data.get('description')
organization = signup_form.cleaned_data.get('organization')
# 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:
send_mail('Signup from %s' % (username), '%s signed up at %s\n' # TODO rewrite see #14
send_mail('Signup from %s' % (request.user.username), '%s signed up at %s\n'
'Username: %s\nMessage: %s\nPlease attend to this request at ' 'Username: %s\nMessage: %s\nPlease attend to this request at '
'your earliest convenience.' % (str(full_name), 'your earliest convenience.' % (str(full_name),
str(timezone.now()).strip(), str(username), str(description)), str(timezone.now()).strip(), str(request.user.username), str(description)),
settings.EMAIL_FROM, [settings.EMAIL_TO]) settings.EMAIL_FROM, [settings.EMAIL_TO])
signup_form.save() signup_form.save()
...@@ -352,4 +356,4 @@ def about(request): ...@@ -352,4 +356,4 @@ def about(request):
def registered(request): def registered(request):
return render(request, 'registered.html', { return render(request, 'registered.html', {
}, },
) )
\ No newline at end of file
...@@ -23,3 +23,6 @@ body { ...@@ -23,3 +23,6 @@ body {
vertical-align: middle; vertical-align: middle;
line-height: normal; line-height: normal;
} }
.asteriskField {
display: none;
}
\ No newline at end of file
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