Skip to content
Permalink
Browse files
fix #16 -- error pages
Signed-off-by: Chris Warrick <kwpolska@gmail.com>
  • Loading branch information
Kwpolska committed Jan 6, 2015
1 parent 4171518 commit 943dcc28a090cc3f9229c9d97e38dbe8fb914b7f
@@ -60,8 +60,8 @@ def generate_menu_alt():
if not current_user.is_authenticated():
return """<li><a href="/login">Log in</a></li>"""
if current_user.is_admin:
edit_entry = """<li><a href="/users">Manage users</a></li>
<li><a href="/users/permissions">Permissions</a></li>"""
edit_entry = """<li><a href="/users">Manage users</a></li>\
<li><a href="/users/permissions">Permissions</a></li>"""
else:
edit_entry = ''
return """
@@ -102,6 +102,10 @@ def render(template_name, context=None, code=200, headers=None):

return _site.render_template(template_name, None, context), code, headers

def error(desc, code, permalink):
return render('comet_error.tmpl', {'title': 'Error', 'code': code, 'desc': desc, 'permalink': permalink}, code)


def unauthorized():
return redirect('/login?status=unauthorized')

@@ -279,7 +283,7 @@ def edit(path):
context = {'path': path, 'site': _site}
post = find_post(path)
if post is None:
return "No such post or page.", 404
return error("No such post or page.", 404, '/edit/' + path)

if request.method == 'POST':
meta = {}
@@ -325,7 +329,7 @@ def delete():
post = p
break
if post is None:
return "No such post or page.", 404
return error("No such post or page.", 404, '/delete')
os.unlink(path)
init_site()
return redirect('/')
@@ -360,9 +364,9 @@ def new_post_or_page(obj):
_site.commands.new_page(title=title, author=current_user.realname,
content_format='html')
else:
return "Cannot create {0} — unknown type.".format(obj), 400
return error("Cannot create {0} — unknown type.".format(obj), 400, '/new/' + obj)
except SystemExit:
return "This {0} already exists!".format(obj), 500
return error("This {0} already exists!".format(obj), 500, '/new/' + obj)
finally:
del _site['ADDITIONAL_METADATA']['author.uid']
# reload post list and go to index
@@ -411,7 +415,7 @@ def acp_users():
alert_status = 'success'
global USERS
if not current_user.is_admin:
return "Not authorized to edit users.", 401
return error("Not authorized to edit users.", 401, "/users")
else:
return render('comet_users.tmpl',
context={'title': 'Users',
@@ -425,7 +429,7 @@ def acp_users():
def acp_users_edit():
global USERS
if not current_user.is_admin:
return "Not authorized to edit users.", 401
return error("Not authorized to edit users.", 401, "/users/edit")
data = request.form
action = data['action']

@@ -440,7 +444,7 @@ def acp_users_edit():
new = False

if not user:
return "User does not exist.", 404
return error("User does not exist.", 404, "/users/edit")

alert = ''
alert_status = ''
@@ -478,25 +482,24 @@ def acp_users_edit():
@login_required
def acp_users_delete():
if not current_user.is_admin:
return "Not authorized to edit users.", 401
return error("Not authorized to edit users.", 401, "/users/delete")
user = get_user(int(request.form['uid']))
direction = request.form['direction']
if not user:
return error("User does not exist.", 404, "/users/edit/delete")
else:
user = get_user(int(request.form['uid']))
direction = request.form['direction']
if not user:
return "User does not exist.", 404
else:
user.active = direction == 'undel'
for p in PERMISSIONS:
setattr(user, p, False)
write_users()
return redirect('/users?status={_del}eted'.format(_del=direction))
user.active = direction == 'undel'
for p in PERMISSIONS:
setattr(user, p, False)
write_users()
return redirect('/users?status={_del}eted'.format(_del=direction))

@app.route('/users/permissions', methods=['GET', 'POST'])
@login_required
def acp_users_permissions():
global USERS
if not current_user.is_admin:
return "Not authorized to edit users.", 401
return error("Not authorized to edit users.", 401, "/users/permissions")


if request.method == 'POST':
@@ -0,0 +1,7 @@
{% extends 'base.tmpl' %}
{% block extra_head %}
{% endblock %}
{% block content %}
<h1>Error {{ code }}</h1>
<p>{{ desc }}</p>
{% endblock %}
@@ -4,6 +4,11 @@
.list-group {
margin-top: 10px;
}

h2 {
margin-top: 20px;
padding-top: 0;
}
</style>
{% endblock %}
{% block extra_js %}
@@ -20,6 +25,22 @@ $('#deleteModal').on('show.bs.modal', function (event) {
{% endblock %}
{% block content %}

<div class="well well-sm">
Show posts of:
<div class="btn-group" role="group">
{% if wants %}
<a href="/?all=0" class="btn btn-default"><i class="fa fa-fw fa-user"></i></a>
<a href="/?all=1" class="btn btn-default active"><i class="fa fa-fw fa-users"></i></a>
{% elif not current_user.can_edit_all_posts %}
<a href="#" class="btn btn-default active"><i class="fa fa-fw fa-user"></i></a>
<a href="#" class="btn btn-default disabled"><i class="fa fa-fw fa-users"></i></a>
{% else %}
<a href="/?all=0" class="btn btn-default active"><i class="fa fa-fw fa-user"></i></a>
<a href="/?all=1" class="btn btn-default"><i class="fa fa-fw fa-users"></i></a>
{% endif %}
</div>
</div>

<div class="row">
<div class="col-md-6">
<h2 style="text-align: center;">Posts</h2>
@@ -28,7 +49,7 @@ $('#deleteModal').on('show.bs.modal', function (event) {
<span class="input-group-btn"><button type="submit" class="btn btn-success"><i class="fa fa-file-o"></i> Create Post</button></span>
</form>
<div class="list-group">
{% for p in site.posts %}
{% for p in posts %}
<div class="list-group-item">
<h3 class="media-heading">{{ p.title() }}</h3>
<small>Date: {{ p.date }}</small>
@@ -50,7 +71,7 @@ $('#deleteModal').on('show.bs.modal', function (event) {
</form>
<div class="page_holder"></div>
<div class="list-group">
{% for p in site.pages %}
{% for p in pages %}
<div class="list-group-item">
<h3 class="media-heading">{{ p.title() }}</h3>
<small>Date: {{ p.date }}</small>
@@ -8,6 +8,10 @@
.uid {
text-align: right;
}

td {
vertical-align: middle !important;
}
</style>
{% endblock %}
{% block extra_js %}
@@ -45,7 +49,7 @@ $('#deleteModal').on('show.bs.modal', function (event) {
<div class="alert alert-{{ alert_status }}" role="alert">{{ alert }}</div>
{% endif %}

<table class="table table-hover">
<table class="table table-hover" style="table-layout: fixed;">
<thead><tr>
<th class="uid">#</th>
<th class="username">Username</th>
@@ -75,26 +79,26 @@ $('#deleteModal').on('show.bs.modal', function (event) {
<input type="hidden" name="action" value="edit">
<div class="btn-group" role="group">
{% if user.active %}
<button type="submit" class="btn btn-info btn-xs" title="Edit"><i class="fa fa-pencil fa-fw"></i> Edit</button>
<button type="submit" class="btn btn-info" title="Edit"><i class="fa fa-pencil fa-fw"></i> Edit</button>
{% if user == current_user %}
<button type="button" class="btn btn-danger btn-xs" title="Delete" disabled><i class="fa fa-trash fa-fw"></i> Delete</button>
<button type="button" class="btn btn-danger" title="Delete" disabled><i class="fa fa-trash fa-fw"></i> Delete</button>
{% else %}
<button type="button" class="btn btn-danger btn-xs" data-toggle="modal" data-target="#deleteModal" data-username="{{ user.username }}" data-uid="{{ uid }}" data-direction="del" title="Delete"><i class="fa fa-trash fa-fw"></i> Delete</button>
<button type="button" class="btn btn-danger" data-toggle="modal" data-target="#deleteModal" data-username="{{ user.username }}" data-uid="{{ uid }}" data-direction="del" title="Delete"><i class="fa fa-trash fa-fw"></i> Delete</button>
{% endif %}
{% else %}
<button type="button" class="btn btn-info btn-xs" title="Edit" disabled><i class="fa fa-pencil fa-fw"></i> Edit</button>
<button type="button" class="btn btn-success btn-xs" data-toggle="modal" data-target="#deleteModal" data-username="{{ user.username }}" data-uid="{{ uid }}" data-direction="undel" title="Undelete"><i class="fa fa-trash-o fa-fw"></i> Undelete</button>
<button type="button" class="btn btn-info" title="Edit" disabled><i class="fa fa-pencil fa-fw"></i> Edit</button>
<button type="button" class="btn btn-success" data-toggle="modal" data-target="#deleteModal" data-username="{{ user.username }}" data-uid="{{ uid }}" data-direction="undel" title="Undelete"><i class="fa fa-trash-o fa-fw"></i> Undelete</button>
{% endif %}
</div>
</form>
</tr>
{% endfor %}
<tr><form action="/users/edit" method="POST">
<td><i class="fa fa-plus"></i></td>
<td><input name="username" placeholder="User to create"></td>
<td></td>
<td><input name="username" placeholder="New user" class="form-control"></td>
<td><input name="action" value="new" type="hidden"></td>
<td></td>
<td><button type="submit" class="btn btn-primary btn-xs"><i class="fa fa-plus-square fa-fw"></i> Create</button></td>
<td><button type="submit" class="btn btn-primary"><i class="fa fa-plus-square fa-fw"></i> Create</button></td>
</form></tr>
</table>

@@ -7,6 +7,9 @@
.perm, .select_all {
text-align: center;
}
td {
vertical-align: middle !important;
}
</style>
{% endblock %}
{% block extra_js %}
@@ -21,11 +24,11 @@ $(document).ready(function() {

for (var i = 0; i < PERMISSIONS.length; i++) {
var p = PERMISSIONS[i];
$('th.' + p + ' i.fa-check-square-o').click(function() {
$('th.' + p + ' button.select_all-perm').click(function() {
p = this.attributes['data-perm'].value;
$('.' + p + ' input').each(function() { this.checked = true; });
});
$('th.' + p + ' i.fa-square-o').click(function() {
$('th.' + p + ' button.select_none-perm').click(function() {
p = this.attributes['data-perm'].value;
$('.' + p + ' input').each(function() {
if (p != 'is_admin' || this.attributes['data-uid'].value != {{ current_user.uid }}) {
@@ -37,11 +40,11 @@ $(document).ready(function() {

for (var i = 0; i < USERS.length; i++) {
uid = USERS[i];
$('.u' + uid + ' td.select_all i.fa-check-square-o').click(function() {
$('.u' + uid + ' button.select_all-user').click(function() {
uid = this.attributes['data-uid'].value;
$('input.u' + uid).each(function() { this.checked = true; });
});
$('.u' + uid + ' td.select_all i.fa-square-o').click(function() {
$('.u' + uid + ' button.select_none-user').click(function() {
uid = this.attributes['data-uid'].value;
$('input.u' + uid).each(function() {
if (uid != current_uid || this.attributes['data-perm'].value != 'is_admin') { this.checked = false; }
@@ -63,12 +66,30 @@ $(document).ready(function() {
<thead><tr>
<th class="uid">#</th>
<th class="username">Username</th>
<th class="perm is_admin">Admin<br><i class="fa fa-check-square-o fa-fw" data-perm="is_admin"></i><i class="fa fa-square-o fa-fw" data-perm="is_admin"></i></th>
<th class="perm can_edit_all_posts">Can all posts<br><i class="fa fa-check-square-o fa-fw" data-perm="can_edit_all_posts"></i><i class="fa fa-square-o fa-fw" data-perm="can_edit_all_posts"></i></th>
<th class="perm wants_all_posts">Want all posts<br><i class="fa fa-check-square-o fa-fw" data-perm="wants_all_posts"></i><i class="fa fa-square-o fa-fw" data-perm="wants_all_posts"></i></th>
<th class="perm can_upload_attachments">Attachments<br><i class="fa fa-check-square-o fa-fw" data-perm="can_upload_attachments"></i><i class="fa fa-square-o fa-fw" data-perm="can_upload_attachments"></i></th>
<th class="perm can_rebuild_site">Rebuild<br><i class="fa fa-check-square-o fa-fw" data-perm="can_rebuild_site"></i><i class="fa fa-square-o fa-fw" data-perm="can_rebuild_site"></i></th>
<th class="perm can_transfer_post_authorship">Transfer authorship<br><i class="fa fa-check-square-o fa-fw" data-perm="can_transfer_post_authorship"></i><i class="fa fa-square-o fa-fw" data-perm="can_transfer_post_authorship"></i></th>
<th class="perm is_admin">Admin<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="is_admin"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="is_admin"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="perm can_edit_all_posts">Can all posts<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="can_edit_all_posts"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="can_edit_all_posts"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="perm wants_all_posts">Wants all posts<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="wants_all_posts"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="wants_all_posts"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="perm can_upload_attachments">Attachments<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="can_upload_attachments"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="can_upload_attachments"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="perm can_rebuild_site">Rebuild<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="can_rebuild_site"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="can_rebuild_site"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="perm can_transfer_post_authorship">Transfer authorship<br>
<button type="button" class="btn btn-info btn-xs select_all-perm" data-perm="can_transfer_post_authorship"><i class="fa fa-check-square-o fa-fw"></i></button>
<button type="button" class="btn btn-info btn-xs select_none-perm" data-perm="can_transfer_post_authorship"><i class="fa fa-square-o fa-fw"></i></button>
</th>
<th class="select_all">Select all</th>
</tr></thead>
{% for uid, user in USERS.items() %}
@@ -79,7 +100,7 @@ $(document).ready(function() {
{% for p in PERMISSIONS %}
<td class="perm {{ p }}">{{ display_permission(user, p) }}</td>
{% endfor %}
<td class="select_all info"><i class="fa fa-check-square-o fa-fw" data-uid="{{ uid }}"></i><i class="fa fa-square-o fa-fw" data-uid="{{ uid }}"></i></td>
<td class="select_all"><button type="button" class="btn btn-info select_all-user" data-uid="{{ uid }}"><i class="fa fa-check-square-o fa-fw"></i></button> <button type="button" class="btn btn-info select_none-user" data-uid="{{ uid }}"><i class="fa fa-square-o fa-fw"></i></button></td>
</tr>
{% endif %}
{% endfor %}
@@ -0,0 +1,7 @@
<%inherit file="base.tmpl"/>
<%block name="extra_head">
</%block>
<%block name="content">
<h1>Error ${code}</h1>
<p>${desc}</p>
</%block>

0 comments on commit 943dcc2

Please sign in to comment.