Skip to content

Commit 7e0b956

Browse files
author
Sam Redmond
committed
Begins to treat the user upgrade issue
1 parent 9e497d5 commit 7e0b956

File tree

6 files changed

+108
-12
lines changed

6 files changed

+108
-12
lines changed

app/models.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import bcrypt
33
ROLE_USER = 0
44
ROLE_ADMIN = 1
5+
ROLE_SUPER = 2
56

67
#Tutors are in the 'left' column, subjects are in the 'right'
78
tutorsSubjects = db.Table('tutorsSubjects',
@@ -32,7 +33,7 @@ class User(db.Model):
3233
last_logged_in = db.Column(db.DateTime)
3334

3435
#Administrative
35-
role = db.Column(db.SmallInteger, default = ROLE_USER)
36+
role = db.Column(db.SmallInteger, default = ROLE_SUPER)
3637

3738
#Requests Made
3839
requests = db.relationship('Request', backref = 'author')

app/templates/admin.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% extends "layout.html" %}
22
{% block content %}
3-
{% if g.user.role == 1 %}
3+
{% if g.user.role > 0 %}
44
<h2>Users</h2>
55
<div class="accordion" id="accordionusers">
66
{% for u in users %}

app/templates/layout.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,12 @@
120120
<li><a href="/">Home</a></li>
121121
<li><a href={{ url_for('learn') }}>Learn</a></li>
122122
<li><a href={{ url_for('teach') }}>Teach</a></li>
123-
{% if g.user.role == 1 %}
123+
{% if g.user.role > 0 %}
124124
<li><a href={{ url_for('admin') }}>Admin</a></li>
125125
{% endif %}
126+
{% if g.user.role > 1 %}
127+
<li><a href={{ url_for('manage_users') }}>Manage</a></li>
128+
{% endif %}
126129
<li class="dropdown">
127130
<a href={{ url_for('teach') }} class="dropdown-toggle" data-toggle="dropdown">Me<b class="caret"></b></a>
128131
<ul class="dropdown-menu">

app/templates/learn.html

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
{% block content %}
33
<h2>Learning</h2>
44
<p>
5-
If you need help, you're in the right place. We're glad you want to learn! You came to the right place. Here at the Math &amp; Science Help Center, our goal is to help <em>you</em> with math and/or science. Simply tell us what you need help with by filling out the form below. When you submit your information, your request will be added to a queue. Everyone (especially tutors) can see all requests at any time, and hopefully you'll soon be contacted by a tutor who is ready to help you. If you're not contacted within, say, a week, email me at [email protected] and I'll personally help you out.
5+
If you need help with math or science, you're in the right place. We're glad you want to learn! Sometimes the hardest part can be seeking help from others. Here at the Math &amp; Science Help Center, our goal is to help <em>you</em> with math and/or science.
66
</p>
77
<p>
8-
In order for this interaction to be effective, you need to really describe the problem you're having. The more information you tell us, the better we can prepare to help you. Really go for it on the optional questions.
8+
Fill out the form below to submit a request for help. All tutors who are able to help will receive a notification, and hopefully you'll soon be contacted by a tutor who is ready to help you. If you're not contacted within a week, email me at [email protected] and I'll personally help you out.
9+
</p>
10+
<p>
11+
In order to maximize the chance that this request succeeds, you need to really describe the problem you're facing. Add as much detail as possible. The more information you tell us, the better we can prepare to help you. The optional questions in particular give you the chance to tell us vital information.
912
</p>
1013
<div id="alert" class="alert alert-error alert-block hidden">
1114
<button type="button" class="close" data-dismiss="alert">&times;</button>
@@ -22,7 +25,7 @@ <h2>Learning</h2>
2225
<input class="hidden" id="subj_title" name="subj_title">
2326
</fieldset>
2427
<fieldset>
25-
<legend>Specific Challenge <small>Select One</small></legend>
28+
<legend>Issue Category<small> Select One</small></legend>
2629
<p>In general, we categorize assistance into three categories - homework help, studying for a test, and working on a long-term project.</p>
2730
<label class="radio">
2831
<input type="radio" name="issue" id="hw" value="hw" checked="checked">
@@ -43,7 +46,7 @@ <h2>Learning</h2>
4346
</label>
4447
</fieldset>
4548
<fieldset>
46-
<legend>Additional Information</legend>
49+
<legend>Required Information</legend>
4750
<div id="title-control-group" class="control-group">
4851
<label class="control-label" for="title">Title</label>
4952
<div class="controls">

app/templates/manage_users.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{% extends "layout.html" %}
2+
{% block content %}
3+
{% if g.user.role == 2 %}
4+
<h2>Change User Roles.<small> The blue button indicates the user's old role.</small></h2>
5+
{%- macro radio(user_id, current_role=0) -%}
6+
<div class="btn-group" data-toggle="buttons-radio">
7+
{% if current_role == 0 %}
8+
<button type="button" class="btn btn-info active">0</button>
9+
<button type="button" class="btn">1</button>
10+
<button type="button" class="btn">2</button>
11+
{% elif current_role == 1 %}
12+
<button type="button" class="btn">0</button>
13+
<button type="button" class="btn btn-info active">1</button>
14+
<button type="button" class="btn">2</button>
15+
{% elif current_role == 2 %}
16+
<button type="button" class="btn">0</button>
17+
<button type="button" class="btn">1</button>
18+
<button type="button" class="btn btn-info active">2</button>
19+
{% endif %}
20+
</div>
21+
{%- endmacro %}
22+
<form action="" name="manage_users" method="post">
23+
{% for u in users %}
24+
<div class="well well-small">
25+
{{ u.first_name }} {{ u.last_name }}
26+
{{ radio(u.id, current_role = u.role) }}
27+
</div>
28+
{% endfor %}
29+
<button type="submit" class="btn btn-primary btn-large">Submit Changes</button>
30+
</form>
31+
32+
{% else %}
33+
<div class="alert alert-error">
34+
<strong>Error!</strong> OFF LIMITS.
35+
</div>
36+
{% endif %}
37+
{% endblock %}

app/views.py

+57-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111
PASSWORD_CHARS = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+?'
1212
NEW_PASSWORD_LENGTH = 13;
13+
ISSUE_MAP = {'hw':'Homework', 'proj': 'Project', 'test': 'Test'}
14+
1315

1416
@lm.user_loader
1517
def load_user(id):
@@ -55,7 +57,7 @@ def login():
5557

5658
user = User.query.filter_by(email = email).first()
5759
if user is None:
58-
flash('The email "{0}" is not stored in our databases.'.format(email))
60+
flash('The email "{0}" is not stored in our database.'.format(email))
5961
return redirect(url_for('login'))
6062

6163
if not user.verify_password(password):
@@ -174,12 +176,47 @@ def learn():
174176
if request.method=='POST': #Looking at the page
175177
info = request.form
176178

179+
'''
180+
Server-side validation, in case bad data slips through
181+
Possible Errors:
182+
Invalid class selected
183+
'Other' issue selected but nothing filled in
184+
Nothing in the 'title' section
185+
Nothing in the 'specific challenge' section
186+
'''
177187
subj = Subject.query.filter_by(title=info['subj_title']).first()
178-
if subj != None:
188+
if subj == None:
189+
flash("Invalid subject selected. Please leave page source alone.");
190+
return redirect(url_for('learn'))
191+
else:
179192
now = datetime.utcnow()
180-
req = Request(title=info['title'],
181-
issue=info['issue'],
182-
body=info['challenge'],
193+
issue=info['issue']
194+
title=info['title']
195+
body=info['challenge']
196+
197+
if issue == 'other':
198+
elaboration=info['elaboration']
199+
if elaboration == "" or elaboration == None:
200+
flash("Somehow, a blank issue type elaboration bypassed client-side validation :( Please leave page source and JS alone.");
201+
return redirect(url_for('learn'))
202+
issue_str = elaboration
203+
elif issue not in ISSUE_MAP:
204+
flash("Somehow, an invalid issue type bypassed client-side validation :( Please leave page source and JS alone.");
205+
return redirect(url_for('learn'))
206+
else:
207+
issue_str = ISSUE_MAP[issue]
208+
209+
210+
if title == "" or title == None: #Remember, different browsers handle blank strings differently
211+
flash("Somehow, an empty title bypassed client-side validation :( Please leave page source and JS alone.");
212+
return redirect(url_for('learn'))
213+
if body == "" or body == None:
214+
flash("Somehow, a blank body bypassed client-side validation :( Please leave page source and JS alone.");
215+
return redirect(url_for('learn'))
216+
217+
req = Request(title=title,
218+
issue=issue_str,
219+
body=body,
183220
extra_requests=info['requests'],
184221
availability=info['availability'],
185222
additional=info['additional_comments'],
@@ -216,6 +253,21 @@ def admin():
216253
requests=Request.query.all(),
217254
subjects=Subject.query.all())
218255

256+
@app.route('/manage_users', methods=['GET', 'POST'])
257+
@login_required
258+
def manage_users():
259+
if request.method == 'POST':
260+
pass
261+
else:
262+
if g.user.role < 2:
263+
flash("You don't have the proper clearance to see this webpage.")
264+
return render_template('me.html',
265+
title="Me")
266+
return render_template('manage_users.html',
267+
users = User.query.all(),
268+
title="Manage Users")
269+
270+
219271
@app.route('/termsconditions')
220272
def terms_and_conditions():
221273
return render_template('termsconditions.html',

0 commit comments

Comments
 (0)