Skip to content

Commit 9e497d5

Browse files
author
Sam Redmond
committed
Adds full client-side validation to the learn request page, adds a helper template for commonly used HTML idioms, minor bug fixes, and some attempts to edit a profile and promote/demote users
1 parent 2441a9d commit 9e497d5

File tree

11 files changed

+131
-100
lines changed

11 files changed

+131
-100
lines changed

app/static/css/extra_styles.css

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
}
88
}
99

10+
@media (max-width: 767px)
11+
{
12+
body {
13+
margin: 0px;
14+
padding: 0px;
15+
}
16+
}
17+
1018
/* Get the footer to stay at the bottom of the page
1119
-------------------------------------------------- */
1220
html, body {

app/static/js/main.js

+42-49
Original file line numberDiff line numberDiff line change
@@ -65,65 +65,46 @@ $(document).ready(function()
6565
return patt.test(str);
6666
}
6767

68-
//Verify a request before it is sent to the server
69-
$("#submitRequest").submit(function()
68+
/**
69+
* Verify a LEARN request before it is sent to the server.
70+
* Possible Errors:
71+
* No class selected
72+
* 'Other' issue selected but nothing filled in
73+
* Nothing in the 'title' section
74+
* Nothing in the 'specific challenge' section
75+
*/
76+
$("#submit_learn_request").click(function()
7077
{
71-
//Kinda a ghetto way to inform people of what error they made. Add the error message to a string.
78+
//An array to hold all the error messages.
7279
var errors = new Array();
7380

74-
//Get the values
75-
var first_name_text = escapeHTML($("#first-name").val().trim());
76-
var last_name_text = escapeHTML($("#last-name").val().trim());
77-
var grade_text = escapeHTML($("#grade").val().trim());
78-
var email_text = escapeHTML($("#email").val().trim());
79-
80-
var num_classes_selected = $("option:selected").length;
81-
81+
//Get values
82+
var subj_title = escapeHTML($("#subj_title").val().trim());
83+
var issue_type = escapeHTML($("input[name=issue]").filter(':checked').val().trim());
84+
var elaboration = escapeHTML($("#elaboration").val().trim());
8285
var title_text = escapeHTML($("#title").val().trim());
8386
var challenge_text = escapeHTML($("#challenge").val().trim());
84-
85-
//Specialized error messages
86-
if (isEmpty(first_name_text))
87+
88+
if (isEmpty(subj_title))
8789
{
88-
errors.push("Your first name cannot be empty.");
90+
errors.push("You must select a class in which you would like help.");
8991
}
90-
else if (!isAlpha(first_name_text))
92+
if (["hw","test","proj","other"].indexOf(issue_type) == -1)
9193
{
92-
errors.push("Your first name must be composed of letters only.");
94+
errors.push("Please do not change the source code of the radio button values.");
9395
}
94-
if (isEmpty(last_name_text))
95-
{
96-
errors.push("Your last name cannot be empty.");
97-
}
98-
else if (!isAlpha(last_name_text))
99-
{
100-
errors.push("Your last name must be composed of letters only.");
101-
}
102-
if (!(grade_text==='9' || grade_text==='10' || grade_text==='11' || grade_text==='12'))
103-
{
104-
errors.push("Your grade must be either '9', '10', '11', or '12'.");
105-
}
106-
if (isEmpty(email_text))
96+
if (issue_type == "other" && isEmpty(elaboration))
10797
{
108-
errors.push("Your email cannot be empty");
109-
}
110-
else if (!isEmail(email_text))
111-
{
112-
errors.push("Your email must contain only letters, numbers, or any of '._+-'.");
113-
}
114-
if (num_classes_selected == 0)
115-
{
116-
errors.push("You must select at least one class.");
98+
errors.push("You indicated an alternative type of challenge, yet failed to elaborate.");
11799
}
118100
if (isEmpty(title_text))
119101
{
120-
errors.push("You must title the request.");
102+
errors.push("You cannot have an empty title.");
121103
}
122104
if (isEmpty(challenge_text))
123105
{
124-
errors.push("You must describe your specific challenge.");
106+
errors.push("You must say something in the speciic challenge field.");
125107
}
126-
127108
//The moment of truth - are there errors?
128109
if (errors.length > 0)
129110
{
@@ -136,13 +117,8 @@ $(document).ready(function()
136117
//Otherwise, the form is sent to the server.
137118
});
138119

139-
$(".btn-group button").click(function ()
140-
{
141-
$("#subj_title").val($(this).text());
142-
});
143-
144-
//Fully validate a teach form submission
145-
$("#submitTeacher").submit(function()
120+
//Verify a request before it is sent to the server
121+
$("#submitRequest").submit(function()
146122
{
147123
//Kinda a ghetto way to inform people of what error they made. Add the error message to a string.
148124
var errors = new Array();
@@ -155,6 +131,9 @@ $(document).ready(function()
155131

156132
var num_classes_selected = $("option:selected").length;
157133

134+
var title_text = escapeHTML($("#title").val().trim());
135+
var challenge_text = escapeHTML($("#challenge").val().trim());
136+
158137
//Specialized error messages
159138
if (isEmpty(first_name_text))
160139
{
@@ -188,6 +167,14 @@ $(document).ready(function()
188167
{
189168
errors.push("You must select at least one class.");
190169
}
170+
if (isEmpty(title_text))
171+
{
172+
errors.push("You must title the request.");
173+
}
174+
if (isEmpty(challenge_text))
175+
{
176+
errors.push("You must describe your specific challenge.");
177+
}
191178

192179
//The moment of truth - are there errors?
193180
if (errors.length > 0)
@@ -201,6 +188,12 @@ $(document).ready(function()
201188
//Otherwise, the form is sent to the server.
202189
});
203190

191+
$(".btn-group button").click(function ()
192+
{
193+
$("#subj_title").val($(this).text());
194+
});
195+
196+
204197
//Some clientside form validation
205198
$("#first-name").blur(function()
206199
{

app/templates/admin.html

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% extends "layout.html" %}
2-
{% block content %}
2+
{% block content %}
3+
{% if g.user.role == 1 %}
34
<h2>Users</h2>
45
<div class="accordion" id="accordionusers">
56
{% for u in users %}
@@ -14,8 +15,6 @@ <h2>Users</h2>
1415
<dl class="dl-horizontal">
1516
<dt>Grade</dt>
1617
<dd>{{ u.grade }}</dd>
17-
<dt>Role</dt>
18-
<dd>{{ u.role }}</dd>
1918
<dt>Created</dt>
2019
<dd>{{ momentjs(u.created).format('LLLL') }}</dd>
2120
<dt>Last Logged In</dt>
@@ -56,6 +55,9 @@ <h2>Users</h2>
5655
No requests available.
5756
{% endfor %}
5857
</dd>
58+
<dt>Role</dt>
59+
<dd>{{ u.role }}</dd>
60+
<div class="pull-right"> <button class="btn btn-small btn-info" value={{ u.id }}>Promote/Demote</button></div>
5961
</dl>
6062
</div>
6163
</div>
@@ -152,4 +154,9 @@ <h2>Subjects</h2>
152154
<p>There are no subjects in the database. This shouldn't happen. Make sure that add_subjects.py has been run.</p>
153155
{% endfor %}
154156
</div>
157+
{% else %}
158+
<div class="alert alert-error">
159+
<strong>Error!</strong> You are not an administrator.
160+
</div>
161+
{% endif %}
155162
{% endblock %}

app/templates/edit.html

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,51 @@
11
{% extends "layout.html" %}
2+
{% import 'helper.html' as forms %}
23
{% block content %}
4+
<h2>Edit your profile</h2>
35
<div id="alert" class="alert alert-error alert-block hidden">
46
<button type="button" class="close" data-dismiss="alert">&times;</button>
57
<span id="alert-text"></span>
68
</div>
7-
<form id="signup" action="signup" method="post" class="form-horizontal">
9+
<div class="control-group">
10+
<label class="control-label" for="old_password">Current Password {{ forms.info("Enter your current password to confirm changes.") }}</label>
11+
<input class="span2" id="old_password" type="password" placeholder="Current Password">
12+
</div>
13+
<form id="edit" action="/edit" method="post" class="form-horizontal">
814
<fieldset>
915
<legend>Account Details</legend>
1016
<div id="email-control-group" class="control-group">
11-
<label class="control-label" for="email">Email Address</label>
17+
<label class="control-label" for="email">
18+
Email Address {{ forms.info("Enter your Menlo School Email address") }}
19+
</label>
1220
<div class="controls">
1321
<div class="input-prepend input-append">
1422
<span class="add-on"><i class="icon-envelope"></i></span>
15-
<input class="span2" id="email" name="email" type="text" placeholder="first.last">
23+
<input class="span2" class="email" id="email" name="email" type="text" placeholder="New Email Address" value={{ g.user.email }}>
1624
<span class="add-on">@menloschool.org</span>
1725
</div>
1826
<span id="email-label" class="label"></span>
1927
</div>
2028
</div>
2129
<div id="password-control-group" class="control-group">
2230
<label class="control-label" for="password">
23-
Old Password <a href="#" rel="tooltip" title="A password must ____"><i class="icon-info-sign"></i></a>
24-
</label>
25-
<div class="controls">
26-
<div class="input-prepend">
27-
<span class="add-on"><i class="icon-font"></i></span>
28-
<input class="span2" id="password" name="password" type="password" placeholder="Password">
29-
</div>
30-
<span id="password-label" class="label"></span>
31-
</div>
32-
</div>
33-
<div id="password-control-group" class="control-group">
34-
<label class="control-label" for="password">
35-
New Password <a href="#" rel="tooltip" title="A password must ____"><i class="icon-info-sign"></i></a>
31+
New Password {{ forms.info("A password must ___") }}
3632
</label>
3733
<div class="controls">
3834
<div class="input-prepend">
3935
<span class="add-on"><i class="icon-font"></i></span>
40-
<input class="span2" id="password" name="password" type="password" placeholder="Password">
36+
<input class="span2 pass" id="password" name="password" type="password" placeholder="New Password">
4137
</div>
4238
<span id="password-label" class="label"></span>
4339
</div>
4440
</div>
4541
<div id="confirm-password-control-group" class="control-group">
4642
<label class="control-label" for="confirm-password">
47-
Confirm New Password <a href="#" rel="tooltip" title="What you enter here must be the same as what you entered above."><i class="icon-info-sign"></i></a>
43+
Confirm Password {{ forms.info("What you enter here must be the same as what you entered above.") }}
4844
</label>
4945
<div class="controls">
5046
<div class="input-prepend">
5147
<span class="add-on"><i class="icon-font"></i></span>
52-
<input class="span2" id="confirm-password" name="confirm-password" type="password" placeholder="Confirm Password">
48+
<input class="span2 pass" id="confirm-password" name="confirm-password" type="password" placeholder="Confirm Password">
5349
</div>
5450
<span id="confirm-password-label" class="label"></span>
5551
</div>
@@ -59,39 +55,43 @@
5955
<legend>About You</legend>
6056
<div id="first-name-control-group" class="control-group">
6157
<label class="control-label" for="first-name">
62-
First Name <a href="#" rel="tooltip" title="A first name must contain only letters (it match the regular expression '^[a-zA-Z]+$')"><i class="icon-info-sign"></i></a>
58+
First Name {{ forms.info("A name may only contain upper- or lowercase letters ") }}
6359
</label>
6460
<div class="controls">
6561
<div class="input-prepend">
6662
<span class="add-on"><i class="icon-font"></i></span>
67-
<input class="span2" id="first-name" name="first_name"type="text" placeholder="First Name">
63+
<input class="span2" id="first-name" name="first_name"type="text" placeholder="First Name" value={{ g.user.first_name }}>
6864
</div>
6965
<span id="first-name-label" class="label"></span>
7066
</div>
7167
</div>
7268
<div id="last-name-control-group" class="control-group">
73-
<label class="control-label" for="last-name">Last Name</label>
69+
<label class="control-label" for="last-name">
70+
Last Name {{ forms.info("A name may only contain upper- or lowercase letters ") }}
71+
</label>
7472
<div class="controls">
7573
<div class="input-prepend">
7674
<span class="add-on"><i class="icon-font"></i></span>
77-
<input class="span2" id="last-name" name="last_name" type="text" placeholder="Last Name">
75+
<input class="span2" id="last-name" name="last_name" type="text" placeholder="Last Name" value={{ g.user.last_name }}>
7876
</div>
7977
<span id="last-name-label" class="label"></span>
8078
</div>
8179
</div>
8280
<div id="grade-control-group" class="control-group">
83-
<label class="control-label" for="grade">Grade</label>
81+
<label class="control-label" for="grade">
82+
Grade {{ forms.info("Your grade must be either '9', '10', '11', or '12'") }}
83+
</label>
8484
<div class="controls">
8585
<div class="input-prepend">
8686
<span class="add-on">#</span>
87-
<input class="span2" id="grade" name="grade" type="number" min=9 max=12 placeholder="Between 9 and 12">
87+
<input class="span2" id="grade" name="grade" type="number" min=9 max=12 placeholder="Between 9 and 12" value={{ g.user.grade }}>
8888
</div>
8989
<span id="grade-label" class="label"></span>
9090
</div>
9191
</div>
9292
</fieldset>
9393
<fieldset>
94-
<legend>Math/Science Classes <em>You Could Tutor</em></legend>
94+
<legend>Math/Science Classes <em>You Could Tutor</em> {{ forms.info("Only select the classes you are capable of tutoring.") }}</legend>
9595
<p><small>To select multiple options, hold down the command key (&#8984;) on Mac or the control (CTRL) button on Windows.</small></p>
9696
<select id="tutor_science" name="tutor_science" multiple="multiple" size="21" class="pull-left span4">
9797
<optgroup label="Physics">
@@ -164,7 +164,7 @@
164164
</select>
165165
</fieldset>
166166
<fieldset>
167-
<legend>Math/Science Classes <em>You Are Currently Taking</em></legend>
167+
<legend>Math/Science Classes <em>You Are Currently Taking</em> {{ forms.info("Only select the classes you are currently taking this school year.") }}</legend>
168168
<p><small>To select multiple options, hold down the command key (&#8984;) on Mac or the control (CTRL) button on Windows.</small></p>
169169
<select id="learn_science" name="learn_science" multiple="multiple" size="21" class="pull-left span4">
170170
<optgroup label="Physics">
@@ -236,6 +236,6 @@
236236
</optgroup>
237237
</select>
238238
</fieldset>
239-
<p><small>By clicking sign up, you agree to our <a href={{ url_for('terms_and_conditions') }}>Terms and Conditions<a></small></p>
240-
<button type="submit" class="btn btn-primary" id="submit_button">Sign Up!</button>
239+
<button type="submit" class="btn btn-primary" id="submit_button">Submit</button>
240+
</form>
241241
{% endblock %}

app/templates/helper.html

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% macro info(title='') -%}
2+
<a href="#" rel="tooltip" title="{{title}}" tabindex="-1"><i class="icon-info-sign"></i></a>
3+
{%- endmacro %}
4+
5+
{%- macro textarea(name, value='', rows=10, cols=40) -%}
6+
<textarea name="{{ name }}" rows="{{ rows }}" cols="{{ cols
7+
}}">{{ value|e }}</textarea>
8+
{%- endmacro %}

app/templates/layout.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@
141141
<ul class="nav">
142142
<li><a href="/">Home</a></li>
143143
</ul>
144-
<form class="navbar-form pull-right" action='login' method='post' name='login'>
144+
<form class="navbar-form pull-right" action='/login' method='post' name='login'>
145145
<input class="span2" type="text" name="email" placeholder="Email">
146146
<input class="span2" type="password" name="password" placeholder="Password">
147147
<button type="submit" class="btn">Log In</button>

app/templates/learn.html

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ <h2>Learning</h2>
1111
<button type="button" class="close" data-dismiss="alert">&times;</button>
1212
<span id="alert-text"></span>
1313
</div>
14-
<form id="learn" action="learn" method="post" class="form-horizontal">
14+
<form id="learn" action="/learn" method="post" class="form-horizontal">
1515
<fieldset>
1616
<legend>Which class do you need help in? <small>Select One</small></legend>
1717
<div class="btn-group" data-toggle="buttons-radio">
@@ -83,6 +83,6 @@ <h2>Learning</h2>
8383
</div>
8484
</div>
8585
</fieldset>
86-
<button type="submit" class="btn btn-primary" id="submit_button">Submit!</button>
86+
<button type="submit" class="btn btn-primary" id="submit_learn_request">Submit!</button>
8787
</form>
8888
{% endblock %}

app/templates/reset_password.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<div class="well well-large span4 offset4">
44
<h2>Forgot your Password?</h2>
55
<form action="" method="post" name="reset_password">
6-
<input type="text" name="email" class="input-block-level" placeholder="Email Address">
6+
<input type="text" id="email" name="email" class="input-block-level" placeholder="Email Address">
77
<button class="btn btn-primary" type="submit">Email Me a New One!</button>
88
</form>
99
</div>

0 commit comments

Comments
 (0)